Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { CONTEXT_OVERFLOW_CODE } from '@tanstack/ai'
3import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
4import { createCloudflareText } from '../src/adapters/text'
5import type { StreamChunk } from '@tanstack/ai'
6
7const logger = resolveDebugOption(false)
8const model = '@cf/zai-org/glm-5.3-flash'
9
10/** Runs one call that gets `status` and `body`. Gives back its RUN_ERROR. */
11async function runError(status: number, body: object) {
12 const fetch = async () => Response.json(body, { status })
13 const chunks: Array<StreamChunk> = []
14 for await (const chunk of createCloudflareText(model, {
15 accountId: 'test-account',
16 apiKey: 'test-key',
17 fetch,
18 maxRetries: 0,
19 }).chatStream({
20 threadId: 'thread-1',
21 logger,
22 model,
23 messages: [{ role: 'user', content: 'Hi' }],
24 })) {
25 chunks.push(chunk)
26 }
27 return chunks.find((chunk) => chunk.type === 'RUN_ERROR')
28}
29
30describe('Cloudflare adapter on a context overflow', () => {
31 it('sets the context_overflow code and keeps the provider message', async () => {
32 const error = await runError(413, {
33 errors: [
34 {
35 message:
36 'AiError: Ai: The estimated number of input and maximum output tokens (60026) exceeded this model context window limit (24000).',
37 code: 5021,
38 },
39 ],
40 success: false,
41 result: {},
42 messages: [],
43 })
44 expect(error).toMatchObject({ code: CONTEXT_OVERFLOW_CODE })
45 expect(error?.message).toContain('exceeded this model context window limit')
46 })
47
48 it('does not set it on a capacity error', async () => {
49 const error = await runError(429, {
50 errors: [
51 { message: 'AiError: Capacity temporarily exceeded.', code: 3040 },
52 ],
53 success: false,
54 result: {},
55 messages: [],
56 })
57 expect(error).toMatchObject({ code: '3040' })
58 })
59})