|
| 1 | +import { namespacedToolName } from "../types"; |
| 2 | +import { collectResponsesToolGroups } from "./tool-groups"; |
| 3 | + |
| 4 | +export interface RoutedNamespaceToolIdentity { |
| 5 | + namespace: string; |
| 6 | + name: string; |
| 7 | +} |
| 8 | + |
| 9 | +export type RoutedNamespaceToolAliases = ReadonlyMap<string, RoutedNamespaceToolIdentity>; |
| 10 | + |
| 11 | +const BUILTIN_FUNCTIONS_NAMESPACE = "functions"; |
| 12 | + |
| 13 | +function isPlainObject(value: unknown): value is Record<string, unknown> { |
| 14 | + return !!value && typeof value === "object" && !Array.isArray(value); |
| 15 | +} |
| 16 | + |
| 17 | +function namespaceIdentity(namespace: string, name: string): string { |
| 18 | + return `${namespace}\u0000${name}`; |
| 19 | +} |
| 20 | + |
| 21 | +function namespaceToolIdentity(tool: unknown): RoutedNamespaceToolIdentity | undefined { |
| 22 | + if ( |
| 23 | + !isPlainObject(tool) |
| 24 | + || tool.type !== "namespace" |
| 25 | + || typeof tool.name !== "string" |
| 26 | + || tool.name.length === 0 |
| 27 | + || !Array.isArray(tool.tools) |
| 28 | + || tool.tools.length === 0 |
| 29 | + ) return undefined; |
| 30 | + return { namespace: tool.name, name: "" }; |
| 31 | +} |
| 32 | + |
| 33 | +function namespaceChildren(tool: unknown): Record<string, unknown>[] | undefined { |
| 34 | + const identity = namespaceToolIdentity(tool); |
| 35 | + if (!identity || !isPlainObject(tool) || !Array.isArray(tool.tools)) return undefined; |
| 36 | + const children: Record<string, unknown>[] = []; |
| 37 | + for (const child of tool.tools) { |
| 38 | + if ( |
| 39 | + !isPlainObject(child) |
| 40 | + || child.type === "namespace" |
| 41 | + || typeof child.name !== "string" |
| 42 | + || child.name.length === 0 |
| 43 | + ) return undefined; |
| 44 | + children.push(child); |
| 45 | + } |
| 46 | + return children; |
| 47 | +} |
| 48 | + |
| 49 | +function addSelector( |
| 50 | + selectors: Map<string, string | null>, |
| 51 | + selector: string, |
| 52 | + wireName: string, |
| 53 | +): void { |
| 54 | + const current = selectors.get(selector); |
| 55 | + if (current === undefined) selectors.set(selector, wireName); |
| 56 | + else if (current !== wireName) selectors.set(selector, null); |
| 57 | +} |
| 58 | + |
| 59 | +type NamespaceRewritePlan = { |
| 60 | + aliases: Map<string, RoutedNamespaceToolIdentity>; |
| 61 | + identities: Map<string, string>; |
| 62 | + selectors: Map<string, string | null>; |
| 63 | +}; |
| 64 | + |
| 65 | +function buildRewritePlan(groups: readonly unknown[][]): NamespaceRewritePlan { |
| 66 | + const aliases = new Map<string, RoutedNamespaceToolIdentity>(); |
| 67 | + const identities = new Map<string, string>(); |
| 68 | + const selectors = new Map<string, string | null>(); |
| 69 | + const directNames = new Set<string>(); |
| 70 | + |
| 71 | + for (const group of groups) { |
| 72 | + for (const tool of group) { |
| 73 | + if ( |
| 74 | + isPlainObject(tool) |
| 75 | + && tool.type !== "namespace" |
| 76 | + && typeof tool.name === "string" |
| 77 | + && tool.name.length > 0 |
| 78 | + ) directNames.add(tool.name); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const wireOwners = new Map<string, string>(); |
| 83 | + for (const name of directNames) wireOwners.set(name, `direct:${name}`); |
| 84 | + |
| 85 | + for (const group of groups) { |
| 86 | + for (const tool of group) { |
| 87 | + const parent = namespaceToolIdentity(tool); |
| 88 | + const children = namespaceChildren(tool); |
| 89 | + if (!parent || !children) continue; |
| 90 | + for (const child of children) { |
| 91 | + const childName = child.name as string; |
| 92 | + const identity = namespaceIdentity(parent.namespace, childName); |
| 93 | + const wireName = parent.namespace === BUILTIN_FUNCTIONS_NAMESPACE |
| 94 | + ? childName |
| 95 | + : namespacedToolName(parent.namespace, childName); |
| 96 | + const owner = wireOwners.get(wireName); |
| 97 | + if (owner !== undefined && owner !== identity) { |
| 98 | + throw new Error( |
| 99 | + `namespace tool wire-name collision for "${wireName}"; rename one of the colliding tools`, |
| 100 | + ); |
| 101 | + } |
| 102 | + wireOwners.set(wireName, identity); |
| 103 | + identities.set(identity, wireName); |
| 104 | + addSelector(selectors, wireName, wireName); |
| 105 | + addSelector(selectors, `${parent.namespace}.${childName}`, wireName); |
| 106 | + addSelector(selectors, childName, wireName); |
| 107 | + if (parent.namespace !== BUILTIN_FUNCTIONS_NAMESPACE) { |
| 108 | + aliases.set(wireName, { namespace: parent.namespace, name: childName }); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return { aliases, identities, selectors }; |
| 115 | +} |
| 116 | + |
| 117 | +function rewriteToolList( |
| 118 | + tools: unknown[], |
| 119 | + plan: NamespaceRewritePlan, |
| 120 | +): { tools: unknown[]; changed: boolean } { |
| 121 | + let changed = false; |
| 122 | + const rewritten: unknown[] = []; |
| 123 | + for (const tool of tools) { |
| 124 | + const parent = namespaceToolIdentity(tool); |
| 125 | + const children = namespaceChildren(tool); |
| 126 | + if (!parent || !children) { |
| 127 | + rewritten.push(tool); |
| 128 | + continue; |
| 129 | + } |
| 130 | + changed = true; |
| 131 | + for (const child of children) { |
| 132 | + const identity = namespaceIdentity(parent.namespace, child.name as string); |
| 133 | + const wireName = plan.identities.get(identity); |
| 134 | + rewritten.push(wireName && wireName !== child.name ? { ...child, name: wireName } : child); |
| 135 | + } |
| 136 | + } |
| 137 | + return changed ? { tools: rewritten, changed: true } : { tools, changed: false }; |
| 138 | +} |
| 139 | + |
| 140 | +function rewriteNamedSelector(value: unknown, plan: NamespaceRewritePlan): unknown { |
| 141 | + if (!isPlainObject(value) || typeof value.name !== "string") return value; |
| 142 | + const explicitNamespace = typeof value.namespace === "string" ? value.namespace : undefined; |
| 143 | + const wireName = explicitNamespace |
| 144 | + ? plan.identities.get(namespaceIdentity(explicitNamespace, value.name)) |
| 145 | + : plan.selectors.get(value.name) ?? undefined; |
| 146 | + if (!wireName) return value; |
| 147 | + const { namespace: _namespace, ...rest } = value; |
| 148 | + return wireName === value.name && explicitNamespace === undefined |
| 149 | + ? value |
| 150 | + : { ...rest, name: wireName }; |
| 151 | +} |
| 152 | + |
| 153 | +function rewriteToolChoice(value: unknown, plan: NamespaceRewritePlan): unknown { |
| 154 | + if (!isPlainObject(value)) return value; |
| 155 | + if ((value.type === "function" || value.type === "custom") && typeof value.name === "string") { |
| 156 | + return rewriteNamedSelector(value, plan); |
| 157 | + } |
| 158 | + if (value.type !== "allowed_tools" || !Array.isArray(value.tools)) return value; |
| 159 | + let changed = false; |
| 160 | + const tools = value.tools.map(tool => { |
| 161 | + if ( |
| 162 | + !isPlainObject(tool) |
| 163 | + || (tool.type !== "function" && tool.type !== "custom") |
| 164 | + || typeof tool.name !== "string" |
| 165 | + ) return tool; |
| 166 | + const rewritten = rewriteNamedSelector(tool, plan); |
| 167 | + changed ||= rewritten !== tool; |
| 168 | + return rewritten; |
| 169 | + }); |
| 170 | + return changed ? { ...value, tools } : value; |
| 171 | +} |
| 172 | + |
| 173 | +function rewriteInputItem(item: unknown, plan: NamespaceRewritePlan): unknown { |
| 174 | + if (!isPlainObject(item)) return item; |
| 175 | + if (item.type === "additional_tools" && Array.isArray(item.tools)) { |
| 176 | + const rewritten = rewriteToolList(item.tools, plan); |
| 177 | + return rewritten.changed ? { ...item, tools: rewritten.tools } : item; |
| 178 | + } |
| 179 | + if ( |
| 180 | + (item.type === "function_call" || item.type === "custom_tool_call") |
| 181 | + && typeof item.name === "string" |
| 182 | + ) return rewriteNamedSelector(item, plan); |
| 183 | + return item; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * Lower Codex's private Responses namespace declarations for public/third-party gateways. |
| 188 | + * |
| 189 | + * Codex 0.147 groups ordinary tools under the reserved `functions` namespace; those children |
| 190 | + * become bare top-level declarations. Other namespaces use the same collision-checked |
| 191 | + * `<namespace>__<name>` wire identity as the chat adapters. The returned request-local aliases |
| 192 | + * are the only names response restoration is allowed to expand. |
| 193 | + */ |
| 194 | +export function rewriteRoutedNamespaceToolsForUpstream(body: unknown): { |
| 195 | + body: unknown; |
| 196 | + aliases: Map<string, RoutedNamespaceToolIdentity>; |
| 197 | +} { |
| 198 | + if (!isPlainObject(body)) return { body, aliases: new Map() }; |
| 199 | + const groups = collectResponsesToolGroups(body); |
| 200 | + const plan = buildRewritePlan(groups); |
| 201 | + if (plan.identities.size === 0) return { body, aliases: plan.aliases }; |
| 202 | + |
| 203 | + let tools = body.tools; |
| 204 | + if (Array.isArray(body.tools)) tools = rewriteToolList(body.tools, plan).tools; |
| 205 | + |
| 206 | + let input = body.input; |
| 207 | + if (Array.isArray(body.input)) input = body.input.map(item => rewriteInputItem(item, plan)); |
| 208 | + |
| 209 | + const toolChoice = rewriteToolChoice(body.tool_choice, plan); |
| 210 | + return { |
| 211 | + body: { |
| 212 | + ...body, |
| 213 | + ...(tools !== body.tools ? { tools } : {}), |
| 214 | + ...(input !== body.input ? { input } : {}), |
| 215 | + ...(toolChoice !== body.tool_choice ? { tool_choice: toolChoice } : {}), |
| 216 | + }, |
| 217 | + aliases: plan.aliases, |
| 218 | + }; |
| 219 | +} |
| 220 | + |
| 221 | +export function restoreRoutedNamespaceCalls( |
| 222 | + value: unknown, |
| 223 | + aliases: RoutedNamespaceToolAliases, |
| 224 | +): { value: unknown; changed: boolean } { |
| 225 | + if (Array.isArray(value)) { |
| 226 | + let changed = false; |
| 227 | + const restored = value.map(entry => { |
| 228 | + const result = restoreRoutedNamespaceCalls(entry, aliases); |
| 229 | + changed ||= result.changed; |
| 230 | + return result.value; |
| 231 | + }); |
| 232 | + return changed ? { value: restored, changed: true } : { value, changed: false }; |
| 233 | + } |
| 234 | + if (!isPlainObject(value)) return { value, changed: false }; |
| 235 | + |
| 236 | + let changed = false; |
| 237 | + const restored: Record<string, unknown> = {}; |
| 238 | + for (const [key, entry] of Object.entries(value)) { |
| 239 | + const result = restoreRoutedNamespaceCalls(entry, aliases); |
| 240 | + restored[key] = result.value; |
| 241 | + changed ||= result.changed; |
| 242 | + } |
| 243 | + |
| 244 | + if ( |
| 245 | + (value.type === "function_call" || value.type === "custom_tool_call") |
| 246 | + && typeof value.name === "string" |
| 247 | + ) { |
| 248 | + const identity = aliases.get(value.name); |
| 249 | + if (identity) { |
| 250 | + restored.name = identity.name; |
| 251 | + restored.namespace = identity.namespace; |
| 252 | + changed = true; |
| 253 | + } |
| 254 | + } |
| 255 | + return changed ? { value: restored, changed: true } : { value, changed: false }; |
| 256 | +} |
| 257 | + |
| 258 | +export function restoreRoutedNamespaceCallsInJson( |
| 259 | + text: string, |
| 260 | + aliases: RoutedNamespaceToolAliases, |
| 261 | +): string { |
| 262 | + if (aliases.size === 0) return text; |
| 263 | + let payload: unknown; |
| 264 | + try { |
| 265 | + payload = JSON.parse(text); |
| 266 | + } catch { |
| 267 | + return text; |
| 268 | + } |
| 269 | + const restored = restoreRoutedNamespaceCalls(payload, aliases); |
| 270 | + return restored.changed ? JSON.stringify(restored.value) : text; |
| 271 | +} |
| 272 | + |
| 273 | +export function createRoutedNamespaceCallRestoreRewrite( |
| 274 | + aliases: RoutedNamespaceToolAliases, |
| 275 | +): (payload: string) => string { |
| 276 | + return payload => restoreRoutedNamespaceCallsInJson(payload, aliases); |
| 277 | +} |
0 commit comments