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

tool-choice-wire.test.ts

116 lines3.3 KB

1import { describe, expect, it } from 'vitest'
2import OpenAI from 'openai'
3import { resolveDebugOption } from '@tanstack/ai/adapter-internals'
4import { OpenAIBaseChatCompletionsTextAdapter } from '../src/adapters/chat-completions-text'
5import { OpenAIBaseResponsesTextAdapter } from '../src/adapters/responses-text'
6import type { TextOptions, Tool } from '@tanstack/ai'
7​
8const logger = resolveDebugOption(false)
9const model = 'gpt-5.5'
10​
11const weatherTool: Tool = {
12 name: 'lookup_weather',
13 description: 'Return the forecast for a location',
15​
16class Completions extends OpenAIBaseChatCompletionsTextAdapter<string> {}
17class Responses extends OpenAIBaseResponsesTextAdapter<string> {}
18​
19const completionsDone = {
20 id: 'response-1',
21 model,
22 choices: [{ delta: { content: 'Done' }, finish_reason: 'stop' }],
24const responsesDone = {
25 type: 'response.completed',
26 response: {
27 id: 'response-1',
28 model,
29 status: 'completed',
30 output: [
31 {
32 type: 'message',
33 id: 'message-1',
34 role: 'assistant',
35 content: [{ type: 'output_text', text: 'Done', annotations: [] }],
36 },
37 ],
38 },
40​
41const apis = [
42 {
43 api: 'Chat Completions',
44 adapter: (client: OpenAI) => new Completions(model, 'openai', client),
45 sse: `data: ${JSON.stringify(completionsDone)}\n\ndata: [DONE]\n\n`,
46 namedChoice: { type: 'function', function: { name: 'lookup_weather' } },
47 },
48 {
49 api: 'Responses',
50 adapter: (client: OpenAI) => new Responses(model, 'openai', client),
51 sse: `data: ${JSON.stringify(responsesDone)}\n\n`,
52 namedChoice: { type: 'function', name: 'lookup_weather' },
53 },
55​
56/** Runs one streaming call and returns the JSON body sent over HTTP. */
57async function sentBody(
58 { adapter, sse }: (typeof apis)[number],
59 request: Pick<TextOptions, 'tools' | 'toolChoice' | 'modelOptions'>,
60) {
61 let body: unknown
62 const client = new OpenAI({
63 apiKey: 'test-key',
64 fetch: async (_url, init) => {
65 body = JSON.parse(String(init?.body))
66 return new Response(sse, {
67 headers: { 'content-type': 'text/event-stream' },
68 })
69 },
70 })
71 for await (const _ of adapter(client).chatStream({
72 threadId: 'thread-1',
73 logger,
74 model,
75 messages: [{ role: 'user', content: 'Weather in Paris?' }],
76 ...request,
77 })) {
78 // drain
79 }
80 return body
82​
83describe.each(apis)('$api tool_choice', (api) => {
84 it.each(['auto', 'none', 'required'] as const)(
85 'sends %s as is',
86 async (choice) => {
87 const body = await sentBody(api, {
88 tools: [weatherTool],
89 toolChoice: choice,
90 })
91 expect(body).toHaveProperty('tool_choice', choice)
92 },
93 )
94​
95 it('sends a named tool as a function choice', async () => {
96 const body = await sentBody(api, {
97 tools: [weatherTool],
98 toolChoice: { type: 'tool', name: 'lookup_weather' },
99 })
100 expect(body).toHaveProperty('tool_choice', api.namedChoice)
101 })
102​
103 it('lets a tool_choice in modelOptions win', async () => {
104 const body = await sentBody(api, {
105 tools: [weatherTool],
106 toolChoice: 'required',
107 modelOptions: { tool_choice: 'none' },
108 })
109 expect(body).toHaveProperty('tool_choice', 'none')
110 })
111​
112 it('sends no tool_choice when the request has no tools', async () => {
113 const body = await sentBody(api, { toolChoice: 'required' })
114 expect(body).not.toHaveProperty('tool_choice')
115 })
116})