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 logger,
34 model,
35 messages,
36 ...request,
37 })) {
38 // drain
39 }
40 return body
41}
42
43describe('byteplusText tool_choice', () => {
44 it('sends required as is', async () => {
45 const body = await sentBody({
46 tools: [weatherTool],
47 toolChoice: 'required',
48 })
49 expect(body).toHaveProperty('tool_choice', 'required')
50 })
51
52 it('sends a named tool as a function choice', async () => {
53 const body = await sentBody({
54 tools: [weatherTool],
55 toolChoice: { type: 'tool', name: 'lookup_weather' },
56 })
57 expect(body).toHaveProperty('tool_choice', {
58 type: 'function',
59 function: { name: 'lookup_weather' },
60 })
61 })
62
63 it('lets a tool_choice in modelOptions win', async () => {
64 const body = await sentBody({
65 tools: [weatherTool],
66 toolChoice: 'required',
67 modelOptions: { tool_choice: 'none' },
68 })
69 expect(body).toHaveProperty('tool_choice', 'none')
70 })
71
72 it('sends no tool_choice when the request has no tools', async () => {
73 const body = await sentBody({ toolChoice: 'required' })
74 expect(body).toHaveProperty('model', model)
75 expect(body).not.toHaveProperty('tool_choice')
76 })
77})