DebugResult.tsx 5.06 KB
import { Spin, Tag, Empty, Typography, Button } from 'antd';
import {
  CheckCircleOutlined,
  CloseCircleOutlined,
  ClockCircleOutlined,
  LoadingOutlined,
  ReloadOutlined,
} from '@ant-design/icons';
import type { CallResultData } from './DebugConsole';

const { Text, Paragraph } = Typography;

interface DebugResultProps {
  result: CallResultData | null;
  loading: boolean;
  onQueryAsync?: (taskId: string) => void;
}

/** 格式化 JSON 为带缩进的字符串 */
function formatJson(data: Record<string, unknown> | null): string {
  if (!data) return '';
  try {
    return JSON.stringify(data, null, 2);
  } catch {
    return String(data);
  }
}

export default function DebugResult({ result, loading, onQueryAsync }: DebugResultProps) {
  const renderStatusTag = () => {
    if (!result) return null;
    switch (result.status) {
      case 'success':
        return (
          <Tag icon={<CheckCircleOutlined />} color="success">
            调用成功
          </Tag>
        );
      case 'error':
        return (
          <Tag icon={<CloseCircleOutlined />} color="error">
            调用失败
          </Tag>
        );
      case 'accepted':
        return (
          <Tag icon={<ClockCircleOutlined />} color="processing">
            任务已提交
          </Tag>
        );
      default:
        return <Tag>{result.status}</Tag>;
    }
  };

  // Loading state
  if (loading) {
    return (
      <div className="debug-result-container">
        <div className="debug-result-header">
          <Text strong>调用结果</Text>
        </div>
        <div className="debug-result-body" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: 300 }}>
          <Spin indicator={<LoadingOutlined style={{ fontSize: 32 }} spin />} tip="调用中..." />
        </div>
      </div>
    );
  }

  // Empty state
  if (!result) {
    return (
      <div className="debug-result-container">
        <div className="debug-result-header">
          <Text strong>调用结果</Text>
        </div>
        <div className="debug-result-body">
          <Empty description="配置左侧参数后发起调用" />
        </div>
      </div>
    );
  }

  return (
    <div className="debug-result-container">
      <div className="debug-result-header">
        <Text strong>调用结果</Text>
        <div className="debug-result-meta">
          {renderStatusTag()}
          {result.modelName && (
            <Text type="secondary" style={{ fontSize: 12 }}>
              模型: {result.modelName}
            </Text>
          )}
        </div>
      </div>

      <div className="debug-result-body">
        {/* Async task info */}
        {result.callType === 'async' && result.taskId && (
          <div className="debug-log-panel">
            <div className="debug-log-title">任务信息</div>
            <div className="debug-log-line">
              <span className="debug-log-step">Task ID</span>
              <code>{result.taskId}</code>
            </div>
            <div className="debug-log-line">
              <span className="debug-log-step">状态</span>
              <span>
                {result.status === 'accepted' ? '等待处理...' : result.status === 'success' ? '已完成' : result.status}
              </span>
            </div>
            {onQueryAsync && (
              <div style={{ marginTop: 8 }}>
                <Button
                  size="small"
                  icon={<ReloadOutlined />}
                  onClick={() => onQueryAsync(result.taskId!)}
                  loading={loading}
                >
                  查询结果
                </Button>
              </div>
            )}
          </div>
        )}

        {/* Response Content (from by-template) */}
        {result.responseContent && (
          <div className="debug-json-panel">
            <div className="debug-json-title">响应内容</div>
            <pre className="debug-json-content">
              <code>{result.responseContent}</code>
            </pre>
          </div>
        )}

        {/* Response Body (from orchestrate) */}
        {result.responseBody && (
          <div className="debug-json-panel">
            <div className="debug-json-title">响应 Body</div>
            <pre className="debug-json-content">
              <code>{formatJson(result.responseBody)}</code>
            </pre>
          </div>
        )}

        {/* Error message */}
        {result.errorMessage && (
          <div className="debug-log-panel" style={{ borderColor: '#ffccc7' }}>
            <div className="debug-log-title" style={{ color: '#f43f5e' }}>错误信息</div>
            <Paragraph style={{ color: '#f43f5e', marginBottom: 0 }}>
              {result.errorMessage}
            </Paragraph>
          </div>
        )}

        {/* Execution Log */}
        {result.executionLog && (
          <div className="debug-log-panel">
            <div className="debug-log-title">执行日志</div>
            <pre className="debug-log-content">
              <code>{result.executionLog}</code>
            </pre>
          </div>
        )}
      </div>
    </div>
  );
}