import { useCallback, useEffect, useMemo, useState } from 'react'; import { Sparkles } from 'lucide-react'; import { toast } from 'sonner'; import { PromptInput, PromptInputFooter, PromptInputSubmit, PromptInputTextarea, } from '@/components/ai-elements/prompt-input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { ArtifactsProvider } from '@/components/workspace/artifacts'; import { MessageList, type SetupWritingApprovalPayload, } from '@/components/workspace/messages'; import { ThreadContext } from '@/components/workspace/messages/context'; import { getAPIClient } from '@/core/api'; import { useModels } from '@/core/models/hooks'; import { useThreadStream } from '@/core/threads/hooks'; import { uuid } from '@/core/utils/uuid'; import { cn } from '@/lib/utils'; import { useArticleTypes } from '../../hooks/useArticleTypes'; import { useWritingForm } from '../../contexts/WritingFormContext'; /** * 写作配置 Q&A 面板(AI 写作入口页右侧,仅在未开始写作时显示)。 * * 复用智能体创建页的「对话 → setup_writing 工具审批卡 → 回填表单」模式: * - 用 ``useThreadStream`` 起一条独立的 lead_agent 线程,context 带 ``is_writing_setup``, * 后端走写作配置专用 bootstrap(只挂 setup_writing 工具)。 * - 模型调用 setup_writing 被 SetupWritingApprovalMiddleware 拦截成审批卡。 * - 用户点「应用到表单」→ ``applyProposal`` 把建议字段写进左侧写作表单(WritingFormContext)。 * * 这条线程只负责「帮用户把表单聊清」,与真正写作的 ai_writing graph 完全解耦。 */ export function WritingSetupChat() { const threadId = useMemo(() => uuid(), []); const [threadPrepared, setThreadPrepared] = useState(false); const [chatModel, setChatModel] = useState(''); const { models, isLoading: modelsLoading } = useModels(); const { articleTypes } = useArticleTypes(); const { applyProposal } = useWritingForm(); // 首个模型可用时默认选中 useEffect(() => { if (models.length === 0) return; setChatModel((prev) => (prev && models.some((m) => m.name === prev) ? prev : models[0]!.name)); }, [models]); // 注入后端 prompt 的合法文章类型(只取 key/label) const validArticleTypes = useMemo( () => articleTypes.map((t) => ({ key: t.key, label: t.label })), [articleTypes], ); const [thread, sendMessage] = useThreadStream({ threadId, context: { mode: 'flash', is_writing_setup: true, valid_article_types: validArticleTypes, model_name: chatModel || undefined, }, }); const ensureThreadExists = useCallback(async () => { if (threadPrepared) return; await getAPIClient().threads.create({ threadId, metadata: { thread_type: 'writing_setup' } }); setThreadPrepared(true); }, [threadId, threadPrepared]); // 生成中点提交按钮 = 暂停:取消当前 SSE 流(与主聊天页一致)。 const handleChatSubmit = useCallback( async (text: string) => { if (thread.isLoading) { void thread.stop(); return; } const trimmed = text.trim(); if (!trimmed) return; await ensureThreadExists(); await sendMessage(threadId, { text: trimmed, files: [] }); }, [thread, ensureThreadExists, sendMessage, threadId], ); // 提交按钮状态:生成中显示「停止」方块,可点击暂停对话 const submitStatus = thread.error ? 'error' : thread.isLoading ? 'streaming' : 'ready'; const handleApprove = useCallback( async (payload: SetupWritingApprovalPayload) => { applyProposal(payload); toast.success('已应用到左侧表单,可继续编辑或开始写作'); }, [applyProposal], ); const handleReject = useCallback(async () => { await handleChatSubmit('这份配置先不应用,请继续帮我调整。'); }, [handleChatSubmit]); return (

写作助手

描述你想写什么,我来帮你填好左侧配置
当前对话模型 {modelsLoading || models.length === 0 ? (
{modelsLoading ? '加载中…' : '暂无模型'}
) : ( )}
void handleChatSubmit(text)}>
); }