UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1,191 lines (1,190 loc) • 44 kB
import { a as normalizeFastMode, c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord, s as ensureRecord } from "./record-coerce-CVPuDqcc.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs"; import { t as AGENT_MODEL_CONFIG_KEYS } from "./configured-model-refs-DtaDJ1Vw.mjs"; import { a as normalizeOptionalAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs"; import { r as resolveModelRuntimePolicy } from "./model-runtime-policy-Cfudvy7O.mjs"; import { a as resolveModelExtraParamSources, n as isAgentRuntimeModelParam } from "./model-extra-params-D2Glrj-b.mjs"; import { t as listMutableCodexRouteAgentEntries } from "./codex-route-agent-entries-LOZntEn-.mjs"; import { d as parseCodexRouteModelRef, f as readAgentPrimaryModelRef, g as toCanonicalOpenAIModelRef, h as resolveRuntimeModelRef, i as isBlockedLegacyCodexModelRef, l as modelRefUsesCodexRuntime, m as resolveImplicitDefaultAgentModelRef, n as canonicalOpenAIModelUsesCodexRuntime, o as isOpenAICodexModelRef, p as readModelConfigPrimaryRef, t as agentUsesCodexRuntimeForCompaction, u as normalizeDefaultProviderModelRef } from "./codex-route-model-ref-BdQcEGbo.mjs"; import { i as rewriteKnownModelRefs } from "./legacy-config-migrations.runtime.models.refs-CDWmXREu.mjs"; import { r as collectBlockedLegacyOpenAICodexProviderPlan } from "./legacy-config-migrations.runtime.models-DVGtmnPu.mjs"; import { n as detectWindowsSpawnCommandInlineArgs } from "./windows-spawn-BcHMKZuG.mjs"; import { _ as visitChannelModelSlots, c as collectModelConfigSlot, d as modelConfigContainsRef, f as recordCodexModelHit, g as rewriteStringModelSlot, h as rewriteModelsMap, l as collectStringModelConfigRef, o as collectCodexRuntimeModelPolicyRefs, p as rewriteModelConfigSlot, s as collectModelConfigRefs, u as collectStringModelSlot, v as visitNonAgentModelSlots } from "./retired-model-ref-repair-B9RO1-mz.mjs"; import { t as migrateLegacyRuntimeModelRef } from "./legacy-runtime-model-providers-BEx2pev8.mjs"; import "./codex-route-session-repair-W0qtGs4p.mjs"; //#region src/commands/doctor/shared/codex-route-compaction-scan.ts const UNSUPPORTED_CODEX_COMPACTION_KEYS = ["provider"]; const LOSSLESS_CONTEXT_ENGINE_ID = "lossless-claw"; function readCompactionSlot(e, t) { const n = asOptionalRecord(asOptionalRecord(e.agent)?.compaction)?.[t]; const r = typeof n === "string" && n.trim(); const i = `${e.path}.compaction`; return { key: t, value: r ? n : asOptionalRecord(e.inheritedCompaction)?.[t], path: `${r ? i : e.inheritedCompactionPath ?? i}.${t}` }; } function collectUnsupportedCodexCompactionOverridesForAgent(e) { const t = asOptionalRecord(e.agent); const n = asOptionalRecord(t?.compaction); const r = asOptionalRecord(e.inheritedCompaction); const i = n?.provider ?? r?.provider; if (normalizeOptionalLowercaseString(i) === "lossless-claw") return []; return UNSUPPORTED_CODEX_COMPACTION_KEYS.map((t) => readCompactionSlot(e, t)).flatMap(({ key: e, path: t, value: n }) => typeof n === "string" && n.trim() ? [{ path: t, key: e, value: n.trim() }] : []); } function collectLegacyLosslessCompactionForAgent(e) { const t = readCompactionSlot(e, "provider"); if (normalizeOptionalLowercaseString(t.value) !== "lossless-claw") return []; const n = readCompactionSlot(e, "model"); return [{ providerPath: t.path, providerValue: String(t.value).trim(), ...typeof n.value === "string" && n.value.trim() ? { modelPath: n.path, modelValue: n.value.trim() } : {} }]; } function collectCompactionConfigs(e, t) { const collectForAgent = (e) => agentUsesCodexRuntimeForCompaction({ ...e, agent: asOptionalRecord(e.agent) }) ? t(e) : []; const n = e.cfg.agents?.defaults; const r = readAgentPrimaryModelRef(n); const i = asOptionalRecord(n?.compaction); const a = collectForAgent({ cfg: e.cfg, agent: n, path: "agents.defaults", env: e.env }); for (const { agent: t, agentId: n, path: o } of listMutableCodexRouteAgentEntries(e.cfg)) a.push(...collectForAgent({ cfg: e.cfg, agent: t, path: o, agentId: n, inheritedModelRef: r, inheritedCompaction: i, inheritedCompactionPath: "agents.defaults.compaction", env: e.env })); return a; } function collectLegacyLosslessCompactionConfigs(e) { return dedupeByKey(collectCompactionConfigs(e, collectLegacyLosslessCompactionForAgent), (e) => `${e.providerPath}\0${e.providerValue}\0${e.modelPath ?? ""}\0${e.modelValue ?? ""}`); } function collectUnsupportedCodexCompactionOverrides(e) { return dedupeByKey(collectCompactionConfigs(e, collectUnsupportedCodexCompactionOverridesForAgent), (e) => `${e.path}\0${e.key}\0${e.value}`); } function getSharedDefaultCompactionOverrideConsumers(e) { const t = { model: false, provider: false }; const n = e.cfg.agents?.defaults; const r = asOptionalRecord(n?.compaction); if (!r) return t; const i = typeof r.model === "string" && r.model.trim(); const a = typeof r.provider === "string" && r.provider.trim(); if (!i && !a) return t; const o = readAgentPrimaryModelRef(n); if (!agentUsesCodexRuntimeForCompaction({ cfg: e.cfg, agent: n, env: e.env })) return { model: Boolean(i), provider: Boolean(a) }; for (const { agent: n, agentId: r } of listMutableCodexRouteAgentEntries(e.cfg)) { const s = asOptionalRecord(n.compaction); const c = Boolean(i) && !(typeof s?.model === "string" && s.model.trim()); const l = Boolean(a) && !(typeof s?.provider === "string" && s.provider.trim()); if (!c && !l) continue; if (!agentUsesCodexRuntimeForCompaction({ cfg: e.cfg, agent: n, agentId: r, inheritedModelRef: o, env: e.env })) { t.model ||= c; t.provider ||= l; if ((!i || t.model) && (!a || t.provider)) break; } } return t; } function sharedDefaultLosslessCompactionHasNonCodexConsumer(e) { const t = e.cfg.agents?.defaults; const n = asOptionalRecord(t?.compaction); const r = normalizeOptionalLowercaseString(n?.provider) === LOSSLESS_CONTEXT_ENGINE_ID; const i = typeof n?.model === "string" && n.model.trim(); if (!r && !i) return false; const a = getSharedDefaultCompactionOverrideConsumers(e); return r && a.provider || Boolean(i) && a.model; } function legacyLosslessSummaryModels(e) { const t = /* @__PURE__ */ new Set(); for (const n of e) { if (!n.modelValue) continue; t.add(toCanonicalOpenAIModelRef(n.modelValue) ?? normalizeDefaultProviderModelRef(n.modelValue)); } return [...t]; } function canAutoMigrateLegacyLosslessCompaction(e) { if (e.contextEngine && e.contextEngine !== "lossless-claw") return false; const t = legacyLosslessSummaryModels(e.hits); if (e.hits.some((e) => !e.modelValue) && (t.length > 0 || e.summaryModel)) return false; if (t.length === 0) return true; if (e.summaryModel) return t.every((t) => t === e.summaryModel); return t.length === 1; } function readLosslessSummaryModel(e) { const t = asOptionalRecord(e?.entries); const n = asOptionalRecord(t?.[LOSSLESS_CONTEXT_ENGINE_ID]); const r = asOptionalRecord(n?.config); return typeof r?.summaryModel === "string" && r.summaryModel.trim() ? r.summaryModel.trim() : void 0; } //#endregion //#region src/commands/doctor/shared/codex-route-runtime-policy.ts function agentExplicitlyReferencesCanonicalModel(e, t) { const n = asOptionalRecord(e); if (!n) return false; for (const e of AGENT_MODEL_CONFIG_KEYS) if (modelConfigContainsRef(n[e], t)) return true; if (modelConfigContainsRef(asOptionalRecord(n.heartbeat)?.model, t)) return true; if (modelConfigContainsRef(asOptionalRecord(n.subagents)?.model, t)) return true; const r = asOptionalRecord(n.compaction); return modelConfigContainsRef(r?.model, t) || asOptionalRecord(n.models)?.[t] !== void 0; } function resolveCurrentRuntimeIdForCanonicalModel(e) { const t = parseCodexRouteModelRef(e.modelRef); if (!t) return "auto"; const n = normalizeOptionalAgentRuntimeId(resolveModelRuntimePolicy({ config: e.cfg, provider: t.provider, modelId: t.modelId, agentId: e.agentId }).policy?.id); if (n) return n; return canonicalOpenAIModelUsesCodexRuntime(e) ? "codex" : "auto"; } function setModelRuntimePolicy(e) { const t = ensureRecord(e.agent, "models"); const n = ensureRecord(t, e.modelRef); const r = asOptionalRecord(n.agentRuntime); if (normalizeOptionalLowercaseString(r?.id) === e.runtimeId) return; n.agentRuntime = { ...r, id: e.runtimeId }; e.changes.push(`Set ${e.agentPath}.models.${e.modelRef}.agentRuntime.id to "${e.runtimeId}" ${e.reason}.`); } function shieldExplicitListedAgentRefsFromDefaultPolicy(e) { for (const { agent: t, agentId: n, path: r } of listMutableCodexRouteAgentEntries(e.cfg)) { if (!agentExplicitlyReferencesCanonicalModel(t, e.modelRef)) continue; const i = resolveCurrentRuntimeIdForCanonicalModel({ ...e, agentId: n }); if (i === e.targetRuntimeId) continue; setModelRuntimePolicy({ ...e, agent: t, agentPath: r, runtimeId: i, reason: "so default runtime repair does not change explicit agent routing" }); } } function legacyEntryExplicitNonDefaultRuntimeId(e, t) { for (const n of Object.keys(e)) { if (n === t || toCanonicalOpenAIModelRef(n) !== t) continue; const r = asOptionalRecord(e[n]); const i = normalizeOptionalLowercaseString(asOptionalRecord(r?.agentRuntime)?.id); if (i && i !== "auto" && i !== "default") return i; } } function agentIdFromAgentPath(e) { for (const t of ["agents.entries.", "agents.list."]) if (e.startsWith(t)) return e.slice(t.length); } function modelIdMatchesProviderModelEntry(e) { if (typeof e.entryId !== "string") return false; const t = e.entryId.trim(); if (t === e.modelId) return true; const n = t.indexOf("/"); return n > 0 && normalizeProviderId(t.slice(0, n)) === normalizeProviderId(e.provider) && t.slice(n + 1).trim() === e.modelId; } function providerModelExplicitNonDefaultRuntimeId(e) { const t = asOptionalRecord(asOptionalRecord(e.cfg.models)?.providers); for (const [n, r] of Object.entries(t ?? {})) { if (normalizeProviderId(n) !== normalizeProviderId(e.provider)) continue; const t = asOptionalRecord(r)?.models; if (!Array.isArray(t)) continue; for (const n of t) { const t = asOptionalRecord(n); if (!modelIdMatchesProviderModelEntry({ entryId: t?.id, provider: e.provider, modelId: e.modelId })) continue; const r = normalizeOptionalAgentRuntimeId(asOptionalRecord(t?.agentRuntime)?.id); if (r && r !== "auto" && r !== "default" && r !== "codex") return r; } } } function agentModelMapExactRuntimeId(e) { const t = normalizeAgentId(e.agentId); const n = t ? listMutableCodexRouteAgentEntries(e.cfg).find((e) => e.agentId === t)?.agent : void 0; const r = [asOptionalRecord(n?.models), asOptionalRecord(e.cfg.agents?.defaults?.models)]; for (const t of r) for (const [n, r] of Object.entries(t ?? {})) { if (!modelIdMatchesProviderModelEntry({ entryId: n, provider: e.provider, modelId: e.modelId })) continue; const t = normalizeOptionalAgentRuntimeId(asOptionalRecord(asOptionalRecord(r)?.agentRuntime)?.id); if (t && t !== "auto" && t !== "default") return t; } } function preRepairLegacyModelPolicyExplicitNonDefaultRuntimePin(e) { if (!e.legacyModelRef || !isOpenAICodexModelRef(e.legacyModelRef)) return; const t = parseCodexRouteModelRef(e.legacyModelRef); if (!t) return; const n = resolveModelRuntimePolicy({ config: e.cfg, provider: t.provider, modelId: t.modelId, agentId: e.agentId }); const r = normalizeOptionalAgentRuntimeId(n.policy?.id); if (!r || r === "auto" || r === "default" || r === "codex") return; if (n.source === "model") { const n = providerModelExplicitNonDefaultRuntimeId({ cfg: e.cfg, provider: t.provider, modelId: t.modelId }); const i = agentModelMapExactRuntimeId({ ...e, ...t }); if (n === r && !i) return { runtimeId: r, source: "provider-model" }; } return { runtimeId: r, source: n.source ?? "model" }; } function ensureCodexRuntimePolicy(e) { const t = asOptionalRecord(e.agent.models); const n = asOptionalRecord(t?.[e.modelRef]); const r = asOptionalRecord(n?.agentRuntime); const i = normalizeOptionalLowercaseString(r?.id); const a = i && i !== "auto" && i !== "default" ? i : void 0; const o = t ? legacyEntryExplicitNonDefaultRuntimeId(t, e.modelRef) : void 0; const s = preRepairLegacyModelPolicyExplicitNonDefaultRuntimePin({ cfg: e.preRepairCfg ?? e.cfg, legacyModelRef: e.legacyModelRef, agentId: e.agentId }); const c = a ?? o ?? (s?.source === "provider" || s?.source === "provider-model" ? s.runtimeId : void 0) ?? "codex"; if (e.agentPath === "agents.defaults") shieldExplicitListedAgentRefsFromDefaultPolicy({ ...e, targetRuntimeId: c }); if (a || o || s?.source === "model") return; setModelRuntimePolicy({ ...e, runtimeId: s?.runtimeId ?? "codex", reason: s ? "so legacy provider runtime pins survive Codex route repair" : "so repaired OpenAI refs keep Codex auth routing" }); } function rewriteStringModelSlotIfCanonicalCodexRuntime(e) { if (typeof e.container?.[e.key] === "string") rewriteModelConfigSlotIfCanonicalCodexRuntime(e); } function rewriteModelConfigSlotIfCanonicalCodexRuntime(e) { const rewrite = (t, n, r = false) => { const i = toCanonicalOpenAIModelRef(t.trim()); if (!i || r && isBlockedLegacyCodexModelRef({ modelRef: t, blockedModelIdentities: e.blockedModelIdentities }) || !canonicalOpenAIModelUsesCodexRuntime({ ...e, modelRef: i })) return t; return recordCodexModelHit({ ...e, path: n, model: t.trim() }) ?? t; }; const { container: t, key: n, path: r } = e; const i = t?.[n]; if (t && typeof i === "string") { t[n] = rewrite(i, r); return; } const a = asOptionalRecord(i); if (!a) return; if (typeof a.primary === "string") a.primary = rewrite(a.primary, `${r}.primary`); if (Array.isArray(a.fallbacks)) { for (const [e, t] of a.fallbacks.entries()) if (typeof t === "string") a.fallbacks[e] = rewrite(t, `${r}.fallbacks.${e}`, true); } } //#endregion //#region src/commands/doctor/shared/codex-route-config-repair.ts function rewriteModelPolicyAllowRefs(e) { const t = asOptionalRecord(e.agent.modelPolicy); if (!Array.isArray(t?.allow)) return; t.allow = t.allow.map((t, n) => { if (typeof t !== "string") return t; return recordCodexModelHit({ hits: e.hits, path: `${e.path}.modelPolicy.allow.${n}`, model: t.trim(), blockedModelIdentities: e.blockedModelIdentities }) ?? t; }); } function rewriteAgentModelRefs(e) { if (!e.agent) return; const t = { ...e, agent: e.agent }; for (const n of AGENT_MODEL_CONFIG_KEYS) { const r = e.hits.length; (n === "model" ? rewriteModelConfigSlot : rewriteModelConfigSlotIfCanonicalCodexRuntime)({ ...t, container: e.agent, key: n, path: `${e.path}.${n}` }); if (n === "model") preserveCodexRuntimePolicyForHits(t, r); } rewriteStringModelSlotIfCanonicalCodexRuntime({ ...t, container: asOptionalRecord(e.agent.heartbeat), key: "model", path: `${e.path}.heartbeat.model` }); rewriteModelConfigSlotIfCanonicalCodexRuntime({ ...t, container: asOptionalRecord(e.agent.subagents), key: "model", path: `${e.path}.subagents.model` }); rewriteAgentCompactionRefs(t); const n = asOptionalRecord(e.agent.mediaModels); for (const r of [ "image", "video", "music" ]) rewriteModelConfigSlot({ ...t, container: n ?? {}, key: r, path: `${e.path}.mediaModels.${r}` }); const r = e.hits.length; rewriteModelPolicyAllowRefs(t); preserveCodexRuntimePolicyForHits(t, r); const i = e.hits.length; rewriteModelsMap({ ...t, models: asOptionalRecord(e.agent.models), path: `${e.path}.models` }); preserveCodexRuntimePolicyForHits(t, i); } function rewriteConfigModelRefs(e) { const t = getSharedDefaultCompactionOverrideConsumers(e); const n = structuredClone(e.cfg); const r = []; const i = []; const a = maybeMigrateLegacyLosslessCompactionConfig({ cfg: n, env: e.env }); const o = new Set(collectLegacyLosslessCompactionConfigs({ cfg: n, env: e.env }).flatMap((e) => e.modelPath ? [e.providerPath, e.modelPath] : [e.providerPath])); const s = /* @__PURE__ */ new Map(); const c = { ...e, cfg: n, preRepairCfg: e.cfg, hits: r, preserveUnsupportedCompactionPaths: o, rewrittenInheritedCompactionModels: s, runtimePolicyChanges: i, unsupportedCompactionChanges: a }; rewriteAgentModelRefs({ ...c, agent: asOptionalRecord(n.agents?.defaults), path: "agents.defaults", preserveUnsupportedCompactionOverrides: t }); const l = readAgentPrimaryModelRef(n.agents?.defaults); const u = listMutableCodexRouteAgentEntries(n); for (const { agent: e, agentId: t, path: r } of u) rewriteAgentModelRefs({ ...c, agent: e, path: r, agentId: t, inheritedModelRef: l, inheritedCompaction: n.agents?.defaults?.compaction, inheritedCompactionPath: "agents.defaults.compaction" }); visitNonAgentModelSlots(n, (t) => { rewriteStringModelSlotIfCanonicalCodexRuntime({ ...e, cfg: n, hits: r, ...t }); }); return { cfg: r.length > 0 || i.length > 0 || a.length > 0 ? n : e.cfg, changes: r, runtimePolicyChanges: i, unsupportedCompactionChanges: a }; } function rewriteAgentCompactionRefs(e) { const t = asOptionalRecord(e.agent.compaction); const n = asOptionalRecord(e.inheritedCompaction); if (!agentUsesCodexRuntimeForCompaction(e)) rewriteStringModelSlotIfCanonicalCodexRuntime({ ...e, container: t, key: "model", path: `${e.path}.compaction.model` }); else if (normalizeOptionalLowercaseString(t?.provider ?? n?.provider) === "lossless-claw") rewriteLosslessCompactionModel(e, t, n); else { removeUnsupportedCodexCompactionOverrides({ agent: e.agent, compaction: t, path: e.path, preserve: e.preserveUnsupportedCompactionOverrides, preservePaths: e.preserveUnsupportedCompactionPaths, changes: e.unsupportedCompactionChanges }); rewriteStringModelSlot({ ...e, container: t, key: "model", path: `${e.path}.compaction.model` }); } } function rewriteLosslessCompactionModel(e, t, n) { const r = e.hits.length; rewriteStringModelSlot({ ...e, container: t, key: "model", path: `${e.path}.compaction.model` }); preserveCodexRuntimePolicyForHits(e, r); const i = typeof t?.model === "string" ? t.model.trim() : ""; const a = e.inheritedCompactionPath ? `${e.inheritedCompactionPath}.model` : void 0; if (i || !a || !e.preserveUnsupportedCompactionPaths?.has(a)) return; const o = e.hits.length; rewriteStringModelSlot({ ...e, container: n, key: "model", path: a }); const s = e.hits[o]; const c = s?.canonicalModel ?? e.rewrittenInheritedCompactionModels?.get(a); if (s) { e.rewrittenInheritedCompactionModels?.set(a, s.canonicalModel); preserveCodexRuntimePolicyForHits(e, o); } else if (c) ensureCodexRuntimePolicy({ ...e, agentPath: e.path, modelRef: c, changes: e.runtimePolicyChanges }); } function preserveCodexRuntimePolicyForHits(e, t) { for (const n of e.hits.slice(t)) ensureCodexRuntimePolicy({ ...e, agentPath: e.path, modelRef: n.canonicalModel, legacyModelRef: n.model, changes: e.runtimePolicyChanges }); } function removeUnsupportedCodexCompactionOverrides(e) { if (!e.compaction) return; if (normalizeOptionalLowercaseString(e.compaction.provider) === "lossless-claw") return; for (const t of UNSUPPORTED_CODEX_COMPACTION_KEYS) { const n = `${e.path}.compaction.${t}`; if (e.preservePaths?.has(n) || e.preserve?.[t]) continue; const r = e.compaction[t]; if (typeof r !== "string" || !r.trim()) continue; delete e.compaction[t]; e.changes.push(`Removed ${n}; Codex runtime uses native server-side compaction.`); } if (Object.keys(e.compaction).length === 0) delete e.agent.compaction; } function maybeMigrateLegacyLosslessCompactionConfig(e) { const t = e.cfg; const n = collectLegacyLosslessCompactionConfigs(e); if (n.length === 0) return []; const r = asOptionalRecord(t.plugins); const i = asOptionalRecord(r?.slots); const a = typeof i?.contextEngine === "string" && i.contextEngine.trim() ? i.contextEngine.trim() : void 0; const o = readLosslessSummaryModel(r); const s = normalizeOptionalLowercaseString(a); if (sharedDefaultLosslessCompactionHasNonCodexConsumer(e) || !canAutoMigrateLegacyLosslessCompaction({ hits: n, contextEngine: s, summaryModel: o })) return []; const c = ensureRecord(t, "plugins"); const l = ensureRecord(c, "slots"); const u = ensureRecord(c, "entries"); const d = ensureRecord(u, LOSSLESS_CONTEXT_ENGINE_ID); const f = ensureRecord(d, "config"); const p = []; if (l.contextEngine !== "lossless-claw") { l.contextEngine = LOSSLESS_CONTEXT_ENGINE_ID; p.push(`Set plugins.slots.contextEngine to "${LOSSLESS_CONTEXT_ENGINE_ID}" for legacy Lossless compaction config.`); } if (d.enabled !== true) { d.enabled = true; p.push(`Enabled plugins.entries.${LOSSLESS_CONTEXT_ENGINE_ID}.`); } let m = o; const h = legacyLosslessSummaryModels(n)[0]; if (!m && h) { m = h; f.summaryModel = m; p.push(`Moved ${n.find((e) => e.modelValue)?.modelPath ?? "legacy compaction model"} to plugins.entries.${LOSSLESS_CONTEXT_ENGINE_ID}.config.summaryModel.`); } ensureLosslessLlmPolicy({ entry: d, summaryModel: m, changes: p }); preserveMigratedLosslessCodexRuntimePolicy({ ...e, hits: n, summaryModel: m, changes: p }); for (const t of n) for (const n of ["provider", "model"]) { const r = n === "provider" ? t.providerPath : t.modelPath; if (!r) continue; const i = readCompactionOwnerForPath(e.cfg, readCompactionOwnerPathForKeyPath(r)); const a = asOptionalRecord(i?.compaction); const o = a?.[n]; if (!i || !a || typeof o !== "string" || !o.trim()) continue; delete a[n]; p.push(n === "provider" ? `Removed ${r}; Lossless now runs through plugins.slots.contextEngine.` : `Removed ${r} after migrating the Lossless summary model.`); if (Object.keys(a).length === 0) delete i.compaction; } return p; } function preserveMigratedLosslessCodexRuntimePolicy(e) { if (!e.summaryModel) return; const t = /* @__PURE__ */ new Set(); for (const n of e.hits) { if (!n.modelValue || !isOpenAICodexModelRef(n.modelValue)) continue; if (toCanonicalOpenAIModelRef(n.modelValue) !== e.summaryModel) continue; const r = readCompactionOwnerPathForKeyPath(n.modelPath ?? n.providerPath); if (t.has(r)) continue; const i = readCompactionOwnerForPath(e.cfg, r); if (!i) continue; t.add(r); ensureCodexRuntimePolicy({ ...e, agent: i, agentPath: r, agentId: agentIdFromAgentPath(r), modelRef: e.summaryModel }); } } function ensureLosslessLlmPolicy(e) { if (!e.summaryModel) return; const t = ensureRecord(e.entry, "llm"); if (t.allowModelOverride !== true) { t.allowModelOverride = true; e.changes.push(`Set plugins.entries.${LOSSLESS_CONTEXT_ENGINE_ID}.llm.allowModelOverride to true for Lossless summary model overrides.`); } const n = Array.isArray(t.allowedModels) ? [...t.allowedModels] : []; if (!n.includes(e.summaryModel)) { n.push(e.summaryModel); t.allowedModels = n; e.changes.push(`Added ${e.summaryModel} to plugins.entries.${LOSSLESS_CONTEXT_ENGINE_ID}.llm.allowedModels.`); } } function readCompactionOwnerForPath(e, t) { if (t === "agents.defaults") return asOptionalRecord(e.agents?.defaults); if (!t.startsWith("agents.list.")) return readMutablePath(e, t); const n = t.slice(12); const r = e.agents?.list ?? []; return asOptionalRecord(r.find((e) => e.id === n)) ?? asOptionalRecord(Number.isInteger(Number(n)) ? r[Number(n)] : void 0); } function readMutablePath(e, t) { let n = e; for (const e of t.split(".")) { const t = asOptionalRecord(n); if (!t) return; n = t[e]; } return asOptionalRecord(n); } function readCompactionOwnerPathForKeyPath(e) { return e.replace(/\.(model|provider)$/, "").replace(/\.compaction$/, ""); } //#endregion //#region src/commands/doctor/shared/codex-route-config-scan.ts function collectModelsMapRefs(e) { const t = asOptionalRecord(e.models); if (!t) return; for (const n of Object.keys(t)) recordCodexModelHit({ ...e, path: `${e.path}.${n}`, model: n }); } function collectModelPolicyAllowRefs(e) { const t = asOptionalRecord(e.modelPolicy)?.allow; if (!Array.isArray(t)) return; for (const [n, r] of t.entries()) collectStringModelSlot({ ...e, path: `${e.path}.allow.${n}`, value: r }); } function collectAgentModelRefs(e) { const t = asOptionalRecord(e.agent); if (!t) return; const collect = (t, n, r = false) => (r ? collectStringModelSlot : collectModelConfigSlot)({ ...e, path: `${e.path}.${t}`, value: n }); for (const e of AGENT_MODEL_CONFIG_KEYS) collect(e, t[e]); const n = asOptionalRecord(t.mediaModels); for (const e of [ "image", "video", "music" ]) collect(`mediaModels.${e}`, n?.[e]); collect("heartbeat.model", asOptionalRecord(t.heartbeat)?.model, true); collect("subagents.model", asOptionalRecord(t.subagents)?.model); collect("compaction.model", asOptionalRecord(t.compaction)?.model, true); collectModelsMapRefs({ ...e, path: `${e.path}.models`, models: t.models }); collectModelPolicyAllowRefs({ ...e, path: `${e.path}.modelPolicy`, modelPolicy: t.modelPolicy }); } function collectConfigModelRefs(e, t) { const n = []; const r = e.agents?.defaults; collectAgentModelRefs({ hits: n, agent: r, path: "agents.defaults", blockedModelIdentities: t }); const i = listMutableCodexRouteAgentEntries(e); for (const { agent: e, path: r } of i) collectAgentModelRefs({ hits: n, agent: e, path: r, blockedModelIdentities: t }); visitNonAgentModelSlots(e, ({ container: e, key: r, path: i }) => { collectStringModelSlot({ hits: n, path: i, value: e[r], blockedModelIdentities: t }); }); return n; } function collectDisabledCodexPluginRouteHits(e, t) { return isCodexPluginUnavailableByConfig(e) ? collectCodexRuntimeRouteHits(e, t) : []; } /** Find effective configured model routes that select the Codex runtime. */ function collectCodexRuntimeRouteHits(e, t) { const n = e.agents?.defaults; const r = collectAgentRuntimeModelRefs({ agent: n, path: "agents.defaults" }); let i; if (e.agents && !hasAgentPrimaryModelConfig(n) && !r.some((t) => resolveRuntimeModelRef({ cfg: e, modelRef: t.modelRef }) === resolveImplicitDefaultAgentModelRef(e))) { i = { path: "agents.defaults.model", modelRef: resolveImplicitDefaultAgentModelRef(e) }; r.push(i); } const a = listMutableCodexRouteAgentEntries(e); const o = r.filter((e) => e.path === "agents.defaults.heartbeat.model" || e.path.startsWith("agents.defaults.subagents.model")); const s = r.filter((e) => e.path.startsWith("agents.defaults.models.")); const c = r.filter((e) => !o.includes(e) && !s.includes(e)); const l = collectChannelAgentRuntimeModelRefs(e); const u = a.length === 0 ? [...r, ...l] : []; for (const { agent: t, agentId: n, path: r } of a) { for (const e of l) u.push({ path: e.path, modelRef: e.modelRef, agentId: n }); const a = o.filter((e) => { if (e.path === "agents.defaults.heartbeat.model") return !normalizeOptionalLowercaseString(asOptionalRecord(t.heartbeat)?.model); if (e.path.startsWith("agents.defaults.subagents.model")) return !readModelConfigPrimaryRef(asOptionalRecord(t.subagents)?.model); return true; }); a.push(...s); for (const o of collectAgentRuntimeModelRefs({ agent: t, path: r, fallbackModelRefs: c.map((t) => t === i ? Object.assign({}, t, { modelRef: resolveImplicitDefaultAgentModelRef(e, n) }) : t), inheritedModelRefs: a })) u.push({ ...o, agentId: n }); } const d = []; const f = /* @__PURE__ */ new Set(); for (const n of u) { const r = resolveRuntimeModelRef({ cfg: e, modelRef: n.modelRef, agentId: n.agentId }); if (!modelRefUsesCodexRuntime({ cfg: e, modelRef: n.modelRef, agentId: n.agentId, env: t })) continue; const i = `${n.agentId ?? ""}\0${n.path}\0${r}`; if (f.has(i)) continue; f.add(i); d.push({ path: n.path, modelRef: n.modelRef, canonicalModel: r, ...n.agentId ? { agentId: n.agentId } : {} }); } return d; } /** Find Codex-routed model refs that require the Codex plugin while it is disabled. */ function collectDisabledCodexPluginRouteIssues(e, t) { const n = codexPluginRepairIsBlocked(e); return collectDisabledCodexPluginRouteHits(e, t).map((e) => ({ path: e.path, modelRef: e.modelRef, canonicalModel: e.canonicalModel, repairBlocked: n })); } function enableCodexPluginForRequiredRoutes(e) { if (e.routeHits.length === 0 || codexPluginRepairIsBlocked(e.cfg)) return { cfg: e.cfg, changes: [] }; const t = structuredClone(e.cfg); const n = ensureRecord(t, "plugins"); const r = ensureRecord(n, "entries"); const i = asOptionalRecord(r.codex) ?? {}; const a = []; if (i.enabled !== true) { r.codex = { ...i, enabled: true }; a.push("Enabled plugins.entries.codex because configured agent routes use Codex runtime."); } if (Array.isArray(n.allow) && n.allow.length > 0 && !n.allow.some((e) => normalizeOptionalLowercaseString(e) === "codex")) { n.allow = [...n.allow, "codex"]; a.push("Added codex to plugins.allow because configured agent routes use Codex runtime."); } return { cfg: t, changes: a }; } function codexPluginRepairIsBlocked(e) { return e.plugins?.enabled === false || pluginIdListIncludes(e.plugins?.deny, "codex") || asOptionalRecord(asOptionalRecord(e.plugins?.entries)?.codex)?.enabled === false; } function isCodexPluginUnavailableByConfig(e) { if (codexPluginRepairIsBlocked(e)) return true; const t = e.plugins?.allow; return Array.isArray(t) && t.length > 0 && !pluginIdListIncludes(t, "codex"); } function pluginIdListIncludes(e, t) { return Array.isArray(e) && e.some((e) => normalizeOptionalLowercaseString(e) === t); } function collectAgentRuntimeModelRefs(e) { const t = []; const n = asOptionalRecord(e.agent); if (n && Object.hasOwn(n, "model")) collectModelConfigRefs({ refs: t, path: `${e.path}.model`, value: n.model }); if (!hasAgentPrimaryModelConfig(n) && e.fallbackModelRefs) t.push(...e.fallbackModelRefs); collectStringModelConfigRef({ refs: t, path: `${e.path}.heartbeat.model`, value: asOptionalRecord(n?.heartbeat)?.model }); collectModelConfigRefs({ refs: t, path: `${e.path}.subagents.model`, value: asOptionalRecord(n?.subagents)?.model }); if (e.inheritedModelRefs) t.push(...e.inheritedModelRefs); collectCodexRuntimeModelPolicyRefs({ refs: t, path: `${e.path}.models`, models: n?.models }); return t; } function hasAgentPrimaryModelConfig(e) { const t = asOptionalRecord(e); return Boolean(t && readModelConfigPrimaryRef(t.model)); } function collectChannelAgentRuntimeModelRefs(e) { const t = []; visitChannelModelSlots(e, ({ container: e, key: n, path: r }) => { collectStringModelConfigRef({ refs: t, path: r, value: e[n] }); }); return t; } //#endregion //#region src/commands/doctor/shared/codex-route-warnings.ts function resolveKnownModelRefMigrationTarget(e, t) { const n = new Set(collectBlockedLegacyOpenAICodexProviderPlan(e).blockedModelIdentities); if (isBlockedLegacyCodexModelRef({ modelRef: t, blockedModelIdentities: n })) return; const r = migrateLegacyRuntimeModelRef(t)?.ref ?? toCanonicalOpenAIModelRef(t) ?? t; const i = rewriteKnownModelRefs(r, "model", []).value; return typeof i === "string" && i !== t ? i : void 0; } function formatUnsupportedCompactionWarning(e) { return [ "- Codex runtime ignores compaction.provider; compaction.model still applies to OpenClaw host summaries.", ...e.hits.map((e) => `- ${e.path}: ${e.value} is ignored while this agent uses Codex runtime.`), e.fixHint ].join("\n"); } function formatLegacyLosslessCompactionWarning(e) { const t = []; const n = /* @__PURE__ */ new Set(); for (const r of e.hits) { if (!n.has(r.providerPath)) { n.add(r.providerPath); t.push(`- ${r.providerPath}: ${r.providerValue} should become plugins.slots.contextEngine: ${LOSSLESS_CONTEXT_ENGINE_ID}.`); } if (r.modelPath && r.modelValue) t.push(`- ${r.modelPath}: ${r.modelValue} should become plugins.entries.${LOSSLESS_CONTEXT_ENGINE_ID}.config.summaryModel.`); } return [ "- Legacy Lossless compaction config should use the Lossless context-engine slot for Codex.", ...t, e.canAutoFix ? "- Run `openclaw doctor --fix`: it migrates legacy Lossless compaction config to the Lossless context-engine slot." : "- Move the Lossless config manually; doctor will not overwrite an existing non-Lossless context-engine slot or collapse conflicting per-agent summary models." ].join("\n"); } function formatDisabledCodexPluginWarning(e) { const t = e.repairBlocked ? "- Enable plugins.entries.codex and plugin loading, and remove `codex` from plugins.deny; or set the affected OpenAI models to an OpenClaw runtime policy." : "- Run `openclaw doctor --fix`: it enables plugins.entries.codex, or set the affected OpenAI models to an OpenClaw runtime policy."; return [ "- Codex runtime is selected, but the Codex plugin is disabled.", ...e.hits.map((e) => `- ${e.path}: ${e.modelRef} resolves to ${e.canonicalModel} with Codex runtime while the Codex plugin is disabled by config.`), t ].join("\n"); } function readCodexPluginConfig(e) { const t = asOptionalRecord(e.plugins); const n = asOptionalRecord(t?.entries); const r = asOptionalRecord(n?.codex); return asOptionalRecord(r?.config); } function collectCodexAppServerCommandWarnings(e) { const t = readCodexPluginConfig(e); const n = asOptionalRecord(t?.appServer); if (typeof n?.command !== "string" || !n.command.trim()) return []; const r = n.command.trim(); const i = detectWindowsSpawnCommandInlineArgs(r); return [...i ? [[ "- Codex app-server command override includes inline arguments.", `- plugins.entries.codex.config.appServer.command: "${r}" starts with "${i.executable}" and embeds "${i.arguments}". The command field must be only the executable path.`, "- Remove the override to use managed Codex startup, or move script/options to plugins.entries.codex.config.appServer.args." ].join("\n")] : [], [ "- Custom Codex app-server command bypasses OpenClaw's managed exact-version binary.", "- plugins.entries.codex.config.appServer.command: Doctor did not execute, inspect, or rewrite this command.", "- Remove the override to use managed Codex startup, or verify the custom binary matches the Codex version bundled with this OpenClaw release." ].join("\n")]; } const FAST_MODE_PARAM_KEYS = ["fastMode", "fast_mode"]; const SERVICE_TIER_PARAM_KEYS = ["serviceTier", "service_tier"]; function ownValues(e, t) { return t.filter((t) => Object.hasOwn(e, t)).map((t) => e[t]); } function modelUsesCodexForEveryAgent(e, t) { const n = parseCodexRouteModelRef(t); if (!n || n.modelId === "*") return false; return listMutableCodexRouteAgentEntries(e).every(({ agentId: t }) => normalizeOptionalLowercaseString(resolveModelRuntimePolicy({ config: e, provider: n.provider, modelId: n.modelId, agentId: t }).policy?.id) === "codex"); } function collectCodexModelParamHits(e, t) { const n = []; const r = /* @__PURE__ */ new Set(); const i = new Map(listMutableCodexRouteAgentEntries(e).map(({ agentId: e, path: t }) => [e, t])); for (const a of collectCodexRuntimeRouteHits(e, t)) { const t = parseCodexRouteModelRef(a.canonicalModel); if (!t || t.provider !== "openai") continue; const o = resolveModelExtraParamSources({ config: e, provider: t.provider, modelId: t.modelId, agentId: a.agentId }); const s = o.modelParams; const c = ownValues(s ?? {}, FAST_MODE_PARAM_KEYS); const l = ownValues(s ?? {}, SERVICE_TIER_PARAM_KEYS); const u = c.length > 0 && c.every((e) => normalizeFastMode(e) === true) && l.length > 0 && l.every((e) => normalizeOptionalLowercaseString(e) === "priority") && modelUsesCodexForEveryAgent(e, a.canonicalModel); const d = [ { params: o.defaultParams, path: "agents.defaults.params", modelScoped: false }, { params: s, path: `agents.defaults.models.${a.canonicalModel}.params`, modelScoped: true }, ...a.agentId ? [{ params: o.agentParams, path: `${i.get(a.agentId) ?? `agents.entries.${a.agentId}`}.params`, modelScoped: false }] : [] ]; for (const e of d) for (const [t, i] of Object.entries(e.params ?? {})) { if (e.modelScoped && isAgentRuntimeModelParam(t, i)) continue; const o = `${e.path}.${t}`; if (r.has(o)) continue; r.add(o); n.push({ key: t, path: o, modelRef: a.canonicalModel, removable: e.modelScoped && u && SERVICE_TIER_PARAM_KEYS.some((e) => e === t) }); } } return n; } function formatCodexModelParamWarning(e) { const t = e.some((e) => e.removable) ? "- Run `openclaw doctor --fix` to remove only redundant priority service-tier params; remove any remaining params or set the affected route's agentRuntime.id to \"openclaw\"." : "- Remove these params or set the affected route's agentRuntime.id to \"openclaw\"; Doctor cannot migrate them without changing behavior."; return [ "- Explicit native Codex model routes cannot reproduce authored request transport parameters.", ...e.map((e) => `- ${e.path}: ${e.removable ? "redundant because this model's fastMode already selects native priority service tier" : `authored ${e.key} cannot be migrated automatically`}.`), t ].join("\n"); } function repairRedundantCodexServiceTiers(e, t) { const n = collectCodexModelParamHits(e, t).filter((e) => e.removable); if (n.length === 0) return { config: e, changes: [] }; const r = structuredClone(e); const i = asOptionalRecord(r.agents?.defaults?.models); const a = []; for (const e of n) { const t = asOptionalRecord(asOptionalRecord(i?.[e.modelRef])?.params); if (t) { delete t[e.key]; a.push(`Removed redundant agents.defaults.models.${e.modelRef}.params.${e.key}; fastMode already selects native priority.`); } } return { config: r, changes: a }; } /** Collect non-executing Codex runtime compatibility diagnostics for Doctor and lint. */ function collectCodexRuntimeCompatibilityWarnings(e, t) { const n = collectCodexAppServerCommandWarnings(e); const r = collectCodexModelParamHits(e, t); if (r.length > 0) n.push(formatCodexModelParamWarning(r)); return n; } function collectCodexComputerUseWarnings(e) { const t = readCodexPluginConfig(e); const n = asOptionalRecord(t?.computerUse); if (!n) return []; if (!(n.enabled === true || n.autoInstall === true || typeof n.marketplaceSource === "string" || typeof n.marketplacePath === "string" || typeof n.marketplaceName === "string")) return []; const r = [ 30, 60, 120, 240 ].find((e) => n.healthCheckIntervalMinutes === e) ?? 60; return [[ "- Codex Computer Use is enabled.", "- Doctor config review found Computer Use enabled; run `/codex computer-use status` to inspect installation, exposure, and the live `list_apps` check.", n.healthCheckEnabled === true ? `- Periodic Computer Use health checks are enabled with a ${r}-minute cadence.` : "- Periodic Computer Use health checks are disabled by default; set `computerUse.healthCheckEnabled` to true to enable them.", n.autoRepair === true ? "- Stale Computer Use MCP child repair is enabled and limited to SkyComputerUseClient children." : "- Stale Computer Use MCP child repair is disabled by default; set `computerUse.autoRepair` to true to repair before retrying a failed check." ].join("\n")]; } /** Collect doctor warnings for legacy Codex model refs and compaction overrides. */ function collectCodexRouteWarnings(e) { const t = e.env ?? process.env; const n = { cfg: e.cfg, env: t }; const r = e.blockedProviderPlan ?? collectBlockedLegacyOpenAICodexProviderPlan(e.cfg); const i = new Set(r.blockedModelIdentities); const a = collectConfigModelRefs(e.cfg, i); const o = collectDisabledCodexPluginRouteHits(e.cfg, t); const s = collectLegacyLosslessCompactionConfigs(n); const c = new Set(s.flatMap((e) => e.modelPath ? [e.providerPath, e.modelPath] : [e.providerPath])); const l = collectUnsupportedCodexCompactionOverrides(n).filter((e) => !c.has(e.path)); const u = getSharedDefaultCompactionOverrideConsumers(n); const d = sharedDefaultLosslessCompactionHasNonCodexConsumer(n); const f = [ ...r.warning ? [r.warning] : [], ...collectCodexRuntimeCompatibilityWarnings(e.cfg, t), ...collectCodexComputerUseWarnings(e.cfg) ]; if (a.length > 0) f.push([ "- Legacy `codex/*` and `openai-codex/*` model refs should be rewritten to `openai/*`.", ...a.map((e) => `- ${e.path}: ${e.model} should become ${e.canonicalModel}.`), "- Run `openclaw doctor --fix`: it rewrites configured model refs and stale sessions to `openai/*`, and moves Codex intent to provider/model runtime policy." ].join("\n")); if (s.length > 0) { const t = asOptionalRecord(e.cfg.plugins); const n = normalizeOptionalLowercaseString(asOptionalRecord(t?.slots)?.contextEngine); f.push(formatLegacyLosslessCompactionWarning({ hits: s, canAutoFix: !d && canAutoMigrateLegacyLosslessCompaction({ hits: s, contextEngine: n, summaryModel: readLosslessSummaryModel(t) }) })); } if (o.length > 0) f.push(formatDisabledCodexPluginWarning({ hits: o, repairBlocked: codexPluginRepairIsBlocked(e.cfg) })); const p = l.filter((e) => e.path.startsWith("agents.defaults.compaction.") && u[e.key]); const m = l.filter((e) => !e.path.startsWith("agents.defaults.compaction.") || !u[e.key]); if (p.length > 0) f.push(formatUnsupportedCompactionWarning({ hits: p, fixHint: "- Move or remove shared `agents.defaults.compaction.provider` manually; doctor keeps shared defaults while non-Codex agents can inherit them." })); if (m.length > 0) f.push(formatUnsupportedCompactionWarning({ hits: m, fixHint: "- Run `openclaw doctor --fix`: it removes unsupported Codex compaction overrides." })); return f; } /** Rewrite legacy Codex config routes to OpenAI refs and explicit runtime policy when allowed. */ function maybeRepairCodexRoutes(e) { const t = e.env ?? process.env; const n = e.blockedProviderPlan ?? collectBlockedLegacyOpenAICodexProviderPlan(e.cfg); const r = new Set(n.blockedModelIdentities); const i = collectConfigModelRefs(e.cfg, r); const a = collectDisabledCodexPluginRouteHits(e.cfg, t); const o = { cfg: e.cfg, env: t }; const s = collectUnsupportedCodexCompactionOverrides(o); const c = collectLegacyLosslessCompactionConfigs(o); const l = collectCodexModelParamHits(e.cfg, t).some((e) => e.removable); if (i.length === 0 && a.length === 0 && s.length === 0 && c.length === 0 && !l && !n.warning) return { cfg: e.cfg, warnings: collectCodexRouteWarnings({ cfg: e.cfg, env: t, blockedProviderPlan: n }), changes: [] }; const u = rewriteConfigModelRefs({ cfg: e.cfg, env: t, blockedModelIdentities: r }); const d = repairRedundantCodexServiceTiers(u.cfg, t); const f = enableCodexPluginForRequiredRoutes({ cfg: d.config, routeHits: collectDisabledCodexPluginRouteHits(d.config, t) }); const p = collectCodexRouteWarnings({ cfg: f.cfg, env: t, blockedProviderPlan: n }); const m = u.changes.length > 0 ? [`Repaired Codex model routes:\n${u.changes.map((e) => `- ${e.path}: ${e.model} -> ${e.canonicalModel}.`).join("\n")}`] : []; return { cfg: f.cfg, warnings: p, changes: [ ...m, ...u.runtimePolicyChanges, ...u.unsupportedCompactionChanges, ...f.changes, ...d.changes ] }; } //#endregion export { collectDisabledCodexPluginRouteIssues as a, resolveKnownModelRefMigrationTarget as i, collectCodexRuntimeCompatibilityWarnings as n, maybeRepairCodexRoutes as r, collectCodexRouteWarnings as t };