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 { createBytePlusText } from '../src/adapters/text'
5import type { StreamChunk } from '@tanstack/ai'
6
7const logger = resolveDebugOption(false)
8const model = 'seed-1-6-250915'
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 createBytePlusText(model, 'test-key', {
15 fetch,
16 maxRetries: 0,
17 }).chatStream({
18 logger,
19 model,
20 messages: [{ role: 'user', content: 'Hi' }],
21 })) {
22 chunks.push(chunk)
23 }
24 return chunks.find((chunk) => chunk.type === 'RUN_ERROR')
25}
26
27describe('BytePlus adapter on a context overflow', () => {
28 it('sets the context_overflow code and keeps the provider message', async () => {
29 const error = await runError(400, {
30 error: {
31 code: 'InvalidParameter',
32 message:
33 'Total tokens of image and text exceed max message tokens. Request id: 0217918724619810f20bbdfc8ca54d9bbb0533a8546905cd6382b',
34 param: '',
35 type: 'BadRequest',
36 },
37 })
38 expect(error).toMatchObject({ code: CONTEXT_OVERFLOW_CODE })
39 expect(error?.message).toContain('exceed max message tokens')
40 })
41
42 it('does not set it on another bad request', async () => {
43 const error = await runError(400, {
44 error: {
45 code: 'InvalidParameter',
46 message:
47 'The parameter `max_tokens` specified in the request is not valid.',
48 param: 'max_tokens',
49 type: 'BadRequest',
50 },
51 })
52 expect(error).toMatchObject({ code: 'InvalidParameter' })
53 })
54})