Просмотр исходного кода

fix(studio): short-circuit probe on 401/403 from /models

When /models returns 401 or 403, the API key is definitively invalid.
Previously the probe continued trying chat completions with fallback
models (MiniMax-M2.7 etc.), producing misleading error messages.
Now returns "API Key 无效" immediately.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
fanghanjun 6 месяцев назад
Родитель
Сommit
fa48c20812
1 измененных файлов с 16 добавлено и 2 удалено
  1. 16 2
      packages/studio/src/api/server.ts

+ 16 - 2
packages/studio/src/api/server.ts

@@ -383,7 +383,7 @@ async function fetchModelsFromServiceBaseUrl(
   serviceId: string,
   baseUrl: string,
   apiKey: string,
-): Promise<{ models: Array<{ id: string; name: string }>; error?: string }> {
+): Promise<{ models: Array<{ id: string; name: string }>; error?: string; authFailed?: boolean }> {
   const modelsBaseUrl = isCustomServiceId(serviceId)
     ? baseUrl
     : resolveServiceModelsBaseUrl(serviceId) ?? baseUrl;
@@ -395,7 +395,11 @@ async function fetchModelsFromServiceBaseUrl(
     });
     if (!res.ok) {
       const body = await res.text().catch(() => "");
-      return { models: [], error: `服务商返回 ${res.status}: ${body.slice(0, 200)}` };
+      return {
+        models: [],
+        error: `服务商返回 ${res.status}: ${body.slice(0, 200)}`,
+        authFailed: res.status === 401 || res.status === 403,
+      };
     }
     const json = await res.json() as { data?: Array<{ id: string }> };
     return {
@@ -429,6 +433,16 @@ async function probeServiceCapabilities(args: {
 
   const baseService = isCustomServiceId(args.service) ? "custom" : args.service;
   const modelsResponse = await fetchModelsFromServiceBaseUrl(baseService, args.baseUrl, args.apiKey);
+
+  // /models returned 401/403 — key is definitively invalid, don't probe further
+  if (modelsResponse.authFailed) {
+    return {
+      ok: false,
+      models: [],
+      error: "API Key 无效,请检查后重试",
+    };
+  }
+
   const discoveredModels = modelsResponse.models;
   const modelCandidates = buildModelCandidates({
     preferredModel: args.preferredModel,