Open History to see the last commit
1import type { JSONSchema, Tool } from '@tanstack/ai'
2import type { Tool as OllamaTool } from 'ollama'
3
4/**
5 * Converts a standard Tool to Ollama's function-tool format.
6 *
7 * Tool schemas are already converted to JSON Schema in the ai layer. We
8 * accept any JSONSchema and hand it to Ollama via a local type cast because
9 * our JSONSchema type is broader than Ollama's (e.g. `type` can be a union
10 * or array of strings).
11 */
12export function convertFunctionToolToAdapterFormat(tool: Tool): OllamaTool {
13 const inputSchema = (tool.inputSchema ?? {
14 type: 'object',
15 properties: {},
16 required: [],
17 }) as JSONSchema
18
19 return {
20 type: 'function',
21 function: {
22 name: tool.name,
23 description: tool.description,
24 parameters: inputSchema as NonNullable<
25 OllamaTool['function']['parameters']
26 >,
27 },
28 }
29}