use-assistant-chat.ts
8.82 KB
-
perf(assistant): artifact 流式渲染 — 卡片边生成边"长出",不再干等整段 · 5737df69
诊断:render_artifact 慢点全在"模型逐字吐 ~5.5KB HTML"(~32s),而 tool-call 要等整段 生成完才触发 → 用户对着"生成中"干等 30s+。MCP/工具往返仅 0.2s,非瓶颈;默认模型已是 deepseek-v4-flash(已是快的)。 优化(感知为主): - 后端:消费 AI SDK 的 tool-input-delta,按 toolCallId 累积入参 JSON,增量提取 html 字段 (extractHtmlField 容忍半截转义),250ms 节流推 artifact_html 事件。 - 前端:artifact_html 按 callId upsert 到 artifact 块(实时增长),最终 tool_call 覆盖完整 html+title; ArtifactView 对 srcDoc 重建做 600ms 节流(避免每增量都重载 iframe 白闪 / Tailwind 反复重扫)。 - 提示词加"HTML 力求紧凑"以略减 token。 实测:卡片 html 从 ~8s 起流式到达(35→5537 字符,65 次增量),界面从第 ~8s 起"长出"卡片, 而非 ~35s 整张蹦出。两端 tsc 0。 注:总时长仍受模型出字速度限制(~40s);若要"秒开"常见列表,需走机制 A(预制组件直渲工具结果)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
luoqi committed