UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

169 lines (168 loc) • 7.77 kB
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs"; import { h as clampPositiveTimerTimeoutMs } from "./number-coercion-D6u-4YD8.mjs"; import { c as filterStringRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import "./utils-C8YECn2Q.mjs"; import { i as loadEnabledBundleMcpConfig } from "./bundle-mcp-DKz8uq8U.mjs"; import { i as prepareOwnedBundleMcpDataDirs, u as partitionMcpServersByConnectionScope } from "./bundle-mcp-config-Ca9TXxki.mjs"; import { n as normalizeBundleMcpServerConfig, t as decodeHeaderEnvPlaceholder } from "./bundle-mcp-adapter-CHYAtbJj.mjs"; import { a as shouldCreateBundleMcpRuntimeForAttempt } from "./attempt-tool-construction-plan-Cp8YcsYs.mjs"; import { i as resolveProjectedMcpCodexToolApprovalMode } from "./mcp-codex-tool-approval-C6Ex4SQ6.mjs"; import crypto from "node:crypto"; //#region src/agents/codex-mcp-config.ts /** * Projects enabled bundle MCP servers into Codex app-server thread config. * The projection keeps loopback approval defaults and header env placeholders * compatible with Codex's MCP config shape. */ function assertCodexExactToolFilters(e, t, n) { const r = n.find((e) => e.includes("*")); if (!r) return; throw new Error(`Cannot project mcp.servers.${e}.toolFilter.${t} pattern "${r}" into Codex ${t === "include" ? "enabled_tools" : "disabled_tools"}: Codex MCP projection only supports exact tool names.`); } function applyCodexToolFilter(e, t, n) { if (!isRecord(n.toolFilter)) return; const r = normalizeTrimmedStringList(n.toolFilter.include); const i = normalizeTrimmedStringList(n.toolFilter.exclude); assertCodexExactToolFilters(t, "include", r); assertCodexExactToolFilters(t, "exclude", i); if (r.length > 0) e.enabled_tools = r; if (i.length > 0) e.disabled_tools = i; } /** Adds exact session denials to a server's configured filter before Codex projection. */ function applyCodexSessionMcpToolDenials(e, t, n) { const r = n?.mcpToolsDeny; const i = r && Object.hasOwn(r, e) ? r[e] : void 0; if (!i?.length) return t; const a = isRecord(t.toolFilter) ? t.toolFilter : {}; const o = normalizeTrimmedStringList(a.exclude); return { ...t, toolFilter: { ...a, exclude: [.../* @__PURE__ */ new Set([...o, ...i])].toSorted() } }; } /** Normalizes one bundle MCP server into Codex's mcp_servers shape. */ function normalizeCodexMcpServerConfig(e, t, n = []) { const r = normalizeBundleMcpServerConfig(t); const i = clampPositiveTimerTimeoutMs(t.connectionTimeoutMs); const a = clampPositiveTimerTimeoutMs(t.requestTimeoutMs); if (i !== void 0) r.startup_timeout_sec = i / 1e3; if (a !== void 0) r.tool_timeout_sec = a / 1e3; if (typeof t.supportsParallelToolCalls === "boolean") r.supports_parallel_tool_calls = t.supportsParallelToolCalls; applyCodexToolFilter(r, e, t); const o = resolveProjectedMcpCodexToolApprovalMode(e, t); if (o) r.default_tools_approval_mode = o; if (o === void 0 || o === "auto") { const t = n.filter((t) => t.server === e).map((e) => [e.tool, { approval_mode: "approve" }]).toSorted(([e], [t]) => e.localeCompare(t)); if (t.length > 0) r.tools = Object.fromEntries(t); } const s = filterStringRecord(t.headers); if (s) { const e = {}; const t = {}; for (const [n, i] of Object.entries(s)) { const a = decodeHeaderEnvPlaceholder(i); if (!a) { e[n] = i; continue; } if (a.bearer && normalizeOptionalLowercaseString(n) === "authorization") { r.bearer_token_env_var = a.envVar; continue; } t[n] = a.envVar; } if (Object.keys(e).length > 0) r.http_headers = e; if (Object.keys(t).length > 0) r.env_http_headers = t; } return r; } /** * Build Codex `mcp_servers` config from normalized bundle MCP config. * Requester-scoped servers are excluded: harness-native MCP clients are * session-shared and must never dial placeholder or requester-bound URLs. */ function buildCodexMcpServersConfig(e) { const { staticServers: t } = partitionMcpServersByConnectionScope(e.mcpServers); return Object.fromEntries(Object.entries(t).map(([e, t]) => [e, normalizeCodexMcpServerConfig(e, t)])); } /** Side questions need bundle policy without provisioning transports or plugin data directories. */ function loadCodexBundleMcpApprovalConfig(e) { const { config: t } = loadEnabledBundleMcpConfig(e); const n = e.cfg?.mcp?.servers ?? {}; const r = selectCodexBundleMcpConfig({ mcpServers: { ...t.mcpServers, ...n } }, n, e.toolOverrides); const { staticServers: i } = partitionMcpServersByConnectionScope(r.mcpServers); return Object.fromEntries(Object.keys(i).map((e) => [e, { default_tools_approval_mode: resolveProjectedMcpCodexToolApprovalMode(e, n[e] ?? {}) ?? resolveProjectedMcpCodexToolApprovalMode(e, t.mcpServers[e] ?? {}) }])); } function selectCodexBundleMcpConfig(e, t, n) { const r = n?.mcpServers; return { mcpServers: Object.fromEntries(Object.entries(e.mcpServers).filter(([e]) => { const n = r && Object.hasOwn(r, e) ? r[e] : void 0; return n !== false && (n === true || t[e]?.enabled !== false); }).map(([e, t]) => [e, applyCodexSessionMcpToolDenials(e, t, n)])) }; } function stableJsonValue(e) { if (Array.isArray(e)) return e.map(stableJsonValue); if (!e || typeof e !== "object") return e; return Object.fromEntries(Object.entries(e).toSorted(([e], [t]) => e.localeCompare(t)).map(([e, t]) => [e, stableJsonValue(t)])); } function fingerprintCodexMcpServersConfig(e) { return crypto.createHash("sha256").update(JSON.stringify(stableJsonValue(e))).digest("hex"); } /** Load bundle MCP config for one Codex app-server thread. */ async function loadCodexBundleMcpThreadConfigCore(e) { if (!shouldCreateBundleMcpRuntimeForAttempt({ toolsEnabled: e.toolsEnabled ?? true, disableTools: e.disableTools, toolsAllow: e.toolsAllow })) return { diagnostics: [], evaluated: true, staticServerNames: [], userStaticServerNames: [] }; const t = loadEnabledBundleMcpConfig({ workspaceDir: e.workspaceDir, cfg: e.cfg, manifestRegistry: e.manifestRegistry }); const n = e.cfg?.mcp?.servers ?? {}; const r = e.toolOverrides?.mcpServers; const i = selectCodexBundleMcpConfig(t.config, n, e.toolOverrides); const a = Object.fromEntries(Object.entries(n).filter(([e, t]) => { const n = r && Object.hasOwn(r, e) ? r[e] : void 0; return n !== false && (n === true || t.enabled !== false); })); const { staticServers: o } = partitionMcpServersByConnectionScope({ ...i.mcpServers, ...a }); const { staticServers: s } = partitionMcpServersByConnectionScope(a); const c = Object.keys(o).toSorted((e, t) => e.localeCompare(t)); const l = Object.keys(s).toSorted((e, t) => e.localeCompare(t)); if (e.preparationOnly && Object.keys(t.prepareDataDirsByServer ?? {}).length) throw new Error("Native fork preparation cannot provision plugin data directories. Complete plugin setup before retrying."); const u = { mcpServers: Object.fromEntries(Object.entries(i.mcpServers).filter(([e]) => !Object.hasOwn(a, e))) }; const d = await prepareOwnedBundleMcpDataDirs({ config: u, prepareDataDirsByServer: t.prepareDataDirsByServer ?? {} }); const f = [...t.diagnostics, ...d.diagnostics]; const p = buildCodexMcpServersConfig(d.config); const m = Object.keys(p).length > 0; return { ...m ? { configPatch: { mcp_servers: p } } : {}, diagnostics: f, evaluated: true, ...m ? { fingerprint: fingerprintCodexMcpServersConfig(p) } : {}, staticServerNames: c, userStaticServerNames: l }; } //#endregion export { normalizeCodexMcpServerConfig as a, loadCodexBundleMcpThreadConfigCore as i, buildCodexMcpServersConfig as n, loadCodexBundleMcpApprovalConfig as r, applyCodexSessionMcpToolDenials as t };