diff --git a/extensions/pazi/src/billing/pazi-billing-message.test.ts b/extensions/pazi/src/billing/pazi-billing-message.test.ts index e7a522561a..32d3a4adeb 100644 --- a/extensions/pazi/src/billing/pazi-billing-message.test.ts +++ b/extensions/pazi/src/billing/pazi-billing-message.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from "vitest"; import { formatPaziBillingErrorMessage, + PAZI_INSUFFICIENT_CREDITS_META, PAZI_OUT_OF_CREDITS_MESSAGE, } from "./pazi-billing-message.js"; @@ -29,6 +30,24 @@ describe("pazi-billing-message", () => { test("should start with warning emoji", () => { expect(PAZI_OUT_OF_CREDITS_MESSAGE).toMatch(/^⚠️/); }); + + test("should end with [insufficient_credits] detection tag", () => { + expect(PAZI_OUT_OF_CREDITS_MESSAGE).toMatch(/\[insufficient_credits\]$/); + }); + }); + + describe("PAZI_INSUFFICIENT_CREDITS_META", () => { + test("should have correct code", () => { + expect(PAZI_INSUFFICIENT_CREDITS_META.code).toBe("insufficient_credits"); + }); + + test("should have subscription action URL", () => { + expect(PAZI_INSUFFICIENT_CREDITS_META.actionUrl).toBe("/dashboard/account/subscription"); + }); + + test("should have Upgrade label", () => { + expect(PAZI_INSUFFICIENT_CREDITS_META.actionLabel).toBe("Upgrade"); + }); }); describe("formatPaziBillingErrorMessage", () => { diff --git a/extensions/pazi/src/billing/pazi-billing-message.ts b/extensions/pazi/src/billing/pazi-billing-message.ts index 43d7effcf1..40b34025ce 100644 --- a/extensions/pazi/src/billing/pazi-billing-message.ts +++ b/extensions/pazi/src/billing/pazi-billing-message.ts @@ -1,16 +1,29 @@ /** * Pazi-specific billing error message for when users run out of credits. * Replaces the generic "API key" message with subscription-specific guidance. + * + * The `[insufficient_credits]` tag at the end is a reliable detection marker + * for the frontend — it survives all agent formatting/sanitization steps. */ export const PAZI_OUT_OF_CREDITS_MESSAGE = - "⚠️ You've run out of Pazi credits. Upgrade your subscription to continue: https://pazi.ai/dashboard/account/subscription"; + "⚠️ You've run out of Pazi credits. Upgrade your subscription to continue: https://pazi.ai/dashboard/account/subscription [insufficient_credits]"; + +/** + * Structured metadata for the insufficient credits error. + * Canonical source of truth for error payloads used by the extension and frontend. + */ +export const PAZI_INSUFFICIENT_CREDITS_META = { + code: "insufficient_credits", + actionUrl: "/dashboard/account/subscription", + actionLabel: "Upgrade", +} as const; /** * Replacement for the core formatBillingErrorMessage function. * Returns the Pazi-specific message regardless of provider/model params * since all LLM calls go through the Pazi API. */ -export function formatPaziBillingErrorMessage(provider?: string, model?: string): string { +export function formatPaziBillingErrorMessage(_provider?: string, _model?: string): string { return PAZI_OUT_OF_CREDITS_MESSAGE; } diff --git a/extensions/pazi/src/proxy/pazi-proxy.test.ts b/extensions/pazi/src/proxy/pazi-proxy.test.ts index 3f5d91d0d3..1713d572c4 100644 --- a/extensions/pazi/src/proxy/pazi-proxy.test.ts +++ b/extensions/pazi/src/proxy/pazi-proxy.test.ts @@ -1,5 +1,9 @@ import { describe, expect, test } from "vitest"; -import { PAZI_OUT_OF_CREDITS_MESSAGE } from "../billing/pazi-billing-message.js"; +import { + PAZI_INSUFFICIENT_CREDITS_META, + PAZI_OUT_OF_CREDITS_MESSAGE, +} from "../billing/pazi-billing-message.js"; +import { getLastBillingError } from "./pazi-proxy.js"; describe("pazi-proxy billing integration", () => { test("should be able to import Pazi billing message", () => { @@ -22,6 +26,7 @@ describe("pazi-proxy billing integration", () => { expect(paziResponse.error.type).toBe("insufficient_credits"); expect(paziResponse.error.message).toBe(PAZI_OUT_OF_CREDITS_MESSAGE); expect(paziResponse.error.message).toContain("subscription"); + expect(paziResponse.error.message).toContain("[insufficient_credits]"); expect(paziResponse.error.message).not.toMatch(/api[- ]?key/i); }); @@ -34,4 +39,14 @@ describe("pazi-proxy billing integration", () => { // Verify the structure we're looking for expect(insufficientCreditsResponse.error).toBe("insufficient_credits"); }); + + test("getLastBillingError should be null initially", () => { + expect(getLastBillingError()).toBeNull(); + }); + + test("PAZI_INSUFFICIENT_CREDITS_META should have correct fields for surface_error event", () => { + expect(PAZI_INSUFFICIENT_CREDITS_META.code).toBe("insufficient_credits"); + expect(PAZI_INSUFFICIENT_CREDITS_META.actionUrl).toBe("/dashboard/account/subscription"); + expect(PAZI_INSUFFICIENT_CREDITS_META.actionLabel).toBe("Upgrade"); + }); }); diff --git a/extensions/pazi/src/proxy/pazi-proxy.ts b/extensions/pazi/src/proxy/pazi-proxy.ts index 5bd247fe33..ec5294f9f6 100644 --- a/extensions/pazi/src/proxy/pazi-proxy.ts +++ b/extensions/pazi/src/proxy/pazi-proxy.ts @@ -1,9 +1,49 @@ import http from "node:http"; import type { IncomingHttpHeaders } from "node:http"; import https from "node:https"; -import { PAZI_OUT_OF_CREDITS_MESSAGE } from "../billing/pazi-billing-message.js"; +import { + PAZI_OUT_OF_CREDITS_MESSAGE, + PAZI_INSUFFICIENT_CREDITS_META, +} from "../billing/pazi-billing-message.js"; import { getProxyContext, markProxyActivity } from "../context.js"; +/** + * Module-level billing error state. Stored so future hook extensibility + * can check if a billing error occurred during the session. + */ +let lastBillingError: { code: string; timestamp: number } | null = null; + +export function getLastBillingError(): { code: string; timestamp: number } | null { + return lastBillingError; +} + +/** + * Best-effort broadcast of a surface_error integration event. + * Uses the gateway request scope if available (inside tool handlers). + * Silently no-ops outside gateway scope (e.g. during proxy-only requests). + */ +function tryBroadcastSurfaceError(message: string): void { + try { + // Dynamic import to avoid hard dependency — the proxy may run + // before the plugin runtime is initialized. + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { getPluginRuntimeGatewayRequestScope } = require("openclaw/plugin-sdk/plugin-runtime"); + const scope = getPluginRuntimeGatewayRequestScope(); + if (!scope?.context) { + return; + } + scope.context.broadcast("integration", { + action: "surface_error", + code: PAZI_INSUFFICIENT_CREDITS_META.code, + message, + actionUrl: PAZI_INSUFFICIENT_CREDITS_META.actionUrl, + actionLabel: PAZI_INSUFFICIENT_CREDITS_META.actionLabel, + }); + } catch { + // Non-fatal: plugin runtime not available in this context + } +} + type ProxyLogger = { info: (message: string) => void; warn: (message: string) => void; @@ -131,6 +171,15 @@ export async function startPaziProxy(params: StartProxyParams): Promise