-
Notifications
You must be signed in to change notification settings - Fork 0
Fix anthropic stream and turn off Encoding when SSE #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,24 @@ import ( | |
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/agentuity/go-common/slice" | ||
| ) | ||
|
|
||
| var skipHeaders = []string{"Content-Encoding", "Content-Length"} | ||
|
|
||
| func copyResponseHeaders(w http.ResponseWriter, headers http.Header) { | ||
| header := w.Header() | ||
|
|
||
| for k, v := range headers { | ||
| if !slice.Contains(skipHeaders, k, slice.WithCaseInsensitive()) { | ||
| for _, val := range v { | ||
| header.Add(k, val) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+18
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't strip These paths forward upstream bytes back to the client, but the shared skip list now always removes Suggested change-var skipHeaders = []string{"Content-Encoding", "Content-Length"}
+var skipHeaders = []string{"Content-Length"}Also applies to: 440-440 🤖 Prompt for AI Agents |
||
|
|
||
| type AutoRouter struct { | ||
| registry Registry | ||
| detector ProviderDetector | ||
|
|
@@ -303,6 +319,9 @@ func (a *AutoRouter) ForwardStreaming(ctx context.Context, req *http.Request, w | |
| upstreamReq.Header[k] = v | ||
| } | ||
|
|
||
| // FOR SSE, turn off compression explicitly | ||
| upstreamReq.Header["Accept-Encoding"] = []string{"identity"} | ||
|
|
||
| if err := provider.RequestEnricher().Enrich(upstreamReq, meta, body); err != nil { | ||
| return ResponseMetadata{}, err | ||
| } | ||
|
|
@@ -340,11 +359,7 @@ func (a *AutoRouter) ForwardStreaming(ctx context.Context, req *http.Request, w | |
| w.Header().Set("Trailer", "X-Gateway-Cost,X-Gateway-Prompt-Tokens,X-Gateway-Completion-Tokens") | ||
| } | ||
|
|
||
| for k, v := range upstreamResp.Header { | ||
| if k != "Content-Length" { | ||
| w.Header()[k] = v | ||
| } | ||
| } | ||
| copyResponseHeaders(w, upstreamResp.Header) | ||
|
|
||
| w.WriteHeader(upstreamResp.StatusCode) | ||
|
|
||
|
|
@@ -469,9 +484,7 @@ func (a *AutoRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| for k, v := range resp.Header { | ||
| w.Header()[k] = v | ||
| } | ||
| copyResponseHeaders(w, resp.Header) | ||
|
|
||
| if billing, ok := meta.Custom["billing_result"].(BillingResult); ok { | ||
| w.Header().Set("X-Gateway-Cost", fmt.Sprintf("%.6f", billing.TotalCost)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -822,3 +822,76 @@ func TestAutoRouter_ResponsesAPIStreamingNoStreamOptions(t *testing.T) { | |||||||
| } | ||||||||
| }) | ||||||||
| } | ||||||||
|
|
||||||||
| func TestAutoRouter_copyResponseHeaders(t *testing.T) { | ||||||||
| w := httptest.NewRecorder() | ||||||||
| copyResponseHeaders(w, http.Header{}) | ||||||||
| var sw strings.Builder | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() != 0 { | ||||||||
| t.Errorf("headers should have been empty but was: %s", sw.String()) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
|
||||||||
| copyResponseHeaders(w, http.Header{"A": []string{"B"}}) | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() == 0 { | ||||||||
| t.Error("headers should have content but was empty") | ||||||||
| } | ||||||||
| val := strings.TrimSpace(sw.String()) | ||||||||
| if val != "A: B" { | ||||||||
| t.Errorf("headers should have A: B but was %s", val) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
|
||||||||
| copyResponseHeaders(w, http.Header{"A": []string{"B"}, "Content-Encoding": []string{"gzip"}}) | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() == 0 { | ||||||||
| t.Error("headers should have content but was empty") | ||||||||
| } | ||||||||
| val = strings.TrimSpace(sw.String()) | ||||||||
| if val != "A: B" { | ||||||||
| t.Errorf("headers should have A: B but was %s", val) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
|
||||||||
| copyResponseHeaders(w, http.Header{"A": []string{"B"}, "content-encoding": []string{"gzip"}}) | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() == 0 { | ||||||||
| t.Error("headers should have content but was empty") | ||||||||
| } | ||||||||
| val = strings.TrimSpace(sw.String()) | ||||||||
| if val != "A: B" { | ||||||||
| t.Errorf("headers should have A: B but was %s", val) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
|
||||||||
| copyResponseHeaders(w, http.Header{"A": []string{"B"}, "Content-Length": []string{"1"}}) | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() == 0 { | ||||||||
| t.Error("headers should have content but was empty") | ||||||||
| } | ||||||||
| val = strings.TrimSpace(sw.String()) | ||||||||
| if val != "A: B" { | ||||||||
| t.Errorf("headers should have A: B but was %s", val) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
|
||||||||
| copyResponseHeaders(w, http.Header{"A": []string{"B"}, "content-length": []string{"1"}}) | ||||||||
| w.Header().Write(&sw) | ||||||||
| if sw.Len() == 0 { | ||||||||
| t.Error("headers should have content but was empty") | ||||||||
| } | ||||||||
| val = strings.TrimSpace(sw.String()) | ||||||||
| if val != "A: B" { | ||||||||
| t.Errorf("headers should have A: B but was %s", val) | ||||||||
| } | ||||||||
| sw.Reset() | ||||||||
| w = httptest.NewRecorder() | ||||||||
|
Comment on lines
+894
to
+895
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove dead code at the end of the test. Line 895 reassigns ✂️ Proposed fix- sw.Reset()
- w = httptest.NewRecorder()
+ sw.Reset()📝 Committable suggestion
Suggested change
🧰 Tools🪛 golangci-lint (2.11.4)[error] 895-895: SA4006: this value of w is never used (staticcheck) 🤖 Prompt for AI Agents |
||||||||
|
|
||||||||
| } | ||||||||
Uh oh!
There was an error while loading. Please reload this page.