import { useEffect, useState } from "react"; import { Button, Dropdown, Space } from "antd"; import type { MenuProps } from "antd"; import type { IArticleListResponse } from "../../api/Article"; import { get } from "../../request"; import { useAppSelector } from "../../hooks"; import { currentUser } from "../../reducers/current-user"; // 接口定义 export interface IPromptNode { text: string; prompt?: string; children?: IPromptNode[]; } // Markdown 解析函数 export function parseMarkdownToPromptNodes(markdown: string): IPromptNode[] { const lines = markdown .split("\n") .map((l) => l.trim()) .filter(Boolean); const result: IPromptNode[] = []; let currentButton: IPromptNode | null = null; let currentChild: { title: string; content: string[] } | null = null; for (const line of lines) { if (line.startsWith("# ")) { if (currentButton) { if (currentChild) { currentButton.children = currentButton.children || []; currentButton.children.push({ text: currentChild.title, prompt: currentChild.content.join("\n"), }); } result.push(currentButton); } currentButton = { text: line.replace("# ", ""), children: [] }; currentChild = null; } else if (line.startsWith("## ")) { if (currentChild) { currentButton!.children!.push({ text: currentChild.title, prompt: currentChild.content.join("\n"), }); } currentChild = { title: line.replace("## ", ""), content: [], }; } else { currentChild?.content.push(line); } } if (currentChild) { currentButton!.children!.push({ text: currentChild.title, prompt: currentChild.content.join("\n"), }); } if (currentButton) { // 若没有children但有currentChild.prompt,作为主按钮 prompt if (!currentButton.children?.length && currentChild?.content.length) { currentButton.prompt = currentChild.content.join("\n"); delete currentButton.children; } result.push(currentButton); } return result; } interface IWidget { onText?: (prompt: string) => void; disabled?: boolean; } // 按钮组组件 const PromptButtonGroup = ({ disabled = false, onText }: IWidget) => { const user = useAppSelector(currentUser); const [data, setData] = useState([]); useEffect(() => { if (!user) { return; } const getPromptOne = async (studio: string) => { const urlTpl = `/v2/article?view=template&studio_name=${studio}&subtitle=_template_prompt_&content=true`; const json = await get(urlTpl); if (json.ok) { if (json.data.rows.length > 0) { if (json.data.rows[0].content) { return json.data.rows[0].content; } } } return false; }; const getPrompt = async () => { const my = await getPromptOne(user.realName); if (my) { setData(parseMarkdownToPromptNodes(my)); return; } const system = await getPromptOne("admin"); if (system) { setData(parseMarkdownToPromptNodes(system)); } }; getPrompt().catch((e) => console.error(e)); }, [user, user?.realName]); return ( {data.map((node) => { if (node.children && node.children.length > 0) { const items: MenuProps["items"] = node.children.map((child, idx) => ({ key: `${node.text}-${idx}`, label: child.text, onClick: () => onText && onText(child.prompt || ""), })); return ( ); } else { return ( ); } })} ); }; export default PromptButtonGroup;