Open History to see the last commit
1import { makeMistralStructuredOutputCompatible } from '../utils/schema-converter'
2import type { JSONSchema, Tool } from '@tanstack/ai'
3import type { ChatCompletionTool } from '../message-types'
4
5export type FunctionTool = ChatCompletionTool
6
7/**
8 * Converts a standard Tool to Mistral ChatCompletionTool format.
9 *
10 * Tool schemas are already converted to JSON Schema in the ai layer.
11 * We apply Mistral-specific transformations for strict mode:
12 * - All properties in required array
13 * - Optional fields made nullable
14 * - additionalProperties: false
15 */
16export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
17 const baseSchema = (tool.inputSchema ?? {
18 type: 'object',
19 properties: {},
20 required: [],
21 }) as JSONSchema
22
23 const inputSchema: JSONSchema =
24 baseSchema.type === 'object' && !baseSchema.properties
25 ? { ...baseSchema, properties: {} }
26 : { ...baseSchema }
27
28 const jsonSchema = makeMistralStructuredOutputCompatible(
29 inputSchema,
30 inputSchema.required || [],
31 )
32
33 return {
34 type: 'function',
35 function: {
36 name: tool.name,
37 description: tool.description,
38 parameters: jsonSchema,
39 strict: true,
40 },
41 } satisfies FunctionTool
42}