Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(cloud): the credits chip tells the truth about trainings and video

purpleda6ad00ce4b5
diff --git a/src/components/create/experimental/Composer.tsx b/src/components/create/experimental/Composer.tsx
index 5b313161..4935a3fc 100644
--- a/src/components/create/experimental/Composer.tsx
+++ b/src/components/create/experimental/Composer.tsx
@@ -5,6 +5,7 @@ import { useCreateStore, MODEL_TYPE_DEFAULTS } from '../../../stores/createStore
import { classifyModel } from '../../../api/comfyui'
import { useCreateExp } from './CreateContext'
import { intentToJob } from '../../../lib/render/cloud-jobs'
+import { meterState } from '../../../lib/render/credits-meter'
import {
cloudModelById,
defaultCloudModel,
@@ -110,11 +111,18 @@ export function Composer({ onOpenAdvanced, onOpenWorkflows }: Props) {
? frames / fps
: undefined
const costFallback = quota?.costs[intentKind === 'audio' ? 'image' : intentKind] ?? 0
+ // The exact rule the meter chip renders, so the button can never invite a run
+ // the chip is already refusing. Credits alone were not enough: a user out of
+ // monthly character trainings kept an enabled Create button and got a 429.
const creditsOk =
backend !== 'cloud' ||
(quota != null &&
- quota.remaining.credits >=
- runCredits(intentKind, intentOp, pickedModel, runSeconds, costFallback, targetResolution))
+ meterState(
+ quota,
+ runCredits(intentKind, intentOp, pickedModel, runSeconds, costFallback, targetResolution),
+ intentKind,
+ intentOp,
+ ).kind === 'ok')
// Match useCloudCreate's submit-time edit fallback so the Neg gate reflects
// the model the run actually uses, not a t2i model still in the picker.
const runModel =
diff --git a/src/components/create/experimental/CreditsMeter.tsx b/src/components/create/experimental/CreditsMeter.tsx
index 17ee8345..225cc4c2 100644
--- a/src/components/create/experimental/CreditsMeter.tsx
+++ b/src/components/create/experimental/CreditsMeter.tsx
@@ -2,6 +2,7 @@ import { useCreateStore } from '../../../stores/createStore'
import { useCreateExp } from './CreateContext'
import { intentToJob } from '../../../lib/render/cloud-jobs'
import { defaultCloudModel, resolveOpPick, runCredits } from '../../../stores/cloudCatalogStore'
+import { meterState } from '../../../lib/render/credits-meter'
import { Tooltip } from '../ui/Tooltip'
import { openExternal } from '../../../api/backend'
import { CLOUD_BASE } from '../../../api/cloud/config'
@@ -53,32 +54,55 @@ export function CreditsMeter() {
)
const remaining = quota.remaining.credits
const limit = quota.limits.credits
- const enough = remaining >= cost
- const pct = limit > 0 ? Math.max(0, Math.min(1, remaining / limit)) : 0
+ const state = meterState(quota, cost, kind, op)
+ const upsell = (label: string) => (
+ <button
+ onClick={() => void openExternal(`${CLOUD_BASE}/pricing`)}
+ className="t-control px-2 h-[var(--control-h-sm)] inline-flex items-center rounded-md bg-amber-500/15 text-amber-300 hover:bg-amber-500/25 transition-colors"
+ >
+ {label}
+ </button>
+ )
- if (!enough) {
- return (
- <button
- onClick={() => void openExternal(`${CLOUD_BASE}/pricing`)}
- className="t-control px-2 h-[var(--control-h-sm)] inline-flex items-center rounded-md bg-amber-500/15 text-amber-300 hover:bg-amber-500/25 transition-colors"
- >
- {remaining <= 0 ? 'Out of credits, upgrade' : `Needs ${cost} credits (${remaining} left)`}
- </button>
+ if (state.kind === 'insufficient') {
+ return upsell(
+ state.remaining <= 0
+ ? 'Out of credits, upgrade'
+ : `Needs ${state.cost} credits (${state.remaining} left)`,
)
}
+ if (state.kind === 'no-trainings') return upsell('No trainings left this month, upgrade')
+ if (state.kind === 'no-video-budget') return upsell('Video budget used up, top up')
+
+ const noun = op === 'lora-train' ? 'training' : kind === 'video' ? 'clip' : kind === 'audio' ? 'track' : 'image'
+ const tail = state.runsLeft === null ? '' : `, about ${state.runsLeft} more like it`
+ const videoTail =
+ state.showVideoBudget && quota.video
+ ? ` (monthly video budget: ${quota.video.remaining} of ${quota.video.limit} credits left)`
+ : ''
+ const trainingTail =
+ op === 'lora-train' && quota.trainings
+ ? ` (${quota.trainings.remaining} of ${quota.trainings.limit} trainings left)`
+ : ''
return (
<Tooltip
- content={`${remaining} of ${limit} credits left this billing period. This ${kind === 'video' ? 'clip' : 'image'} uses ${cost}.`}
+ content={`${remaining} of ${limit} credits left this billing period. This ${noun} uses ${cost}${tail}${videoTail}${trainingTail}.`}
>
<div className="flex items-center gap-1.5 px-2 h-[var(--control-h-sm)] rounded-md bg-white/[0.04] text-gray-400 t-control">
<div className="w-12 h-1 rounded-full bg-white/10 overflow-hidden">
<div
- className={cn('h-full rounded-full', pct > 0.25 ? 'bg-emerald-400/80' : 'bg-amber-400/80')}
- style={{ width: `${pct * 100}%` }}
+ className={cn('h-full rounded-full', state.pct > 0.25 ? 'bg-emerald-400/80' : 'bg-amber-400/80')}
+ style={{ width: `${state.pct * 100}%` }}
/>
</div>
<span className="tabular-nums">{remaining}</span>
+ {state.runsLeft !== null && (
+ <>
+ <span className="text-gray-600">·</span>
+ <span className="tabular-nums whitespace-nowrap">≈{state.runsLeft} {state.unit}</span>
+ </>
+ )}
</div>
</Tooltip>
)
diff --git a/src/lib/render/__tests__/credits-meter.test.ts b/src/lib/render/__tests__/credits-meter.test.ts
new file mode 100644
index 00000000..5ad76f5c
--- /dev/null
+++ b/src/lib/render/__tests__/credits-meter.test.ts
@@ -0,0 +1,170 @@
+/**
+ * The desktop credits chip, and the Create button that shares its rule.
+ *
+ * The desktop meter read only `remaining.credits` while the server had been
+ * sending `video`, `trainings` and `topup` since migration 0029. Two things
+ * followed: a user who had spent their monthly character trainings saw a
+ * healthy green bar and an enabled Create button, then got a 429; and the chip
+ * could never say "about N more like it", which is the one number a person
+ * actually wants from a meter.
+ *
+ * These lock the three rules that are easy to get backwards: an ABSENT field
+ * means uncapped and must never gate, the top-up wallet is exempt from the
+ * video sub-budget and therefore extends it, and the sub-budget is named only
+ * when it really binds.
+ *
+ * Run: npx vitest run src/lib/render/__tests__/credits-meter.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { meterState, drawsVideoBudget } from '../credits-meter'
+import type { CloudQuota } from '../cloud-jobs'
+
+/** Hosted as it stands after the 2026-08-05 lift: video limit equals the pool. */
+function hosted(over: Partial<CloudQuota> = {}): CloudQuota {
+ return {
+ tier: 'hosted',
+ period: '2026-08-05',
+ limits: { credits: 900_000 },
+ costs: { image: 300, video: 15_000 },
+ used: { credits_used: 0 },
+ remaining: { credits: 900_000 },
+ topup: { credits: 0 },
+ video: { limit: 900_000, used: 0, remaining: 900_000 },
+ trainings: { limit: 2, used: 0, remaining: 2 },
+ ...over,
+ }
+}
+
+describe('drawsVideoBudget', () => {
+ it('counts real video renders and never a training', () => {
+ expect(drawsVideoBudget('video', 'generate')).toBe(true)
+ expect(drawsVideoBudget('video', 'animate')).toBe(true)
+ expect(drawsVideoBudget('video', 'lora-train')).toBe(false)
+ expect(drawsVideoBudget('image', 'generate')).toBe(false)
+ expect(drawsVideoBudget('audio', 'music')).toBe(false)
+ })
+})
+
+describe('with the video cap lifted (the shipping shape)', () => {
+ it('does not name a sub-budget that equals the pool', () => {
+ // Quoting "900000 of 900000 credits left" would advertise a limit the
+ // pricing page says we do not have.
+ const s = meterState(hosted(), 40_000, 'video', 'generate')
+ expect(s).toMatchObject({ kind: 'ok', showVideoBudget: false, runsLeft: 22, unit: 'clips' })
+ })
+
+ it('counts images off the whole pool', () => {
+ expect(meterState(hosted(), 300, 'image', 'generate')).toMatchObject({
+ kind: 'ok', runsLeft: 3000, unit: 'images',
+ })
+ })
+})
+
+describe('with a real cap back in place', () => {
+ it('binds the count and names the budget', () => {
+ // Exactly the old 300k Hosted shape. Lowering one number brings the whole
+ // behaviour back, which is why the branch is derived and not deleted.
+ const s = meterState(
+ hosted({ video: { limit: 300_000, used: 0, remaining: 300_000 } }),
+ 40_000, 'video', 'generate',
+ )
+ expect(s).toMatchObject({ kind: 'ok', showVideoBudget: true, runsLeft: 7 })
+ })
+
+ it('refuses the clip once the sub-budget cannot cover it', () => {
+ expect(
+ meterState(
+ hosted({ video: { limit: 300_000, used: 290_000, remaining: 10_000 } }),
+ 40_000, 'video', 'generate',
+ ),
+ ).toEqual({ kind: 'no-video-budget' })
+ })
+})
+
+describe('the top-up wallet is exempt from the video sub-budget', () => {
+ it('funds a clip even when the monthly video room is spent', () => {
+ // The dialog and the 429 body both promise this. Gating on the monthly
+ // figure alone would refuse a clip the server would happily run.
+ const s = meterState(
+ hosted({
+ remaining: { credits: 100_000 },
+ topup: { credits: 100_000 },
+ video: { limit: 900_000, used: 900_000, remaining: 0 },
+ }),
+ 40_000, 'video', 'generate',
+ )
+ expect(s).toMatchObject({ kind: 'ok', runsLeft: 2 })
+ })
+})
+
+describe('character trainings are a count, not a budget', () => {
+ it('refuses the training when the monthly count is spent', () => {
+ const q = hosted({ trainings: { limit: 2, used: 2, remaining: 0 } })
+ expect(meterState(q, 100_000, 'image', 'lora-train')).toEqual({ kind: 'no-trainings' })
+ })
+
+ it('still lets an ordinary image through on the same quota', () => {
+ // The count gates trainings only. Blocking the whole surface would be the
+ // obvious over-correction.
+ const q = hosted({ trainings: { limit: 2, used: 2, remaining: 0 } })
+ expect(meterState(q, 300, 'image', 'generate')).toMatchObject({ kind: 'ok' })
+ })
+
+ it('counts down by the remaining trainings, not by what the credits allow', () => {
+ // 900k credits buy 9 runs at 100k, but the plan allows 2.
+ const s = meterState(hosted(), 100_000, 'image', 'lora-train')
+ expect(s).toMatchObject({ kind: 'ok', runsLeft: 2, unit: 'trainings' })
+ })
+})
+
+describe('an older server never gates', () => {
+ it('treats absent video and training fields as uncapped', () => {
+ // A pre-0029 server sends neither. Reading a missing field as zero would
+ // lock every training and every clip out of the app.
+ const bare: CloudQuota = {
+ tier: 'hosted',
+ period: '2026-08-05',
+ limits: { credits: 900_000 },
+ costs: { image: 300, video: 15_000 },
+ used: { credits_used: 0 },
+ remaining: { credits: 900_000 },
+ }
+ expect(meterState(bare, 100_000, 'image', 'lora-train')).toMatchObject({ kind: 'ok' })
+ expect(meterState(bare, 40_000, 'video', 'generate')).toMatchObject({
+ kind: 'ok', showVideoBudget: false,
+ })
+ })
+})
+
+describe('the balance itself', () => {
+ it('reports insufficient with the numbers the chip prints', () => {
+ const q = hosted({ remaining: { credits: 100 } })
+ expect(meterState(q, 300, 'image', 'generate')).toEqual({
+ kind: 'insufficient', remaining: 100, cost: 300,
+ })
+ })
+
+ it('claims no count while the run cost is still unknown', () => {
+ // costs are 0 until the catalogue loads. floor(pool / 0) is Infinity, and
+ // "≈Infinity images" is worse than showing the balance alone.
+ expect(meterState(hosted(), 0, 'image', 'generate')).toMatchObject({
+ kind: 'ok', runsLeft: null,
+ })
+ })
+
+ it('uses the singular when exactly one run is left', () => {
+ const q = hosted({ remaining: { credits: 40_000 } })
+ expect(meterState(q, 40_000, 'video', 'generate')).toMatchObject({
+ runsLeft: 1, unit: 'clip',
+ })
+ })
+
+ it('fills the bar from the plan limit and clamps it', () => {
+ expect(meterState(hosted(), 300, 'image', 'generate')).toMatchObject({ pct: 1 })
+ const half = hosted({ remaining: { credits: 450_000 } })
+ expect(meterState(half, 300, 'image', 'generate')).toMatchObject({ pct: 0.5 })
+ // A bonus grant can push remaining past the plan limit; the bar stops at 1.
+ const over = hosted({ remaining: { credits: 1_800_000 } })
+ expect(meterState(over, 300, 'image', 'generate')).toMatchObject({ pct: 1 })
+ })
+})
diff --git a/src/lib/render/cloud-jobs.ts b/src/lib/render/cloud-jobs.ts
index a77621b2..7f72040a 100644
--- a/src/lib/render/cloud-jobs.ts
+++ b/src/lib/render/cloud-jobs.ts
@@ -15,6 +15,11 @@ export type RenderOp =
// One shared compute-credit wallet — text + media draw from the same budget
// (server shape: uselu /api/jobs/quota).
+//
+// `topup`, `video` and `trainings` have been on the wire since migration 0029;
+// they are optional here because an older server does not send them, and the
+// meter must read an absent field as "uncapped" rather than as zero. See
+// credits-meter.ts for the rule both the chip and the Create gate follow.
export interface CloudQuota {
tier: string
period: string
@@ -22,6 +27,14 @@ export interface CloudQuota {
costs: { image: number; video: number }
used: { credits_used: number }
remaining: { credits: number }
+ /** Non-expiring wallet, spent only after the monthly allowance. Exempt from
+ * the video sub-budget, so it extends video room. */
+ topup?: { credits: number }
+ /** Monthly video sub-budget. `remaining` is the MONTHLY share only. */
+ video?: { limit: number; used: number; remaining: number }
+ /** Character trainings this cycle. A count, not a budget: the wallet cannot
+ * buy past it. */
+ trainings?: { limit: number; used: number; remaining: number }
}
/** Which queue kind + workflow op a Create intent renders as.
diff --git a/src/lib/render/credits-meter.ts b/src/lib/render/credits-meter.ts
new file mode 100644
index 00000000..64d5bf69
--- /dev/null
+++ b/src/lib/render/credits-meter.ts
@@ -0,0 +1,81 @@
+// What the credits meter says, and what the Create button gates on. One rule,
+// two surfaces, so the chip can never promise a run the button refuses.
+//
+// The web Studio decides this inline in its CreditsMeter JSX. That is why its
+// training branch has never had a test and why the desktop copy silently drifted
+// behind it: the desktop meter knew only about credits, so a user who had spent
+// their monthly character trainings saw a healthy green bar, pressed Create and
+// got a 429 from the server. The server has sent `video`, `trainings` and
+// `topup` since migration 0029; only the client ignored them.
+
+import type { CloudQuota, RenderKind, RenderOp } from './cloud-jobs'
+
+export type MeterState =
+ | { kind: 'insufficient'; remaining: number; cost: number }
+ | { kind: 'no-trainings' }
+ | { kind: 'no-video-budget' }
+ | {
+ kind: 'ok'
+ /** null when the run's cost is not known yet (catalogue still loading),
+ * so the chip shows the balance without inventing a count. */
+ runsLeft: number | null
+ unit: string
+ pct: number
+ /** Name the video sub-budget only when it is really the binding limit. */
+ showVideoBudget: boolean
+ }
+
+/** Video renders draw the 0029 sub-budget. A character training never does; it
+ * draws the separate monthly COUNT instead. */
+export function drawsVideoBudget(kind: RenderKind, op: RenderOp): boolean {
+ return kind === 'video' && op !== 'lora-train'
+}
+
+function unitFor(kind: RenderKind, isTraining: boolean, n: number): string {
+ if (isTraining) return n === 1 ? 'training' : 'trainings'
+ if (kind === 'video') return n === 1 ? 'clip' : 'clips'
+ if (kind === 'audio') return n === 1 ? 'track' : 'tracks'
+ return n === 1 ? 'image' : 'images'
+}
+
+export function meterState(
+ quota: CloudQuota,
+ cost: number,
+ kind: RenderKind,
+ op: RenderOp,
+): MeterState {
+ const remaining = quota.remaining.credits
+ const limit = quota.limits.credits
+ const topup = quota.topup?.credits ?? 0
+ const isTraining = op === 'lora-train'
+ const isVideoBudget = drawsVideoBudget(kind, op)
+
+ // Absent fields mean a pre-0029 server: uncapped, never gate on data we do
+ // not have. Video's monthly room is extended by the wallet because top-up
+ // credits are exempt from the sub-budget; the training count is a hard count
+ // the wallet cannot buy past.
+ const videoRoom = quota.video ? quota.video.remaining + topup : Infinity
+ const trainingsLeft = quota.trainings ? quota.trainings.remaining : Infinity
+
+ if (remaining < cost) return { kind: 'insufficient', remaining, cost }
+ if (isTraining && trainingsLeft <= 0) return { kind: 'no-trainings' }
+ if (isVideoBudget && videoRoom < cost) return { kind: 'no-video-budget' }
+
+ const runsPool = isVideoBudget ? Math.min(remaining, videoRoom) : remaining
+ const byCredits = cost > 0 ? Math.floor(runsPool / cost) : null
+ const runsLeft =
+ byCredits === null ? null : isTraining ? Math.min(trainingsLeft, byCredits) : byCredits
+
+ // No tier caps video below its pool since 2026-08-05, so on a plain plan
+ // videoRoom always covers `remaining` and this stays false. It turns itself
+ // back on the day a real cap returns, rather than being deleted and forgotten.
+ const showVideoBudget = isVideoBudget && quota.video != null && videoRoom < remaining
+
+ return {
+ kind: 'ok',
+ runsLeft,
+ unit: unitFor(kind, isTraining, runsLeft ?? 0),
+ pct: limit > 0 ? Math.max(0, Math.min(1, remaining / limit)) : 0,
+ showVideoBudget,
+ }
+}