Open History to see the last commit
1import {
2 getOpenAIProviderToolMetadata,
3 openAIProviderTool,
4} from './openai-provider-tool'
5import type { FileSearchTool as FileSearchToolConfig } from 'openai/resources/responses/responses'
6import type { Tool } from '@tanstack/ai'
7
8export type { FileSearchToolConfig }
9
10const validateMaxNumResults = (maxNumResults: number | undefined) => {
11 if (
12 maxNumResults !== undefined &&
13 (maxNumResults < 1 || maxNumResults > 50)
14 ) {
15 throw new Error('max_num_results must be between 1 and 50.')
16 }
17}
18
19/**
20 * Converts a standard Tool to OpenAI FileSearchTool format
21 */
22export function convertFileSearchToolToAdapterFormat(
23 tool: Tool,
24): FileSearchToolConfig {
25 const metadata = getOpenAIProviderToolMetadata(tool) as FileSearchToolConfig
26 // Conditional spread: SDK's `FileSearchToolConfig` declares the
27 // optional fields without `| undefined`, so we omit absent values
28 // rather than passing them through as explicit `undefined`.
29 return {
30 type: 'file_search',
31 vector_store_ids: metadata.vector_store_ids,
32 ...(metadata.max_num_results !== undefined && {
33 max_num_results: metadata.max_num_results,
34 }),
35 ...(metadata.ranking_options !== undefined && {
36 ranking_options: metadata.ranking_options,
37 }),
38 ...(metadata.filters !== undefined && { filters: metadata.filters }),
39 }
40}
41
42/**
43 * Creates a standard Tool from FileSearchTool parameters.
44 *
45 * Validates max_num_results. Base (non-branded) factory; providers that need
46 * branded return types should re-wrap in their own package.
47 */
48export function fileSearchTool(toolData: FileSearchToolConfig): Tool {
49 validateMaxNumResults(toolData.max_num_results)
50 return openAIProviderTool(
51 {
52 name: 'file_search',
53 description: 'Search files in vector stores',
54 metadata: {
55 ...toolData,
56 },
57 },
58 'file_search',
59 )
60}
61
62export { validateMaxNumResults }