import React, { useState } from 'react'; import { format } from 'sql-formatter'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { solarizedlight } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { Button, message } from 'antd'; import { PREFIX_CLS } from '../../common/constants'; import { CheckCircleFilled, DownloadOutlined, UpOutlined } from '@ant-design/icons'; import { SqlInfoType } from '../../common/type'; import { exportTextFile } from '../../utils/utils'; type Props = { agentId?: number; queryId: number; question: string; llmReq?: any; llmResp?: any; integrateSystem?: string; queryMode?: string; sqlInfo: SqlInfoType; sqlTimeCost?: number; executeErrorMsg: string; }; const SqlItem: React.FC = ({ agentId, queryId, question, llmReq, llmResp, integrateSystem, queryMode, sqlInfo, sqlTimeCost, executeErrorMsg, }) => { const [sqlType, setSqlType] = useState(''); const tipPrefixCls = `${PREFIX_CLS}-item`; const prefixCls = `${PREFIX_CLS}-sql-item`; const handleCopy = (_: string, result: any) => { result ? message.success('复制SQL成功', 1) : message.error('复制SQL失败', 1); }; const onCollapse = () => { setSqlType(''); }; if (!llmReq && !sqlInfo.parsedS2SQL && !sqlInfo.correctedS2SQL && !sqlInfo.querySQL) { return null; } const { schema, terms, priorExts } = llmReq || {}; const fewShots = (Object.values(llmResp?.sqlRespMap || {})[0] as any)?.fewShots || []; const getSchemaMapText = () => { return ` Schema映射 ${schema?.fieldNameList?.length > 0 ? `名称:${schema.fieldNameList.join('、')}` : ''}${ schema?.values?.length > 0 ? ` 取值:${schema.values .map((item: any) => { return `${item.fieldName}: ${item.fieldValue}`; }) .join('、')}` : '' }${ priorExts ? ` 附加:${priorExts}` : '' }${ terms?.length > 0 ? ` 术语:${terms .map((item: any) => { return `${item.name}${item.alias?.length > 0 ? `(${item.alias.join(',')})` : ''}: ${ item.description }`; }) .join('、')}` : '' } `; }; const getFewShotText = () => { return ` Few-shot示例${fewShots .map((item: any, index: number) => { return ` 示例${index + 1}: 问题:${item.question} SQL: ${format(item.sql)} `; }) .join('')} `; }; const getParsedS2SQLText = () => { return ` ${queryMode === 'LLM_S2SQL' || queryMode === 'PLAIN_TEXT' ? 'LLM' : 'Rule'}解析S2SQL ${format(sqlInfo.parsedS2SQL)} `; }; const getCorrectedS2SQLText = () => { return ` 修正S2SQL ${format(sqlInfo.correctedS2SQL)} `; }; const getQuerySQLText = () => { return ` 最终执行SQL ${format(sqlInfo.querySQL)} `; }; const getErrorMsgText = () => { return ` 异常日志 ${executeErrorMsg} `; }; const onExportLog = () => { let text = ''; if (question) { text += ` 问题:${question} `; } if (llmReq) { text += getSchemaMapText(); } if (fewShots.length > 0) { text += getFewShotText(); } if (sqlInfo.parsedS2SQL) { text += getParsedS2SQLText(); } if (sqlInfo.correctedS2SQL) { text += getCorrectedS2SQLText(); } if (sqlInfo.querySQL) { text += getQuerySQLText(); } if (!!executeErrorMsg) { text += getErrorMsgText(); } exportTextFile(text, `supersonic-debug-${agentId}-${queryId}.log`); }; return (
SQL生成 {!!sqlTimeCost && ( (耗时: {sqlTimeCost}ms) )} : {sqlType && ( )}
{llmReq && (
{ setSqlType(sqlType === 'schemaMap' ? '' : 'schemaMap'); }} > Schema映射
)} {fewShots.length > 0 && (
{ setSqlType(sqlType === 'fewShots' ? '' : 'fewShots'); }} > Few-shot示例
)} {sqlInfo.parsedS2SQL && (
{ setSqlType(sqlType === 'parsedS2SQL' ? '' : 'parsedS2SQL'); }} > {queryMode === 'LLM_S2SQL' || queryMode === 'PLAIN_TEXT' ? 'LLM' : 'Rule'}解析S2SQL
)} {sqlInfo.correctedS2SQL && (
{ setSqlType(sqlType === 'correctedS2SQL' ? '' : 'correctedS2SQL'); }} > 修正S2SQL
)} {sqlInfo.querySQL && (
{ setSqlType(sqlType === 'querySQL' ? '' : 'querySQL'); }} > 最终执行SQL
)}
{sqlType === 'schemaMap' && (
{schema?.fieldNameList?.length > 0 && (
名称:
{schema.fieldNameList.join('、')}
)} {schema?.values?.length > 0 && (
取值:
{schema.values .map((item: any) => { return `${item.fieldName}: ${item.fieldValue}`; }) .join('、')}
)} {priorExts && (
附加:
{priorExts}
)} {terms?.length > 0 && (
术语:
{terms .map((item: any) => { return `${item.name}${ item.alias?.length > 0 ? `(${item.alias.join(',')})` : '' }: ${item.description}`; }) .join('、')}
)}
)} {sqlType === 'fewShots' && (
{fewShots.map((item: any, index: number) => { return (
示例{index + 1}:
问题:
{item.question}
SQL:
{item.sql}
); })}
)} {sqlType && sqlInfo[sqlType] && ( <> {format(sqlInfo[sqlType])} handleCopy(text, result)} > )}
); }; export default SqlItem;