An open-source, faster Devin alternative
TypeScript
Show managed model usage on billing page (#458)
Mohamed Habib411424a2b301
diff --git a/cloudflare-workers/api-edge/src/dashboard.ts b/cloudflare-workers/api-edge/src/dashboard.tsindex 48084443..e13a6a4d 100644--- a/cloudflare-workers/api-edge/src/dashboard.ts+++ b/cloudflare-workers/api-edge/src/dashboard.ts@@ -1313,6 +1313,29 @@ async function handleAutumnBilling(_req: Request, env: DashboardEnv, caller: { o// recharge or just save. Free — the customer is already loaded above.const hasToppedUp = (r.customer.purchases ?? []).some((p) => p.plan_id === "top_up");+ const model = await env.OPENCOMPUTER_DB.prepare(+ `SELECT+ o.model_billing_status AS status,+ o.model_markup_bps AS markup_bps,+ COUNT(CASE WHEN k.status = 'active' THEN 1 END) AS active_key_count,+ COALESCE(SUM(k.committed_micro), 0) AS committed_micro+ FROM orgs o+ LEFT JOIN managed_model_keys k+ ON k.org_id = o.id+ AND k.status IN ('active', 'superseded', 'deleting')+ WHERE o.id = ?1+ GROUP BY o.model_billing_status, o.model_markup_bps`,+ ).bind(caller.orgID).first<{+ status: string;+ markup_bps: number;+ active_key_count: number;+ committed_micro: number;+ }>();+ const modelStatus = model?.status ?? "off";+ const modelMarkupBps = model?.markup_bps ?? 0;+ const modelProviderSpendCents = Math.round((model?.committed_micro ?? 0) / 10_000);+ const modelBilledCreditsCents = Math.round(modelProviderSpendCents * (1 + modelMarkupBps / 10_000));+return json({creditsRemainingCents: Math.round(r.creditsRemaining * 100),maxConcurrentSandboxes: r.maxConcurrent,@@ -1320,6 +1343,14 @@ async function handleAutumnBilling(_req: Request, env: DashboardEnv, caller: { oisHalted: r.halted,hasToppedUp,autoTopup: at ? { enabled: at.enabled, threshold: at.threshold, quantity: at.quantity } : null,+ modelUsage: {+ enabled: modelStatus === "active",+ status: modelStatus,+ markupBps: modelMarkupBps,+ providerSpendCents: modelProviderSpendCents,+ billedCreditsCents: modelBilledCreditsCents,+ activeKeyCount: model?.active_key_count ?? 0,+ },});}diff --git a/web/src/api/schemas.ts b/web/src/api/schemas.tsindex fd730c1e..d4631fcd 100644--- a/web/src/api/schemas.ts+++ b/web/src/api/schemas.ts@@ -200,6 +200,15 @@ export const AutumnAutoTopupSchema = z.object({quantity: z.number(),})+export const AutumnModelUsageSchema = z.object({+ enabled: z.boolean(),+ status: z.string(),+ markupBps: z.number(),+ providerSpendCents: z.number(),+ billedCreditsCents: z.number(),+ activeKeyCount: z.number(),+})+export const AutumnBillingSchema = z.object({creditsRemainingCents: z.number(),maxConcurrentSandboxes: z.number(),@@ -207,6 +216,7 @@ export const AutumnBillingSchema = z.object({isHalted: z.boolean(),hasToppedUp: z.boolean(),autoTopup: AutumnAutoTopupSchema.nullable(),+ modelUsage: AutumnModelUsageSchema.optional(),})export const StripeInvoiceSchema = z.object({diff --git a/web/src/pages/Billing.tsx b/web/src/pages/Billing.tsxindex 31117207..fb40354c 100644--- a/web/src/pages/Billing.tsx+++ b/web/src/pages/Billing.tsx@@ -30,10 +30,10 @@ import { ConfirmDialog } from '@/components/confirm-dialog'import { ResourceTable, type Column } from '@/components/resource-table'import { cn } from '@/lib/utils'-type Tab = 'sandboxes' | 'invoices'+type Tab = 'usage' | 'invoices'export default function Billing() {- const [tab, setTab] = useState<Tab>('sandboxes')+ const [tab, setTab] = useState<Tab>('usage')return (<div>@@ -43,7 +43,7 @@ export default function Billing() {/><div className="mb-6 flex gap-1 border-b">- {(['sandboxes', 'invoices'] as Tab[]).map((t) => (+ {(['usage', 'invoices'] as Tab[]).map((t) => (<buttonkey={t}onClick={() => setTab(t)}@@ -59,7 +59,7 @@ export default function Billing() {))}</div>- {tab === 'sandboxes' ? <PlanTab /> : <InvoicesTab />}+ {tab === 'usage' ? <PlanTab /> : <InvoicesTab />}</div>)}@@ -348,6 +348,8 @@ function PrepaidPlan() {hasToppedUp={autumn?.hasToppedUp ?? false}/>+ <ModelUsageCard usage={autumn?.modelUsage ?? null} />+{/* Concurrency */}<Panel className="p-6"><h2 className="mb-2 text-sm font-semibold">Concurrency</h2>@@ -434,6 +436,56 @@ function PrepaidPlan() {)}+function ModelUsageCard({+ usage,+}: {+ usage: AutumnBilling['modelUsage'] | null+}) {+ const status = usage?.status ?? 'off'+ const enabled = usage?.enabled ?? false+ const markupPct = ((usage?.markupBps ?? 0) / 100).toFixed(1)++ return (+ <Panel className="p-6">+ <div className="mb-3 flex items-center justify-between gap-3">+ <h2 className="text-sm font-semibold">Managed model usage</h2>+ <StatusBadge+ status={enabled ? 'success' : status === 'error' ? 'error' : 'stopped'}+ label={status}+ />+ </div>+ <div className="flex items-baseline gap-2">+ <span className="text-foreground font-mono text-3xl font-semibold">+ {formatCost(usage?.billedCreditsCents ?? 0)}+ </span>+ <span className="text-muted-foreground text-xs">credits used</span>+ </div>+ <p className="text-muted-foreground mt-2 text-sm">+ Managed agent models draw from the same prepaid credits balance as+ sandbox compute.+ </p>+ <div className="mt-4 grid grid-cols-2 gap-3 text-sm">+ <div>+ <div className="text-muted-foreground text-xs">Provider cost</div>+ <div className="text-foreground font-mono">+ {formatCost(usage?.providerSpendCents ?? 0)}+ </div>+ </div>+ <div>+ <div className="text-muted-foreground text-xs">Markup</div>+ <div className="text-foreground font-mono">{markupPct}%</div>+ </div>+ <div>+ <div className="text-muted-foreground text-xs">Active keys</div>+ <div className="text-foreground font-mono">+ {usage?.activeKeyCount ?? 0}+ </div>+ </div>+ </div>+ </Panel>+ )+}+function AutoTopupCard({current,hasToppedUp,