mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 17:57:26 +00:00
fix: include provider billing context (openclaw#14697) thanks @fagemx
This commit is contained in:
@@ -7,11 +7,30 @@ import {
|
||||
} from "./pi-embedded-helpers.js";
|
||||
|
||||
describe("formatAssistantErrorText", () => {
|
||||
const makeAssistantError = (errorMessage: string): AssistantMessage =>
|
||||
({
|
||||
stopReason: "error",
|
||||
errorMessage,
|
||||
}) as AssistantMessage;
|
||||
const makeAssistantError = (errorMessage: string): AssistantMessage => ({
|
||||
role: "assistant",
|
||||
api: "openai-responses",
|
||||
provider: "openai",
|
||||
model: "test-model",
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
total: 0,
|
||||
},
|
||||
},
|
||||
stopReason: "error",
|
||||
errorMessage,
|
||||
content: [{ type: "text", text: errorMessage }],
|
||||
timestamp: 0,
|
||||
});
|
||||
|
||||
it("returns a friendly message for context overflow", () => {
|
||||
const msg = makeAssistantError("request_too_large");
|
||||
|
||||
@@ -4,8 +4,9 @@ import type { FailoverReason } from "./types.js";
|
||||
import { formatSandboxToolPolicyBlockedMessage } from "../sandbox.js";
|
||||
|
||||
export function formatBillingErrorMessage(provider?: string): string {
|
||||
if (provider) {
|
||||
return `⚠️ ${provider} returned a billing error — your API key has run out of credits or has an insufficient balance. Check your ${provider} billing dashboard and top up or switch to a different API key.`;
|
||||
const providerName = provider?.trim();
|
||||
if (providerName) {
|
||||
return `⚠️ ${providerName} 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.`;
|
||||
}
|
||||
return "⚠️ API provider returned a billing error — your API key has run out of credits or has an insufficient balance. Check your provider's billing dashboard and top up or switch to a different API key.";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { formatBillingErrorMessage } from "../../pi-embedded-helpers.js";
|
||||
import { buildEmbeddedRunPayloads } from "./payloads.js";
|
||||
|
||||
describe("buildEmbeddedRunPayloads", () => {
|
||||
@@ -14,13 +15,31 @@ describe("buildEmbeddedRunPayloads", () => {
|
||||
},
|
||||
"request_id": "req_011CX7DwS7tSvggaNHmefwWg"
|
||||
}`;
|
||||
const makeAssistant = (overrides: Partial<AssistantMessage>): AssistantMessage =>
|
||||
({
|
||||
stopReason: "error",
|
||||
errorMessage: errorJson,
|
||||
content: [{ type: "text", text: errorJson }],
|
||||
...overrides,
|
||||
}) as AssistantMessage;
|
||||
const makeAssistant = (overrides: Partial<AssistantMessage>): AssistantMessage => ({
|
||||
role: "assistant",
|
||||
api: "openai-responses",
|
||||
provider: "openai",
|
||||
model: "test-model",
|
||||
usage: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
totalTokens: 0,
|
||||
cost: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
cacheRead: 0,
|
||||
cacheWrite: 0,
|
||||
total: 0,
|
||||
},
|
||||
},
|
||||
timestamp: 0,
|
||||
stopReason: "error",
|
||||
errorMessage: errorJson,
|
||||
content: [{ type: "text", text: errorJson }],
|
||||
...overrides,
|
||||
});
|
||||
|
||||
it("suppresses raw API error JSON when the assistant errored", () => {
|
||||
const lastAssistant = makeAssistant({});
|
||||
@@ -80,6 +99,27 @@ describe("buildEmbeddedRunPayloads", () => {
|
||||
expect(payloads.some((payload) => payload.text?.includes("request_id"))).toBe(false);
|
||||
});
|
||||
|
||||
it("includes provider context for billing errors", () => {
|
||||
const lastAssistant = makeAssistant({
|
||||
errorMessage: "insufficient credits",
|
||||
content: [{ type: "text", text: "insufficient credits" }],
|
||||
});
|
||||
const payloads = buildEmbeddedRunPayloads({
|
||||
assistantTexts: [],
|
||||
toolMetas: [],
|
||||
lastAssistant,
|
||||
sessionKey: "session:telegram",
|
||||
provider: "Anthropic",
|
||||
inlineToolResultsAllowed: false,
|
||||
verboseLevel: "off",
|
||||
reasoningLevel: "off",
|
||||
});
|
||||
|
||||
expect(payloads).toHaveLength(1);
|
||||
expect(payloads[0]?.text).toBe(formatBillingErrorMessage("Anthropic"));
|
||||
expect(payloads[0]?.isError).toBe(true);
|
||||
});
|
||||
|
||||
it("suppresses raw error JSON even when errorMessage is missing", () => {
|
||||
const lastAssistant = makeAssistant({ errorMessage: undefined });
|
||||
const payloads = buildEmbeddedRunPayloads({
|
||||
@@ -98,10 +138,15 @@ describe("buildEmbeddedRunPayloads", () => {
|
||||
});
|
||||
|
||||
it("does not suppress error-shaped JSON when the assistant did not error", () => {
|
||||
const lastAssistant = makeAssistant({
|
||||
stopReason: "stop",
|
||||
errorMessage: undefined,
|
||||
content: [],
|
||||
});
|
||||
const payloads = buildEmbeddedRunPayloads({
|
||||
assistantTexts: [errorJsonPretty],
|
||||
toolMetas: [],
|
||||
lastAssistant: { stopReason: "end_turn" } as AssistantMessage,
|
||||
lastAssistant,
|
||||
sessionKey: "session:telegram",
|
||||
inlineToolResultsAllowed: false,
|
||||
verboseLevel: "off",
|
||||
@@ -132,10 +177,15 @@ describe("buildEmbeddedRunPayloads", () => {
|
||||
});
|
||||
|
||||
it("does not add tool error fallback when assistant output exists", () => {
|
||||
const lastAssistant = makeAssistant({
|
||||
stopReason: "stop",
|
||||
errorMessage: undefined,
|
||||
content: [],
|
||||
});
|
||||
const payloads = buildEmbeddedRunPayloads({
|
||||
assistantTexts: ["All good"],
|
||||
toolMetas: [],
|
||||
lastAssistant: { stopReason: "end_turn" } as AssistantMessage,
|
||||
lastAssistant,
|
||||
lastToolError: { toolName: "browser", error: "tab not found" },
|
||||
sessionKey: "session:telegram",
|
||||
inlineToolResultsAllowed: false,
|
||||
@@ -149,20 +199,22 @@ describe("buildEmbeddedRunPayloads", () => {
|
||||
});
|
||||
|
||||
it("adds tool error fallback when the assistant only invoked tools", () => {
|
||||
const lastAssistant = makeAssistant({
|
||||
stopReason: "toolUse",
|
||||
errorMessage: undefined,
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "toolu_01",
|
||||
name: "exec",
|
||||
arguments: { command: "echo hi" },
|
||||
},
|
||||
],
|
||||
});
|
||||
const payloads = buildEmbeddedRunPayloads({
|
||||
assistantTexts: [],
|
||||
toolMetas: [],
|
||||
lastAssistant: {
|
||||
stopReason: "toolUse",
|
||||
content: [
|
||||
{
|
||||
type: "toolCall",
|
||||
id: "toolu_01",
|
||||
name: "exec",
|
||||
arguments: { command: "echo hi" },
|
||||
},
|
||||
],
|
||||
} as AssistantMessage,
|
||||
lastAssistant,
|
||||
lastToolError: { toolName: "exec", error: "Command exited with code 1" },
|
||||
sessionKey: "session:telegram",
|
||||
inlineToolResultsAllowed: false,
|
||||
|
||||
@@ -6,6 +6,7 @@ import { parseReplyDirectives } from "../../../auto-reply/reply/reply-directives
|
||||
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../../../auto-reply/tokens.js";
|
||||
import { formatToolAggregate } from "../../../auto-reply/tool-meta.js";
|
||||
import {
|
||||
BILLING_ERROR_USER_MESSAGE,
|
||||
formatAssistantErrorText,
|
||||
formatRawAssistantErrorForUi,
|
||||
getApiErrorPayloadFingerprint,
|
||||
@@ -77,6 +78,7 @@ export function buildEmbeddedRunPayloads(params: {
|
||||
? normalizeTextForComparison(rawErrorMessage)
|
||||
: null;
|
||||
const normalizedErrorText = errorText ? normalizeTextForComparison(errorText) : null;
|
||||
const normalizedGenericBillingErrorText = normalizeTextForComparison(BILLING_ERROR_USER_MESSAGE);
|
||||
const genericErrorText = "The AI service returned an error. Please try again.";
|
||||
if (errorText) {
|
||||
replyItems.push({ text: errorText, isError: true });
|
||||
@@ -135,6 +137,13 @@ export function buildEmbeddedRunPayloads(params: {
|
||||
if (trimmed === genericErrorText) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
normalized &&
|
||||
normalizedGenericBillingErrorText &&
|
||||
normalized === normalizedGenericBillingErrorText
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (rawErrorMessage && trimmed === rawErrorMessage) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user