Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { ChatStreamSummarizeAdapter } from '@tanstack/ai/adapters'
3import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
4import { openaiCompatible } from '../src/compatible/index'
5
6function setup(api: 'chat-completions' | 'responses', name?: string) {
7 const requests: Array<Record<string, unknown>> = []
8 const provider = openaiCompatible({
9 api,
10 name: 'custom-provider',
11 baseURL: 'https://never-called.invalid/v1',
12 apiKey: 'synthetic-test-key',
13 models: ['test-model'],
14 maxRetries: 0,
15 fetch: async (_input, init) => {
16 requests.push(JSON.parse(String(init?.body)))
17 return new Response('data: [DONE]\n\n', {
18 headers: { 'content-type': 'text/event-stream' },
19 })
20 },
21 })
22 return {
23 requests,
24 adapter: new ChatStreamSummarizeAdapter(
25 provider('test-model'),
26 'test-model',
27 name,
28 ),
29 }
30}
31
32describe('compatible summarize token limits on the wire', () => {
33 for (const api of ['chat-completions', 'responses'] as const) {
34 const key = api === 'responses' ? 'max_output_tokens' : 'max_tokens'
35 for (const name of [undefined, 'custom-provider', 'openai']) {
36 for (const stream of [false, true]) {
37 it(`${api}, name=${name}, stream=${stream}: forwards maxLength`, async () => {
38 const { adapter, requests } = setup(api, name)
39 const options = {
40 threadId: 'thread-1',
41 model: 'test-model',
42 text: 'Synthetic input.',
43 maxLength: 73,
44 logger: resolveDebugOption(false),
45 }
46 if (stream) {
47 for await (const _chunk of adapter.summarizeStream(options)) {
48 // Consume the full stream so the SDK request is made.
49 }
50 } else {
51 await adapter.summarize(options)
52 }
53 expect(requests).toHaveLength(1)
54 expect(requests[0]?.[key]).toBe(73)
55 const wrongKey =
56 api === 'responses' ? 'max_tokens' : 'max_output_tokens'
57 expect(requests[0]).not.toHaveProperty(wrongKey)
58 })
59 }
60 }
61
62 it(`${api}: preserves the caller's explicit limit`, async () => {
63 const { adapter, requests } = setup(api)
64 const modelOptions = { [key]: 41 }
65 await adapter.summarize({
66 threadId: 'thread-1',
67 model: 'test-model',
68 text: 'Synthetic input.',
69 maxLength: 73,
70 modelOptions,
71 logger: resolveDebugOption(false),
72 })
73 expect(requests[0]?.[key]).toBe(41)
74 expect(modelOptions).toEqual({ [key]: 41 })
75 })
76
77 it(`${api}: does not add a limit when maxLength is absent`, async () => {
78 const { adapter, requests } = setup(api)
79 await adapter.summarize({
80 threadId: 'thread-1',
81 model: 'test-model',
82 text: 'Synthetic input.',
83 logger: resolveDebugOption(false),
84 })
85 expect(requests[0]).not.toHaveProperty(key)
86 })
87 }
88})