feat(provider):增加透明重传以及解决缓冲区溢出问题#171
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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.
变更说明
本 PR 解决 OpenAI provider 层 SSE 流的两个核心缺陷:传输中途断连时无自动恢复能力、无缓冲区溢出防护。改动对上层(runtime/tui)完全透明,不影响 Provider 接口签名。
主要改动
1. 新增
internal/provider/openai/sse_reader.go(~70 行)纯同步有界 SSE 行读取器
boundedSSEReader,包装bufio.Reader并施加两级硬限制:ReadLine()返回去\r\n的行内容ErrLineTooLong/ErrStreamTooLarge哨兵错误2.
internal/provider/errors.go— 追加内容IsRecoverableStreamError判定逻辑:context.Canceled/DeadlineExceededErrLineTooLong/ErrStreamTooLargeProviderError.Retryable == false(4xx)ProviderError.Retryable == true(5xx/429)*net.OpErrorErrStreamInterrupted3.
internal/provider/openai/openai.go— 核心重构Chat()— 从单次执行变为重连循环跨周期状态通过闭包局部变量持久化:
accumText strings.Builder— 已累积文本accumCalls map[int]*provider.ToolCall— 已累积 tool call提取
chatOnce()— 单次 HTTP 请求 + 流消费从原
Chat()提取,职责单一:构建请求 → 发送 HTTP → 校验状态码 →consumeStream()。改写
consumeStream()— 接入累积状态 + 有界读取器bufio.NewReader(body).ReadString('\n')newBoundedSSEReader(body).ReadLine()accumText+ 发送事件mergeToolCallDelta()mergeToolCallDeltaWithAccum()同步写入accumCallsErrStreamInterrupted新增辅助方法
buildAssistantMsg(accumText, accumCalls) provider.Message— 从累积状态构造 assistant 角色 OpenAI 消息,用于重连时注入上下文mergeToolCallDeltaWithAccum(ctx, events, accumCalls, delta)— 在原mergeToolCallDelta基础上同步维护跨周期累积状态预期收益