Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

evaluator.ts

71 lines1.8 KB

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 zod
5 * schema and the listing compilers.
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'
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.
54 */
55export function evaluateCard(
56 card: RateCard,
57 {
58 request = {},
59 usage = {},
60 }: {
61 request?: Record<string, unknown>
62 usage?: Record<string, unknown>
63 } = {},
64): CardEvaluation {
65 const { amount, currency, estimated } = priceDetailed(
66 card,
67 request,
68 normalizeUsage(card, usage),
69 )
70 return { amount, currency, estimated }