Open History to see the last commit
1import { makeGroqStructuredOutputCompatible } 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 Groq ChatCompletionTool format.
9 *
10 * Tool schemas are already converted to JSON Schema in the ai layer.
11 * We apply Groq-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 inputSchema = (tool.inputSchema ?? {
18 type: 'object',
19 properties: {},
20 required: [],
21 }) as JSONSchema
22
23 // Ensure object schemas always have properties (e.g. z.object({}) may produce { type: 'object' } without properties)
24 if (inputSchema.type === 'object' && !inputSchema.properties) {
25 inputSchema.properties = {}
26 }
27
28 const jsonSchema = makeGroqStructuredOutputCompatible(
29 inputSchema,
30 inputSchema.required || [],
31 )
32
33 jsonSchema.additionalProperties = false
34
35 return {
36 type: 'function',
37 function: {
38 name: tool.name,
39 description: tool.description,
40 parameters: jsonSchema,
41 strict: true,
42 },
43 } satisfies FunctionTool
44}