Repository navigation
fix: OpenRouter 语音合成流式输出逐块转发 - #126
Open
sheepbox8646 wants to merge 1 commit into
Open
sheepbox8646 wants to merge 1 commit into
sheepbox8646 wants to merge 1 commit into
Conversation
DoStream 原来先读完整个 SSE 流、拼成完整 WAV,再作为单个分块交给调用方,流式接口没有降低首包延迟。现在每收到一个音频 delta 就解码并转发:第一块是大小字段为 0xFFFFFFFF 的流式 WAV 头加第一段 PCM,之后是按样本对齐的 PCM-16,ContentType 仍为 audio/wav。DoSynthesize 与 DoStream 共用 SSE 解析,GenerateSpeech 的输出形态不变;流中途的 OpenRouter 错误事件现在会作为 APIError 返回。 Linear: TWI-16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
概要
provider/openrouter/speech的DoStream原来先读完整个 SSE 流,收集全部 base64 PCM-16 分块,拼成完整 WAV 后作为单个分块交给调用方。StreamSpeech因此要等合成全部结束才拿到数据,与GenerateSpeech没有区别。audio/wav。0xFFFFFFFF,这是长度未知的流式 WAV 的通用写法。GenerateSpeech逐字节一致。audio/L16表示大端,会标错。ctx.Done(),取消时报告ctx.Err(),不泄漏 goroutine。非 2xx 响应仍在DoStream中同步返回,并走DecodeOpenRouter。流中途的 OpenRouter 错误事件转成*sdk.APIError,通过Err()报告。GenerateSpeech的行为变化:utils.FetchSSE一致。Accept: text/event-stream。docs/speech.md新增 OpenRouter TTS 一节。关联
验证
stream_test.go,用 httptest 覆盖以下情况:DoSynthesize的 PCM 一致。DoStream改回“读完再发”,首块测试会超时失败,确认它能抓到这类回归。go build ./...、go vet ./...、go vet -tags integration ./...、go test ./... -short -count=1 -race、golangci-lint run ./...,全部通过。go mod tidy无变化。未验证
/chat/completions输出音频的模型只有openai/gpt-audio*和音乐生成模型google/lyria-3-*。测试环境调用这些模型全部返回 403(服务条款限制,与本改动无关)。provider/mimo/speech的DoStream有同样的单分块问题,不在本 PR 范围内。🤖 Generated with Claude Code