Repository navigation
feat: v1.6 SSE 逐块流式(LIMITATIONS #1 部分闭环:chat 客户端 ← anthropic 上游) - #12
Merged
Merged
Conversation
- 新增 src/gateway/sse.py:Anthropic 事件逐块翻 Chat chunk 即时下发;工具参数按既定折损缓冲到 content_block_stop;usage 流尾合并照常进 5 项埋点;synthetic_response() 复用 assistant_from_upstream(流式与非流式落库同一路径) - 网关:open_stream 行级增量读取(urllib 行迭代实测可行,保持零依赖);do_POST 尾部收敛为 finalize_turn() 共用;流式占 _GATE 并发名额 - mock:Anthropic 路径发完整官方事件序列(含 usage/缓存字段) - 测试:TestSseConversion 7 项单测 + 冒烟第 6 组 7 项(实测首尾帧间隔 30.7ms,证明逐块到达非整读补吐);53 单测 + 18 冒烟全过 - 文档:LIMITATIONS #1 标部分闭环(未闭环方向与 dropped 可见性写明);README/REVIEW/capability-matrix/CHANGELOG 同步
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.
目的
闭环 LIMITATIONS #1 中最常用的一条方向:
openai_chat客户端stream:true→anthropic上游。此前该方向经网关要么 502(上游返回 SSE,post_json按 JSON 解析必炸),要么只能非流式绕行——首 token 延迟(TTFB)是协议转换层最直观的体验指标。方案
src/gateway/sse.py:parse_sse_lines:行流分帧(空行边界、注释/心跳行跳过、尾部半帧容错丢弃),urllib 响应对象直接喂入——实测 urllib 按行增量吐数据,无需引入第三方 SSE 库,保持零依赖AnthropicToChatStream:事件 → Chatcompletion.chunk。text_delta 即时下发(TTFB 与直连一致);input_json_delta是非完整 JSON 片段,按既定折损缓冲到 content_block_stop 一次性发(LIMITATIONS 早已声明);usage 在 message_start / message_delta 分次到达,流尾合并后照常进 5 项埋点;synthetic_response()拼出 Anthropic 非流式形状,复用assistant_from_upstream——流式与非流式的落库/埋点走同一条代码路径,不另造语义_backend_headers/_backend_opener/open_stream;do_POST 尾部逻辑收敛为finalize_turn()(两条传输路径共用);流式请求同样占_GATE并发名额;客户端中途断开仍完成流尾落库(费用已实际发生)测试(53 单测 + 18 冒烟全过)
TestSseConversion7 项:分帧容错 / text 即时下发 / 工具参数缓冲 / stop_reason 映射(end_turn→stop、max_tokens→length、tool_use→tool_calls)/ usage 合并 + 落库复用 / 上游错误帧范围声明(诚实边界)
仍为整读透传的方向:chat←chat、response 源、anthropic 客户端;流式轮的 dropped 清单对客户端不可见(SSE 帧无其位置,埋点中 degradation 仍如实记录)——均已写入 LIMITATIONS #1。