Open History to see the last commit
1import nativeDocs from '../fixtures/native-reasoning-docs.json'
2import { describe, expect, it } from 'vitest'
3
4import { price } from '@modelschemas/rate-card'
5
6import { classifyAndBundle } from '#/server/ingest/sync.ts'
7
8import { provider, togetherRateCard } from './together.ts'
9
10const SPEC_YAML = `openapi: 3.1.0
11info:
12 title: Together APIs
13paths:
14 /chat/completions:
15 post:
16 summary: Create chat completion
17 requestBody:
18 content:
19 application/json:
20 schema:
21 type: object
22 required: [model, messages]
23 properties:
24 model: { type: string }
25 messages: { type: array }
26 responses:
27 "200":
28 content:
29 application/json:
30 schema:
31 type: object
32 properties:
33 id: { type: string }
34 /embeddings:
35 post:
36 summary: Create embedding
37 requestBody:
38 content:
39 application/json:
40 schema:
41 type: object
42 required: [model, input]
43 properties:
44 model: { type: string }
45 input: { type: string }
46 responses:
47 "200":
48 content:
49 application/json:
50 schema:
51 type: object
52 /images/generations:
53 post:
54 summary: Create image
55 requestBody:
56 content:
57 application/json:
58 schema:
59 type: object
60 required: [model, prompt]
61 properties:
62 model: { type: string }
63 prompt: { type: string }
64 responses:
65 "200":
66 content:
67 application/json:
68 schema:
69 type: object
70 /videos:
71 post:
72 summary: Create video
73 requestBody:
74 content:
75 application/json:
76 schema:
77 type: object
78 required: [model]
79 properties:
80 model: { type: string }
81 responses:
82 "200":
83 content:
84 application/json:
85 schema:
86 type: object
87 /audio/speech:
88 post:
89 summary: Create audio
90 requestBody:
91 content:
92 application/json:
93 schema:
94 type: object
95 required: [model, input]
96 properties:
97 model: { type: string }
98 input: { type: string }
99 responses:
100 "200":
101 content:
102 application/json:
103 schema:
104 type: object
105 /files:
106 post:
107 summary: Upload file
108 requestBody:
109 content:
110 application/json:
111 schema:
112 type: object
113 responses:
114 "200":
115 content:
116 application/json:
117 schema:
118 type: object
119`
120
121describe('together classify', () => {
122 it('maps generation paths to activities', () => {
123 expect(provider.classify('/chat/completions', {})).toBe('chat')
124 expect(provider.classify('/completions', {})).toBe('chat')
125 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
126 expect(provider.classify('/embeddings', {})).toBe('embeddings')
127 expect(provider.classify('/images/generations', {})).toBe('image')
128 expect(provider.classify('/videos', {})).toBe('video')
129 expect(provider.classify('/audio/speech', {})).toBe('audio')
130 expect(provider.classify('/audio/transcriptions', {})).toBe('audio')
131 expect(provider.classify('/audio/translations', {})).toBe('audio')
132 })
133
134 it('drops files, fine-tune, rerank, batches, and endpoint admin', () => {
135 expect(provider.classify('/files', {})).toBeNull()
136 expect(provider.classify('/fine-tunes', {})).toBeNull()
137 expect(provider.classify('/rerank', {})).toBeNull()
138 expect(provider.classify('/batches', {})).toBeNull()
139 expect(provider.classify('/endpoints', {})).toBeNull()
140 expect(provider.classify('/projects/{projectId}/endpoints', {})).toBeNull()
141 expect(provider.classify('/models', {})).toBeNull()
142 expect(provider.classify('/videos/{id}', {})).toBeNull()
143 })
144})
145
146describe('together listModels', () => {
147 it('skips when TOGETHER_API_KEY is absent', async () => {
148 const result = await provider.listModels({})
149 expect(result.models).toEqual([])
150 expect(result.skipped).toBe('together: TOGETHER_API_KEY not set — skipped')
151 })
152
153 it('parses Together’s bare-array catalog', async () => {
154 const restore = installTogetherFetch()
155 try {
156 const result = await provider.listModels({ TOGETHER_API_KEY: 'test-key' })
157 expect(result.skipped).toBeUndefined()
158 expect(result.docsFailures).toEqual({ failed: 0, skipped: 0, first: [] })
159 const byId = Object.fromEntries(
160 result.models.map((model) => [model.rawId, model]),
161 )
162 const qwen = byId['Qwen/Qwen3.5-9B']
163 expect(qwen).toMatchObject({
164 displayName: 'Qwen 3.5 9B',
165 activity: 'chat',
166 contextWindow: 32768,
167 maxOutput: 8192,
168 releasedAt: 1692896905,
169 modalities: { input: ['text'], output: ['text'] },
170 capabilities: ['tools', 'reasoning'],
171 reasoning: { mode: 'toggle', mandatory: false },
172 })
173 expect(qwen?.factSources?.contextWindow).toEqual({
174 derivation: 'listing',
175 sourceUrl: 'https://api.together.xyz/v1/models',
176 path: 'context_length',
177 })
178 expect(qwen?.factSources?.maxOutput?.path).toBe(
179 'config.max_output_length',
180 )
181 expect(qwen?.factSources?.pricing?.sourceUrl).toBe(
182 'https://api.together.xyz/v1/models',
183 )
184 expect(qwen?.factSources?.modalities?.sourceUrl).toBe(
185 'https://api.together.ai/v2/supported-models',
186 )
187 expect(qwen?.factSources?.reasoning?.sourceUrl).toBe(
188 'https://docs.together.ai/docs/inference/chat/reasoning.md',
189 )
190 const kimi = byId['moonshotai/Kimi-K3']
191 expect(kimi?.reasoning).toEqual({
192 mode: 'effort',
193 mandatory: false,
194 efforts: ['low', 'medium', 'high', 'max'],
195 })
196 expect(kimi?.factSources?.reasoning?.sourceUrl).toBe(
197 'https://docs.together.ai/docs/kimi-k3-quickstart.md',
198 )
199 expect(kimi?.contextWindow).toBe(1048576)
200 if (!kimi?.pricing || typeof kimi.pricing !== 'object') {
201 throw new Error('kimi card missing')
202 }
203 expect(
204 price(
205 kimi.pricing as never,
206 {},
207 { input_tokens: 1e6, output_tokens: 0 },
208 ),
209 ).toBeCloseTo(2.7, 9)
210 // Audio is priced from the catalog, never the listing's token object.
211 expect(byId['hexgrad/Kokoro-82M']?.pricing).toBeTruthy()
212 expect(byId['hexgrad/Kokoro-82M']?.absent).toBeUndefined()
213 const rime = byId['rime-labs/rime-arcana-v2']
214 expect(rime?.pricing).toBeUndefined()
215 expect(rime?.absent).toEqual({ pricing: 'cleared' })
216 expect(qwen?.absent).toBeUndefined()
217 const gpt = byId['openai/gpt-oss-120b']
218 expect(gpt?.reasoning).toEqual({
219 mode: 'effort',
220 mandatory: true,
221 efforts: ['low', 'medium', 'high'],
222 })
223 expect(gpt?.factSources?.reasoning?.sourceUrl).toBe(
224 'https://docs.together.ai/docs/gpt-oss.md',
225 )
226 expect(byId['zai-org/GLM-5.3']?.reasoning).toEqual({
227 mode: 'effort',
228 mandatory: true,
229 efforts: ['low', 'medium', 'high', 'max'],
230 })
231 expect(byId['zai-org/GLM-5.3']?.contextWindow).toBe(1048575)
232 expect(byId['zai-org/GLM-5.3-Flash']?.reasoning).toBeUndefined()
233 expect(byId['org/vision-only']?.contextWindow).toBeNull()
234 expect(byId['org/vision-only']?.modalities).toEqual({
235 input: ['text', 'image'],
236 output: ['text'],
237 })
238 expect(byId['org/vision-only']?.factSources?.modalities?.path).toBe(
239 'Vision models',
240 )
241 const flagged = byId['org/reason-flag']
242 expect(flagged?.reasoning).toBeUndefined()
243 expect(flagged?.capabilities).toEqual(['reasoning'])
244 expect(flagged?.factSources?.reasoning?.path).toBe('silent')
245 expect(byId['BAAI/bge-large-en-v1.5']?.activity).toBe('embeddings')
246 expect(byId['org/reranker']?.activity).toBeNull()
247 } finally {
248 restore()
249 }
250 })
251
252 it('reads a JSON docs-cache hit back into reasoning and chat facts', async () => {
253 const docsFetches = { count: 0 }
254 const restore = installTogetherFetch(docsFetches)
255 const kv = jsonKv()
256 try {
257 await provider.listModels({ TOGETHER_API_KEY: 'test-key' }, kv)
258 const warmed = docsFetches.count
259 expect(warmed).toBeGreaterThan(0)
260 const result = await provider.listModels(
261 { TOGETHER_API_KEY: 'test-key' },
262 kv,
263 )
264 expect(docsFetches.count).toBe(warmed)
265 expect(result.docsFailures).toEqual({ failed: 0, skipped: 0, first: [] })
266 const byId = Object.fromEntries(
267 result.models.map((model) => [model.rawId, model]),
268 )
269 expect(byId['Qwen/Qwen3.5-9B']?.reasoning).toEqual({
270 mode: 'toggle',
271 mandatory: false,
272 })
273 expect(byId['moonshotai/Kimi-K3']?.reasoning).toEqual({
274 mode: 'effort',
275 mandatory: false,
276 efforts: ['low', 'medium', 'high', 'max'],
277 })
278 expect(byId['openai/gpt-oss-120b']?.reasoning).toEqual({
279 mode: 'effort',
280 mandatory: true,
281 efforts: ['low', 'medium', 'high'],
282 })
283 expect(byId['zai-org/GLM-5.3']?.contextWindow).toBe(1048575)
284 expect(byId['org/vision-only']?.modalities).toEqual({
285 input: ['text', 'image'],
286 output: ['text'],
287 })
288 } finally {
289 restore()
290 }
291 })
292})
293
294/** KV that stores the same JSON `cachedDocs` writes. A Map comes back as `{}`. */
295function jsonKv(): KVNamespace {
296 const store = new Map<string, string>()
297 return {
298 get: (key: string) => Promise.resolve(store.get(key) ?? null),
299 put: (key: string, value: string) => {
300 store.set(key, value)
301 return Promise.resolve()
302 },
303 } as KVNamespace
304}
305
306function installTogetherFetch(docsFetches?: { count: number }): () => void {
307 const original = globalThis.fetch
308 globalThis.fetch = ((url: string, init?: RequestInit) => {
309 const target = String(url)
310 const headers = new Headers(init?.headers)
311 if (
312 target.startsWith('https://api.together.xyz/') ||
313 target.startsWith('https://api.together.ai/')
314 ) {
315 expect(headers.get('Authorization')).toBe('Bearer test-key')
316 }
317 if (target.includes('/v2/supported-models')) {
318 return Promise.resolve(
319 new Response(
320 JSON.stringify({
321 data: [
322 {
323 name: 'Qwen/Qwen3.5-9B',
324 inputModalities: ['MODALITY_TEXT'],
325 outputModalities: ['MODALITY_TEXT'],
326 features: ['FEATURE_TOOL_CALLING', 'FEATURE_REASONING'],
327 },
328 {
329 name: 'org/reason-flag',
330 inputModalities: ['MODALITY_TEXT'],
331 outputModalities: ['MODALITY_TEXT'],
332 features: ['FEATURE_REASONING'],
333 },
334 ],
335 }),
336 { status: 200, headers: { 'content-type': 'application/json' } },
337 ),
338 )
339 }
340 if (target.includes('.md') && docsFetches) docsFetches.count += 1
341 if (target.includes('/deepseek-v4-quickstart.md'))
342 return textResponse(
343 nativeDocs['https://docs.together.ai/docs/deepseek-v4-quickstart.md'],
344 )
345 if (target.includes('/serverless/models.md')) {
346 return textResponse(LIST_DOCS)
347 }
348 if (target.includes('/inference/chat/reasoning.md')) {
349 return textResponse(
350 LIST_REASONING +
351 '\n' +
352 nativeDocs[
353 'https://docs.together.ai/docs/inference/chat/reasoning.md'
354 ],
355 )
356 }
357 if (target.includes('/kimi-k3-quickstart.md'))
358 return textResponse(LIST_KIMI)
359 if (target.includes('/glm-5.3-quickstart.md'))
360 return textResponse(
361 nativeDocs['https://docs.together.ai/docs/glm-5.3-quickstart.md'],
362 )
363 if (target.endsWith('/gpt-oss.md')) return textResponse(LIST_GPT)
364 expect(target).toBe('https://api.together.xyz/v1/models')
365 return Promise.resolve(
366 new Response(
367 JSON.stringify([
368 {
369 id: 'Qwen/Qwen3.5-9B',
370 display_name: 'Qwen 3.5 9B',
371 created: 1692896905,
372 type: 'chat',
373 context_length: 32768,
374 config: { max_output_length: 8192 },
375 pricing: { input: 0.17, output: 0.25, cached_input: 0 },
376 },
377 {
378 id: 'moonshotai/Kimi-K3',
379 type: 'chat',
380 context_length: 1048576,
381 pricing: { input: 2.7, output: 13.5, cached_input: 0.27 },
382 },
383 { id: 'openai/gpt-oss-120b', type: 'chat', context_length: 131072 },
384 { id: 'zai-org/GLM-5.3', type: 'chat' },
385 {
386 id: 'zai-org/GLM-5.3-Flash',
387 type: 'chat',
388 context_length: 1048575,
389 },
390 { id: 'org/vision-only', type: 'chat', context_length: 0 },
391 { id: 'org/reason-flag', type: 'chat', context_length: 4096 },
392 { id: 'BAAI/bge-large-en-v1.5', type: 'embedding' },
393 { id: 'org/reranker', type: 'rerank' },
394 { id: 'hexgrad/Kokoro-82M', type: 'audio', pricing: { input: 4 } },
395 {
396 id: 'rime-labs/rime-arcana-v2',
397 type: 'audio',
398 pricing: { input: 0.27, output: 0 },
399 },
400 ]),
401 { status: 200, headers: { 'content-type': 'application/json' } },
402 ),
403 )
404 }) as typeof fetch
405 return () => {
406 globalThis.fetch = original
407 }
408}
409
410function textResponse(body: string): Promise<Response> {
411 return Promise.resolve(
412 new Response(body, {
413 status: 200,
414 headers: { 'content-type': 'text/markdown' },
415 }),
416 )
417}
418
419const LIST_DOCS = `
420## Chat models
421
422| Organization | Model name | API model string | Context length | Input pricing (per 1M tokens) | Cached input pricing (per 1M tokens) | Output pricing (per 1M tokens) | Quantization | Function calling | Structured outputs |
423| :- | :- | :- | :- | :- | :- | :- | :- | :- | :- |
424| Qwen | Qwen3.5 9B | Qwen/Qwen3.5-9B | 262144 | \\$0.17 | - | \\$0.25 | FP8 | Yes | Yes |
425| Z.ai | GLM-5.3 | zai-org/GLM-5.3 | 1048575 | \\$1.40 | \\$0.26 | \\$4.40 | FP4 | Yes | Yes |
426
427## Vision models
428
429| Organization | Model name | API model string | Context length | Input pricing (per 1M tokens) | Output pricing (per 1M tokens) |
430| :- | :- | :- | :- | :- | :- |
431| Org | Vision only | org/vision-only | 8192 | \\$1.00 | \\$1.00 |
432
433## Audio models
434
435| Organization | Modality | Model name | Model string for API | Pricing |
436| :- | :- | :- | :- | :- |
437| Kokoro | Text-to-Speech | Kokoro | hexgrad/Kokoro-82M | \\$4.00 per 1M chars |
438`
439
440const LIST_REASONING = `
441* **Hybrid:** Supports both reasoning and non-reasoning modes via \`reasoning={"enabled": True/False}\`.
442* **Adjustable effort:** Supports the \`reasoning_effort\` parameter to control reasoning depth (\`"low"\`, \`"medium"\`, or \`"high"\`).
443
444## Supported models
445
446| Model | API string | Type | Context length |
447| :- | :- | :- | :- |
448| Qwen3.5 9B | \`Qwen/Qwen3.5-9B\` | Hybrid (on by default) | 262K |
449| Kimi K3 | \`moonshotai/Kimi-K3\` | Hybrid (on by default) | 1M |
450| GPT-OSS 120B | \`openai/gpt-oss-120b\` | Adjustable effort | 128K |
451`
452
453const LIST_KIMI = `
454The model ID is \`moonshotai/Kimi-K3\`.
455
456| Parameter | Behavior on Together |
457| - | - |
458| \`reasoning_effort\` | \`"low"\`, \`"medium"\`, \`"high"\`, or \`"max"\` (default). |
459| \`reasoning\` | \`{"enabled": False}\` disables thinking entirely. |
460`
461
462const LIST_GPT = `
463The model ID is \`openai/gpt-oss-120b\`.
464
465\`reasoning_effort\` accepts \`"low"\`, \`"medium"\`, and \`"high"\`.
466
467Reasoning cannot be disabled entirely.
468`
469
470describe('togetherRateCard', () => {
471 it('compiles per-million listing rates into a token card', async () => {
472 const card = await togetherRateCard({
473 hourly: 0,
474 input: 0.88,
475 output: 0.88,
476 cached_input: 0.2,
477 base: 0,
478 finetune: 0,
479 })
480 if (!card) throw new Error('did not compile')
481 expect(
482 price(card, {}, { input_tokens: 1e6, output_tokens: 0 }),
483 ).toBeCloseTo(0.88, 9)
484 expect(
485 price(
486 card,
487 {},
488 { cache_read_tokens: 1e6, input_tokens: 0, output_tokens: 0 },
489 ),
490 ).toBeCloseTo(0.2, 9)
491 expect(card.source.url).toBe('https://api.together.xyz/v1/models')
492 })
493
494 it('serves no card for an all-zero or absent listing', async () => {
495 // Issue #111 examples Together does not quote per token.
496 expect(
497 await togetherRateCard({
498 hourly: 0,
499 input: 0,
500 output: 0,
501 cached_input: 0,
502 base: 0,
503 }),
504 ).toBeNull()
505 expect(await togetherRateCard(undefined)).toBeNull()
506 })
507})
508
509describe('together provider', () => {
510 it('keeps reasoning flags off the shared chat schema', () => {
511 expect(provider.perModelSchemaFlags).toEqual([
512 'reasoning',
513 'reasoning_effort',
514 ])
515 })
516})
517
518describe('together fetchSpec', () => {
519 it('loads the published yaml and classifies generation POSTs', async () => {
520 const original = globalThis.fetch
521 globalThis.fetch = ((url: string) => {
522 expect(String(url)).toBe('https://docs.together.ai/openapi.yaml')
523 return Promise.resolve(new Response(SPEC_YAML, { status: 200 }))
524 }) as typeof fetch
525 try {
526 const fetched = await provider.fetchSpec({})
527 expect(fetched.outputStrategy).toBe('post-200')
528 expect(fetched.sources[0]?.url).toBe(
529 'https://docs.together.ai/openapi.yaml',
530 )
531 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
532
533 const { endpoints, warnings } = classifyAndBundle(provider, fetched)
534 expect(warnings).toEqual([])
535 expect(
536 endpoints.map((endpoint) => [endpoint.dbId, endpoint.activity]).sort(),
537 ).toEqual([
538 ['together/audio/speech', 'audio'],
539 ['together/chat/completions', 'chat'],
540 ['together/embeddings', 'embeddings'],
541 ['together/images/generations', 'image'],
542 ['together/videos', 'video'],
543 ])
544 expect(
545 endpoints.every((endpoint) => endpoint.derivation === 'upstream-spec'),
546 ).toBe(true)
547 } finally {
548 globalThis.fetch = original
549 }
550 })
551})