fix(agents): recognize flat JSON billing payloads and snake_case error codes (#74188)

* fix(agents): recognize flat JSON billing payloads and snake_case error codes

Two independent fixes for billing error detection:

1. isErrorPayloadObject/parseApiErrorInfo now recognize flat JSON like
   {"error":"string_code","message":"..."} where error is a string code
   at the top level, not just nested {"error":{"type":"...","message":"..."}}
   envelopes.

2. isBillingErrorMessage now matches "insufficient_balance" (underscore)
   and "Insufficient MBT balance" (one word between insufficient/balance)
   via two new patterns in the billing pattern list.

Together these prevent raw JSON from leaking to user-facing chat when
providers return 402-style flat payloads.

Fixes #74079

* fix(agents): remove redundant billing pattern and fix misleading regex comment
This commit is contained in:
Logan Ye
2026-04-29 19:15:45 +08:00
committed by GitHub
parent 1f8ccf2d2a
commit ef7c528c8a
4 changed files with 46 additions and 1 deletions

View File

@@ -47,6 +47,10 @@ function isErrorPayloadObject(payload: unknown): payload is ErrorPayload {
return true;
}
}
// Flat error payloads: {"error":"insufficient_balance","message":"..."}
if (typeof err === "string" && typeof record.message === "string") {
return true;
}
}
return false;
}
@@ -165,6 +169,9 @@ export function parseApiErrorInfo(raw?: string): ApiErrorInfo | null {
if (typeof err.message === "string") {
errMessage = err.message;
}
} else if (typeof payload.error === "string") {
// Flat error payloads: {"error":"insufficient_balance","message":"..."}
errType = payload.error;
}
return {