import { useEffect, useRef, useState } from 'react' import { X, ExternalLink, Star, Send, Loader2, Sparkles } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import type { AiPanelState } from '../types' import { useStore, conditionsText, nowStr } from '../lib/store' import { projectAnalysis, projectFollowUp, searchQaAnswer } from '../lib/ai' import { Markdown } from './Markdown' import { useStreaming } from '../hooks/useStreaming' import { cn } from '@/lib/utils' interface Msg { role: 'user' | 'assistant' content: string streaming?: boolean } const PROJECT_QUICK = ['报名和投标截止时间', '公告参与条件', '预算口径与报价参考', '风险与注意事项'] const SEARCH_QUICK = ['哪些项目报名快截止了', '这些项目的预算披露情况', '这些项目有哪些共同要求', '比较一下这些项目'] export function AiPanel({ state, onClose }: { state: AiPanelState; onClose: () => void }) { const { profile, batches, addWatch, isWatched, removeWatch, findProject } = useStore() const [messages, setMessages] = useState([]) const [input, setInput] = useState('') const [busy, setBusy] = useState(false) const scrollRef = useRef(null) const { stream, stop } = useStreaming() const project = state.kind === 'project' ? findProject(state.projectId) : null const batch = state.kind === 'search' ? batches.find(b => b.id === state.batchId) : null const open = state.kind !== 'closed' // 打开或切换项目/批次时重置对话,保证对话绑定当前对象 useEffect(() => { if (!open) return stop() setInput('') setBusy(true) const initText = state.kind === 'project' && project ? projectAnalysis(project, profile) : state.kind === 'search' && batch ? `### 基于本次搜索提问\n\n本次对话对应实际执行的检索条件:**${conditionsText(batch.conditions)}**,执行时间 ${batch.executedAt},共 **${batch.total} 条结果**。\n\n你可以围绕这批结果比较、提问。我只基于本次结果回答,不会修改结果列表的范围或顺序;如结果分页,默认覆盖全部 ${batch.total} 条而非仅当前页。` : '暂无上下文' setMessages([{ role: 'assistant', content: '', streaming: true }]) stream(initText, (partial, done) => { setMessages([{ role: 'assistant', content: partial, streaming: !done }]) if (done) setBusy(false) }) // eslint-disable-next-line react-hooks/exhaustive-deps }, [state]) useEffect(() => { scrollRef.current?.scrollTo({ top: scrollRef.current.scrollHeight, behavior: 'smooth' }) }, [messages]) const ask = (q: string) => { const question = q.trim() if (!question || busy) return setInput('') setBusy(true) const answer = state.kind === 'project' && project ? projectFollowUp(project, question, profile) : state.kind === 'search' && batch ? searchQaAnswer(batch, question, profile) : '暂无上下文' setMessages(m => [...m, { role: 'user', content: question }, { role: 'assistant', content: '', streaming: true }]) setTimeout(() => { stream(answer, (partial, done) => { setMessages(m => { const next = [...m] next[next.length - 1] = { role: 'assistant', content: partial, streaming: !done } return next }) if (done) setBusy(false) }) }, 400) } const title = state.kind === 'project' ? '项目 AI 分析' : '基于本次搜索提问' const subtitle = state.kind === 'project' && project ? `${project.region} · ${project.purchaser}` : state.kind === 'search' && batch ? conditionsText(batch.conditions) : '' return ( {open && (
{/* 头部:项目名称与操作(加入重点关注不单独占行) */}
{title}

{state.kind === 'project' ? project?.name : '围绕当前检索结果提问'}

{subtitle}
{state.kind === 'project' && project && (
{project.sourceUrl ? ( 查看公告原文({project.sourceName}) ) : ( 来源:{project.sourceName}(链接暂未收录) )}
)}
{state.kind === 'project' && project && ( isWatched(project.id) ? ( ) : ( ) )}
{/* 消息区 */}
{messages.map((m, i) => (
{m.role === 'user' ? (
{m.content}
) : (
)}
))} {busy && messages[messages.length - 1]?.role === 'user' && (
正在分析…
)}
{/* 快捷问题 + 输入区(紧凑排列) */}