Skip to content
Merged
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
16 changes: 5 additions & 11 deletions backend/cmd/server/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/internal/accounttools/policy/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestImportPolicyPlansEachItemByIdentityMatches(t *testing.T) {
}
request.Phase = "finalize"
raw, _ := json.Marshal(request)
if _, err := New().Invoke(context.Background(), extensionv1.Invocation{Capability: extensionv1.CapabilityAdmin, Operation: "import.plan", Payload: raw}); err == nil {
if _, err := New().Invoke(context.Background(), extensionv1.Invocation{Operation: "import.plan", Payload: raw}); err == nil {
t.Fatal("incomplete prepared phase was accepted")
}
}
20 changes: 0 additions & 20 deletions backend/internal/accounttools/policy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ import (
type Module struct{}

func New() *Module { return &Module{} }
func (m *Module) ValidateConfig(_ context.Context, raw json.RawMessage) (json.RawMessage, error) {
var fields map[string]json.RawMessage
if json.Unmarshal(raw, &fields) != nil || fields == nil || len(fields) != 0 {
return nil, errors.New("account tools has no additional configuration")
}
return json.RawMessage(`{}`), nil
}
func (m *Module) ApplyConfig(ctx context.Context, raw json.RawMessage) error {
_, err := m.ValidateConfig(ctx, raw)
return err
}
func (m *Module) Status(context.Context) (json.RawMessage, error) {
return json.Marshal(map[string]any{"taxonomy": true, "reasoning_selection": true, "text_prompt": true})
}

func failure(code, message string, status int) extensionv1.Result {
return extensionv1.Result{Code: code, Message: message, HTTPStatus: status}
Expand All @@ -46,9 +32,6 @@ func (m *Module) Invoke(ctx context.Context, in extensionv1.Invocation) (extensi
if err := ctx.Err(); err != nil {
return extensionv1.Result{}, err
}
if in.Capability != extensionv1.CapabilityAdmin {
return extensionv1.Result{}, errors.New("unsupported account tools capability")
}
var output any = map[string]bool{"valid": true}
switch in.Operation {
case "import.plan":
Expand All @@ -68,9 +51,6 @@ func (m *Module) Invoke(ctx context.Context, in extensionv1.Invocation) (extensi
if !request.ValidUTF8 || request.Characters < 0 || request.Characters > 8192 {
return failure("ACCOUNT_TEST_PROMPT_INVALID", "test prompt must be valid UTF-8 and at most 8192 characters", 400), nil
}
case "tools.describe":
raw, err := m.Status(ctx)
return extensionv1.Result{Payload: raw}, err
case "taxonomy.name":
var request extensionv1.TaxonomyName
if json.Unmarshal(in.Payload, &request) != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/internal/accounttools/policy/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestTaxonomyPoliciesRejectAmbiguityAndInvalidAssignments(t *testing.T) {
{"taxonomy.assignment", `{"tag_ids":[-1]}`, "ACCOUNT_TAG_ID_INVALID"},
{"taxonomy.assignment", `{"tag_ids":[1,1,2]}`, ""},
} {
out, err := New().Invoke(context.Background(), extensionv1.Invocation{Capability: extensionv1.CapabilityAdmin, Operation: tc.operation, Payload: json.RawMessage(tc.payload)})
out, err := New().Invoke(context.Background(), extensionv1.Invocation{Operation: tc.operation, Payload: json.RawMessage(tc.payload)})
if err != nil || out.Code != tc.code {
t.Fatalf("%s: %v %+v", tc.operation, err, out)
}
Expand All @@ -34,7 +34,7 @@ func TestReasoningSelectionValidatesTheSuppliedWireCapabilities(t *testing.T) {
valid bool
}{{"default", "ultra", true}, {"text", "ultra", true}, {"compact", "ultra", false}, {"default", " high ", false}, {"default", "high", false}, {"default", "", true}} {
raw, _ := json.Marshal(extensionv1.ReasoningSelection{Mode: tc.mode, Effort: tc.effort, Levels: []string{"ultra"}})
out, err := New().Invoke(context.Background(), extensionv1.Invocation{Capability: extensionv1.CapabilityAdmin, Operation: "test.reasoning", Payload: raw})
out, err := New().Invoke(context.Background(), extensionv1.Invocation{Operation: "test.reasoning", Payload: raw})
if err != nil || (out.Code == "") != tc.valid {
t.Fatalf("%s/%s: %v %+v", tc.mode, tc.effort, err, out)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAccountHandlerTrafficTelemetryFollowsTelemetrySwitch(t *testing.T) {
stub.getAccountResult = &service.Account{ID: 18, Platform: service.PlatformOpenAI, Type: service.AccountTypeAPIKey, Concurrency: 3, Credentials: map[string]any{"api_key": "never-return-secret"}}
cache := &stubAccountTrafficObserveCache{snapshot: map[service.AccountTrafficProtocol]service.AccountTrafficObserveState{service.AccountTrafficProtocolHTTP: {Started: 3, Completed2xx: 2}}}
handler := NewAccountHandler(stub, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
handler.SetAccountTrafficObserver(service.NewAccountTrafficObserver(cache, nil))
handler.SetAccountTrafficObserver(service.NewAccountTrafficObserver(cache))
router := gin.New()
router.GET("/accounts/:id/traffic-telemetry", handler.GetTrafficTelemetry)
recorder := httptest.NewRecorder()
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestAccountHandlerGetTrafficTelemetryReturnsSnapshot(t *testing.T) {
service.AccountTrafficProtocolHTTP: {Started: 3, Completed2xx: 2, Upstream429: 1, PeakInFlight: 2, RequestsLast60s: 1},
service.AccountTrafficProtocolWS: {},
},
}, nil))
}))
router := gin.New()
router.GET("/accounts/:id/traffic-telemetry", handler.GetTrafficTelemetry)

Expand Down
2 changes: 1 addition & 1 deletion backend/internal/handler/grok_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (h *OpenAIGatewayHandler) handleGrokMedia(c *gin.Context, endpoint service.
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, grokMediaScheduleModel(account, routingModel, nil), lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"grok_media", reqLog, !endpoint.IsVideoLookupRequest(),
func() { h.handleFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/handler/openai_alpha_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (h *OpenAIGatewayHandler) AlphaSearch(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(requestedModel), lastFailoverErr, failedAccountIDs,
&switchCount, h.maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, h.maxAccountSwitches, &oauth429FailoverState,
"alpha_search", reqLog, true,
func() { h.handleFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/handler/openai_chat_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (h *OpenAIGatewayHandler) ChatCompletions(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(routingModel), lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"chat_completions", reqLog, true,
func() { h.handleFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/handler/openai_codex_models_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (h *OpenAIGatewayHandler) CodexModels(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, "", lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, false,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"codex_models", nil, true,
func() {
h.errorResponse(c, infraerrors.Code(lastUpstreamErr), "upstream_error", infraerrors.Message(lastUpstreamErr))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestWriteCountTokensFailoverErrorPreservesModelNotSupported(t *testing.T) {
writeCountTokensFailoverError(ctx, &service.UpstreamFailoverError{
StatusCode: http.StatusBadRequest,
Reason: service.GatewayFailureReason("upstream_400_model_not_supported"),
}, nil)
})

var envelope map[string]any
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &envelope))
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/handler/openai_embeddings.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (h *OpenAIGatewayHandler) Embeddings(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(reqModel), lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"embeddings", reqLog, true,
func() { h.handleFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down
1 change: 0 additions & 1 deletion backend/internal/handler/openai_failover_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (h *OpenAIGatewayHandler) failoverAfterSameAccountSlotFailure(
switchCount *int,
maxAccountSwitches int,
oauth429State *service.OpenAIOAuth429FailoverState,
streamStarted bool,
logScope string,
reqLog *zap.Logger,
allowAccountSwitch bool,
Expand Down
24 changes: 11 additions & 13 deletions backend/internal/handler/openai_gateway_count_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ func (h *OpenAIGatewayHandler) CountTokens(c *gin.Context) {
switchCount := 0
failedAccountIDs := make(map[int64]struct{})
retryState := newOpenAIFailoverRetryState()
var lastUpstreamErr error
var lastFailoverErr *service.UpstreamFailoverError
var sameAccountRetrySelection *service.AccountSelectionResult
var oauth429FailoverState service.OpenAIOAuth429FailoverState
Expand Down Expand Up @@ -316,12 +315,12 @@ func (h *OpenAIGatewayHandler) CountTokens(c *gin.Context) {
if c.Request.Context().Err() != nil {
return
}
if retryingSameAccount && lastUpstreamErr != nil {
writeCountTokensFailoverError(c, lastFailoverErr, lastUpstreamErr)
if retryingSameAccount && lastFailoverErr != nil {
writeCountTokensFailoverError(c, lastFailoverErr)
return
}
if len(failedAccountIDs) > 0 && lastUpstreamErr != nil {
writeCountTokensFailoverError(c, lastFailoverErr, lastUpstreamErr)
if len(failedAccountIDs) > 0 && lastFailoverErr != nil {
writeCountTokensFailoverError(c, lastFailoverErr)
return
}
requestPlatform := openAICompatibleRequestPlatform(c.Request.Context(), apiKey)
Expand Down Expand Up @@ -349,8 +348,8 @@ func (h *OpenAIGatewayHandler) CountTokens(c *gin.Context) {
if h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(currentRoutingModel), lastFailoverErr,
failedAccountIDs, &switchCount, maxAccountSwitches, &oauth429FailoverState,
false, "count_tokens", reqLog, true,
func() { writeCountTokensFailoverError(c, lastFailoverErr, lastUpstreamErr) },
"count_tokens", reqLog, true,
func() { writeCountTokensFailoverError(c, lastFailoverErr) },
) {
continue
}
Expand All @@ -377,10 +376,9 @@ func (h *OpenAIGatewayHandler) CountTokens(c *gin.Context) {
}
if !failoverErr.ShouldRetryNextAccount() {
finalizeOpenAIFailoverSelection(h.gatewayService, selection, account, account.GetMappedModel(currentRoutingModel), failoverErr, openAIFailoverRetryStop)
writeCountTokensFailoverError(c, failoverErr, attemptErr)
writeCountTokensFailoverError(c, failoverErr)
return
}
lastUpstreamErr = attemptErr
lastFailoverErr = failoverErr
retryAction := retryState.Handle(c.Request.Context(), h.gatewayService, account, account.GetMappedModel(currentRoutingModel), failoverErr, true, sameAccountRetryDelay, "count_tokens")
finalizeOpenAIFailoverSelection(h.gatewayService, selection, account, account.GetMappedModel(currentRoutingModel), failoverErr, retryAction)
Expand All @@ -391,24 +389,24 @@ func (h *OpenAIGatewayHandler) CountTokens(c *gin.Context) {
case openAIFailoverRetryCanceled:
return
case openAIFailoverRetryStop:
writeCountTokensFailoverError(c, failoverErr, attemptErr)
writeCountTokensFailoverError(c, failoverErr)
return
}
failedAccountIDs[account.ID] = struct{}{}
if switchCount >= maxAccountSwitches {
writeCountTokensFailoverError(c, failoverErr, attemptErr)
writeCountTokensFailoverError(c, failoverErr)
return
}
switchCount++
if h.gatewayService.ShouldStopOpenAIOAuth429Failover(account, failoverErr.StatusCode, switchCount, &oauth429FailoverState) {
writeCountTokensFailoverError(c, failoverErr, attemptErr)
writeCountTokensFailoverError(c, failoverErr)
return
}
h.gatewayService.RecordOpenAIAccountSwitch()
}
}

func writeCountTokensFailoverError(c *gin.Context, failoverErr *service.UpstreamFailoverError, _ error) {
func writeCountTokensFailoverError(c *gin.Context, failoverErr *service.UpstreamFailoverError) {
status := http.StatusBadGateway
errorType := "upstream_error"
errorCode := ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestOpenAIFirstOutputFailoverStopsAfterOneAccountSwitch(t *testing.T) {
}

func TestOpenAIFirstOutputFailoverDoesNotCapRefusalRecoveryAccountSwitches(t *testing.T) {
failoverErr := service.NewOpenAICyberFailoverError(nil, nil)
failoverErr := service.NewOpenAICyberFailoverError(nil)
count := 0

for range 3 {
Expand Down
4 changes: 2 additions & 2 deletions backend/internal/handler/openai_gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ func (h *OpenAIGatewayHandler) Responses(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(routingModel), lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"responses", reqLog, true,
func() { h.handleFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down Expand Up @@ -1496,7 +1496,7 @@ func (h *OpenAIGatewayHandler) Messages(c *gin.Context) {
if slotResult != openAISlotAcquireOK {
if retryingSameAccount && lastFailoverErr != nil && h.failoverAfterSameAccountSlotFailure(
c, account, account.GetMappedModel(currentRoutingModel), lastFailoverErr, failedAccountIDs,
&switchCount, maxAccountSwitches, &oauth429FailoverState, streamStarted,
&switchCount, maxAccountSwitches, &oauth429FailoverState,
"messages", reqLog, true,
func() { h.handleAnthropicFailoverExhausted(c, lastFailoverErr, streamStarted) },
) {
Expand Down
Loading
Loading