mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 00:01:05 +00:00
fix: preserve pre-upstream selection diagnostics
This commit is contained in:
1 parent
9c484ae072
commit
cd6369d0c8
6 files changed
+78
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -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"))
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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:
|
||||
) : (
|
||||
<div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-2 px-6 text-center text-muted-foreground">
|
||||
<TriangleAlert className="size-7 stroke-1" />
|
||||
<p>{t("audits.noFailureAttempts")}</p>
|
||||
<p>{t(detailQuery.data.audit.errorCode && PRE_UPSTREAM_ERROR_CODES.has(detailQuery.data.audit.errorCode) ? "audits.noUpstreamAttempt" : "audits.noFailureAttempts")}</p>
|
||||
{detailQuery.data.audit.errorCode ? <span className="max-w-full break-words">{detailQuery.data.audit.errorCode}</span> : null}
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -779,6 +779,7 @@ const resources = {
|
||||
failedAttemptCount: "{{count}} 次失败",
|
||||
attemptTimeline: "失败尝试",
|
||||
noFailureAttempts: "该请求没有可用的失败尝试诊断。",
|
||||
noUpstreamAttempt: "该请求在发送到上游前失败,因此没有上游尝试记录。请根据下方错误码检查账号可用性、额度、冷却或并发状态。",
|
||||
attemptNumber: "尝试 #{{number}}",
|
||||
openDiagnostics: "查看安全诊断",
|
||||
viewDetails: "查看详情",
|
||||
@@ -1324,6 +1325,7 @@ const resources = {
|
||||
failedAttemptCount_other: "{{count}} failed attempts",
|
||||
attemptTimeline: "Failed attempts",
|
||||
noFailureAttempts: "No failed-attempt diagnostics are available for this request.",
|
||||
noUpstreamAttempt: "This request failed before it was sent upstream, so no upstream attempt was recorded. Use the error code below to check account availability, quota, cooldown, or concurrency.",
|
||||
attemptNumber: "Attempt #{{number}}",
|
||||
openDiagnostics: "View safe diagnostics",
|
||||
viewDetails: "View details",
|
||||
|
||||
Reference in new issue
Block a user