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

custom-tool.ts

46 lines1.4 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/** @deprecated Renamed to `CustomToolConfig`. Will be removed in a future release. */
11export type CustomTool = CustomToolConfig
12​
13/**
14 * Converts a standard Tool to OpenAI CustomTool format
15 */
16export function convertCustomToolToAdapterFormat(tool: Tool): CustomToolConfig {
17 const metadata = getOpenAIProviderToolMetadata(tool) as CustomToolConfig
18 // Conditional spread: the SDK's `CustomToolConfig` declares optional
19 // fields as `description?: string` (no `| undefined`) under
20 // exactOptionalPropertyTypes, so we omit absent fields rather than
21 // passing them through as explicit `undefined`.
22 return {
23 type: 'custom',
24 name: metadata.name,
25 ...(metadata.description !== undefined && {
26 description: metadata.description,
27 }),
28 ...(metadata.format !== undefined && { format: metadata.format }),
29 }
31​
32/**
33 * Creates a standard Tool from CustomTool parameters.
34 */
35export function customTool(toolData: CustomToolConfig): Tool {
36 return openAIProviderTool(
37 {
38 name: 'custom',
39 description: toolData.description || 'A custom tool',
40 metadata: {
41 ...toolData,
42 },
43 },
44 'custom',
45 )