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

custom-tool.ts

43 lines1.3 KB

1import {
2 getOpenAIProviderToolMetadata,
3 openAIProviderTool,
4} from './openai-provider-tool'
5import type { CustomTool as CustomToolConfig } from 'openai/resources/responses/responses'
6import type { Tool } from '@tanstack/ai'
7​
8export type { CustomToolConfig }
9​
10/**
11 * Converts a standard Tool to OpenAI CustomTool format
12 */
13export function convertCustomToolToAdapterFormat(tool: Tool): CustomToolConfig {
14 const metadata = getOpenAIProviderToolMetadata(tool) as CustomToolConfig
15 // Conditional spread: the SDK's `CustomToolConfig` declares optional
16 // fields as `description?: string` (no `| undefined`) under
17 // exactOptionalPropertyTypes, so we omit absent fields rather than
18 // passing them through as explicit `undefined`.
19 return {
20 type: 'custom',
21 name: metadata.name,
22 ...(metadata.description !== undefined && {
23 description: metadata.description,
24 }),
25 ...(metadata.format !== undefined && { format: metadata.format }),
26 }
28​
29/**
30 * Creates a standard Tool from CustomTool parameters.
31 */
32export function customTool(toolData: CustomToolConfig): Tool {
33 return openAIProviderTool(
34 {
35 name: 'custom',
36 description: toolData.description || 'A custom tool',
37 metadata: {
38 ...toolData,
39 },
40 },
41 'custom',
42 )