diff --git a/e2e/tests/integrations.test.mjs b/e2e/tests/integrations.test.mjs index 83027591..204f78e8 100644 --- a/e2e/tests/integrations.test.mjs +++ b/e2e/tests/integrations.test.mjs @@ -1375,7 +1375,7 @@ test("agent tasks and routines follow the engine contract", async () => { async (app) => { await seedTerms(app); const credits = await app.invoke("get_agent_credits"); - assert.equal(credits.allowance, 10000); + assert.equal(credits.allowance, 50000); assert.equal(credits.balance, credits.allowance - credits.used); const profile = (name) => diff --git a/scripts/clips/agent-demo.js b/scripts/clips/agent-demo.js index d13930e6..704f229b 100644 --- a/scripts/clips/agent-demo.js +++ b/scripts/clips/agent-demo.js @@ -464,15 +464,13 @@ list_agent_routine_runs: (args) => args.id === "routine-daily" ? copy(routineRuns) : [], get_agent_credits: { - allowance: 10000, - used: 2350, + allowance: 200000, + used: 23500, extra: 0, - balance: 7650, + balance: 176500, periodEnd: new Date(nowMs + 12 * 24 * 60 * MIN).toISOString(), plan: "pro", unlimited: false, - freeGrant: null, - freeGrantAt: null, }, capture_profile_preview: (args) => { const page = pageOf.get(args.profileId); diff --git a/src-tauri/src/agent_tasks/cloud.rs b/src-tauri/src/agent_tasks/cloud.rs index eaa56d01..40d9e1a6 100644 --- a/src-tauri/src/agent_tasks/cloud.rs +++ b/src-tauri/src/agent_tasks/cloud.rs @@ -41,13 +41,6 @@ pub struct Credits { /// Enterprise and service accounts. #[serde(default)] pub unlimited: bool, - /// A free account's monthly credits: "granted", "pending" (the account is - /// new and checked first) or "denied". None on paid plans. - #[serde(default)] - pub free_grant: Option, - /// When a pending grant is expected. - #[serde(default)] - pub free_grant_at: Option, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] @@ -720,15 +713,15 @@ mod tests { let started: StartedBatch = serde_json::from_str(r#"{"batchId":"b","runs":[{"runId":"r","profileId":"p"}]}"#).unwrap(); assert_eq!(started.runs[0].run_id, "r"); - let credits: Credits = serde_json::from_str(r#"{"allowance":10000,"used":2500,"extra":0,"balance":7500,"periodEnd":"2026-12-01T00:00:00Z","plan":"free"}"#).unwrap(); - assert_eq!(credits.balance, 7500); + let credits: Credits = serde_json::from_str(r#"{"allowance":50000,"used":2500,"extra":0,"balance":47500,"periodEnd":"2026-12-01T00:00:00Z","plan":"solo"}"#).unwrap(); + assert_eq!(credits.balance, 47500); assert!(!credits.unlimited); - assert_eq!(credits.free_grant, None); - let pending: Credits = serde_json::from_str( - r#"{"allowance":0,"balance":0,"plan":"free","freeGrant":"pending","freeGrantAt":"2026-11-18T12:30:00Z"}"#, + let free: Credits = serde_json::from_str( + r#"{"allowance":0,"used":0,"extra":0,"balance":0,"periodEnd":null,"plan":"free","unlimited":false}"#, ) .unwrap(); - assert_eq!(pending.free_grant.as_deref(), Some("pending")); + assert_eq!(free.allowance, 0); + assert_eq!(free.plan.as_deref(), Some("free")); let fires: Fires = serde_json::from_str(r#"{"fires":[{"routineId":"r","batchId":"b"},{"routineId":"r","batchId":"c","trigger":"webhook","input":"order 42"}]}"#).unwrap(); assert_eq!(fires.fires[0].batch_id, "b"); diff --git a/src-tauri/src/agent_tasks/host.rs b/src-tauri/src/agent_tasks/host.rs index b38c6fc3..489fa407 100644 --- a/src-tauri/src/agent_tasks/host.rs +++ b/src-tauri/src/agent_tasks/host.rs @@ -1080,7 +1080,6 @@ mod tests { period_end: None, plan: None, unlimited: false, - ..Default::default() }); assert_eq!(credits["balance"], 6); assert_eq!(credits["periodEnd"], Value::Null); diff --git a/src-tauri/src/agent_tasks/mock.rs b/src-tauri/src/agent_tasks/mock.rs index a0ae9cd2..c9a00b28 100644 --- a/src-tauri/src/agent_tasks/mock.rs +++ b/src-tauri/src/agent_tasks/mock.rs @@ -297,7 +297,7 @@ pub fn table(routine_id: &str, batch_id: Option<&str>) -> Result }) } -const ALLOWANCE: i64 = 10_000; +const ALLOWANCE: i64 = 50_000; const ALREADY_USED: i64 = 1_250; pub fn credits() -> Credits { @@ -318,10 +318,8 @@ pub fn credits() -> Credits { extra: 0, balance: ALLOWANCE - used, period_end: Some("2026-12-01T00:00:00Z".to_string()), - plan: Some("free".to_string()), + plan: Some("solo".to_string()), unlimited: false, - free_grant: Some("granted".to_string()), - free_grant_at: None, } } diff --git a/src/components/agent-chat/chat-turn.tsx b/src/components/agent-chat/chat-turn.tsx index f82e37d9..78ef246c 100644 --- a/src/components/agent-chat/chat-turn.tsx +++ b/src/components/agent-chat/chat-turn.tsx @@ -22,7 +22,11 @@ import { type ChatTurn, isTurnActive, } from "@/lib/agent-chat"; -import { type AgentCredits, isTaskMoving } from "@/lib/agent-tasks"; +import { + type AgentCredits, + isTaskMoving, + needsPaidPlan, +} from "@/lib/agent-tasks"; import { parseBackendError, translateBackendError } from "@/lib/backend-errors"; import { MOTION_DURATION, MOTION_EASE_OUT } from "@/lib/motion"; import { showErrorToast } from "@/lib/toast-utils"; @@ -311,7 +315,32 @@ function Outcome({ ); } const code = errorCode(turn.error); - if (turn.status === "no_credits" || code === "NO_CREDITS") { + const outOfCredits = turn.status === "no_credits" || code === "NO_CREDITS"; + if ( + code === "AGENT_PLAN_REQUIRED" || + (outOfCredits && credits !== null && needsPaidPlan(credits)) + ) { + return ( +
+ + {t("backendErrors.agentPlanRequired")} + + +
+ ); + } + if (outOfCredits) { return (
void; onOpenConsole: () => void; + onUpgrade: () => void; }) { const { t } = useTranslation(); const { closeSidebar } = useAgentShell(); @@ -598,7 +600,14 @@ export function AgentSidebar({
{credits && (
- + { + onUpgrade(); + closeSidebar(); + }} + />
)} + )} +
+ ); + } const left = creditsLeft(credits); if (!left) { return ( @@ -123,17 +147,6 @@ export function CreditsMeter({ } const empty = left.left === 0; const spread = layout === "spread"; - const waiting = - credits.freeGrant === "pending" && credits.freeGrantAt - ? t("agent.credits.freePending", { - time: new Date(credits.freeGrantAt).toLocaleTimeString([], { - hour: "numeric", - minute: "2-digit", - }), - }) - : credits.freeGrant === "denied" - ? t("agent.credits.freeDenied") - : null; return (
- {empty && waiting - ? waiting - : t("agent.credits.left", { n: format(left.left) })} + {t("agent.credits.left", { n: format(left.left) })} { assert.equal(taskState("missed"), "idle"); }); +test("agent credits come with a paid plan", () => { + const free = { + allowance: 0, + used: 0, + extra: 0, + balance: 0, + periodEnd: null, + plan: "free", + unlimited: false, + }; + assert.ok(needsPaidPlan(free)); + // Credits a free plan bought show as usual. + assert.ok(!needsPaidPlan({ ...free, extra: 5000, balance: 5000 })); + assert.deepEqual(creditsLeft({ ...free, extra: 5000, balance: 5000 }), { + left: 5000, + percentUsed: 0, + }); + assert.ok(!needsPaidPlan({ ...free, plan: "solo", allowance: 50000 })); + // A paid plan with nothing left is out of credits, not on the wrong plan. + assert.ok( + !needsPaidPlan({ ...free, plan: "pro", allowance: 200000, used: 200000 }), + ); + assert.ok(!needsPaidPlan({ ...free, plan: "enterprise", unlimited: true })); +}); + test("progress, titles and credits read plainly", () => { assert.equal( batchProgress({ done: 2, failed: 1, stopped: 1, queued: 4, active: 2 }, 10), @@ -92,8 +118,8 @@ test("progress, titles and credits read plainly", () => { assert.equal(promptTitle("Check the inbox\nthen reply"), "Check the inbox"); assert.equal(promptTitle("x".repeat(100), 10).length, 10); assert.deepEqual( - creditsLeft({ allowance: 10000, extra: 0, used: 2500, balance: 7500 }), - { left: 7500, percentUsed: 25 }, + creditsLeft({ allowance: 50000, extra: 0, used: 12500, balance: 37500 }), + { left: 37500, percentUsed: 25 }, ); assert.equal(creditsLeft({ unlimited: true }), null); assert.ok(isCron("0 9 * * 1-5")); diff --git a/src/lib/agent-tasks.ts b/src/lib/agent-tasks.ts index 2f020003..3c10dd8d 100644 --- a/src/lib/agent-tasks.ts +++ b/src/lib/agent-tasks.ts @@ -142,10 +142,6 @@ export interface AgentCredits { periodEnd: string | null; plan: string | null; unlimited: boolean; - /** A free account's credits: checked first while the account is new. */ - freeGrant: "granted" | "pending" | "denied" | null; - /** When a pending grant is expected. */ - freeGrantAt: string | null; } export interface AgentRoutineSchedule { @@ -477,6 +473,11 @@ export function isWebUrl(url: string): boolean { } } +/** Agent credits come with a paid plan; a free plan has only what it bought. */ +export function needsPaidPlan(credits: AgentCredits): boolean { + return !credits.unlimited && credits.plan === "free" && credits.extra <= 0; +} + /** Credits left for the period, and the share used, or null when unlimited. */ export function creditsLeft( credits: AgentCredits, diff --git a/src/lib/backend-errors.ts b/src/lib/backend-errors.ts index ef079f08..6d751cb6 100644 --- a/src/lib/backend-errors.ts +++ b/src/lib/backend-errors.ts @@ -297,6 +297,7 @@ export type BackendErrorCode = | "AGENT_SESSION_NOT_FOUND" | "AGENT_NOT_ENTITLED" | "NO_CREDITS" + | "AGENT_PLAN_REQUIRED" | "TASK_BUDGET" | "AGENT_UNSUPPORTED" | "AGENT_BUSY" @@ -321,7 +322,6 @@ export type BackendErrorCode = | "DESKTOP_OFFLINE" | "DESKTOP_REFUSED" | "RUN_LOST" - | "FREE_CREDITS_PENDING" | "AGENT_NO_PROFILES" | "AGENT_TOO_MANY_PROFILES" | "AGENT_TASK_NOT_FOUND" @@ -1173,10 +1173,10 @@ export function translateBackendError(t: TFunction, err: unknown): string { return t("backendErrors.desktopRefused"); case "RUN_LOST": return t("backendErrors.runLost"); - case "FREE_CREDITS_PENDING": - return t("backendErrors.freeCreditsPending"); case "NO_CREDITS": return t("backendErrors.noCredits"); + case "AGENT_PLAN_REQUIRED": + return t("backendErrors.agentPlanRequired"); case "TASK_BUDGET": return t("backendErrors.taskBudget"); case "AGENT_UNSUPPORTED":