From d3466b31af5b4ff3ced0a69a9abe93dcfcc0700a Mon Sep 17 00:00:00 2001 From: Finessse Date: Fri, 26 Dec 2025 05:10:43 +0700 Subject: [PATCH] fix(sdk): update stream forwarder for compression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update stream forwarder to work with token-aware compression middleware. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- sdk/api/handlers/stream_forwarder.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/sdk/api/handlers/stream_forwarder.go b/sdk/api/handlers/stream_forwarder.go index 401baca8..4a16074c 100644 --- a/sdk/api/handlers/stream_forwarder.go +++ b/sdk/api/handlers/stream_forwarder.go @@ -13,6 +13,10 @@ type StreamForwardOptions struct { // If nil, the configured default is used. If set to <= 0, keep-alives are disabled. KeepAliveInterval *time.Duration + // MaxChunkSize overrides the configured maximum chunk size. + // If nil, the configured default is used. If set to <= 0, chunk splitting is disabled. + MaxChunkSize *int + // WriteChunk writes a single data chunk to the response body. It should not flush. WriteChunk func(chunk []byte) @@ -61,6 +65,12 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can keepAliveC = keepAlive.C } + // Determine max chunk size for splitting large payloads + maxChunkSize := StreamingMaxChunkSize(h.Cfg) + if opts.MaxChunkSize != nil { + maxChunkSize = *opts.MaxChunkSize + } + var terminalErr *interfaces.ErrorMessage for { select { @@ -94,8 +104,20 @@ func (h *BaseAPIHandler) ForwardStream(c *gin.Context, flusher http.Flusher, can cancel(nil) return } - writeChunk(chunk) - flusher.Flush() + // Split large chunks to reduce latency (like vibeproxy's 64KB optimization) + if maxChunkSize > 0 && len(chunk) > maxChunkSize { + for offset := 0; offset < len(chunk); offset += maxChunkSize { + end := offset + maxChunkSize + if end > len(chunk) { + end = len(chunk) + } + writeChunk(chunk[offset:end]) + flusher.Flush() + } + } else { + writeChunk(chunk) + flusher.Flush() + } case errMsg, ok := <-errs: if !ok { continue