Ϊ roundtable µÚÈý²½ɳÏ䲹»ØÐ´°¿ڴò¿ªÓëÏÂÔزÙ×÷£¬²¢ÐÞÕýд×÷ÅäÖóÎÇåµÄÌύÁ÷³ÌÓëĬÈÏËزÄÀ´Դ£¬±ÜÃâÓû§ÔڶƬ³¡¾°ÏÂÌáǰÌύ»ò½øÈ벻·ûºÏԤÆڵļìË÷·¾¶¡£
172 lines
6.8 KiB
TypeScript
172 lines
6.8 KiB
TypeScript
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 (
|
||
<ThreadContext.Provider value={{ thread }}>
|
||
<ArtifactsProvider>
|
||
<div className="flex size-full flex-col bg-background">
|
||
<header className="flex shrink-0 items-center gap-2 border-b px-4 py-3">
|
||
<Sparkles className="size-4 text-primary" />
|
||
<h2 className="text-sm font-semibold">写作助手</h2>
|
||
<span className="text-muted-foreground text-xs">描述你想写什么,我来帮你填好左侧配置</span>
|
||
</header>
|
||
|
||
<div className="min-h-0 flex-1">
|
||
<MessageList
|
||
className="size-full"
|
||
threadId={threadId}
|
||
thread={thread}
|
||
onClarificationSubmit={handleChatSubmit}
|
||
onSetupWritingApprove={handleApprove}
|
||
onSetupWritingReject={handleReject}
|
||
/>
|
||
</div>
|
||
|
||
<div className="bg-background shrink-0 space-y-2 border-t px-4 py-4">
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-muted-foreground text-xs">当前对话模型</span>
|
||
{modelsLoading || models.length === 0 ? (
|
||
<div className="border-input bg-background text-muted-foreground flex h-7 min-w-[10rem] items-center rounded-md border px-2 text-xs shadow-xs dark:border-[#1B477E] dark:bg-[#022248]">
|
||
{modelsLoading ? '加载中…' : '暂无模型'}
|
||
</div>
|
||
) : (
|
||
<Select value={chatModel} onValueChange={setChatModel} disabled={thread.isLoading}>
|
||
<SelectTrigger
|
||
className={cn(
|
||
'border-input bg-background h-7 min-w-[10rem] px-2 text-xs shadow-xs outline-none',
|
||
'dark:border-[#1B477E] dark:bg-[#022248] dark:text-[#AACEFF]',
|
||
)}
|
||
>
|
||
<SelectValue placeholder="选择模型" />
|
||
</SelectTrigger>
|
||
<SelectContent position="popper" className="w-[var(--radix-select-trigger-width)] dark:border-[#1B477E] dark:bg-[#022248]">
|
||
{models.map((model) => (
|
||
<SelectItem key={model.name} value={model.name} className="dark:text-[#AACEFF]">
|
||
{model.display_name || model.name}
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
)}
|
||
</div>
|
||
<PromptInput onSubmit={({ text }) => void handleChatSubmit(text)}>
|
||
<PromptInputTextarea
|
||
placeholder="例如:帮我写一篇 2000 字、面向行业专家的新能源汽车出口分析"
|
||
/>
|
||
<PromptInputFooter className="justify-end">
|
||
<PromptInputSubmit status={submitStatus} />
|
||
</PromptInputFooter>
|
||
</PromptInput>
|
||
</div>
|
||
</div>
|
||
</ArtifactsProvider>
|
||
</ThreadContext.Provider>
|
||
);
|
||
}
|