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

estimate.ts

133 lines4.1 KB

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