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

model-catalog.test.ts

233 lines6.1 KB

1import { describe, expect, it } from 'vitest'
2import {
3 buildRecords,
4 inputModalities,
5 isChatModel,
6 pricesFrom,
7 reasoningFrom,
8} from './model-catalog'
9import type { RateCard } from '@modelschemas/client'
10import type { SchemaModel } from './model-catalog'
11​
12const row = (fields: Partial<SchemaModel> = {}): SchemaModel => ({
13 provider: 'p',
14 rawId: 'm',
15 displayName: null,
16 activity: 'chat',
17 deprecatedAt: null,
18 aliases: null,
19 contextWindow: null,
20 maxOutput: null,
21 modalities: { input: ['text'], output: ['text'] },
22 capabilities: null,
23 reasoning: null,
24 requestMap: null,
25 pricing: null,
26 ...fields,
27})
28​
29const card = (price: unknown, tables: RateCard['tables'] = {}): RateCard => ({
30 inputs: {},
31 tables,
32 price,
33 examples: [],
34 source: { url: 'https://example.com', hash: '', extractedAt: '' },
35})
36​
37const rate = (name: string) => ({
38 if: [
39 { '>': [{ var: name }, 0] },
40 {
41 '*': [{ var: name }, { lookup: { table: 'rate', keys: ['base', name] } }],
42 },
43 0,
44 ],
45})
46​
47describe('reasoningFrom', () => {
48 it('is false for a model that does not reason', () => {
49 expect(reasoningFrom(row())).toBe(false)
50 })
51​
52 it("takes the source's level map when it has one", () => {
53 expect(
54 reasoningFrom(
55 row({
56 reasoning: {
57 mode: 'effort',
58 mandatory: true,
59 efforts: ['instant', 'low', 'high'],
60 },
61 requestMap: {
62 thinking: {
63 on: {},
64 off: null,
65 levels: {
66 off: null,
67 minimal: null,
68 low: 'low',
69 medium: null,
70 high: 'high',
71 xhigh: null,
72 max: null,
73 },
74 },
75 },
76 }),
77 ),
78 ).toEqual({ levels: { low: 'low', high: 'high' }, budget: false })
79 })
80​
81 it('maps efforts to levels, none to off, and skips default', () => {
82 expect(
83 reasoningFrom(
84 row({
85 reasoning: {
86 mode: 'effort',
87 mandatory: false,
88 efforts: ['none', 'default', 'HIGH'],
89 },
90 }),
91 ),
92 ).toEqual({ levels: { off: 'none', high: 'HIGH' }, budget: false })
93 })
94​
95 it('throws on an effort it does not know', () => {
96 expect(() =>
97 reasoningFrom(
98 row({
99 reasoning: { mode: 'effort', mandatory: null, efforts: ['ultra'] },
100 }),
101 ),
102 ).toThrow("unknown reasoning effort 'ultra'")
103 })
104​
105 it('invents no levels for a budget model', () => {
106 expect(
107 reasoningFrom(row({ reasoning: { mode: 'budget', mandatory: false } })),
108 ).toEqual({
109 levels: {},
110 budget: true,
111 })
112 })
113})
114​
115describe('pricesFrom', () => {
116 it('has no prices when the row has no price card', () => {
117 expect(pricesFrom(row())).toBeUndefined()
118 })
119​
120 it('reads each rate the card prices, and leaves the others undefined', () => {
121 const prices = pricesFrom(
122 row({
123 pricing: card(
124 {
125 currency: [
126 'CNY',
127 {
128 '+': ['input_tokens', 'output_tokens'].map((name) => ({
129 '*': [
130 { var: name },
131 {
132 lookup: {
133 table: 'rate',
134 keys: [
135 {
136 if: [
137 { '>': [{ var: 'input_tokens' }, 200000] },
138 '200000',
139 'base',
140 ],
141 },
142 name,
143 ],
144 },
145 },
146 ],
147 })),
148 },
149 ],
150 },
151 {
152 rate: {
153 base: { input_tokens: 3e-6, output_tokens: 15e-6 },
154 '200000': { input_tokens: 6e-6, output_tokens: 22.5e-6 },
155 },
156 },
157 ),
158 }),
159 )
160 expect(prices).toEqual({
161 currency: 'CNY',
162 input: 3,
163 output: 15,
164 tiers: [{ inputTokensAbove: 200000, input: 6, output: 22.5 }],
165 })
166 })
167​
168 it('throws on a price operator it does not know', () => {
169 expect(() => pricesFrom(row({ pricing: card({ max: [1, 2] }) }))).toThrow(
170 "Unknown price operator 'max'",
171 )
172 })
173​
174 it('throws on a price summary, which has no rate card', () => {
175 expect(() =>
176 pricesFrom(
177 row({ pricing: { currency: 'USD', per: 'token', inputPerMillion: 3 } }),
178 ),
179 ).toThrow('a price summary, not a rate card')
180 })
181​
182 it('prices the 1-hour cache write with its own rate', () => {
183 expect(
184 pricesFrom(
185 row({
186 pricing: card(
187 {
188 '+': [rate('cache_write_tokens'), rate('cache_write_1h_tokens')],
189 },
190 {
191 rate: {
192 base: {
193 cache_write_tokens: 3.75e-6,
194 cache_write_1h_tokens: 6e-6,
195 },
196 },
197 },
198 ),
199 }),
200 ),
201 ).toEqual({ currency: 'USD', cacheWrite: 3.75, cacheWrite1h: 6 })
202 })
203})
204​
205describe('rows', () => {
206 it('keeps current chat models that write text', () => {
207 expect(isChatModel(row())).toBe(true)
208 expect(isChatModel(row({ activity: 'embeddings' }))).toBe(false)
209 expect(isChatModel(row({ deprecatedAt: 1 }))).toBe(false)
210 expect(
211 isChatModel(row({ modalities: { input: ['text'], output: ['image'] } })),
212 ).toBe(false)
213 })
214​
215 it('maps input kinds, and throws on an unknown one', () => {
216 expect(
217 inputModalities(row({ modalities: { input: ['text', 'pdf', 'file'] } })),
218 ).toEqual(['text', 'document'])
219 expect(inputModalities(row({ modalities: null }))).toBeUndefined()
220 expect(() =>
221 inputModalities(row({ modalities: { input: ['smell'] } })),
222 ).toThrow("unknown input modality 'smell'")
223 })
224​
225 it('gives an alias its own record, and throws on a repeated id', () => {
226 expect(
227 buildRecords([row({ rawId: 'b', aliases: ['a'] })]).map((r) => r.id),
228 ).toEqual(['a', 'b'])
229 expect(() => buildRecords([row(), row()])).toThrow(
230 "Two rows have the id 'm'",
231 )
232 })
233})