codeburn/app/renderer/lib/budget.ts
iamtoruk 93349e7eba feat(app): daily budget alert (USD or token cap)
- Settings/General: Daily budget row (Off / USD amount / Tokens),
  positive-finite validation, warns at 80% and alerts at 100%
- app-wide one-line banner above every section: --warn at 80%+, --bad
  at 100%+, dismissible for the rest of the day
- USD caps compare raw USD against per-provider daily cost (works under
  any filter); token caps evaluate only on the all-providers view since
  provider-filtered history zeroes token fields (comparing against a
  false zero would always pass)

212/212, build green.
2026-07-16 03:16:07 -07:00

19 lines
930 B
TypeScript

// Renderer-only daily budget setting (localStorage `codeburn.dailyBudget`).
// A 'usd' cap is raw-USD (compared against history.daily cost, then displayed
// via the currency-aware formatUsd); a 'tokens' cap counts input+output tokens.
export type DailyBudget = { kind: 'usd' | 'tokens'; value: number }
/** Parse the persisted budget, returning null when absent or malformed. */
export function readDailyBudget(): DailyBudget | null {
let raw: string | null = null
try { raw = globalThis.localStorage?.getItem('codeburn.dailyBudget') ?? null } catch { return null }
if (!raw) return null
try {
const parsed = JSON.parse(raw) as Partial<DailyBudget>
if ((parsed.kind === 'usd' || parsed.kind === 'tokens') && typeof parsed.value === 'number' && Number.isFinite(parsed.value) && parsed.value > 0) {
return { kind: parsed.kind, value: parsed.value }
}
} catch { /* malformed JSON */ }
return null
}