import { createUuid } from '../lib/uuid' import { useEffect, useRef, useState } from 'react' import type { ReactNode } from 'react' import { Send, Loader2 } from 'lucide-react' import { useNavigate } from 'react-router' import { api } from '../lib/api' import { conversationsChanged } from '../lib/conversations' import type { Conversation, ConversationMessage, QaScope } from '../lib/conversations' import type { AgentTask } from '../hooks/useTaskEvents' import { beijingDate, message } from '../lib/day2' import { ResearchTask } from './ResearchTask' import { PrimaryButton } from './ui-ext/AiButton' /** 会话保存任务引用,恢复历史只执行 GET;只有明确提交问题才创建一次收费模型任务。 */ export function ConversationView({ id: initialId, scope: initialScope, embedded = false, children }: { id?: string; scope?: QaScope; embedded?: boolean; children?: ReactNode }) { // 面板内创建会话后原地回读,不跳转离开检索页;关闭面板不取消已创建任务。 const [id, setId] = useState(initialId) const navigate = useNavigate() const [messages, setMessages] = useState([]) const [scope, setScope] = useState(initialScope) const [input, setInput] = useState(''), [error, setError] = useState(''), [busy, setBusy] = useState(false) const [ready, setReady] = useState(!id) const scrollRef = useRef(null) useEffect(() => { scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: 'smooth' }) }, [messages]) const inFlight = useRef(false) // 网络结果未知时保留会话与幂等键;重复点击或手动重试不产生第二次模型请求。 const pending = useRef<{ signature: string; key: string; conversationId?: string } | null>(null) useEffect(() => { if (!id) return let active = true void Promise.all([api(`/conversations/${id}`), api(`/conversations/${id}/messages`)]).then(async ([, rows]) => { if (!active) return setMessages(rows) const taskId = rows.at(-1)?.task_id if (taskId) { const task = await api(`/agent/tasks/${taskId}`) if (active) setScope(task.request.qa ?? undefined) } if (active) setReady(true) }).catch(e => { if (active) setError(message(e)) }) return () => { active = false } }, [id]) const ask = async (raw = input) => { if (inFlight.current || !raw.trim() || !scope) return inFlight.current = true; setBusy(true); setError('') try { // 生成提交键也属于提交流程;即使本地准备失败,finally 仍须解除发送锁以便重试。 const body = { question: raw.trim(), scope } const signature = JSON.stringify(body) if (pending.current?.signature !== signature) pending.current = { signature, key: createUuid(), conversationId: id } if (!pending.current.conversationId) { const c = await api('/conversations', { method: 'POST', body: JSON.stringify({ title: '' }) }) pending.current.conversationId = c.id } const conv = pending.current.conversationId await api(`/conversations/${conv}/messages`, { method: 'POST', headers: { 'Idempotency-Key': pending.current.key }, body: JSON.stringify(body) }) pending.current = null; setInput(''); conversationsChanged() if (id) { setMessages(await api(`/conversations/${id}/messages`)) } else if (embedded) setId(conv) else navigate(`/research?conv=${conv}`, { replace: true }) } catch (e) { setError(message(e)) } finally { inFlight.current = false; setBusy(false) } } const quick = scope?.projectId ? ['报名和投标截止时间', '公告参与条件', '预算口径与报价参考', '风险与注意事项'] : ['哪些项目报名快截止了', '这些项目的预算披露情况', '这些项目有哪些共同要求', '比较一下这些项目'] return
{children} {error &&

{error}

} {!ready && !error &&

正在读取历史会话…

} {messages.map(row =>

{row.content}

{row.task_id && }
)}
{ready && scope &&