Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
3import { createBytePlusText } from '../src/adapters/text'
4import type { ModelMessage, Tool, ToolChoice } from '@tanstack/ai'
5
6const logger = resolveDebugOption(false)
7const model = 'seed-2-0-lite-260428'
8const messages: Array<ModelMessage> = [
9 { role: 'user', content: 'Weather in Paris?' },
10]
11const weatherTool: Tool = {
12 name: 'lookup_weather',
13 description: 'Return the forecast for a location',
14}
15
16interface ToolRequest {
17 tools?: Array<Tool>
18 toolChoice?: ToolChoice
19 modelOptions?: { tool_choice: 'none' }
20}
21
22/** Runs one streaming call and returns the JSON body sent over HTTP. */
23async function sentBody(request: ToolRequest) {
24 let body: unknown
25 const fetch: typeof globalThis.fetch = async (_url, init) => {
26 body = JSON.parse(String(init?.body))
27 return new Response('', {
28 headers: { 'content-type': 'text/event-stream' },
29 })
30 }
31 const adapter = createBytePlusText(model, 'test-key', { fetch })
32 for await (const _ of adapter.chatStream({
33 threadId: 'thread-1',
34 logger,
35 model,
36 messages,
37 ...request,
38 })) {
39 // drain
40 }
41 return body
42}
43
44describe('byteplusText tool_choice', () => {
45 it('sends required as is', async () => {
46 const body = await sentBody({
47 tools: [weatherTool],
48 toolChoice: 'required',
49 })
50 expect(body).toHaveProperty('tool_choice', 'required')
51 })
52
53 it('sends a named tool as a function choice', async () => {
54 const body = await sentBody({
55 tools: [weatherTool],
56 toolChoice: { type: 'tool', name: 'lookup_weather' },
57 })
58 expect(body).toHaveProperty('tool_choice', {
59 type: 'function',
60 function: { name: 'lookup_weather' },
61 })
62 })
63
64 it('lets a tool_choice in modelOptions win', async () => {
65 const body = await sentBody({
66 tools: [weatherTool],
67 toolChoice: 'required',
68 modelOptions: { tool_choice: 'none' },
69 })
70 expect(body).toHaveProperty('tool_choice', 'none')
71 })
72
73 it('sends no tool_choice when the request has no tools', async () => {
74 const body = await sentBody({ toolChoice: 'required' })
75 expect(body).toHaveProperty('model', model)
76 expect(body).not.toHaveProperty('tool_choice')
77 })
78})