fix(sdk): update stream forwarder for compression

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 <noreply@anthropic.com>
This commit is contained in:
Finessse
2025-12-26 05:10:43 +07:00
co-authored by Claude Opus 4.5
parent f19594e490
commit d3466b31af
+24 -2
View File
@@ -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