|
| 1 | +export const TOOL_SEARCH_FUNCTION_NAME = "tool_search"; |
| 2 | +export const TOOL_SEARCH_DEFAULT_DESCRIPTION = "Search for additional tools to load for the next turn."; |
| 3 | + |
| 4 | +function isPlainObject(value: unknown): value is Record<string, unknown> { |
| 5 | + return !!value && typeof value === "object" && !Array.isArray(value); |
| 6 | +} |
| 7 | + |
| 8 | +export function toolSearchDescription(tool: unknown): string { |
| 9 | + return isPlainObject(tool) && typeof tool.description === "string" |
| 10 | + ? tool.description |
| 11 | + : TOOL_SEARCH_DEFAULT_DESCRIPTION; |
| 12 | +} |
| 13 | + |
| 14 | +export function toolSearchParameters(tool: unknown): Record<string, unknown> { |
| 15 | + if (isPlainObject(tool) && isPlainObject(tool.parameters)) return tool.parameters; |
| 16 | + // Fresh per parse/build: downstream normalizers are allowed to clone or extend schemas, and a |
| 17 | + // shared mutable default would couple otherwise unrelated requests. |
| 18 | + return { |
| 19 | + type: "object", |
| 20 | + properties: { |
| 21 | + query: { type: "string", description: "Search query for tools to load." }, |
| 22 | + limit: { type: "number", description: "Maximum number of tools to return." }, |
| 23 | + }, |
| 24 | + required: ["query"], |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +function rewriteToolList(tools: unknown[]): { tools: unknown[]; changed: boolean } { |
| 29 | + let changed = false; |
| 30 | + const rewritten = tools.map(tool => { |
| 31 | + if (!isPlainObject(tool) || tool.type !== "tool_search") return tool; |
| 32 | + const { |
| 33 | + execution: _execution, |
| 34 | + defer_loading: _deferLoading, |
| 35 | + ...rest |
| 36 | + } = tool; |
| 37 | + changed = true; |
| 38 | + return { |
| 39 | + ...rest, |
| 40 | + type: "function", |
| 41 | + name: TOOL_SEARCH_FUNCTION_NAME, |
| 42 | + description: toolSearchDescription(tool), |
| 43 | + parameters: toolSearchParameters(tool), |
| 44 | + }; |
| 45 | + }); |
| 46 | + return changed ? { tools: rewritten, changed: true } : { tools, changed: false }; |
| 47 | +} |
| 48 | + |
| 49 | +function rewriteToolChoice(choice: unknown): unknown { |
| 50 | + if (!isPlainObject(choice)) return choice; |
| 51 | + if (choice.type === "tool_search") { |
| 52 | + return { type: "function", name: TOOL_SEARCH_FUNCTION_NAME }; |
| 53 | + } |
| 54 | + if (choice.type !== "allowed_tools" || !Array.isArray(choice.tools)) return choice; |
| 55 | + let changed = false; |
| 56 | + const tools = choice.tools.map(tool => { |
| 57 | + if (!isPlainObject(tool) || tool.type !== "tool_search") return tool; |
| 58 | + changed = true; |
| 59 | + return { type: "function", name: TOOL_SEARCH_FUNCTION_NAME }; |
| 60 | + }); |
| 61 | + return changed ? { ...choice, tools } : choice; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Third-party Responses gateways generally implement the public function-tool schema, not Codex's |
| 66 | + * private client-executed `tool_search` declaration. Lower only request catalogs sent to a |
| 67 | + * noncanonical upstream; the caller-facing response is restored separately. |
| 68 | + */ |
| 69 | +export function rewriteRoutedToolSearchForUpstream(body: unknown): { |
| 70 | + body: unknown; |
| 71 | + names: Set<string>; |
| 72 | +} { |
| 73 | + const names = new Set<string>(); |
| 74 | + if (!isPlainObject(body)) return { body, names }; |
| 75 | + |
| 76 | + let tools = body.tools; |
| 77 | + if (Array.isArray(tools)) { |
| 78 | + const result = rewriteToolList(tools); |
| 79 | + tools = result.tools; |
| 80 | + if (result.changed) names.add(TOOL_SEARCH_FUNCTION_NAME); |
| 81 | + } |
| 82 | + |
| 83 | + let input = body.input; |
| 84 | + if (Array.isArray(input)) { |
| 85 | + input = input.map(item => { |
| 86 | + if (!isPlainObject(item) || item.type !== "additional_tools" || !Array.isArray(item.tools)) { |
| 87 | + return item; |
| 88 | + } |
| 89 | + const result = rewriteToolList(item.tools); |
| 90 | + if (!result.changed) return item; |
| 91 | + names.add(TOOL_SEARCH_FUNCTION_NAME); |
| 92 | + return { ...item, tools: result.tools }; |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + if (names.size === 0) return { body, names }; |
| 97 | + const toolChoice = rewriteToolChoice(body.tool_choice); |
| 98 | + return { |
| 99 | + body: { |
| 100 | + ...body, |
| 101 | + ...(tools !== body.tools ? { tools } : {}), |
| 102 | + ...(input !== body.input ? { input } : {}), |
| 103 | + ...(toolChoice !== body.tool_choice ? { tool_choice: toolChoice } : {}), |
| 104 | + }, |
| 105 | + names, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +export function toolSearchItemId(id: unknown): unknown { |
| 110 | + if (typeof id !== "string") return id; |
| 111 | + return id.startsWith("fc_") ? `tsc_${id.slice(3)}` : id; |
| 112 | +} |
| 113 | + |
| 114 | +function toolSearchArguments(value: unknown): Record<string, unknown> { |
| 115 | + if (isPlainObject(value)) return value; |
| 116 | + if (typeof value !== "string" || value.length === 0) return {}; |
| 117 | + try { |
| 118 | + const parsed: unknown = JSON.parse(value); |
| 119 | + return isPlainObject(parsed) ? parsed : {}; |
| 120 | + } catch { |
| 121 | + return {}; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export function restoreRoutedToolSearchCalls( |
| 126 | + value: unknown, |
| 127 | + names: ReadonlySet<string>, |
| 128 | +): { value: unknown; changed: boolean } { |
| 129 | + if (Array.isArray(value)) { |
| 130 | + let changed = false; |
| 131 | + const restored = value.map(entry => { |
| 132 | + const result = restoreRoutedToolSearchCalls(entry, names); |
| 133 | + changed ||= result.changed; |
| 134 | + return result.value; |
| 135 | + }); |
| 136 | + return changed ? { value: restored, changed: true } : { value, changed: false }; |
| 137 | + } |
| 138 | + if (!isPlainObject(value)) return { value, changed: false }; |
| 139 | + |
| 140 | + let changed = false; |
| 141 | + const restored: Record<string, unknown> = {}; |
| 142 | + for (const [key, entry] of Object.entries(value)) { |
| 143 | + const result = restoreRoutedToolSearchCalls(entry, names); |
| 144 | + restored[key] = result.value; |
| 145 | + changed ||= result.changed; |
| 146 | + } |
| 147 | + |
| 148 | + if (value.type === "function_call" && typeof value.name === "string" && names.has(value.name)) { |
| 149 | + restored.type = "tool_search_call"; |
| 150 | + restored.id = toolSearchItemId(value.id); |
| 151 | + restored.execution = "client"; |
| 152 | + restored.arguments = toolSearchArguments(value.arguments); |
| 153 | + delete restored.name; |
| 154 | + changed = true; |
| 155 | + } |
| 156 | + return changed ? { value: restored, changed: true } : { value, changed: false }; |
| 157 | +} |
| 158 | + |
| 159 | +export function restoreRoutedToolSearchCallsInJson( |
| 160 | + text: string, |
| 161 | + names: ReadonlySet<string>, |
| 162 | +): string { |
| 163 | + if (names.size === 0) return text; |
| 164 | + let payload: unknown; |
| 165 | + try { |
| 166 | + payload = JSON.parse(text); |
| 167 | + } catch { |
| 168 | + return text; |
| 169 | + } |
| 170 | + const restored = restoreRoutedToolSearchCalls(payload, names); |
| 171 | + return restored.changed ? JSON.stringify(restored.value) : text; |
| 172 | +} |
0 commit comments