mind-panel.tsx
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { useState } from "react";
import { ControlParamItem } from "../sd";
import { SideBarTail } from "@/app/components/sidebar";
import { IconButton } from "../button";
import { getMindPrompt } from "@/app/utils/prompt";
import { useChatStore } from "@/app/store";
import { type MindElixirData } from "mind-elixir";
import { message } from "antd";
export interface MindPanelProps {
setData: React.Dispatch<React.SetStateAction<MindElixirData>>;
isLoading: boolean;
setIsLoading: React.Dispatch<React.SetStateAction<boolean>>;
}
export function MindPanel(props: MindPanelProps) {
const { setData, isLoading, setIsLoading } = props;
const [inputValue, setInputValue] = useState("");
const [loading, setLoading] = useState(false);
const chatStore = useChatStore();
const submit = async () => {
if (!inputValue.trim()) return message.error("请输入提示词!");
setIsLoading(true);
try {
const prompt = getMindPrompt(inputValue, false);
const response = await chatStore.directLlmInvoke(prompt, "gpt-4o-mini");
console.log("原始响应:", response);
let cleanedContent = response.startsWith("```json")
? response.substring(8)
: response;
if (cleanedContent.endsWith("```")) {
cleanedContent = cleanedContent.substring(0, cleanedContent.length - 4);
}
const parsedData: MindElixirData = JSON.parse(response);
console.log("解析后响应:", parsedData);
// 增强校验逻辑
if (
!parsedData?.nodeData?.id ||
!Array.isArray(parsedData.nodeData.children)
) {
throw new Error("数据结构不完整");
}
setData(parsedData);
} catch (error) {
console.log(error);
message.error("请求失败,请重试");
} finally {
setIsLoading(false); // 确保关闭加载状态:ml-citation{ref="2,5" data="citationList"}
}
};
return (
<>
<div>
<ControlParamItem title="提示词" required={true}>
<textarea
rows={3}
style={{ maxWidth: "100%", width: "100%", padding: "10px" }}
placeholder="请输入"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value);
}}
></textarea>
</ControlParamItem>
<SideBarTail
secondaryAction={
<IconButton
text="提交生成"
type="primary"
shadow
onClick={submit}
disabled={isLoading}
></IconButton>
}
/>
</div>
</>
);
}