mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-28 00:55:49 +00:00
feat(desktop): typed IPC channels for period comparison
Two bridge channels follow the existing envelope + argv-validation pattern: codeburn:getPeriodCompare spawns compare-periods --format json with two REQUIRED local YYYY-MM-DD range pairs (validated per range), and codeburn:getPeriodCompareSessions spawns the sessions drill-down for one project/model contribution key. Contribution keys ride the --key VALUE position, so a dash-leading slug (Claude sanitizes /work/pricing to -work-pricing) passes vContributionKey where vToken would wrongly reject it. Preload and the renderer CodeburnBridge types mirror the two calls.
This commit is contained in:
parent
f7a66e51f5
commit
833d97fa1d
4 changed files with 144 additions and 0 deletions
|
|
@ -45,6 +45,8 @@ const CHANNELS = [
|
|||
'codeburn:getSessionsContributions',
|
||||
'codeburn:getCompareModels',
|
||||
'codeburn:getCompare',
|
||||
'codeburn:getPeriodCompare',
|
||||
'codeburn:getPeriodCompareSessions',
|
||||
'codeburn:getYield',
|
||||
'codeburn:getSpendFlow',
|
||||
'codeburn:getBranchSpend',
|
||||
|
|
@ -106,6 +108,12 @@ const ARGV_CASES: Array<{ channel: string; args: unknown[]; argv: string[] }> =
|
|||
{ channel: 'codeburn:getSessionsContributions', args: ['30days', 'claude', { from: '2026-07-01', to: '2026-07-11' }], argv: ['sessions', '--format', 'json', '--contributions', '--period', '30days', '--provider', 'claude', '--from', '2026-07-01', '--to', '2026-07-11'] },
|
||||
{ channel: 'codeburn:getCompareModels', args: ['month', 'codex'], argv: ['compare', '--format', 'json', '--period', 'month', '--provider', 'codex'] },
|
||||
{ channel: 'codeburn:getCompare', args: ['month', 'all', 'model-a', 'model-b'], argv: ['compare', '--format', 'json', '--period', 'month', '--model-a', 'model-a', '--model-b', 'model-b'] },
|
||||
{ channel: 'codeburn:getPeriodCompare', args: [{ from: '2026-07-01', to: '2026-07-07' }, { from: '2026-07-08', to: '2026-07-14' }, 'claude'], argv: ['compare-periods', '--format', 'json', '--from-a', '2026-07-01', '--to-a', '2026-07-07', '--from-b', '2026-07-08', '--to-b', '2026-07-14', '--provider', 'claude'] },
|
||||
{ channel: 'codeburn:getPeriodCompare', args: [{ from: '2026-07-01', to: '2026-07-07' }, { from: '2026-07-08', to: '2026-07-14' }, 'all'], argv: ['compare-periods', '--format', 'json', '--from-a', '2026-07-01', '--to-a', '2026-07-07', '--from-b', '2026-07-08', '--to-b', '2026-07-14'] },
|
||||
{ channel: 'codeburn:getPeriodCompareSessions', args: [{ from: '2026-07-01', to: '2026-07-07' }, { from: '2026-07-08', to: '2026-07-14' }, 'all', 'project', '/work/app'], argv: ['compare-periods', '--format', 'sessions', '--from-a', '2026-07-01', '--to-a', '2026-07-07', '--from-b', '2026-07-08', '--to-b', '2026-07-14', '--dimension', 'project', '--key', '/work/app'] },
|
||||
// Claude sanitizes project paths to dash-leading slugs; the key rides in the
|
||||
// VALUE position of --key, so a dash-leading key must survive validation.
|
||||
{ channel: 'codeburn:getPeriodCompareSessions', args: [{ from: '2026-07-01', to: '2026-07-07' }, { from: '2026-07-08', to: '2026-07-14' }, 'all', 'project', '-work-pricing'], argv: ['compare-periods', '--format', 'sessions', '--from-a', '2026-07-01', '--to-a', '2026-07-07', '--from-b', '2026-07-08', '--to-b', '2026-07-14', '--dimension', 'project', '--key', '-work-pricing'] },
|
||||
{ channel: 'codeburn:getYield', args: ['today', 'all'], argv: ['yield', '--format', 'json', '--period', 'today'] },
|
||||
{ channel: 'codeburn:getYield', args: ['today', 'claude'], argv: ['yield', '--format', 'json', '--period', 'today', '--provider', 'claude'] },
|
||||
{ channel: 'codeburn:getSpendFlow', args: ['month', 'openai'], argv: ['spend', '--format', 'flow-json', '--period', 'month', '--provider', 'openai'] },
|
||||
|
|
|
|||
|
|
@ -286,6 +286,25 @@ function vRange(range: DateRange | undefined): DateRange | undefined {
|
|||
}
|
||||
return range
|
||||
}
|
||||
/**
|
||||
* Drill-down contribution key (canonical project path or model id). Unlike
|
||||
* vToken, a leading '-' is legal here: Claude sanitizes project paths by
|
||||
* replacing separators with '-' (e.g. `/work/pricing` → `-work-pricing`), and
|
||||
* the key is only ever emitted in the VALUE position of `--key`/`--dimension`
|
||||
* pairs, where Commander binds the next token as the value — a dash-leading
|
||||
* value cannot inject a flag through the argv array (no shell involved).
|
||||
* Empty and NUL are still rejected.
|
||||
*/
|
||||
function vContributionKey(value: string): string {
|
||||
if (!value || value.includes('\0')) throw new CliError('bad-args', 'invalid contribution key')
|
||||
return value
|
||||
}
|
||||
/** vRange for channels where the range is REQUIRED (compare periods). */
|
||||
function vRequiredRange(range: DateRange | undefined, name: string): DateRange {
|
||||
const v = vRange(range)
|
||||
if (!v) throw new CliError('bad-args', `missing ${name} date range`)
|
||||
return v
|
||||
}
|
||||
function vCurrency(code: string): string {
|
||||
if (!/^[A-Z]{3}$/.test(code)) throw new CliError('bad-args', 'invalid currency code')
|
||||
return code
|
||||
|
|
@ -561,6 +580,24 @@ export function createBridgeHandlers(deps: Deps = { spawnCli, spawnCliAction, re
|
|||
...projectArgs(),
|
||||
'--model-a', vToken(modelA), '--model-b', vToken(modelB),
|
||||
]),
|
||||
// Compare periods (B minus A). Both ranges are REQUIRED local YYYY-MM-DD
|
||||
// key pairs; the renderer computes the 7v7 default so argv stays explicit.
|
||||
'codeburn:getPeriodCompare': run((rangeA: DateRange, rangeB: DateRange, provider: string, background?: boolean) => [
|
||||
'compare-periods', '--format', 'json',
|
||||
'--from-a', vRequiredRange(rangeA, 'A').from, '--to-a', vRequiredRange(rangeA, 'A').to,
|
||||
'--from-b', vRequiredRange(rangeB, 'B').from, '--to-b', vRequiredRange(rangeB, 'B').to,
|
||||
...providerArgs(vProvider(provider)),
|
||||
], 3),
|
||||
'codeburn:getPeriodCompareSessions': run((rangeA: DateRange, rangeB: DateRange, provider: string, dimension: string, key: string) => {
|
||||
if (dimension !== 'project' && dimension !== 'model') throw new CliError('bad-args', 'invalid drill-down dimension')
|
||||
return [
|
||||
'compare-periods', '--format', 'sessions',
|
||||
'--from-a', vRequiredRange(rangeA, 'A').from, '--to-a', vRequiredRange(rangeA, 'A').to,
|
||||
'--from-b', vRequiredRange(rangeB, 'B').from, '--to-b', vRequiredRange(rangeB, 'B').to,
|
||||
...providerArgs(vProvider(provider)),
|
||||
'--dimension', dimension, '--key', vContributionKey(key),
|
||||
]
|
||||
}),
|
||||
'codeburn:getYield': run((period: string, provider: string, range?: DateRange) => [
|
||||
'yield', '--format', 'json', '--period', vPeriod(period),
|
||||
...providerArgs(vProvider(provider)),
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ const bridge = {
|
|||
getSessionsContributions: (period: string, provider: string, range?: DateRange, background?: boolean) => invoke('codeburn:getSessionsContributions', period, provider, range, background),
|
||||
getCompareModels: (period: string, provider: string, background?: boolean) => invoke('codeburn:getCompareModels', period, provider, background),
|
||||
getCompare: (period: string, provider: string, modelA: string, modelB: string) => invoke('codeburn:getCompare', period, provider, modelA, modelB),
|
||||
getPeriodCompare: (rangeA: DateRange, rangeB: DateRange, provider: string, background?: boolean) => invoke('codeburn:getPeriodCompare', rangeA, rangeB, provider, background),
|
||||
getPeriodCompareSessions: (rangeA: DateRange, rangeB: DateRange, provider: string, dimension: string, key: string) => invoke('codeburn:getPeriodCompareSessions', rangeA, rangeB, provider, dimension, key),
|
||||
getYield: (period: string, provider: string, range?: DateRange, background?: boolean) => invoke('codeburn:getYield', period, provider, range, background),
|
||||
getSpendFlow: (period: string, provider: string, range?: DateRange, background?: boolean) => invoke('codeburn:getSpendFlow', period, provider, range, background),
|
||||
getBranchSpend: (period: string, provider: string, range?: DateRange, background?: boolean) => invoke('codeburn:getBranchSpend', period, provider, range, background),
|
||||
|
|
|
|||
|
|
@ -723,6 +723,99 @@ export type CompareJsonReport = {
|
|||
workingStyle: WorkingStyleRow[]
|
||||
}
|
||||
|
||||
// ---- Compare periods (period-diff; B minus A, A is the reference) ----
|
||||
// Mirrors the JSON emitted by `codeburn compare-periods --format json`
|
||||
// (src/period-diff.ts). Raw (unrounded) numbers; the UI formats.
|
||||
|
||||
export type PeriodRangeInfo = { from: string; to: string; days: number }
|
||||
|
||||
export type PeriodContribution = {
|
||||
key: string
|
||||
costA: number
|
||||
costB: number
|
||||
diff: number
|
||||
/** diff / |costA| x 100; null when costA is 0 (the row is `new`). */
|
||||
pct: number | null
|
||||
status: 'new' | 'gone' | 'up' | 'down' | 'flat'
|
||||
callsA: number
|
||||
callsB: number
|
||||
}
|
||||
|
||||
export type PeriodTotalsRow = {
|
||||
cost: number
|
||||
calls: number
|
||||
sessions: number
|
||||
inputTokens: number
|
||||
outputTokens: number
|
||||
cacheReadTokens: number
|
||||
cacheWriteTokens: number
|
||||
savingsUSD: number
|
||||
estimatedCostUSD: number
|
||||
}
|
||||
|
||||
export type PeriodTotalsDiff = {
|
||||
A: PeriodTotalsRow
|
||||
B: PeriodTotalsRow
|
||||
diff: PeriodTotalsRow
|
||||
pct: Record<keyof PeriodTotalsRow, number | null>
|
||||
}
|
||||
|
||||
export type NormalizedMetric = { a: number | null; b: number | null; diff: number | null; pct: number | null }
|
||||
|
||||
export type AggregateDayRow = { date: string; historyCost: number; detailCost: number; aggregateOnly: number }
|
||||
|
||||
export type PeriodHistoryBasis = {
|
||||
historyCost: { A: number; B: number }
|
||||
detailCost: { A: number; B: number }
|
||||
days: { A: AggregateDayRow[]; B: AggregateDayRow[] }
|
||||
aggregateOnly: { A: number; B: number }
|
||||
basis: string
|
||||
}
|
||||
|
||||
export type PeriodDiffReport = {
|
||||
schema: 1
|
||||
provider: string
|
||||
rangeA: PeriodRangeInfo
|
||||
rangeB: PeriodRangeInfo
|
||||
overlapDays: number
|
||||
durationDeltaDays: number
|
||||
totals: PeriodTotalsDiff
|
||||
projects: PeriodContribution[]
|
||||
models: PeriodContribution[]
|
||||
normalized: {
|
||||
perDay: NormalizedMetric
|
||||
per100Calls: NormalizedMetric
|
||||
denominators: { perDay: string; per100Calls: string }
|
||||
}
|
||||
coverage: {
|
||||
unpricedModelsA: Array<{ model: string; calls: number }>
|
||||
unpricedModelsB: Array<{ model: string; calls: number }>
|
||||
pricingCoverageA: number | null
|
||||
pricingCoverageB: number | null
|
||||
}
|
||||
history?: PeriodHistoryBasis
|
||||
}
|
||||
|
||||
export type PeriodSessionDiff = {
|
||||
dimension: 'project' | 'model'
|
||||
key: string
|
||||
provider: string
|
||||
rangeA: PeriodRangeInfo
|
||||
rangeB: PeriodRangeInfo
|
||||
sessions: Array<{
|
||||
identity: string
|
||||
provider: string
|
||||
sessionId: string
|
||||
project: string
|
||||
title?: string
|
||||
costA: number
|
||||
costB: number
|
||||
diff: number
|
||||
callsA: number
|
||||
callsB: number
|
||||
}>
|
||||
}
|
||||
|
||||
// ————— src/models.ts + src/audit-report.ts (audit --format json) —————
|
||||
|
||||
/** Per-token rates used for pricing (src/models.ts ModelCosts). */
|
||||
|
|
@ -890,6 +983,10 @@ export interface CodeburnBridge {
|
|||
getSessionsContributions(period: Period, provider: string, range?: DateRange, background?: boolean): Promise<SessionDrillRow[]>
|
||||
getCompareModels(period: Period, provider: string, background?: boolean): Promise<ModelStats[]>
|
||||
getCompare(period: Period, provider: string, modelA: string, modelB: string): Promise<CompareJsonReport>
|
||||
/** Compare periods (B minus A). Both ranges are required local YYYY-MM-DD keys. */
|
||||
getPeriodCompare(rangeA: DateRange, rangeB: DateRange, provider: string, background?: boolean): Promise<PeriodDiffReport>
|
||||
/** Sessions behind one project/model contribution, joined across A and B. */
|
||||
getPeriodCompareSessions(rangeA: DateRange, rangeB: DateRange, provider: string, dimension: 'project' | 'model', key: string): Promise<PeriodSessionDiff>
|
||||
getYield(period: Period, provider: string, range?: DateRange, background?: boolean): Promise<YieldJsonReport>
|
||||
getSpendFlow(period: Period, provider: string, range?: DateRange, background?: boolean): Promise<SpendFlow>
|
||||
/** Spend per canonical project × branch (`spend --format branch-json`). */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue