diff --git a/backend/internal/application/gateway/selector.go b/backend/internal/application/gateway/selector.go index 660a31e8..d7ebc711 100644 --- a/backend/internal/application/gateway/selector.go +++ b/backend/internal/application/gateway/selector.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + "net/http" "strings" "sync" "time" @@ -128,6 +129,36 @@ func (e *SelectionUnavailableError) Error() string { } } +// HTTPStatus returns the client-facing status for a routing refusal. +func (e *SelectionUnavailableError) HTTPStatus() int { + if e != nil { + switch e.Reason { + case SelectionCooling, SelectionModelCooling, SelectionQuotaExhausted: + return http.StatusTooManyRequests + } + } + return http.StatusServiceUnavailable +} + +// Code returns the stable diagnostic code for a routing refusal. +func (e *SelectionUnavailableError) Code() string { + if e != nil { + switch e.Reason { + case SelectionCooling: + return "upstream_cooling" + case SelectionModelCooling: + return "upstream_model_cooling" + case SelectionQuotaExhausted: + return "upstream_quota_exhausted" + case SelectionSaturated: + return "upstream_saturated" + case SelectionUnsupportedModel: + return "upstream_model_unavailable" + } + } + return "upstream_unavailable" +} + func (l *accountLease) Release() { if l == nil { return diff --git a/backend/internal/application/gateway/selector_test.go b/backend/internal/application/gateway/selector_test.go index d0028875..155b4262 100644 --- a/backend/internal/application/gateway/selector_test.go +++ b/backend/internal/application/gateway/selector_test.go @@ -3,6 +3,7 @@ package gateway import ( "context" "errors" + "net/http" "path/filepath" "slices" "sync" @@ -15,6 +16,29 @@ import ( "github.com/chenyme/grok2api/backend/internal/repository" ) +func TestSelectionUnavailableErrorClassification(t *testing.T) { + tests := []struct { + reason SelectionUnavailableReason + status int + code string + }{ + {reason: SelectionNoAccounts, status: http.StatusServiceUnavailable, code: "upstream_unavailable"}, + {reason: SelectionUnsupportedModel, status: http.StatusServiceUnavailable, code: "upstream_model_unavailable"}, + {reason: SelectionCooling, status: http.StatusTooManyRequests, code: "upstream_cooling"}, + {reason: SelectionModelCooling, status: http.StatusTooManyRequests, code: "upstream_model_cooling"}, + {reason: SelectionQuotaExhausted, status: http.StatusTooManyRequests, code: "upstream_quota_exhausted"}, + {reason: SelectionSaturated, status: http.StatusServiceUnavailable, code: "upstream_saturated"}, + } + for _, test := range tests { + t.Run(string(test.reason), func(t *testing.T) { + failure := &SelectionUnavailableError{Reason: test.reason} + if failure.HTTPStatus() != test.status || failure.Code() != test.code { + t.Fatalf("status=%d code=%q", failure.HTTPStatus(), failure.Code()) + } + }) + } +} + func TestSelectorPrioritizesDueQuotaProbeOnce(t *testing.T) { ctx := context.Background() database, err := relational.OpenSQLite(ctx, filepath.Join(t.TempDir(), "selector.db")) diff --git a/backend/internal/application/gateway/service.go b/backend/internal/application/gateway/service.go index 6710ef11..fc0d033f 100644 --- a/backend/internal/application/gateway/service.go +++ b/backend/internal/application/gateway/service.go @@ -1182,6 +1182,11 @@ attemptLoop: record.StatusCode = http.StatusServiceUnavailable record.DurationMS = time.Since(startedAt).Milliseconds() record.ErrorCode = "upstream_unavailable" + var selectionFailure *SelectionUnavailableError + if errors.As(lastErr, &selectionFailure) { + record.StatusCode = selectionFailure.HTTPStatus() + record.ErrorCode = selectionFailure.Code() + } record.Attempts = failureAttempts.snapshot() record.CreatedAt = time.Now().UTC() applyAuditEgress(&record, egressTrace, route.Provider) diff --git a/backend/internal/transport/http/inference/handler.go b/backend/internal/transport/http/inference/handler.go index 164a3c19..cb17d04f 100644 --- a/backend/internal/transport/http/inference/handler.go +++ b/backend/internal/transport/http/inference/handler.go @@ -1643,17 +1643,18 @@ func selectionErrorResponse(c *gin.Context, failure *gateway.SelectionUnavailabl if failure == nil { return status, code, message } + status, code = failure.HTTPStatus(), failure.Code() switch failure.Reason { case gateway.SelectionCooling: - status, code, message = http.StatusTooManyRequests, "upstream_cooling", "上游账号正在冷却" + message = "上游账号正在冷却" case gateway.SelectionModelCooling: - status, code, message = http.StatusTooManyRequests, "upstream_model_cooling", "上游账号的目标模型正在冷却" + message = "上游账号的目标模型正在冷却" case gateway.SelectionQuotaExhausted: - status, code, message = http.StatusTooManyRequests, "upstream_quota_exhausted", "上游账号额度等待恢复" + message = "上游账号额度等待恢复" case gateway.SelectionSaturated: - code, message = "upstream_saturated", "上游账号当前均达到并发上限" + message = "上游账号当前均达到并发上限" case gateway.SelectionUnsupportedModel: - code, message = "upstream_model_unavailable", "当前账号池不支持该模型" + message = "当前账号池不支持该模型" } if failure.RetryAfter > 0 { seconds := max(int64(1), int64((failure.RetryAfter+time.Second-1)/time.Second)) diff --git a/frontend/src/features/audits/request-audit-detail-dialog.tsx b/frontend/src/features/audits/request-audit-detail-dialog.tsx index 6ce85cb4..d52efb48 100644 --- a/frontend/src/features/audits/request-audit-detail-dialog.tsx +++ b/frontend/src/features/audits/request-audit-detail-dialog.tsx @@ -13,6 +13,15 @@ import { cn } from "@/shared/lib/cn"; import { formatDateTime, formatNumber } from "@/shared/lib/format"; const AUDIT_DETAIL_CACHE_TIME_MS = 60_000; +const PRE_UPSTREAM_ERROR_CODES = new Set([ + "model_not_allowed", + "upstream_cooling", + "upstream_model_cooling", + "upstream_model_unavailable", + "upstream_quota_exhausted", + "upstream_saturated", + "upstream_unavailable", +]); export function RequestAuditDetailDialog({ audit, open, onOpenChange }: { audit: AuditDTO | null; open: boolean; onOpenChange: (open: boolean) => void }) { const { t, i18n } = useTranslation(); @@ -61,7 +70,7 @@ export function RequestAuditDetailDialog({ audit, open, onOpenChange }: { audit: ) : (
{t("audits.noFailureAttempts")}
+{t(detailQuery.data.audit.errorCode && PRE_UPSTREAM_ERROR_CODES.has(detailQuery.data.audit.errorCode) ? "audits.noUpstreamAttempt" : "audits.noFailureAttempts")}
{detailQuery.data.audit.errorCode ? {detailQuery.data.audit.errorCode} : null}