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

context-overflow-wire.test.ts

58 lines1.8 KB

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 logger,
21 model,
22 messages: [{ role: 'user', content: 'Hi' }],
23 })) {
24 chunks.push(chunk)
25 }
26 return chunks.find((chunk) => chunk.type === 'RUN_ERROR')
28​
29describe('Cloudflare adapter on a context overflow', () => {
30 it('sets the context_overflow code and keeps the provider message', async () => {
31 const error = await runError(413, {
32 errors: [
33 {
34 message:
35 'AiError: Ai: The estimated number of input and maximum output tokens (60026) exceeded this model context window limit (24000).',
36 code: 5021,
37 },
38 ],
39 success: false,
40 result: {},
41 messages: [],
42 })
43 expect(error).toMatchObject({ code: CONTEXT_OVERFLOW_CODE })
44 expect(error?.message).toContain('exceeded this model context window limit')
45 })
46​
47 it('does not set it on a capacity error', async () => {
48 const error = await runError(429, {
49 errors: [
50 { message: 'AiError: Capacity temporarily exceeded.', code: 3040 },
51 ],
52 success: false,
53 result: {},
54 messages: [],
55 })
56 expect(error).toMatchObject({ code: '3040' })
57 })
58})