审查视图

app/components/mind/mind-panel.tsx 2.1 KB
202304001 authored
1 2 3 4 5 6 7 8
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";
202304001 authored
9
import Locale from "@/app/locales";
202304001 authored
10 11 12 13 14 15 16 17 18 19 20 21 22

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 () => {
202304001 authored
23
    if (!inputValue.trim()) return message.error(Locale.BgRemoval.error.prompt);
202304001 authored
24 25
    setIsLoading(true);
    try {
26
      const prompt = getMindPrompt(inputValue, false);
202304001 authored
27
      const response = await chatStore.directLlmInvoke(prompt, "gpt-4o-mini");
202304001 authored
28 29
      const cleanedContent = response.replace(/^```json|```$/g, "");
      const parsedData: MindElixirData = JSON.parse(cleanedContent);
202304001 authored
30 31
      setData(parsedData);
    } catch (error) {
202304001 authored
32
      message.error(Locale.BgRemoval.error.reqErr);
202304001 authored
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
    } 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>
    </>
  );
}