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