Open History to see the last commit
1/**
2 * Per-unit rate sheet → card. The shape behind everything a host does not
3 * bill by token: a rate per second of video, per minute of audio, per
4 * character, per image — optionally looked up by request fields that change
5 * it (size, quality, resolution).
6 *
7 * `price` is `quantity × rate[key…]`, the same shape the hand-written media
8 * fixtures use. A quantity that is a real request field is request-bound and
9 * priced from the request alone; one the request cannot state (how long the
10 * uploaded audio is) is usage-bound and supplied by the caller.
11 */
12import type { Expr, RateCard, Table } from './card.ts'
13
14export interface UnitQuantity {
15 param: string
16 bound?: 'request' | 'usage'
17 /** Omitted means the caller must supply it rather than be guessed for. */
18 default?: number
19}
20
21export interface UnitKey {
22 param: string
23 values: Array<string>
24 bound?: 'request' | 'usage'
25 default?: string
26}
27
28export interface UnitCardSpec {
29 /** Units billed. Omitted prices one unit (a flat per-request rate). */
30 quantity?: UnitQuantity
31 /** Enum dimensions the rate is looked up by, outermost first. */
32 keys?: Array<UnitKey>
33 /** A flat rate, or a table nested in `keys` order. */
34 rates: number | Table
35}
36
37/** Every leaf of a rate table is a usable, non-zero price. */
38function priced(rates: number | Table, depth: number): boolean {
39 if (typeof rates === 'number') {
40 return depth === 0 && Number.isFinite(rates) && rates > 0
41 }
42 const entries = Object.values(rates)
43 return (
44 depth > 0 &&
45 entries.length > 0 &&
46 entries.every((entry) => priced(entry, depth - 1))
47 )
48}
49
50/** Every combination the key values name has a rate. */
51function covered(rates: number | Table, keys: Array<UnitKey>): boolean {
52 const [key, ...rest] = keys
53 if (!key) return typeof rates === 'number'
54 if (typeof rates === 'number') return false
55 return key.values.every((value) => {
56 const next = rates[value]
57 return next !== undefined && covered(next, rest)
58 })
59}
60
61/**
62 * `null` when the sheet prices nothing it can stand behind: a zero or
63 * unusable rate, or a key value the table has no rate for.
64 */
65export function compileUnitCard(
66 spec: UnitCardSpec,
67 source: RateCard['source'],
68): RateCard | null {
69 const keys = spec.keys ?? []
70 if (!priced(spec.rates, keys.length)) return null
71 if (!covered(spec.rates, keys)) return null
72
73 const inputs: RateCard['inputs'] = {}
74 if (spec.quantity) {
75 inputs[spec.quantity.param] = {
76 param: spec.quantity.param,
77 bound: spec.quantity.bound ?? 'request',
78 kind: 'number',
79 ...(spec.quantity.default !== undefined && {
80 default: spec.quantity.default,
81 }),
82 }
83 }
84 for (const key of keys) {
85 if (key.default !== undefined && !key.values.includes(key.default)) {
86 return null
87 }
88 inputs[key.param] = {
89 param: key.param,
90 bound: key.bound ?? 'request',
91 kind: 'enum',
92 values: key.values,
93 ...(key.default !== undefined && { default: key.default }),
94 }
95 }
96
97 const rate: Expr =
98 typeof spec.rates === 'number'
99 ? spec.rates
100 : {
101 lookup: {
102 table: 'rate',
103 keys: keys.map((key) => ({ var: key.param })),
104 },
105 }
106
107 return {
108 inputs,
109 tables: typeof spec.rates === 'number' ? {} : { rate: spec.rates },
110 price: spec.quantity ? { '*': [{ var: spec.quantity.param }, rate] } : rate,
111 examples: [],
112 source,
113 }
114}