fix: 修复代码分析报错

This commit is contained in:
shuguang 2025-05-08 20:56:16 +08:00
parent cac4384184
commit 9b0db2cd2f

View File

@ -11,17 +11,8 @@ import useAIRequest from '@/hooks/useAIRequest';
const { Dragger } = Upload; const { Dragger } = Upload;
const { TabPane } = Tabs; const { TabPane } = Tabs;
interface AnalysisResponse {
id: string;
choices: {
message: {
content: string;
};
}[];
}
const CodeAnalysisPage: React.FC = () => { const CodeAnalysisPage: React.FC = () => {
const [fileList, setFileList] = useState<UploadFile[]>([]);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [analysisType, setAnalysisType] = useState<'er' | 'module'>('er'); const [analysisType, setAnalysisType] = useState<'er' | 'module'>('er');
const [analysisResult, setAnalysisResult] = useState<string>(''); const [analysisResult, setAnalysisResult] = useState<string>('');
@ -71,40 +62,12 @@ const CodeAnalysisPage: React.FC = () => {
} }
setLoading(true); setLoading(true);
try { try {
const response = await fetch('http://8.218.106.190:3000/v1/chat/completions', { // 删除未使用的 fetch 请求代码
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-mw9ekhJlSj3GeGiw0hLRSHlwdkDFst8q6oBfQrW0L15QilbY'
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{
role: 'user',
content: [
{
type: 'text',
text: analysisType === 'er'
? '请分析这段代码并生成对应的ER图使用mermaid语法,用于计算机科学与技术毕业论文。分析要点1. 实体关系 2. 属性定义 3. 关系类型'
: '请分析这段代码并生成数据流图使用mermaid语法,用于计算机科学与技术毕业论文。。分析要点1. 模块划分 2. 依赖关系 3. 调用流程'
},
{
type: 'text',
text: codeContent
}
]
}
],
max_tokens: 2000
})
});
try {
const content = await sendRequest([ const content = await sendRequest([
{ {
type: 'text', type: 'text',
text: analysisType === 'er' text: analysisType === 'er'
? '请分析这段代码并生成对应的ER图使用mermaid语法,用于计算机科学与技术毕业论文。分析要点1. 实体关系 2. 属性定义 3. 关系类型' ? '请分析这段代码并生成对应的ER图使用mermaid语法,用于计算机科学与技术毕业论文。分析要点1. 实体关系 2. 属性定义 3. 关系类型'
: '请分析这段代码并生成功能模块图使用mermaid语法,用于计算机科学与技术毕业论文。。分析要点1. 模块划分 2. 依赖关系 3. 调用流程' : '请分析这段代码并生成功能模块图使用mermaid语法,用于计算机科学与技术毕业论文。。分析要点1. 模块划分 2. 依赖关系 3. 调用流程'
}, },
@ -124,6 +87,8 @@ const CodeAnalysisPage: React.FC = () => {
} catch (error) { } catch (error) {
console.error('分析失败:', error); console.error('分析失败:', error);
message.error('分析失败,请重试'); message.error('分析失败,请重试');
} finally {
setLoading(false);
} }
}; };
@ -152,7 +117,6 @@ const CodeAnalysisPage: React.FC = () => {
value={analysisType} value={analysisType}
onChange={(e) => { onChange={(e) => {
setAnalysisType(e.target.value); setAnalysisType(e.target.value);
setFileList([]);
setDiagramCode(''); setDiagramCode('');
setAnalysisResult(''); setAnalysisResult('');
}} }}
@ -199,7 +163,7 @@ const CodeAnalysisPage: React.FC = () => {
<Card title="可视化图表" extra={ <Card title="可视化图表" extra={
<Space> <Space>
<Tooltip title="复制图表代码"> <Tooltip title="复制图表代码">
<Button <Button
icon={<CopyOutlined />} icon={<CopyOutlined />}
onClick={() => { onClick={() => {
navigator.clipboard.writeText(diagramCode); navigator.clipboard.writeText(diagramCode);
@ -224,4 +188,4 @@ const CodeAnalysisPage: React.FC = () => {
); );
}; };
export default CodeAnalysisPage; export default CodeAnalysisPage;