| 1 | /* Chat:agentic 对话(检索/图谱工具 + remember 记忆)。 |
| 2 | 会话持久化:左栏会话列表;上下文由服务端拼,前端只发 conversation_id + 新消息; |
| 3 | 行动轨迹(steps)与引用(sources)随消息落库,历史回放与实时流共用渲染。 */ |
| 4 | import { useEffect, useRef, useState } from "react"; |
| 5 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 6 | import { Link, useNavigate, useParams } from "@tanstack/react-router"; |
| 7 | import Markdown from "react-markdown"; |
| 8 | import remarkGfm from "remark-gfm"; |
| 9 | import rehypeHighlight from "rehype-highlight"; |
| 10 | import remend from "remend"; |
| 11 | import { |
| 12 | ArrowUp, |
| 13 | BookOpen, |
| 14 | Check, |
| 15 | ChevronDown, |
| 16 | Database, |
| 17 | GitCompareArrows, |
| 18 | History, |
| 19 | Search as SearchIcon, |
| 20 | Square, |
| 21 | SquarePen, |
| 22 | MoreHorizontal, |
| 23 | Waypoints, |
| 24 | Wrench, |
| 25 | } from "lucide-react"; |
| 26 | import { ThinkingOrb, type OrbState } from "thinking-orbs"; |
| 27 | import { |
| 28 | conversationsApi, |
| 29 | streamChat, |
| 30 | type ChatStep, |
| 31 | type ConversationRow, |
| 32 | type Source, |
| 33 | } from "../api"; |
| 34 | import { S } from "../i18n"; |
| 35 | import { toast } from "../toast"; |
| 36 | import { useKb } from "../kb"; |
| 37 | import { DangerConfirm, RAIL_CLS } from "../ui"; |
| 38 | |
| 39 | interface Turn { |
| 40 | role: "user" | "assistant"; |
| 41 | content: string; |
| 42 | sources?: Source[]; |
| 43 | steps?: ChatStep[]; |
| 44 | error?: string; |
| 45 | } |
| 46 | |
| 47 | /** 同标签页记忆:上次会话(按库)与未发送草稿——切页回来还原,新标签页从头开始 */ |
| 48 | const lastKey = (kbId: string) => `chat:last:${kbId}`; |
| 49 | const DRAFT_KEY = "chat:draft"; |
| 50 | |
| 51 | export function Chat() { |
| 52 | const { kb, kbs, setKb } = useKb(); |
| 53 | const queryClient = useQueryClient(); |
| 54 | const navigate = useNavigate(); |
| 55 | // 会话即路由:/chat/$conversationId,URL 是当前会话的唯一事实来源(刷新/回退天然可用) |
| 56 | const { conversationId: routeConvId } = useParams({ strict: false }) as { |
| 57 | conversationId?: string; |
| 58 | }; |
| 59 | const [activeId, setActiveId] = useState<string | null>(null); |
| 60 | // 路由同步 effect 的判据:state 的提交时序晚于 navigate 触发的重渲染, |
| 61 | // 用 ref 同步写入才能让"流式新建后仅换 URL"的守卫可靠命中 |
| 62 | const activeIdRef = useRef<string | null>(null); |
| 63 | const [turns, setTurns] = useState<Turn[]>([]); |
| 64 | const [input, setInput] = useState(() => sessionStorage.getItem(DRAFT_KEY) ?? ""); |
| 65 | const [streaming, setStreaming] = useState(false); |
| 66 | const [scopeOpen, setScopeOpen] = useState(false); |
| 67 | const [pendingDelete, setPendingDelete] = useState<ConversationRow | null>(null); |
| 68 | // 会话搜索。**搜标题也搜正文**——人记得住的往往是问过的那句话 |
| 69 | const [convSearch, setConvSearch] = useState(""); |
| 70 | // 三点菜单展开的是哪一条。同时只开一个 |
| 71 | const [menuFor, setMenuFor] = useState<string | null>(null); |
| 72 | const abortRef = useRef<(() => void) | null>(null); |
| 73 | const scopeRef = useRef<HTMLDivElement>(null); |
| 74 | const bottomRef = useRef<HTMLDivElement>(null); |
| 75 | const inputRef = useRef<HTMLTextAreaElement>(null); |
| 76 | |
| 77 | // 作用域弹层:点外面 / Esc 关闭(与 ui/Dropdown 同惯例) |
| 78 | useEffect(() => { |
| 79 | if (!scopeOpen) return; |
| 80 | const onDoc = (e: MouseEvent) => { |
| 81 | if (!scopeRef.current?.contains(e.target as Node)) setScopeOpen(false); |
| 82 | }; |
| 83 | const onKey = (e: KeyboardEvent) => { |
| 84 | if (e.key === "Escape") setScopeOpen(false); |
| 85 | }; |
| 86 | document.addEventListener("mousedown", onDoc); |
| 87 | document.addEventListener("keydown", onKey); |
| 88 | return () => { |
| 89 | document.removeEventListener("mousedown", onDoc); |
| 90 | document.removeEventListener("keydown", onKey); |
| 91 | }; |
| 92 | }, [scopeOpen]); |
| 93 | |
| 94 | const convs = useQuery({ |
| 95 | queryKey: ["conversations", kb?.id, convSearch], |
| 96 | queryFn: () => conversationsApi.list(kb!.id, convSearch), |
| 97 | enabled: !!kb, |
| 98 | placeholderData: (prev) => prev, |
| 99 | }); |
| 100 | // 改标题:**就地编辑**,不弹对话框——改一个名字不值得打断整页 |
| 101 | const [renamingId, setRenamingId] = useState<string | null>(null); |
| 102 | const [renameDraft, setRenameDraft] = useState(""); |
| 103 | const rename = useMutation({ |
| 104 | mutationFn: (v: { id: string; title: string }) => |
| 105 | conversationsApi.rename(kb!.id, v.id, v.title), |
| 106 | onSuccess: () => { |
| 107 | setRenamingId(null); |
| 108 | queryClient.invalidateQueries({ queryKey: ["conversations", kb?.id] }); |
| 109 | }, |
| 110 | onError: (e: Error) => toast.error(e.message), |
| 111 | }); |
| 112 | |
| 113 | // 直落底部(instant):平滑滚动在流式追加下会一路慢爬 |
| 114 | useEffect(() => { |
| 115 | bottomRef.current?.scrollIntoView({ behavior: "instant" }); |
| 116 | }, [turns]); |
| 117 | |
| 118 | // 切库回到新会话(首次拿到 kb 不算切换——直刷 /chat/$id 时不能把 URL 冲掉) |
| 119 | const prevKbRef = useRef<string | null>(null); |
| 120 | useEffect(() => { |
| 121 | const prev = prevKbRef.current; |
| 122 | prevKbRef.current = kb?.id ?? null; |
| 123 | if (prev && kb && prev !== kb.id) { |
| 124 | if (streaming) abortRef.current?.(); |
| 125 | setStreaming(false); |
| 126 | activeIdRef.current = null; |
| 127 | setActiveId(null); |
| 128 | setTurns([]); |
| 129 | navigate({ to: "/chat", replace: true }); |
| 130 | } |
| 131 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 132 | }, [kb?.id]); |
| 133 | |
| 134 | // 路由 → 会话装载;裸 /chat 还原本库上次会话(切页回来仍在原对话) |
| 135 | useEffect(() => { |
| 136 | if (!kb) return; |
| 137 | if (!routeConvId) { |
| 138 | const last = sessionStorage.getItem(lastKey(kb.id)); |
| 139 | if (last) { |
| 140 | navigate({ |
| 141 | to: "/chat/$conversationId", |
| 142 | params: { conversationId: last }, |
| 143 | replace: true, |
| 144 | }); |
| 145 | } |
| 146 | return; |
| 147 | } |
| 148 | if (routeConvId === activeIdRef.current) return; // 流式新建会话后仅 URL 同步,勿重载 |
| 149 | loadConversation(routeConvId); |
| 150 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 151 | }, [kb?.id, routeConvId]); |
| 152 | |
| 153 | // 还原的草稿撑开输入框(高度平时由 onChange 维护) |
| 154 | useEffect(() => { |
| 155 | const el = inputRef.current; |
| 156 | if (el && el.value) { |
| 157 | el.style.height = "auto"; |
| 158 | el.style.height = `${Math.min(el.scrollHeight, 192)}px`; |
| 159 | } |
| 160 | }, []); |
| 161 | |
| 162 | const invalidateList = () => |
| 163 | queryClient.invalidateQueries({ queryKey: ["conversations", kb?.id] }); |
| 164 | |
| 165 | /** 列表点击只改 URL,装载由路由同步 effect 负责 */ |
| 166 | const openConversation = (id: string) => |
| 167 | navigate({ to: "/chat/$conversationId", params: { conversationId: id } }); |
| 168 | |
| 169 | const loadConversation = async (id: string) => { |
| 170 | if (streaming) abortRef.current?.(); |
| 171 | setStreaming(false); |
| 172 | activeIdRef.current = id; |
| 173 | setActiveId(id); |
| 174 | try { |
| 175 | const { messages } = await conversationsApi.detail(kb!.id, id); |
| 176 | sessionStorage.setItem(lastKey(kb!.id), id); |
| 177 | setTurns( |
| 178 | messages.map((m) => ({ |
| 179 | role: m.role, |
| 180 | content: m.content, |
| 181 | steps: m.steps.length ? m.steps : undefined, |
| 182 | sources: m.sources.length ? m.sources : undefined, |
| 183 | })), |
| 184 | ); |
| 185 | } catch { |
| 186 | // 失效链接(会话已删 / 属于别的库):安静回到新对话 |
| 187 | sessionStorage.removeItem(lastKey(kb!.id)); |
| 188 | activeIdRef.current = null; |
| 189 | setActiveId(null); |
| 190 | setTurns([]); |
| 191 | navigate({ to: "/chat", replace: true }); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | const newChat = () => { |
| 196 | if (streaming) abortRef.current?.(); |
| 197 | setStreaming(false); |
| 198 | if (kb) sessionStorage.removeItem(lastKey(kb.id)); |
| 199 | activeIdRef.current = null; |
| 200 | setActiveId(null); |
| 201 | setTurns([]); |
| 202 | navigate({ to: "/chat" }); |
| 203 | inputRef.current?.focus(); |
| 204 | }; |
| 205 | |
| 206 | const removeConversation = async (id: string) => { |
| 207 | await conversationsApi.remove(kb!.id, id); |
| 208 | if (sessionStorage.getItem(lastKey(kb!.id)) === id) { |
| 209 | sessionStorage.removeItem(lastKey(kb!.id)); |
| 210 | } |
| 211 | invalidateList(); |
| 212 | if (id === activeId) newChat(); |
| 213 | }; |
| 214 | |
| 215 | const send = () => { |
| 216 | const q = input.trim(); |
| 217 | if (!q || streaming || !kb) return; |
| 218 | setInput(""); |
| 219 | sessionStorage.removeItem(DRAFT_KEY); |
| 220 | if (inputRef.current) inputRef.current.style.height = "auto"; |
| 221 | setTurns((prev) => [...prev, { role: "user", content: q }, { role: "assistant", content: "" }]); |
| 222 | setStreaming(true); |
| 223 | |
| 224 | abortRef.current = streamChat( |
| 225 | kb.id, |
| 226 | { conversation_id: activeId ?? undefined, message: q }, |
| 227 | { |
| 228 | onConversation: (id) => { |
| 229 | // 先同步写 ref 再换 URL:路由同步 effect 因 id 相等而跳过重载,不打断流 |
| 230 | activeIdRef.current = id; |
| 231 | setActiveId(id); |
| 232 | sessionStorage.setItem(lastKey(kb.id), id); |
| 233 | navigate({ |
| 234 | to: "/chat/$conversationId", |
| 235 | params: { conversationId: id }, |
| 236 | replace: true, |
| 237 | }); |
| 238 | invalidateList(); |
| 239 | }, |
| 240 | onSources: (sources) => |
| 241 | setTurns((prev) => { |
| 242 | const next = [...prev]; |
| 243 | next[next.length - 1] = { ...next[next.length - 1], sources }; |
| 244 | return next; |
| 245 | }), |
| 246 | onStep: (step) => |
| 247 | setTurns((prev) => { |
| 248 | const next = [...prev]; |
| 249 | const last = next[next.length - 1]; |
| 250 | next[next.length - 1] = { ...last, steps: [...(last.steps ?? []), step] }; |
| 251 | return next; |
| 252 | }), |
| 253 | onDelta: (text) => |
| 254 | setTurns((prev) => { |
| 255 | const next = [...prev]; |
| 256 | const last = next[next.length - 1]; |
| 257 | next[next.length - 1] = { ...last, content: last.content + text }; |
| 258 | return next; |
| 259 | }), |
| 260 | onDone: () => { |
| 261 | setStreaming(false); |
| 262 | invalidateList(); |
| 263 | }, |
| 264 | onError: (message) => { |
| 265 | setStreaming(false); |
| 266 | setTurns((prev) => { |
| 267 | const next = [...prev]; |
| 268 | next[next.length - 1] = { ...next[next.length - 1], error: message }; |
| 269 | return next; |
| 270 | }); |
| 271 | }, |
| 272 | }, |
| 273 | ); |
| 274 | }; |
| 275 | |
| 276 | /* Composer 卡:新对话首屏居中出场,进入对话后停靠底部(同一块 JSX 两处复用) */ |
| 277 | const composerCard = ( |
| 278 | <div className="rounded-2xl border border-white/[0.12] bg-white/[0.04] backdrop-blur-md focus-within:border-white/30 transition-colors px-4 pt-3 pb-2"> |
| 279 | <textarea |
| 280 | ref={inputRef} |
| 281 | rows={1} |
| 282 | className="w-full bg-transparent outline-none text-sm resize-none leading-relaxed max-h-48 u-scroll placeholder:text-neutral-600" |
| 283 | placeholder={S.ask.placeholder} |
| 284 | value={input} |
| 285 | onChange={(e) => { |
| 286 | setInput(e.target.value); |
| 287 | sessionStorage.setItem(DRAFT_KEY, e.target.value); |
| 288 | const el = e.currentTarget; |
| 289 | el.style.height = "auto"; |
| 290 | el.style.height = `${Math.min(el.scrollHeight, 192)}px`; |
| 291 | }} |
| 292 | onKeyDown={(e) => { |
| 293 | if (e.key === "Enter" && !e.shiftKey && !e.nativeEvent.isComposing) { |
| 294 | e.preventDefault(); |
| 295 | send(); |
| 296 | } |
| 297 | }} |
| 298 | /> |
| 299 | <div className="flex items-center justify-between gap-3 pt-1"> |
| 300 | <div className="flex items-center gap-2.5 min-w-0"> |
| 301 | {/* 作用域 chip:提问点位可见"在问哪个库",切库沿用现有语义(开新会话) */} |
| 302 | <div ref={scopeRef} className="relative shrink-0"> |
| 303 | <button |
| 304 | onClick={() => setScopeOpen((v) => !v)} |
| 305 | title={S.ask.scopeLabel} |
| 306 | className="flex items-center gap-1.5 rounded-lg px-2 py-1 text-xs text-neutral-400 hover:text-neutral-200 hover:bg-white/[0.07] transition-colors max-w-52" |
| 307 | > |
| 308 | <Database size={12} className="shrink-0 text-neutral-500" /> |
| 309 | <span className="truncate">{kb?.name ?? "…"}</span> |
| 310 | <ChevronDown |
| 311 | size={11} |
| 312 | className={`shrink-0 text-neutral-600 transition-transform ${ |
| 313 | scopeOpen ? "rotate-180" : "" |
| 314 | }`} |
| 315 | /> |
| 316 | </button> |
| 317 | {scopeOpen && ( |
| 318 | <div className="u-pop u-pop-up absolute bottom-full mb-1.5 left-0 z-50 w-56 rounded-lg shadow-xl overflow-hidden"> |
| 319 | <div className="px-2.5 pt-2 pb-1 text-[9.5px] font-medium uppercase tracking-[0.1em] text-neutral-600 border-b border-white/5"> |
| 320 | {S.ask.scopeLabel} |
| 321 | </div> |
| 322 | <div className="u-scroll max-h-60 overflow-y-auto"> |
| 323 | {kbs.map((k) => ( |
| 324 | <button |
| 325 | key={k.id} |
| 326 | onClick={() => { |
| 327 | setScopeOpen(false); |
| 328 | if (k.id !== kb?.id) setKb(k.id); |
| 329 | }} |
| 330 | className={`w-full flex items-center gap-2 text-left px-2.5 py-1.5 text-xs ${ |
| 331 | k.id === kb?.id |
| 332 | ? "bg-white/[0.12] text-white" |
| 333 | : "text-neutral-300 hover:bg-white/[0.06] hover:text-white" |
| 334 | }`} |
| 335 | > |
| 336 | <span className="flex-1 min-w-0 truncate">{k.name}</span> |
| 337 | {k.id === kb?.id && <Check size={12} className="shrink-0 text-neutral-400" />} |
| 338 | </button> |
| 339 | ))} |
| 340 | </div> |
| 341 | </div> |
| 342 | )} |
| 343 | </div> |
| 344 | <span className="text-[11px] text-neutral-600 truncate">{S.ask.composerHint}</span> |
| 345 | </div> |
| 346 | {streaming ? ( |
| 347 | <button |
| 348 | onClick={() => { |
| 349 | abortRef.current?.(); |
| 350 | setStreaming(false); |
| 351 | }} |
| 352 | title={S.ask.stop} |
| 353 | className="h-8 w-8 shrink-0 rounded-lg grid place-items-center bg-white/[0.08] text-neutral-200 hover:bg-white/[0.14] transition-colors" |
| 354 | > |
| 355 | <Square size={11} fill="currentColor" /> |
| 356 | </button> |
| 357 | ) : ( |
| 358 | <button |
| 359 | onClick={send} |
| 360 | disabled={!input.trim()} |
| 361 | title={S.ask.send} |
| 362 | className={`h-8 w-8 shrink-0 rounded-lg grid place-items-center transition-colors ${ |
| 363 | input.trim() |
| 364 | ? "bg-white text-black hover:bg-neutral-200" |
| 365 | : "bg-white/[0.07] text-neutral-600" |
| 366 | }`} |
| 367 | > |
| 368 | <ArrowUp size={15} strokeWidth={2.4} /> |
| 369 | </button> |
| 370 | )} |
| 371 | </div> |
| 372 | </div> |
| 373 | ); |
| 374 | |
| 375 | return ( |
| 376 | <div className="h-full flex"> |
| 377 | {/* 会话栏 */} |
| 378 | <aside className={`${RAIL_CLS} flex flex-col`}> |
| 379 | <div className="px-2 pt-3 pb-1"> |
| 380 | {/* 与会话行同一套样式:左栏是一列同质的行,新对话只是第一行 */} |
| 381 | <button |
| 382 | onClick={newChat} |
| 383 | className="w-full flex items-center gap-2 rounded-lg px-2.5 py-2 text-left text-[13px] text-neutral-300 hover:bg-white/[0.05] hover:text-white transition-colors" |
| 384 | > |
| 385 | <SquarePen size={14} className="shrink-0 text-neutral-500" /> |
| 386 | {S.ask.newChat} |
| 387 | </button> |
| 388 | </div> |
| 389 | {/* 搜索。**标题重是常态**(同一个问题问两次就重了),而正文里那句话 |
| 390 | 才是人记得住的——所以服务端两处都搜 */} |
| 391 | <div className="px-2 pb-2"> |
| 392 | <input |
| 393 | className="input-dark w-full px-2.5 py-1.5 text-[12.5px]" |
| 394 | placeholder={S.ask.searchConversations} |
| 395 | value={convSearch} |
| 396 | onChange={(e) => setConvSearch(e.target.value)} |
| 397 | onKeyDown={(e) => e.key === "Escape" && setConvSearch("")} |
| 398 | /> |
| 399 | </div> |
| 400 | <div className="u-scroll flex-1 overflow-y-auto px-2 pb-3 space-y-0.5"> |
| 401 | {(convs.data?.conversations ?? []).map((c: ConversationRow) => ( |
| 402 | <div |
| 403 | key={c.id} |
| 404 | className={`group relative rounded-lg transition-colors ${ |
| 405 | c.id === activeId ? "u-nav-active" : "hover:bg-white/[0.05]" |
| 406 | }`} |
| 407 | > |
| 408 | {/* 单行标题;删除键悬停浮现(弹确认,不直接删) */} |
| 409 | {renamingId === c.id ? ( |
| 410 | /* 就地编辑:Enter 保存、Esc 取消。改一个名字不值得弹对话框 */ |
| 411 | <input |
| 412 | autoFocus |
| 413 | className="input-dark w-full px-2 py-1.5 text-[13px]" |
| 414 | value={renameDraft} |
| 415 | onChange={(e) => setRenameDraft(e.target.value)} |
| 416 | onBlur={() => setRenamingId(null)} |
| 417 | onKeyDown={(e) => { |
| 418 | if (e.key === "Enter" && renameDraft.trim()) |
| 419 | rename.mutate({ id: c.id, title: renameDraft }); |
| 420 | if (e.key === "Escape") setRenamingId(null); |
| 421 | }} |
| 422 | /> |
| 423 | ) : ( |
| 424 | <button |
| 425 | onClick={() => openConversation(c.id)} |
| 426 | className="w-full text-left px-2.5 py-2" |
| 427 | > |
| 428 | <span |
| 429 | className={`block truncate pr-5 text-[13px] ${ |
| 430 | c.id === activeId ? "text-white" : "text-neutral-300" |
| 431 | }`} |
| 432 | > |
| 433 | {c.title || S.ask.untitled} |
| 434 | </span> |
| 435 | </button> |
| 436 | )} |
| 437 | {/* 三点菜单:**一个入口装下所有动作**。从前右边直接是删除, |
| 438 | 而删除是这里最不该一步到位的那个 */} |
| 439 | {renamingId !== c.id && ( |
| 440 | <button |
| 441 | onClick={() => setMenuFor(menuFor === c.id ? null : c.id)} |
| 442 | title={S.ask.moreActions} |
| 443 | className="absolute right-2 top-1/2 -translate-y-1/2 hidden group-hover:block text-neutral-600 hover:text-neutral-200" |
| 444 | > |
| 445 | <MoreHorizontal size={14} /> |
| 446 | </button> |
| 447 | )} |
| 448 | {menuFor === c.id && ( |
| 449 | <> |
| 450 | {/* 点别处就关。铺满全屏而不是监听 document:不必在卸载时 |
| 451 | 记得摘监听器 */} |
| 452 | <div |
| 453 | className="fixed inset-0 z-10" |
| 454 | onClick={() => setMenuFor(null)} |
| 455 | /> |
| 456 | <div className="glass-strong absolute right-2 top-8 z-20 w-32 rounded-lg py-1 shadow-xl"> |
| 457 | <button |
| 458 | className="w-full px-3 py-1.5 text-left text-xs text-neutral-300 hover:bg-white/5" |
| 459 | onClick={() => { |
| 460 | setRenameDraft(c.title || ""); |
| 461 | setRenamingId(c.id); |
| 462 | setMenuFor(null); |
| 463 | }} |
| 464 | > |
| 465 | {S.ask.rename} |
| 466 | </button> |
| 467 | <button |
| 468 | className="w-full px-3 py-1.5 text-left text-xs text-neutral-300 hover:bg-white/5" |
| 469 | onClick={() => { |
| 470 | navigator.clipboard?.writeText(c.title || ""); |
| 471 | setMenuFor(null); |
| 472 | }} |
| 473 | > |
| 474 | {S.ask.copyTitle} |
| 475 | </button> |
| 476 | <button |
| 477 | className="w-full px-3 py-1.5 text-left text-xs text-[var(--u-danger)] hover:bg-white/5" |
| 478 | onClick={() => { |
| 479 | setPendingDelete(c); |
| 480 | setMenuFor(null); |
| 481 | }} |
| 482 | > |
| 483 | {S.ask.deleteConversation} |
| 484 | </button> |
| 485 | </div> |
| 486 | </> |
| 487 | )} |
| 488 | </div> |
| 489 | ))} |
| 490 | {convs.data?.conversations.length === 0 && ( |
| 491 | <p className="px-2.5 py-2 text-xs text-neutral-600">{S.ask.noConversations}</p> |
| 492 | )} |
| 493 | </div> |
| 494 | </aside> |
| 495 | |
| 496 | {/* 对话区:新对话首屏 = 问候 + 居中 composer(ChatGPT/Claude 惯例); |
| 497 | 有消息后 composer 停靠底部 */} |
| 498 | <div className="flex-1 min-w-0 flex flex-col"> |
| 499 | {turns.length === 0 ? ( |
| 500 | /* 锚定上三分之一而非垂直居中:居中在高窗口下会显得下坠。 |
| 501 | 22vh + 顶部 chrome(~100px) ≈ 问候落在 37% 高度、composer 中心 ~49% */ |
| 502 | <div className="flex-1 px-4 pt-[22vh]"> |
| 503 | <div className="w-full max-w-3xl mx-auto"> |
| 504 | <h1 |
| 505 | className="text-center text-[26px] text-neutral-100 mb-9" |
| 506 | style={{ fontFamily: "var(--font-brand)", letterSpacing: "0.03em" }} |
| 507 | > |
| 508 | {S.ask.greeting} |
| 509 | </h1> |
| 510 | {composerCard} |
| 511 | </div> |
| 512 | </div> |
| 513 | ) : ( |
| 514 | <> |
| 515 | <div className="flex-1 overflow-y-auto u-scroll px-4 py-6"> |
| 516 | <div className="max-w-3xl mx-auto space-y-4"> |
| 517 | {turns.map((t, i) => ( |
| 518 | <TurnView key={i} turn={t} live={streaming && i === turns.length - 1} /> |
| 519 | ))} |
| 520 | <div ref={bottomRef} /> |
| 521 | </div> |
| 522 | </div> |
| 523 | <div className="px-4 pb-4 pt-2"> |
| 524 | <div className="max-w-3xl mx-auto">{composerCard}</div> |
| 525 | </div> |
| 526 | </> |
| 527 | )} |
| 528 | </div> |
| 529 | |
| 530 | {pendingDelete && ( |
| 531 | <DangerConfirm |
| 532 | title={S.ask.deleteTitle} |
| 533 | hint={S.ask.deleteHint(pendingDelete.title || S.ask.untitled)} |
| 534 | confirmLabel={S.ask.deleteBtn} |
| 535 | cancelLabel={S.ask.cancel} |
| 536 | onConfirm={() => { |
| 537 | removeConversation(pendingDelete.id); |
| 538 | setPendingDelete(null); |
| 539 | }} |
| 540 | onCancel={() => setPendingDelete(null)} |
| 541 | /> |
| 542 | )} |
| 543 | </div> |
| 544 | ); |
| 545 | } |
| 546 | |
| 547 | function stepIcon(kind: ChatStep["kind"]) { |
| 548 | if (kind === "search") return <SearchIcon size={11} />; |
| 549 | if (kind === "docs") return <BookOpen size={11} />; |
| 550 | if (kind === "entity") return <Waypoints size={11} />; |
| 551 | if (kind === "facts") return <History size={11} />; |
| 552 | // facts 读世界轴、changes 读认知轴,两个图谱工具给不同的图标—— |
| 553 | // 用户看步骤条时该看得出问的是哪根轴 |
| 554 | if (kind === "changes") return <GitCompareArrows size={11} />; |
| 555 | if (kind === "query") return <Database size={11} />; |
| 556 | return <Wrench size={11} />; |
| 557 | } |
| 558 | |
| 559 | /** 工具步骤 → 球体状态:思考球讲当前动作的语言 */ |
| 560 | function orbState(kind?: ChatStep["kind"]): OrbState { |
| 561 | if (kind === "search" || kind === "docs") return "searching"; |
| 562 | if (kind === "entity") return "connecting"; |
| 563 | if (kind === "facts" || kind === "changes") return "solving"; |
| 564 | if (kind === "query" || kind === "tool") return "working"; |
| 565 | return "listening"; // 尚无步骤:刚接到消息 |
| 566 | } |
| 567 | |
| 568 | /** 思考指示:thinking-orbs 球体 + 当前动作(应用是深色定妆,theme 钉死 dark)。 */ |
| 569 | function Thinking({ step }: { step?: ChatStep }) { |
| 570 | return ( |
| 571 | <span className="inline-flex items-center gap-2.5 text-neutral-500"> |
| 572 | <ThinkingOrb state={orbState(step?.kind)} size={20} theme="dark" /> |
| 573 | {step && ( |
| 574 | <span className="text-xs truncate"> |
| 575 | {step.label} · {step.detail} |
| 576 | </span> |
| 577 | )} |
| 578 | </span> |
| 579 | ); |
| 580 | } |
| 581 | |
| 582 | function TurnView({ turn, live }: { turn: Turn; live?: boolean }) { |
| 583 | if (turn.role === "user") { |
| 584 | return ( |
| 585 | <div className="flex justify-end"> |
| 586 | <div className="u-bubble-user max-w-[85%] rounded-2xl rounded-tr-sm px-4 py-2 text-sm whitespace-pre-wrap text-neutral-100"> |
| 587 | {turn.content} |
| 588 | </div> |
| 589 | </div> |
| 590 | ); |
| 591 | } |
| 592 | |
| 593 | const thinking = live && !turn.content && !turn.error; |
| 594 | const lastStep = turn.steps?.[turn.steps.length - 1]; |
| 595 | |
| 596 | return ( |
| 597 | <div className="max-w-[95%]"> |
| 598 | {/* agent 回复无气泡:正文直接落在画布上(用户消息保留气泡以区分角色) */} |
| 599 | <div className="py-1 text-sm text-neutral-200 leading-relaxed"> |
| 600 | {turn.steps && turn.steps.length > 0 && ( |
| 601 | <div className="mb-2.5 space-y-1 border-l border-white/15 pl-2.5"> |
| 602 | {turn.steps.map((s, i) => ( |
| 603 | <div key={i} className="flex items-center gap-1.5 text-xs"> |
| 604 | <span className="text-neutral-600">{stepIcon(s.kind)}</span> |
| 605 | <span className="text-neutral-400 truncate">{s.label}</span> |
| 606 | <span className="text-neutral-600 shrink-0">· {s.detail}</span> |
| 607 | </div> |
| 608 | ))} |
| 609 | </div> |
| 610 | )} |
| 611 | {turn.content && ( |
| 612 | /* react-markdown 承载渲染(皮肤全归 u-chat-prose 设计系统), |
| 613 | 流式中经 remend 修补未闭合语法(粗体/围栏/链接), |
| 614 | rehype-highlight 做代码高亮——成熟件组装,观感自持 */ |
| 615 | <div className="u-chat-prose"> |
| 616 | <Markdown remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeHighlight]}> |
| 617 | {live ? remend(turn.content) : turn.content} |
| 618 | </Markdown> |
| 619 | </div> |
| 620 | )} |
| 621 | {thinking && <Thinking step={lastStep} />} |
| 622 | {turn.error && <div className="text-rose-400">{turn.error}</div>} |
| 623 | </div> |
| 624 | {turn.sources && turn.sources.length > 0 && ( |
| 625 | <div className="mt-2 space-y-1"> |
| 626 | {turn.sources.map((s) => |
| 627 | s.kind === "charter" ? ( |
| 628 | /* 手册引用:视觉上与数据引用隔离(BookOpen),跳排版好的 /docs 小节 */ |
| 629 | <Link |
| 630 | key={s.n} |
| 631 | to="/docs/$slug" |
| 632 | params={{ slug: s.slug! }} |
| 633 | hash={s.anchor || undefined} |
| 634 | title={s.excerpt} |
| 635 | className="flex items-center gap-1.5 text-xs text-neutral-500 glass rounded-lg px-3 py-1.5 glass-hover hover:text-neutral-300" |
| 636 | > |
| 637 | <span className="u-num text-[var(--u-accent)]">[{s.n}]</span> |
| 638 | <BookOpen size={11} className="shrink-0 text-neutral-600" /> |
| 639 | <span className="truncate"> |
| 640 | {/* 引言节 heading 即文章名,避免 "X › X" */} |
| 641 | {s.heading && s.heading !== s.filename |
| 642 | ? `${s.filename} › ${s.heading}` |
| 643 | : s.filename} |
| 644 | </span> |
| 645 | </Link> |
| 646 | ) : ( |
| 647 | <Link |
| 648 | key={s.n} |
| 649 | to="/doc/$docId" |
| 650 | params={{ docId: s.document_id! }} |
| 651 | search={{ chunk: s.chunk_id }} |
| 652 | title={s.excerpt} |
| 653 | className="block text-xs text-neutral-500 glass rounded-lg px-3 py-1.5 glass-hover hover:text-neutral-300" |
| 654 | > |
| 655 | <span className="u-num text-[var(--u-accent)]">[{s.n}]</span> {s.filename} ·{" "} |
| 656 | {s.excerpt.slice(0, 60)}… |
| 657 | </Link> |
| 658 | ), |
| 659 | )} |
| 660 | </div> |
| 661 | )} |
| 662 | </div> |
| 663 | ); |
| 664 | } |