Skip to content

Commit 561360f

Browse files
committed
fix(billing): tailor billing error message for OAuth vs API key auth
- now accepts an optional parameter. - When , messages refer to "account limit" instead of "API key has run out of credits". - Pipe through (fallback branch) to . - Adds a targeted test verifying OAuth wording. Closes #80877
1 parent 951dd5a commit 561360f

6 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎src/agents/pi-embedded-helpers.formatassistanterrortext.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ describe("formatAssistantErrorText", () => {
146146
});
147147
expect(result).toBe(formatBillingErrorMessage("google", "gemini-3.1-pro-preview"));
148148
});
149+
it("uses OAuth-specific billing message when authMode is oauth", () => {
150+
const msg = makeAssistantError("insufficient credits");
151+
const result = formatAssistantErrorText(msg, {
152+
provider: "Anthropic",
153+
model: "claude-3-5-sonnet",
154+
authMode: "oauth",
155+
});
156+
expect(result).toBe(formatBillingErrorMessage("Anthropic", "claude-3-5-sonnet", "oauth"));
157+
expect(result).toContain("account limit");
158+
expect(result).not.toContain("API key");
159+
});
149160
it("returns a friendly message for rate limit errors", () => {
150161
const msg = makeAssistantError("429 rate limit reached");
151162
expect(formatAssistantErrorText(msg)).toContain("rate limit reached");

‎src/agents/pi-embedded-helpers/errors.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,13 @@ export function classifyProviderRuntimeFailureKind(
986986

987987
export function formatAssistantErrorText(
988988
msg: AssistantMessage,
989-
opts?: { cfg?: OpenClawConfig; sessionKey?: string; provider?: string; model?: string },
989+
opts?: {
990+
cfg?: OpenClawConfig;
991+
sessionKey?: string;
992+
provider?: string;
993+
model?: string;
994+
authMode?: string;
995+
},
990996
): string | undefined {
991997
// Also format errors if errorMessage is present, even if stopReason isn't "error"
992998
const raw = (msg.errorMessage ?? "").trim();
@@ -1138,7 +1144,7 @@ export function formatAssistantErrorText(
11381144
}
11391145

11401146
if (isBillingErrorMessage(raw)) {
1141-
return formatBillingErrorMessage(opts?.provider, opts?.model ?? msg.model);
1147+
return formatBillingErrorMessage(opts?.provider, opts?.model ?? msg.model, opts?.authMode);
11421148
}
11431149

11441150
if (providerRuntimeFailureKind === "schema") {

‎src/agents/pi-embedded-helpers/sanitize-user-facing-text.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@ import {
2727
isTimeoutErrorMessage,
2828
} from "./failover-matches.js";
2929

30-
export function formatBillingErrorMessage(provider?: string, model?: string): string {
30+
export function formatBillingErrorMessage(
31+
provider?: string,
32+
model?: string,
33+
authMode?: string,
34+
): string {
3135
const providerName = provider?.trim();
3236
const modelName = model?.trim();
3337
const providerLabel =
3438
providerName && modelName ? `${providerName} (${modelName})` : providerName || undefined;
39+
if (authMode === "oauth") {
40+
if (providerLabel) {
41+
return `⚠️ ${providerLabel} returned a billing error — your ${providerName} account limit has been reached. Check your ${providerName} billing dashboard or switch to a different account.`;
42+
}
43+
return "⚠️ API provider returned a billing error — your account limit has been reached. Check your provider's billing dashboard or switch to a different account.";
44+
}
3545
if (providerLabel) {
3646
return `⚠️ ${providerLabel} returned a billing error — your API key has run out of credits or has an insufficient balance. Check your ${providerName} billing dashboard and top up or switch to a different API key.`;
3747
}

‎src/agents/pi-embedded-runner/run.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,6 +2382,7 @@ export async function runEmbeddedPiAgent(
23822382
config: params.config,
23832383
sessionKey: params.sessionKey ?? params.sessionId,
23842384
authFailure,
2385+
authMode: runtimeAuthState?.authMode,
23852386
rateLimitFailure,
23862387
billingFailure,
23872388
cloudCodeAssistFormatError,
@@ -2483,6 +2484,7 @@ export async function runEmbeddedPiAgent(
24832484
sessionKey: params.sessionKey ?? params.sessionId,
24842485
provider: activeErrorContext.provider,
24852486
model: activeErrorContext.model,
2487+
authMode: runtimeAuthState?.authMode,
24862488
verboseLevel: params.verboseLevel,
24872489
reasoningLevel: params.reasoningLevel,
24882490
thinkingLevel: params.thinkLevel,

‎src/agents/pi-embedded-runner/run/assistant-failover.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export async function handleAssistantFailover(params: {
5353
config: OpenClawConfig | undefined;
5454
sessionKey?: string;
5555
authFailure: boolean;
56+
authMode?: string;
5657
rateLimitFailure: boolean;
5758
billingFailure: boolean;
5859
cloudCodeAssistFormatError: boolean;
@@ -290,6 +291,7 @@ function resolveAssistantFailoverErrorMessage(params: {
290291
sessionKey: params.sessionKey,
291292
provider: params.activeErrorContext.provider,
292293
model: params.activeErrorContext.model,
294+
authMode: params.authMode,
293295
})
294296
: undefined) ||
295297
params.lastAssistant?.errorMessage?.trim() ||
@@ -301,6 +303,7 @@ function resolveAssistantFailoverErrorMessage(params: {
301303
? formatBillingErrorMessage(
302304
params.activeErrorContext.provider,
303305
params.activeErrorContext.model,
306+
params.authMode,
304307
)
305308
: params.authFailure
306309
? "LLM request unauthorized."

‎src/agents/pi-embedded-runner/run/payloads.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export function buildEmbeddedRunPayloads(params: {
180180
sessionKey: string;
181181
provider?: string;
182182
model?: string;
183+
authMode?: string;
183184
verboseLevel?: VerboseLevel;
184185
reasoningLevel?: ReasoningLevel;
185186
thinkingLevel?: ThinkLevel;
@@ -228,6 +229,7 @@ export function buildEmbeddedRunPayloads(params: {
228229
sessionKey: params.sessionKey,
229230
provider: params.provider,
230231
model: params.model,
232+
authMode: params.authMode,
231233
})
232234
: undefined;
233235
const rawErrorMessage = lastAssistantErrored

0 commit comments

Comments
 (0)