Open History to see the last commit
1/**
2 * Served shapes that the server also builds as TypeScript values. Each is a
3 * zod schema with a `.meta` id: the TS type is inferred from it and
4 * `openapi.ts` writes the component from it, so the spec and the types
5 * cannot drift. Every field carries a `description` because agents read the
6 * served schema. No imports beyond zod and the rate-card schema, so the spec
7 * emitter loads it without the server.
8 */
9import { rateCardSchema } from '@modelschemas/rate-card/schema'
10import { z } from 'zod'
11
12const currencyCode = z.string().regex(/^[A-Z]{3}$/)
13
14/**
15 * What a list row says about a stored card. `null` on the row means no
16 * card; this means there is one. Token cards carry their per-million rates
17 * when those are linear (`tiered` marks a long-prompt re-quote above some
18 * threshold — the rates shown are the base); everything else names the
19 * unit it bills by and points at the full card. `currency` is the
20 * ISO-4217 code the rates are in; rows in different currencies do not
21 * compare.
22 */
23export const compactPricingSchema = z
24 .object({
25 currency: currencyCode.meta({
26 description:
27 'ISO-4217 code the rates are in. Rows in different currencies do not compare; nothing is converted.',
28 }),
29 per: z
30 .enum(['token', 'second', 'character', 'image', 'request', 'unit'])
31 .meta({
32 description:
33 'The unit the card bills by. token: inputPerMillion and outputPerMillion are set when the rates are linear. Every other value: fetch the full card.',
34 }),
35 inputPerMillion: z.number().optional().meta({
36 description:
37 'Price per million input tokens at the base tier, in `currency`. per=token only.',
38 }),
39 outputPerMillion: z.number().optional().meta({
40 description:
41 'Price per million output tokens at the base tier, in `currency`. per=token only.',
42 }),
43 tiered: z.literal(true).optional().meta({
44 description:
45 'Present when the card re-quotes the rates above some prompt-length threshold. The rates shown are the base tier; fetch the card for the rest.',
46 }),
47 })
48 .meta({
49 id: 'CompactPricing',
50 description:
51 'List-row summary of a stored rate card. per=token cards carry per-million rates at the base tier (tiered marks a long-prompt re-quote above some threshold; fetch the card for it). Other values name the unit the card bills by; the full card is on the detail route or ?pricing=1.',
52 })
53export type CompactPricing = z.infer<typeof compactPricingSchema>
54
55const priceBoundsSchema = z
56 .object({
57 min: z.number(),
58 max: z.number(),
59 })
60 .meta({
61 id: 'PriceBounds',
62 description:
63 'A bound over route prices, per million tokens: never the billed price.',
64 })
65
66/**
67 * Null unless every route has a linear token card in one currency: a
68 * route with no card, a tier, or a different currency leaves the bounds
69 * unknown.
70 */
71export const pricingRangeSchema = z
72 .object({
73 currency: currencyCode.meta({
74 description:
75 'ISO-4217 code every route is priced in. A range is only built when the routes share one.',
76 }),
77 routes: z.array(
78 z.object({
79 model: z.string().meta({
80 description:
81 "The route's catalog rawId, or its gateway provider name when card is inline.",
82 }),
83 provider: z.string().optional().meta({
84 description:
85 'Gateway route name (Vercel `provider_name`) when card is stored on this route instead of a catalog row.',
86 }),
87 card: rateCardSchema.optional().meta({
88 description:
89 'Compiled card for a route with no catalog row. Estimate prices this card. Absent when model names a row. Its `examples` is empty.',
90 }),
91 pricing: compactPricingSchema,
92 }),
93 ),
94 inputPerMillion: priceBoundsSchema.meta({
95 description: "Bounds over the routes' input price per million tokens.",
96 }),
97 outputPerMillion: priceBoundsSchema.optional().meta({
98 description:
99 "Bounds over the routes' output price per million tokens, when every route states one.",
100 }),
101 })
102 .meta({
103 id: 'PricingRange',
104 description:
105 'A routed model id with no single billed price: the gateway picks one route per call, and each route bills its own published card. min/max are bounds over those cards, never the billed price. A route is either its own model row under the same provider (model is that rawId) or, when the gateway has no per-route row, a provider name with the compiled card inline. POST /v1/estimate on this row prices the call on every route and returns min and max.',
106 })
107type PricingRangeShape = z.infer<typeof pricingRangeSchema>
108
109/**
110 * A route's inline card is built with no examples. Typing that as `[]` keeps
111 * the catalog response serializable through TanStack server functions, which
112 * refuse the `unknown` in an example's `params`.
113 */
114export type PricingRange = Omit<PricingRangeShape, 'routes'> & {
115 routes: Array<
116 Omit<PricingRangeShape['routes'][number], 'card'> & {
117 card?: Omit<
118 NonNullable<PricingRangeShape['routes'][number]['card']>,
119 'examples'
120 > & {
121 examples: []
122 }
123 }
124 >
125}
126
127/**
128 * `models.reasoning`. The cross-field rules (a toggle is never stored with
129 * `mandatory` null, takes no efforts, and so on) live in
130 * `reasoningViolation` in `providers/types.ts`, which gates every poll
131 * write; the full rules are on `ModelReasoning` there.
132 */
133export const modelReasoningSchema = z
134 .object({
135 mode: z.enum(['adaptive', 'budget', 'effort', 'toggle']).meta({
136 description:
137 'The one control the request exposes. adaptive: the model decides (Anthropic thinking.type adaptive, or a switch with an auto value). budget: a token budget (thinking.budget_tokens, Gemini thinkingBudget). effort: a level (reasoning_effort, Gemini thinkingLevel). toggle: an on/off switch and nothing else (thinking.type enabled|disabled, enable_thinking); it says nothing about the default when the field is omitted.',
138 }),
139 mandatory: z.boolean().nullable().meta({
140 description:
141 "true: the provider states thinking cannot be turned off for this model (on a toggle: the off value is rejected, so send on or omit the field). false: the provider states it can be, in prose or by an off value in this model's own schema or level list. null: unstated; do not read it as false, test mandatory === false before offering an off option. A toggle is never stored with null.",
142 }),
143 efforts: z.array(z.string()).optional().meta({
144 description:
145 'The accepted effort values as published, an off value such as none included. Never present on a toggle.',
146 }),
147 })
148 .meta({ id: 'ModelReasoning' })
149export type ModelReasoning = z.infer<typeof modelReasoningSchema>
150
151/** Shared effort levels to the value this model accepts. null omits it. */
152export const effortLevelMapSchema = z.object({
153 off: z.string().nullable(),
154 minimal: z.string().nullable(),
155 low: z.string().nullable(),
156 medium: z.string().nullable(),
157 high: z.string().nullable(),
158 xhigh: z.string().nullable(),
159 max: z.string().nullable(),
160})
161export type EffortLevelMap = z.infer<typeof effortLevelMapSchema>
162
163/**
164 * How a caller turns a shared chat intent into one provider's request body
165 * (issue #95). `reasoning` stays the readable fact. This object is the wire
166 * map. Unverified providers and non-chat rows are null — no guessed body.
167 * Unverified flags inside a verified map are null, not a default.
168 */
169export const chatRequestMapSchema = z.object({
170 thinking: z
171 .object({
172 on: z.record(z.string(), z.unknown()).meta({
173 description: 'Request body for thinking on at effort high.',
174 }),
175 off: z.record(z.string(), z.unknown()).nullable().meta({
176 description:
177 'Request body that turns thinking off, or null when silence is off (the model does not think unless asked).',
178 }),
179 levels: effortLevelMapSchema.nullable().meta({
180 description:
181 'Shared effort levels to the value this model accepts. null omits the field. The object itself is null when the subset is unverified.',
182 }),
183 })
184 .nullable(),
185 maxTokensField: z
186 .enum(['max_completion_tokens', 'max_tokens'])
187 .nullable()
188 .meta({
189 description: 'Name of the request field that caps output tokens.',
190 }),
191 developerRole: z.boolean().nullable().meta({
192 description:
193 'true when the model wants `role: "developer"` instead of `system`; false when it rejects it.',
194 }),
195 replayReasoningContent: z.boolean().nullable().meta({
196 description:
197 "true when the provider needs the previous turn's `reasoning_content` replayed on the assistant message.",
198 }),
199 store: z.boolean().nullable(),
200 strictTools: z.boolean().nullable(),
201 sessionAffinity: z.boolean().nullable(),
202 cacheControl: z.literal('anthropic').nullable(),
203 toolStream: z.boolean().nullable().meta({
204 description:
205 'true when the model takes `tool_stream: true` alongside `stream: true` (Z.AI GLM). null: unverified.',
206 }),
207 reasoningEffort: z.boolean().nullable().meta({
208 description:
209 'false when the provider rejects a top-level `reasoning_effort` field.',
210 }),
211})
212export type ChatRequestMap = z.infer<typeof chatRequestMapSchema>
213export type ThinkingRequest = NonNullable<ChatRequestMap['thinking']>
214
215/**
216 * The served component. A stored map from before a flag existed lacks that
217 * key, so the spec leaves every key optional; the server always builds the
218 * full `ChatRequestMap`.
219 */
220export const servedChatRequestMapSchema = chatRequestMapSchema.partial().meta({
221 id: 'ChatRequestMap',
222 description:
223 'Verified chat request wire map. A null field is unverified, not a guess. thinking.levels null means the level subset is unverified; a null level value means omit that field.',
224})