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

function-tool.ts

46 lines1.1 KB

1import { makeStructuredOutputCompatible } from '@tanstack/openai-base'
2import type { JSONSchema, Tool } from '@tanstack/ai'
3​
4export interface FunctionTool {
5 type: 'function'
6 function: {
7 name: string
8 description?: string
9 parameters: Record<string, unknown>
10 strict?: boolean
11 }
13​
14/**
15 * Converts a standard Tool to Vercel AI Gateway Chat Completions tool format.
16 *
17 * Tool schemas are already converted to JSON Schema in the ai layer.
18 */
19export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
20 const inputSchema = (tool.inputSchema ?? {
21 type: 'object',
22 properties: {},
23 required: [],
24 }) as JSONSchema
25​
26 if (inputSchema.type === 'object' && !inputSchema.properties) {
27 inputSchema.properties = {}
28 }
29​
30 const jsonSchema = makeStructuredOutputCompatible(
31 inputSchema,
32 inputSchema.required || [],
33 )
34​
35 jsonSchema.additionalProperties = false
36​
37 return {
38 type: 'function',
39 function: {
40 name: tool.name,
41 description: tool.description,
42 parameters: jsonSchema,
43 strict: true,
44 },
45 }