{"version":3,"file":"fileSearch.cjs","names":[],"sources":["../../src/tools/fileSearch.ts"],"sourcesContent":["import { OpenAI as OpenAIClient } from \"openai\";\nimport type { ServerTool } from \"@langchain/core/tools\";\n\n/**\n * Comparison operators for file attribute filtering.\n */\nexport type FileSearchComparisonType =\n  | \"eq\"\n  | \"ne\"\n  | \"gt\"\n  | \"gte\"\n  | \"lt\"\n  | \"lte\";\n\n/**\n * A filter used to compare a specified attribute key to a given value.\n */\nexport interface FileSearchComparisonFilter {\n  /**\n   * The comparison operator to use.\n   * - `eq`: equals\n   * - `ne`: not equal\n   * - `gt`: greater than\n   * - `gte`: greater than or equal\n   * - `lt`: less than\n   * - `lte`: less than or equal\n   */\n  type: FileSearchComparisonType;\n  /**\n   * The attribute key to compare against.\n   */\n  key: string;\n  /**\n   * The value to compare against the attribute key.\n   * Supports string, number, boolean, or array of strings/numbers.\n   */\n  value: string | number | boolean | Array<string | number>;\n}\n\n/**\n * Combine multiple filters using `and` or `or`.\n */\nexport interface FileSearchCompoundFilter {\n  /**\n   * Type of operation: `and` or `or`.\n   */\n  type: \"and\" | \"or\";\n  /**\n   * Array of filters to combine.\n   */\n  filters: Array<FileSearchComparisonFilter | FileSearchCompoundFilter>;\n}\n\n/**\n * Filter type for file search - can be a comparison or compound filter.\n */\nexport type FileSearchFilter =\n  | FileSearchComparisonFilter\n  | FileSearchCompoundFilter;\n\n/**\n * Weights for hybrid search balancing semantic and keyword matches.\n */\nexport interface FileSearchHybridSearchWeights {\n  /**\n   * The weight of semantic embedding matches (0-1).\n   */\n  embeddingWeight: number;\n  /**\n   * The weight of keyword/text matches (0-1).\n   */\n  textWeight: number;\n}\n\n/**\n * Ranking options for file search results.\n */\nexport interface FileSearchRankingOptions {\n  /**\n   * The ranker to use for the file search.\n   * - `auto`: Automatically select the best ranker\n   * - `default-2024-11-15`: Default ranker as of November 2024\n   */\n  ranker?: \"auto\" | \"default-2024-11-15\";\n  /**\n   * The score threshold for results (0-1).\n   * Numbers closer to 1 return only the most relevant results but may return fewer.\n   */\n  scoreThreshold?: number;\n  /**\n   * Weights that control how reciprocal rank fusion balances semantic\n   * embedding matches versus sparse keyword matches when hybrid search is enabled.\n   */\n  hybridSearch?: FileSearchHybridSearchWeights;\n}\n\n/**\n * Options for the File Search tool.\n */\nexport interface FileSearchOptions {\n  /**\n   * The IDs of the vector stores to search.\n   * You must have previously created vector stores and uploaded files to them.\n   */\n  vectorStoreIds: string[];\n  /**\n   * The maximum number of results to return.\n   * Must be between 1 and 50 inclusive.\n   */\n  maxNumResults?: number;\n  /**\n   * A filter to apply based on file attributes/metadata.\n   * Use this to narrow down search results to specific categories or file types.\n   */\n  filters?: FileSearchFilter;\n  /**\n   * Ranking options to customize how results are scored and ordered.\n   */\n  rankingOptions?: FileSearchRankingOptions;\n}\n\n/**\n * OpenAI File Search tool type for the Responses API.\n */\nexport type FileSearchTool = OpenAIClient.Responses.FileSearchTool;\n\n/**\n * Converts ranking options to the API format.\n */\nfunction convertRankingOptions(\n  options: FileSearchRankingOptions | undefined\n): OpenAIClient.Responses.FileSearchTool.RankingOptions | undefined {\n  if (!options) return undefined;\n  return {\n    ranker: options.ranker,\n    score_threshold: options.scoreThreshold,\n    hybrid_search: options.hybridSearch\n      ? {\n          embedding_weight: options.hybridSearch.embeddingWeight,\n          text_weight: options.hybridSearch.textWeight,\n        }\n      : undefined,\n  };\n}\n\n/**\n * Creates a File Search tool that allows models to search your files\n * for relevant information using semantic and keyword search.\n *\n * File Search enables models to retrieve information from a knowledge base\n * of previously uploaded files stored in vector stores. This is a hosted tool\n * managed by OpenAI - you don't need to implement the search execution yourself.\n *\n * **Prerequisites**: Before using File Search, you must:\n * 1. Upload files to the File API with `purpose: \"assistants\"`\n * 2. Create a vector store\n * 3. Add files to the vector store\n *\n * @see {@link https://platform.openai.com/docs/guides/tools-file-search | OpenAI File Search Documentation}\n *\n * @param options - Configuration options for the File Search tool\n * @returns A File Search tool definition to be passed to the OpenAI Responses API\n *\n * @example\n * ```typescript\n * import { ChatOpenAI, tools } from \"@langchain/openai\";\n *\n * const model = new ChatOpenAI({ model: \"gpt-4.1\" });\n *\n * // Basic usage with a vector store\n * const response = await model.invoke(\n *   \"What is deep research by OpenAI?\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\"]\n *     })]\n *   }\n * );\n *\n * // Limit the number of results for lower latency\n * const response = await model.invoke(\n *   \"Find information about pricing\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\"],\n *       maxNumResults: 5\n *     })]\n *   }\n * );\n *\n * // With metadata filtering\n * const response = await model.invoke(\n *   \"Find recent blog posts about AI\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\"],\n *       filters: {\n *         type: \"eq\",\n *         key: \"category\",\n *         value: \"blog\"\n *       }\n *     })]\n *   }\n * );\n *\n * // With compound filters (AND/OR)\n * const response = await model.invoke(\n *   \"Find technical docs from 2024\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\"],\n *       filters: {\n *         type: \"and\",\n *         filters: [\n *           { type: \"eq\", key: \"category\", value: \"technical\" },\n *           { type: \"gte\", key: \"year\", value: 2024 }\n *         ]\n *       }\n *     })]\n *   }\n * );\n *\n * // With ranking options for more relevant results\n * const response = await model.invoke(\n *   \"Find the most relevant information\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\"],\n *       rankingOptions: {\n *         scoreThreshold: 0.8,\n *         ranker: \"auto\"\n *       }\n *     })]\n *   }\n * );\n *\n * // Search multiple vector stores\n * const response = await model.invoke(\n *   \"Search across all knowledge bases\",\n *   {\n *     tools: [tools.fileSearch({\n *       vectorStoreIds: [\"vs_abc123\", \"vs_def456\"]\n *     })]\n *   }\n * );\n * ```\n *\n * @remarks\n * - Vector stores must be created and populated before using this tool\n * - The tool returns file citations in the response annotations\n * - Use `include: [\"file_search_call.results\"]` in the API call to get search results\n * - Supported file types include PDF, DOCX, TXT, MD, and many code file formats\n */\nexport function fileSearch(options: FileSearchOptions): ServerTool {\n  return {\n    type: \"file_search\",\n    vector_store_ids: options.vectorStoreIds,\n    max_num_results: options.maxNumResults,\n    filters: options.filters as FileSearchTool[\"filters\"],\n    ranking_options: convertRankingOptions(options.rankingOptions),\n  } satisfies FileSearchTool;\n}\n"],"mappings":";;;;AAiIA,SAAS,sBACP,SACkE;AAClE,KAAI,CAAC,QAAS,QAAO,KAAA;AACrB,QAAO;EACL,QAAQ,QAAQ;EAChB,iBAAiB,QAAQ;EACzB,eAAe,QAAQ,eACnB;GACE,kBAAkB,QAAQ,aAAa;GACvC,aAAa,QAAQ,aAAa;GACnC,GACD,KAAA;EACL;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+GH,SAAgB,WAAW,SAAwC;AACjE,QAAO;EACL,MAAM;EACN,kBAAkB,QAAQ;EAC1B,iBAAiB,QAAQ;EACzB,SAAS,QAAQ;EACjB,iBAAiB,sBAAsB,QAAQ,eAAe;EAC/D"}