import React, { useEffect, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { api } from '../api';
import { Card, Button, Badge, Notice, Bar, Field, Switch, ColumnChart } from './ui';
import { IconChart } from './icons';
/**
* AI usage and budget.
*
* Every provider call writes a ledger row (tokens, cost, latency, outcome);
* this shows the sums and lets the owner set the ceilings the client checks
* before every call. Costs are list prices per model, so they are estimates
* of the vendor's invoice, not the invoice — the panel says so.
*/
const usd = (micro) => {
const v = (micro || 0) / 1e6;
return v > 0 && v < 0.01 ? `$${v.toFixed(4)}` : `$${v.toFixed(2)}`;
};
export default function AiBudget() {
const [board, setBoard] = useState(null);
const [draft, setDraft] = useState(null);
const [busy, setBusy] = useState(false);
const [saved, setSaved] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
api.get('/ai/usage').then(setBoard).catch((e) => setError(e.message));
}, []);
if (!board) {
return (
}>
{error ? {error} : {__('Loading…', 'ai-rank-suite')}}
);
}
const stored = board.budget;
const b = draft || stored;
const dirty = draft !== null && JSON.stringify(draft) !== JSON.stringify(stored);
const set = (k, v) => setDraft({ ...b, [k]: v });
const save = async () => {
setBusy(true);
setError(null);
try {
setBoard(await api.put('/ai/usage', { budget: b }));
setDraft(null);
setSaved(true);
setTimeout(() => setSaved(false), 1800);
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
};
const todayPct = board.pct.today;
const monthPct = board.pct.month;
const warn = b.warn_pct || 80;
const tone = (p) => (p >= 100 ? 'serious' : p >= warn ? 'warning' : 'good');
return (
}
action={saved ? ✓ {__('Saved', 'ai-rank-suite')} : null}
>
{error && {error}}
{__('Today', 'ai-rank-suite')}
{usd(board.spend.today)} {b.daily_usd > 0 ? `/ $${Number(b.daily_usd).toFixed(2)}` : ''} · {board.spend.today_calls} {__('calls', 'ai-rank-suite')}
{__('This month', 'ai-rank-suite')}
{usd(board.spend.month)} {b.monthly_usd > 0 ? `/ $${Number(b.monthly_usd).toFixed(2)}` : ''} · {board.spend.month_calls} {__('calls', 'ai-rank-suite')}
{(todayPct >= 100 || monthPct >= 100) && b.hard_stop && (
{__('A budget is reached and the hard stop is on: AI calls are refused until the window rolls over or the limit is raised.', 'ai-rank-suite')}
)}
{board.by_day.length > 1 && (
{__('Spend, last 30 days', 'ai-rank-suite')}
({ label: d.day.slice(5), cost: Math.round(d.cost / 1e4) / 100 }))}
series={[{ key: 'cost', label: __('USD', 'ai-rank-suite'), color: 'var(--ars-brand)' }]}
/>
)}
{board.by_job.length > 0 && (
{__('By job, this month', 'ai-rank-suite')}
{board.by_job.map((j) => (
{j.job}
{usd(j.cost)} · {j.calls} {__('calls', 'ai-rank-suite')} · {(j.tokens / 1000).toFixed(1)}k {__('tokens', 'ai-rank-suite')} · {j.latency} ms
{j.failed ? ` · ${j.failed} ${__('failed', 'ai-rank-suite')}` : ''}
))}
)}
set('daily_usd', Number(e.target.value))} />
set('monthly_usd', Number(e.target.value))} />
set('per_job_usd', Number(e.target.value))} />
);
}