import { Shield, UploadCloud, FileCheck, Loader2, RefreshCw } from 'lucide-react'; import { useState } from 'react'; import { useAppStore, useCurrentProject } from '../../stores/useAppStore'; import { useUIStore } from '../../stores/useUIStore'; import { useChatStore } from '../../stores/useChatStore'; import { ParseDeliveryStandard } from '../../../bindings/engimind/internal/parser/parseservice.js'; import { StreamTemplateDirectory } from '../../../bindings/engimind/internal/chat/chatservice.js'; import { Events } from '@wailsio/runtime'; export function TemplateCard() { const currentProject = useCurrentProject(); const { isParsingTemplate, hasNewTemplatePending, setParsingTemplate, setNewTemplatePending, setEditingOutline } = useUIStore(); const setProjects = useAppStore(s => s.setProjects); const currentProjectId = useAppStore(s => s.currentProjectId); const [pendingMarkdown, setPendingMarkdown] = useState(''); const startTemplateParse = async () => { if (!pendingMarkdown) return; try { setParsingTemplate(true); setNewTemplatePending(false); const activeModelId = useAppStore.getState().activeModelId; if (!activeModelId) { alert("提示:请先在配置中心连接一个有效的大语言模型提供商,否则无法进行文件解析!"); setParsingTemplate(false); setNewTemplatePending(true); return; } const cs = useChatStore.getState(); const userMsgId = Date.now(); const assistantMsgId = userMsgId + 1; const msgIdStr = assistantMsgId.toString(); cs.addMessage({ id: userMsgId, role: 'user', content: '我上传了一份工程交付文档。请帮我深度解析并归纳出标准大纲结构。' }); cs.addMessage({ id: assistantMsgId, role: 'assistant', content: '', status: 'processing' }); let unSub = Events.On("chat_stream_" + msgIdStr, (e: any) => { const fullText = Array.isArray(e.data) ? e.data[0] : e.data; if (typeof fullText === 'string') { const msg = cs.messages.find(m => m.id === assistantMsgId); if (msg && fullText.length > msg.content.length) { cs.updateMessage(assistantMsgId, { content: fullText }); } } }); // Invoke LLM to extract standard chapters let jsonStr = await StreamTemplateDirectory(pendingMarkdown, activeModelId, msgIdStr); unSub(); cs.updateMessage(assistantMsgId, { status: 'success' }); let jsonStrClean = jsonStr.replace(/[\s\S]*?<\/think>/g, '').trim(); jsonStrClean = jsonStrClean.replace(/```json/g, '').replace(/```/g, '').trim(); const startIdx = jsonStrClean.indexOf('['); const endIdx = jsonStrClean.lastIndexOf(']'); if (startIdx !== -1 && endIdx !== -1 && endIdx > startIdx) { jsonStrClean = jsonStrClean.substring(startIdx, endIdx + 1); } let parsedChapters; try { parsedChapters = JSON.parse(jsonStrClean); } catch (err) { console.error("LLM json decode failed:", jsonStrClean); alert("模型解析出的结果不符合要求格式,请重试!\n模型原始输出片段:" + jsonStrClean.substring(0, 100) + "..."); setParsingTemplate(false); setNewTemplatePending(true); return; } if (!Array.isArray(parsedChapters)) { parsedChapters = parsedChapters.chapters || parsedChapters.data || []; if (!Array.isArray(parsedChapters)) { parsedChapters = [parsedChapters]; } } const chapters = parsedChapters.map((c: any, i: number) => ({ id: c.id || `generated-${Date.now()}-${i}`, title: c.title || `第${i+1}节 内容生成`, status: 'idle', progress: 0, content: c.content || '' })); // Update project directory globally setProjects(prev => prev.map(p => { if (p.id !== currentProjectId) return p; return { ...p, activeTemplate: { name: 'AI 深层解析大纲', version: 'v1.0 (Auto)', chapters, }, }; })); setParsingTemplate(false); setPendingMarkdown(''); setEditingOutline(true); } catch (err: any) { console.error(err); alert("模型解析遇到错误:" + (err.message || err)); setParsingTemplate(false); setNewTemplatePending(true); } }; const handleUploadClick = async () => { if (currentProject.activeTemplate.chapters && currentProject.activeTemplate.chapters.length > 0) { if (!window.confirm("当前已经存在解析好的交付标准目录,确定要重新上传并替换吗?已有的内容和结构将会被覆盖。")) { return; } } try { const markdownContent = await ParseDeliveryStandard(); if (markdownContent) { setPendingMarkdown(markdownContent); setNewTemplatePending(true); } } catch (error: any) { alert("读取或转换文件内容失败: " + (error.message || error)); } }; return (
交付标准
{isParsingTemplate ? (
AI 解析目录中...
) : hasNewTemplatePending ? ( ) : (

{currentProject.activeTemplate.name}

{currentProject.activeTemplate.version} ACTIVE
)}
); }