fix(gateway): distinguish gateway auth 401 from provider API key errors

The api_server adapter returned error code "invalid_api_key" for
API_SERVER_KEY authentication failures, which the Desktop error
classifier misidentified as a provider (OpenRouter/OpenAI) key
problem — showing "OpenRouter API key missing" when the real issue
was gateway auth.

Changes:
- gateway/platforms/api_server.py: return "gateway_auth_failed" code
  with descriptive message for API_SERVER_KEY auth failures
- apps/desktop/src/store/notifications.ts: add "gateway_auth_failed"
  handler before "invalid_api_key" to show correct error message
- agent/error_classifier.py: add "gateway_auth_failed" to auth patterns
- tests: update test_session_api.py to expect new error code

Fixes #39365
This commit is contained in:
annguyenNous 2026-06-05 08:00:29 +07:00 committed by Teknium
parent 6ced760a3d
commit 32f7c5afaf
4 changed files with 8 additions and 3 deletions

View file

@ -413,6 +413,7 @@ _CONTENT_POLICY_BLOCKED_PATTERNS = [
_AUTH_PATTERNS = [
"invalid api key",
"invalid_api_key",
"gateway_auth_failed",
"authentication",
"unauthorized",
"forbidden",

View file

@ -78,7 +78,11 @@ function cleanErrorText(value: string) {
const ERROR_SUMMARIES: { test: (msg: string) => boolean; summarize: (msg: string) => string }[] = [
{
test: msg => /incorrect api key provided/i.test(msg) || /['"]code['"]\s*:\s*['"]invalid_api_key['"]/i.test(msg),
test: msg => /['"']code['"']\s*:\s*['"']gateway_auth_failed['"']/i.test(msg),
summarize: () => 'Gateway authentication failed — check your API_SERVER_KEY.'
},
{
test: msg => /incorrect api key provided/i.test(msg) || /['"']code['"']\s*:\s*['"']invalid_api_key['"']/i.test(msg),
summarize: msg => {
const status = msg.match(/(?:error code|status(?:Code)?)[^\d]*(\d{3})/i)?.[1]

View file

@ -1335,7 +1335,7 @@ class APIServerAdapter(BasePlatformAdapter):
self._request_audit_log_suffix(request),
)
return web.json_response(
{"error": {"message": "Invalid API key", "type": "invalid_request_error", "code": "invalid_api_key"}},
{"error": {"message": "Invalid gateway API key (API_SERVER_KEY)", "type": "gateway_auth_error", "code": "gateway_auth_failed"}},
status=401,
)

View file

@ -416,7 +416,7 @@ async def test_session_endpoints_require_auth_when_key_configured(auth_adapter):
resp = await cli.get("/api/sessions")
assert resp.status == 401
body = await resp.json()
assert body["error"]["code"] == "invalid_api_key"
assert body["error"]["code"] == "gateway_auth_failed"
ok = await cli.get("/api/sessions", headers={"Authorization": "Bearer sk-test"})
assert ok.status == 200