Open History to see the last commit
1/**
2 * `@modelschemas/rate-card/evaluator`: the evaluator with no runtime
3 * dependencies, for edge runtimes and clients that price cards they fetched
4 * from `GET /v1/models/{provider}/{id}`. The package root adds the listing
5 * compilers; `./schema` is the zod schema.
6 */
7import { priceDetailed } from './evaluate.ts'
8import type { PriceResult } from './evaluate.ts'
9import type { RateCard } from './card.ts'
10import { normalizeUsage } from './usage.ts'
11
12export { CORE_OPS, RATE_CARD_LANGUAGE } from './card.ts'
13export type {
14 CoreOp,
15 Expr,
16 RateCard,
17 RateCardEstimate,
18 RateCardExample,
19 RateCardInput,
20 RateCardPlainInput,
21 RateCardPrice,
22 Table,
23} from './card.ts'
24export {
25 RateCardError,
26 bindInputs,
27 cardCurrency,
28 cardParams,
29 cardPrice,
30 price,
31 priceDetailed,
32 verifyExamples,
33} from './evaluate.ts'
34export type {
35 ExampleResult,
36 PriceResult,
37 RateCardErrorCode,
38} from './evaluate.ts'
39export { normalizeUsage, usageFormat } from './usage.ts'
40export type { UsageFormat } from './usage.ts'
41
42export type CardEvaluation = Pick<
43 PriceResult,
44 'amount' | 'currency' | 'estimated'
45>
46
47/**
48 * `{ amount, currency, estimated }` for the card and inputs
49 * `POST /v1/estimate` prices. The route also returns `usd` (USD cards),
50 * `cardSource`, `defaulted`, and `ignored`. `usage`
51 * is card levers or the provider's own usage object (`normalizeUsage`).
52 * Throws `RateCardError` rather than return a number it cannot stand
53 * behind. A zero usage lever the card does not read is dropped. A
54 * non-zero one throws `unknown-usage-key`.
55 */
56export function evaluateCard(
57 card: RateCard,
58 {
59 request = {},
60 usage = {},
61 }: {
62 request?: Record<string, unknown>
63 usage?: Record<string, unknown>
64 } = {},
65): CardEvaluation {
66 const { amount, currency, estimated } = priceDetailed(
67 card,
68 request,
69 normalizeUsage(card, usage),
70 )
71 return { amount, currency, estimated }
72}