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

estimate.ts

142 lines4.4 KB

1/**
2 * POST /v1/estimate — evaluate a stored rate card against a request/usage
3 * pair. Thin wrapper around `normalizeUsage` and `priceDetailed` from
4 * @modelschemas/rate-card, so `usage` is card levers or the provider's own
5 * usage object;
6 * `estimated` names any input filled by the card's published estimate,
7 * `defaulted` any input that took the card's default, and `ignored` any
8 * request key the card does not read.
9 */
10import {
11 normalizeUsage,
12 priceDetailed,
13 RateCardError,
14} from '@modelschemas/rate-card'
15import type { RateCard } from '@modelschemas/rate-card'
16​
17import type { Db } from '#/db/index.ts'
18import { getModelDetail } from '#/server/catalog.ts'
19import { parseStoredRateCard } from '#/server/rate-card.ts'
20​
21export interface EstimateRequestBody {
22 provider: string
23 model: string
24 request?: Record<string, unknown>
25 usage?: Record<string, unknown>
27​
28export type EstimateOutcome =
29 | {
30 ok: true
31 result: {
32 /** The price, in `currency` (ISO 4217). Never converted. */
33 amount: number
34 currency: string
35 /** `amount` again for a USD card; absent for any other currency. */
36 usd?: number
37 cardSource: RateCard['source']
38 /** Omitted params the card estimated; empty means billed price. */
39 estimated: string[]
40 /** Omitted params that took the card's default value. */
41 defaulted: string[]
42 /** Request keys the card does not read. */
43 ignored: string[]
44 }
45 }
46 | { ok: false; status: number; code: string; message: string }
47​
48const isRecord = (value: unknown): value is Record<string, unknown> =>
49 typeof value === 'object' && value !== null && !Array.isArray(value)
50​
51export function parseEstimateBody(raw: unknown): EstimateRequestBody | null {
52 if (!isRecord(raw)) return null
53 if (typeof raw.provider !== 'string' || typeof raw.model !== 'string') {
54 return null
55 }
56 if (raw.request !== undefined && !isRecord(raw.request)) return null
57 if (raw.usage !== undefined && !isRecord(raw.usage)) return null
58 return {
59 provider: raw.provider,
60 model: raw.model,
61 request: raw.request,
62 usage: raw.usage,
63 }
65​
66function mapRateCardError(
67 error: RateCardError,
68 card: RateCard,
69 body: EstimateRequestBody,
70): { code: string; message: string } {
71 const name = error.message.split(/[\s(]/)[0]
72 const input =
73 (name !== undefined ? card.inputs[name] : undefined) ??
74 Object.values(card.inputs).find((entry) => entry.param === name)
75 const slot = input
76 ? `${input.bound === 'usage' ? 'usage' : 'request'}.${input.param}`
77 : undefined
78 if (
79 error.code === 'bad-input' &&
80 error.message.includes('required') &&
81 slot
82 ) {
83 return {
84 code: 'unbound_input',
85 message:
86 `Rate card requires ${slot} which was not provided. ` +
87 `Pass it on the estimate body. See GET /v1/models/${body.provider}/${body.model}.`,
88 }
89 }
90 if (error.code === 'estimate-unavailable' && slot) {
91 return {
92 code: 'unbound_input',
93 message:
94 `Rate card requires ${slot}; it was not provided and could not be estimated (${error.message}). ` +
95 `Pass it on the estimate body. See GET /v1/models/${body.provider}/${body.model}.`,
96 }
97 }
98 return {
99 code: error.code.replaceAll('-', '_'),
100 message:
101 `${error.message}. Check request/usage against the model's rate card ` +
102 `at GET /v1/models/${body.provider}/${body.model}.`,
103 }
105​
106export async function estimateCost(
107 db: Db,
108 body: EstimateRequestBody,
109): Promise<EstimateOutcome> {
110 const model = await getModelDetail(db, body.provider, body.model)
111 if (!model) {
112 return {
113 ok: false,
114 status: 404,
115 code: 'unknown_model',
116 message: `Unknown model '${body.model}' for provider '${body.provider}'. Try GET /v1/providers/${body.provider}/models for valid ids.`,
117 }
118 }
119 const card = parseStoredRateCard(model.pricing)
120 if (!card) {
121 return {
122 ok: false,
123 status: 404,
124 code: 'unknown_pricing',
125 message: `No rate card for '${body.provider}/${body.model}'. See GET /v1/models/${body.provider}/${body.model}.`,
126 }
127 }
128 try {
129 const result = priceDetailed(
130 card,
131 body.request ?? {},
132 normalizeUsage(card, body.usage ?? {}),
133 )
134 return { ok: true, result: { ...result, cardSource: card.source } }
135 } catch (error) {
136 if (error instanceof RateCardError) {
137 const mapped = mapRateCardError(error, card, body)
138 return { ok: false, status: 422, ...mapped }
139 }
140 throw error
141 }