import { useEffect, useRef, useState } from 'react' import { useNavigate, useSearchParams } from 'react-router' import { MessagesSquare, UserSearch, GitCompareArrows, Map, Send, X, Loader2 } from 'lucide-react' import { api } from '../lib/api' import { conversationsChanged } from '../lib/conversations' import type { Conversation } from '../lib/conversations' import type { AgentTask } from '../hooks/useTaskEvents' import type { ResearchRequest } from '../lib/day3' import { initialQuery, message, words } from '../lib/day2' import { ConversationView } from '../components/ConversationView' import { ResearchTask } from '../components/ResearchTask' import { PrimaryButton } from '../components/ui-ext/AiButton' const ENTRIES = [ { key: 'customer', icon: UserSearch, title: '查目标客户', desc: '这家公司做什么,采购过什么?', placeholder: '输入完整企业名称,如:某某水务集团有限公司' }, { key: 'competitor', icon: GitCompareArrows, title: '查竞争对手', desc: '这家公司的业务和客户情况怎样?', placeholder: '输入竞争对手的完整名称,可补充关注业务' }, { key: 'market', icon: Map, title: '查区域市场', desc: '某地区智慧水务需求如何?', placeholder: '如:调研华东地区近一年的智慧水务市场' }, ] as const /** 界面只使用原型的三个入口及输入框;真实任务仍在服务端创建、确认和保存。 */ export function ResearchPage() { const [params, setParams] = useSearchParams() const navigate = useNavigate() const entry = params.get('entry') || '' const selected = params.get('task') const conversationId = params.get('conv') const qaBatch = params.get('qaBatch'), qaProject = params.get('qaProject') const [input, setInput] = useState(params.get('input') || '') const [busy, setBusy] = useState(false), [error, setError] = useState('') const [budget, setBudget] = useState({ maxCalls: 18, maxCostUnits: 10 }) const [fileId, setFileId] = useState(null) const pending = useRef<{ signature: string; key: string; conversationId?: string } | null>(null) useEffect(() => { let active = true // 请求保护沿用服务端上限,不为了展示技术参数增加原型没有的设置入口。 void api<{ maxCalls: number; maxCostUnits: number }>('/system/limits').then(value => { if (active) setBudget({ maxCalls: Math.min(18, value.maxCalls), maxCostUnits: Math.min(10, value.maxCostUnits) }) }).catch(e => { if (active) setError(message(e)) }) return () => { active = false } }, []) useEffect(() => { const id = params.get('file') if (!id) return let active = true // 从已有项目携带的附件仍须校验账号归属与有效期,不能信任 URL 中的文件 ID。 void api<{ status: string }>(`/files/${encodeURIComponent(id)}`).then(file => { if (!active) return if (file.status !== 'READY') { setError('文件已删除或过期,请重新上传'); return } setFileId(id) }).catch(e => { if (active) setError(message(e)) }) return () => { active = false } }, [params]) const create = async (request: unknown, path = '/agent/tasks') => { const signature = `${path}:${JSON.stringify(request)}` if (pending.current?.signature !== signature) pending.current = { signature, key: crypto.randomUUID() } if (!pending.current.conversationId) { const conversation = await api('/conversations', { method: 'POST', body: JSON.stringify({ title: '' }) }) pending.current.conversationId = conversation.id } // 响应未知时保留原键和会话;重试只回读同一次提交,不重复创建收费任务。 const task = await api(path, { method: 'POST', headers: { 'Idempotency-Key': pending.current.key }, body: JSON.stringify(request) }) const conv = pending.current.conversationId await api(`/conversations/${conv}/tasks`, { method: 'POST', body: JSON.stringify({ taskId: task.id }) }) pending.current = null; conversationsChanged() if (path === '/searches') navigate(`/?batch=${task.id}`) else setParams({ conv }) } const start = async () => { if (busy || (!input.trim() && !fileId)) return setBusy(true); setError('') try { if (entry === 'market') { await create({ conditions: { ...initialQuery(), keywords: words(input), mode: 'MARKET' }, ...budget }, '/searches') } else { const project = entry === 'project' const research: ResearchRequest = { companies: project ? [] : [input.trim()], selections: {}, mode: 'QUICK', fileId: project ? fileId : null, parentId: params.get('parent'), targetProjectId: params.get('project'), focus: null, acknowledgedCalls: 0, } await create({ operation: project ? 'BID_RESEARCH' : 'COMPANY_RESEARCH', workflow: project ? 'BID_DECISION' : 'COMPANY_INTEL', input: project ? input.trim() : '', entityConfirmed: false, ...budget, research }) } } catch (e) { setError(message(e)) } finally { setBusy(false) } } if (conversationId || qaBatch || qaProject) return return
{selected ?
:

对话调研

{ENTRIES.map(item => )}
} {error &&

{error}

}
{!selected &&
{ e.preventDefault(); void start() }} className="mx-auto max-w-[820px]"> {ENTRIES.some(item => item.key === entry) &&
已选入口:{ENTRIES.find(item => item.key === entry)?.title}
}