Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions backend/internal/service/openai_ws_http_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,15 @@ func (s *OpenAIGatewayService) proxyOpenAIWSHTTPBridgeTurn(
if err != nil {
return nil, fmt.Errorf("prepare http bridge body: %w", err)
}
grokIntentSourceBody := append([]byte(nil), body...)
_, grokExplicitToolsField := openAIWSHTTPBridgeRawField(grokIntentSourceBody, "tools")
grokExplicitToolIntent := account.Platform == PlatformGrok && hasGrokResponsesToolIntent(grokIntentSourceBody)
var grokIntentSourceBody []byte
var clientToolMapping apicompat.ResponsesClientToolMapping
functionToolUpstream := (account.Platform == PlatformOpenAI && account.Type == AccountTypeAPIKey) || account.Platform == PlatformGrok
if functionToolUpstream {
var grokExplicitToolsField, grokExplicitToolIntent bool
if account.Platform == PlatformGrok {
grokIntentSourceBody = append([]byte(nil), body...)
_, grokExplicitToolsField = openAIWSHTTPBridgeRawField(grokIntentSourceBody, "tools")
grokExplicitToolIntent = hasGrokResponsesToolIntent(grokIntentSourceBody)
body, err = sanitizeGrokResponsesInput(body)
if err != nil {
return nil, fmt.Errorf("sanitize Grok WS HTTP bridge input: %w", err)
Expand Down
40 changes: 40 additions & 0 deletions backend/internal/service/openai_ws_http_bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2565,3 +2565,43 @@ func TestOpenAIWSHTTPBridge_IdleTimeoutClosesClientSession(t *testing.T) {
}
require.Len(t, upstream.bodies, 1, "an idle client must not leave a continuation request running")
}

func BenchmarkOpenAIWSHTTPBridgeLongTurn(b *testing.B) {
for _, tc := range []struct {
name, platform, accountType, model string
repetitions int
}{
{"OpenAI-OAuth", PlatformOpenAI, AccountTypeOAuth, "gpt-5", 36_000},
{"OpenAI-APIKey", PlatformOpenAI, AccountTypeAPIKey, "gpt-5", 36_000},
{"Grok-OAuth", PlatformGrok, AccountTypeOAuth, "grok-4.5", 36_000},
{"OpenAI-OAuth-16MiB", PlatformOpenAI, AccountTypeOAuth, "gpt-5", 600_000},
} {
b.Run(tc.name, func(b *testing.B) {
history := strings.Repeat(`synthetic code: \"example\"\n`, tc.repetitions)
payload := []byte(`{"type":"response.create","model":"` + tc.model +
`","instructions":"test","input":[{"role":"user","content":"` + history +
`"}],"tools":[{"type":"function","name":"example","parameters":{"type":"object"}}]}`)
upstream := &httpUpstreamStub{resp: &http.Response{
StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"text/event-stream"}},
}}
svc := &OpenAIGatewayService{cfg: &config.Config{}, httpUpstream: upstream}
account := &Account{ID: 1, Platform: tc.platform, Type: tc.accountType, Concurrency: 1}
const completed = "data: {\"type\":\"response.completed\",\"response\":{\"id\":\"resp_fixture\",\"usage\":{\"input_tokens\":11,\"output_tokens\":3}}}\n\n"
b.SetBytes(int64(len(payload)))
b.ReportAllocs()
for b.Loop() {
upstream.resp.Body = io.NopCloser(strings.NewReader(completed))
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request = httptest.NewRequest(http.MethodGet, "/v1/responses", nil)
result, err := svc.proxyOpenAIWSHTTPBridgeTurn(b.Context(), c, account, "fixture", payload, len(payload),
tc.model, "", "", "", "", 1, func([]byte) error { return nil })
if err != nil {
b.Fatal(err)
}
if result.Usage.InputTokens != 11 || result.Usage.OutputTokens != 3 {
b.Fatalf("unexpected usage: %+v", result.Usage)
}
}
})
}
}
Loading