mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-08-13 10:34:32 +00:00
87 lines
4.1 KiB
TypeScript
87 lines
4.1 KiB
TypeScript
import chalk from 'chalk'
|
|
import type { ProjectSummary } from './types.js'
|
|
|
|
// Re-exported from currency.ts so existing imports from './format.js' keep working.
|
|
// The currency-aware version applies exchange rate and symbol automatically.
|
|
// Imported locally too since renderStatusBar below uses it directly.
|
|
import { formatCost } from './currency.js'
|
|
export { formatCost }
|
|
|
|
/// Prefix a formatted cost with the estimated marker (`~`) when the figure is
|
|
/// priced from estimated tokens rather than metered. Keeps the marker identical
|
|
/// across the report, overview, and MCP surfaces so a legend line can explain it
|
|
/// once. `isEstimated` is typically `entry.estimatedCostUSD > 0`.
|
|
export function markEstimated(costStr: string, isEstimated: boolean): string {
|
|
return isEstimated ? `~${costStr}` : costStr
|
|
}
|
|
|
|
export function formatTokens(n: number): string {
|
|
// Guard against Infinity / NaN / negatives that would otherwise leak into
|
|
// the UI as "Infinity" or "NaN" strings when an upstream calculation glitches.
|
|
if (!Number.isFinite(n)) return '?'
|
|
if (n < 0) return '0'
|
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`
|
|
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`
|
|
return Math.round(n).toString()
|
|
}
|
|
|
|
/// Returns YYYY-MM-DD for the given date in the process-local timezone. Cheaper than shelling
|
|
/// out to Intl.DateTimeFormat for every turn in a loop and avoids the UTC drift that bites
|
|
/// `Date.toISOString().slice(0,10)` whenever the user runs this between local midnight and
|
|
/// UTC midnight.
|
|
function localDateString(d: Date): string {
|
|
const y = d.getFullYear()
|
|
const m = String(d.getMonth() + 1).padStart(2, '0')
|
|
const day = String(d.getDate()).padStart(2, '0')
|
|
return `${y}-${m}-${day}`
|
|
}
|
|
|
|
/// Precomputed today/month totals from the durable daily cache. When supplied,
|
|
/// the status bar renders these instead of bucketing the live parse, so the
|
|
/// figures match the menubar exactly (carried, expired-source days included).
|
|
export type StatusBarTotals = {
|
|
today: { cost: number; calls: number }
|
|
month: { cost: number; calls: number }
|
|
}
|
|
|
|
export function renderStatusBar(projects: ProjectSummary[], totals?: StatusBarTotals): string {
|
|
const now = new Date()
|
|
const today = localDateString(now)
|
|
const monthStart = `${today.slice(0, 7)}-01`
|
|
|
|
let todayCost = 0, todayCalls = 0, monthCost = 0, monthCalls = 0
|
|
if (totals) {
|
|
todayCost = totals.today.cost; todayCalls = totals.today.calls
|
|
monthCost = totals.month.cost; monthCalls = totals.month.calls
|
|
const lines: string[] = ['']
|
|
lines.push(` ${chalk.bold('Today')} ${chalk.yellowBright(formatCost(todayCost))} ${chalk.dim(`${todayCalls} calls`)} ${chalk.bold('Month')} ${chalk.yellowBright(formatCost(monthCost))} ${chalk.dim(`${monthCalls} calls`)}`)
|
|
lines.push('')
|
|
return lines.join('\n')
|
|
}
|
|
|
|
for (const project of projects) {
|
|
for (const session of project.sessions) {
|
|
for (const turn of session.turns) {
|
|
if (turn.assistantCalls.length === 0) continue
|
|
// Bucket by the first assistant call's local date -- the moment the cost was
|
|
// incurred. Bucketing by `turn.timestamp` (the user message time) drops turns
|
|
// that straddle midnight (user asked at 23:58, response arrived at 00:30) and
|
|
// disagrees with parseAllSessions' dateRange filter which is also on assistant
|
|
// time.
|
|
const bucketTs = turn.assistantCalls[0]!.timestamp
|
|
if (!bucketTs) continue
|
|
const day = localDateString(new Date(bucketTs))
|
|
const turnCost = turn.assistantCalls.reduce((s, c) => s + c.costUSD, 0)
|
|
const turnCalls = turn.assistantCalls.length
|
|
if (day === today) { todayCost += turnCost; todayCalls += turnCalls }
|
|
if (day >= monthStart) { monthCost += turnCost; monthCalls += turnCalls }
|
|
}
|
|
}
|
|
}
|
|
|
|
const lines: string[] = ['']
|
|
lines.push(` ${chalk.bold('Today')} ${chalk.yellowBright(formatCost(todayCost))} ${chalk.dim(`${todayCalls} calls`)} ${chalk.bold('Month')} ${chalk.yellowBright(formatCost(monthCost))} ${chalk.dim(`${monthCalls} calls`)}`)
|
|
lines.push('')
|
|
|
|
return lines.join('\n')
|
|
}
|