chat 流式优化:LLM streaming + 前端 SSE 全事件对接 + bug 修复#37
Merged
Conversation
- main: _get_harness_llm 加 streaming=True,使 astream_events 逐 token 产出 on_chat_model_stream;否则 ChatOpenAI._agenerate 一次性产出整段答案, SSE streamer 只发一个 chunk 帧含全文,前端无法增量渲染 - agent_sse: 监听 on_chat_model_start,重试时(如 peer closed)发 reset 帧 并清空 full_content,避免半截+重复内容流式与落库 - agent_sse/orchestrator: streamer 标记 had_error/error_message,post-stream 据此走 fail_turn 而非 finalize_turn,失败回合不再当成功消息入库(且不重复 error 帧) - orchestrator/chat: ask_agent_stream 补齐 api_key_manager 预加载与 _resolve_usage_metadata(uid, api_key_manager),用量不再误归系统默认 key
- chat-stream: 补齐 route/step/reset 事件处理(原仅 chunk/sources/error/done) - api: askStream 切到 /chat/ask/agent/stream 获取 route 帧 - ChatPanel/ChatDockPanel: 接 onStep(按 step 号合并 tool_start/end)/onRoute/onReset - ChatMessage: 包 React.memo 避免每 token 重渲染全部消息;流式中用纯文本 渲染,完成后再切 ReactMarkdown;route 标签 + 思考过程流式自动展开 - types: ChatMessageData 加 agent 字段;globals.css 加 route badge 样式 - 删除无引用的根 components/ChatPanel.tsx 死代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
前端聊天长期"不流式"(答案一次性出现)。代码审查(5 agent 并行,含容器内 langgraph 实证)定位到多因叠加:后端 LLM 未开 streaming、前端 SSE 只处理 4/6 种事件、前端渲染未 memo、重试内容重复、失败回合误存、用量归因错误。
改动
后端
[chat] fix(b317fa4)_get_harness_llm加streaming=True——核心修复。否则ChatOpenAI._agenerate一次性产出整段答案,SSE streamer 只发一个 chunk 帧含全文,前端无法增量渲染。on_chat_model_start,重试时(如peer closed connection)发reset帧并清空full_content,避免半截+重复内容流式与落库。had_error,post-stream 据此走fail_turn而非finalize_turn,失败回合不再当成功消息入库(且不重复 error 帧)。ask_agent_stream补齐api_key_manager预加载与_resolve_usage_metadata(uid, api_key_manager),用量不再误归系统默认 key。前端
[frontend] feat(d2f427a)route/step/reset事件处理(原仅 chunk/sources/error/done)。askStream切到/chat/ask/agent/stream获取 route 帧。onStep(按 step 号合并 tool_start/end)/onRoute/onReset。React.memo避免每 token 重渲染全部消息;流式中纯文本渲染,完成后再切ReactMarkdown;route 标签 + 思考过程流式自动展开。components/ChatPanel.tsx死代码。验证
py_compile通过;前端tsc --noEmit无类型错误。Test Plan