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

tool-choice.ts

52 lines1.1 KB

1import type { ToolChoice as CoreToolChoice } from '@tanstack/ai'
2​
3/**
4 * Maps the `chat({ toolChoice })` value to the Chat Completions
5 * `tool_choice` wire value.
6 */
7export function toChatCompletionsToolChoice(choice: CoreToolChoice) {
8 if (typeof choice === 'string') return choice
9 return { type: 'function' as const, function: { name: choice.name } }
11​
12/**
13 * Maps the `chat({ toolChoice })` value to the Responses `tool_choice` wire
14 * value.
15 */
16export function toResponsesToolChoice(choice: CoreToolChoice) {
17 if (typeof choice === 'string') return choice
18 return { type: 'function' as const, name: choice.name }
20​
21interface MCPToolChoice {
22 type: 'mcp'
23 server_label: string
25​
26interface FunctionToolChoice {
27 type: 'function'
28 name: string
30​
31interface CustomToolChoice {
32 type: 'custom'
33 name: string
35​
36interface HostedToolChoice {
37 type:
38 | 'file_search'
39 | 'web_search_preview'
40 | 'computer'
41 | 'computer_use_preview'
42 | 'code_interpreter'
43 | 'image_generation'
44 | 'shell'
45 | 'apply_patch'
47​
48export type ToolChoice =
49 | MCPToolChoice
50 | FunctionToolChoice
51 | CustomToolChoice
52 | HostedToolChoice