Open History to see the last commit
1/**
2 * Reactor prices (issue #120). `GET /pricing` publishes credits per second
3 * and, when a dollar rate exists, `amount_per_sec_usd`. Credits alone are
4 * not a RateCard. This does not divide credits by `credits_per_dollar`.
5 */
6import { compileUnitCard } from '@modelschemas/rate-card'
7import type { RateCard } from '@modelschemas/rate-card'
8
9import { tagDocsFacts } from './fact-sources.ts'
10import type { ModelInfo } from './types.ts'
11
12export interface ReactorRate {
13 amount_per_sec?: number
14 amount_per_sec_usd?: string | number
15 currency_code?: string
16 unit?: string
17 denomination?: string
18}
19
20type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
21
22function usdPerSecond(rate: ReactorRate): number | null {
23 if (rate.currency_code !== 'USD') return null
24 const raw = rate.amount_per_sec_usd
25 if (typeof raw !== 'string' && typeof raw !== 'number') return null
26 const dollars = typeof raw === 'number' ? raw : Number(raw)
27 if (!Number.isFinite(dollars) || dollars <= 0) return null
28 return dollars
29}
30
31/** USD per second of video, or null when the row has no published USD rate. */
32export function compileReactorRate(
33 rate: ReactorRate | null | undefined,
34 source: RateCard['source'],
35): RateCard | null {
36 if (!rate) return null
37 const dollars = usdPerSecond(rate)
38 if (dollars === null) return null
39 return compileUnitCard(
40 {
41 quantity: { param: 'video_seconds', bound: 'usage' },
42 rates: dollars,
43 },
44 source,
45 )
46}
47
48export function reactorPricingFacts(
49 rate: ReactorRate | null | undefined,
50 source: RateCard['source'],
51): PricedFacts {
52 const pricing = compileReactorRate(rate, source)
53 if (!pricing) return {}
54 return {
55 pricing,
56 factSources: tagDocsFacts({ pricing }, source.url, source.hash),
57 }
58}