openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
217 lines (216 loc) • 8.38 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import "./utils-C8YECn2Q.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as listAgentEntries, u as tryResolveDefaultAgentId } from "./agent-roster-t1ooUdXp.mjs";
import { U as isSameSessionStoreConfig, V as isPerAgentSessionStoreConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as coerceConfig } from "./io.read-helpers-qAcqsRC0.mjs";
import { r as setConfigValueAtPath, t as getConfigValueAtPath } from "./config-paths-DTAbnrwT.mjs";
import { l as applyUnsetPathsForWrite } from "./deferred-plugin-migration-config-B3TJC-SY.mjs";
import { a as retireLegacyAgentDefaultMarkers, r as LEGACY_AGENT_ROSTER_RULES, s as findLegacyConfigRuleIssues } from "./legacy-tools-by-sender-Cj1HdJFy.mjs";
import { r as resolveLegacyAgentRosterOwner } from "./legacy.roster-BcYJITMZ.mjs";
import { n as materializeLegacyAgentOwnershipForActiveChannelsResultAsync } from "./validation-DSwgLhC7.mjs";
import { r as restoreEnvVarRefsFromResolved } from "./env-preserve-D_8K8swo.mjs";
import { n as prepareConfigWriteValues } from "./io.write-prepare-B19yEmrf.mjs";
import { n as pinLegacyInheritedAuthOwnerForRosterTransition, t as assertSafeLegacyInheritedAuthDirTransition } from "./legacy-inherited-auth-dir-1Y-Cs3kS.mjs";
import { r as pinSurvivorWorkspaceForRosterCollapse } from "./agent-workspace-roster-transition-B9fDuucP.mjs";
import { isDeepStrictEqual } from "node:util";
//#region src/config/io.auth-inheritance-owner.ts
const AUTH_INHERITANCE_PATH = "agents.defaults.authInheritance";
function explicitlySetsAuthInheritance(e) {
return Boolean(e?.some((e) => {
const t = e.join(".");
return t === AUTH_INHERITANCE_PATH || t.startsWith(`${AUTH_INHERITANCE_PATH}.`);
}));
}
function prepareAuthInheritanceOwnerForWrite(e) {
if (!e.writesOwnershipTopology || explicitlySetsAuthInheritance(e.explicitSetPaths)) return {
config: e.targetConfig,
insertedPaths: []
};
assertSafeLegacyInheritedAuthDirTransition(e.currentConfig, e.targetConfig, e.env);
const t = pinLegacyInheritedAuthOwnerForRosterTransition(e.currentConfig, e.targetConfig);
return {
config: t,
insertedPaths: t === e.targetConfig ? [] : [[
"agents",
"defaults",
"authInheritance",
"agentId"
]]
};
}
//#endregion
//#region src/config/io.ownership-write-guard.ts
function assertAutomaticBindingsWriteAllowed(e) {
if (e.bindingsIncludeOwned && e.ownershipPaths.some((e) => e[0] === "bindings")) throw Object.assign(/* @__PURE__ */ new Error("Automatic agent ownership materialization cannot append to $include-owned bindings. Add the required channel-wide binding to the include, then retry."), { code: "CONFIG_WRITE_REJECTED" });
}
//#endregion
//#region src/config/io.session-store-owner.ts
const SESSION_STORE_OWNER_PATH = [
"agents",
"defaults",
"sessionStore",
"agentId"
];
const SESSION_STORE_CONFIG_PATH = SESSION_STORE_OWNER_PATH.slice(0, -1);
function prepareSessionStoreOwnershipForWrite(e) {
const t = isSameSessionStoreConfig(e.currentStore, e.targetConfig.session?.store, e.env);
const n = t && !isPerAgentSessionStoreConfig(e.currentStore);
const r = e.currentConfig.agents?.defaults?.sessionStore?.agentId;
const i = getConfigValueAtPath(e.explicitSetValueSource ?? e.targetConfig, SESSION_STORE_CONFIG_PATH);
const a = Boolean(isRecord(i) && typeof i.agentId === "string" && e.explicitSetPaths?.some((e) => isDeepStrictEqual(e, SESSION_STORE_CONFIG_PATH) || isDeepStrictEqual(e, SESSION_STORE_OWNER_PATH)));
if (t || !r || a) return {
config: e.targetConfig,
sameFixedSessionStore: n,
ownershipPaths: []
};
const o = applyUnsetPathsForWrite(e.targetConfig, [[...SESSION_STORE_OWNER_PATH]]);
return {
config: {
...o,
agents: o.agents ?? {}
},
sameFixedSessionStore: n,
ownershipPaths: [[...SESSION_STORE_OWNER_PATH]]
};
}
//#endregion
//#region src/config/io.write-topology.ts
function cloneConfigPathParents(e, t, n) {
let r = e;
let i = t;
for (const e of n.slice(0, -1)) {
const t = isRecord(r) ? r[e] : void 0;
const n = i[e];
if (n === t) {
const n = isRecord(t) ? { ...t } : {};
i[e] = n;
i = n;
} else if (isRecord(n)) i = n;
else {
const t = {};
i[e] = t;
i = t;
}
r = t;
}
}
async function prepareConfigWriteTopology(e) {
const { snapshot: t, options: n, unsetPaths: r, env: i, homedir: a, pluginMetadataSnapshot: o } = e;
const s = prepareConfigWriteValues({
snapshot: t,
nextConfig: e.nextConfig,
writeOptions: n,
env: i,
lowerPrecedenceEnv: e.lowerPrecedenceEnv,
explicitSetPaths: n.explicitSetPaths,
explicitSetValueSource: n.explicitSetValueSource
});
const c = findLegacyConfigRuleIssues(t.sourceConfigBeforeMigrations ?? t.sourceConfig, LEGACY_AGENT_ROSTER_RULES).length === 0 ? retireLegacyAgentDefaultMarkers(s.resolvedConfig) : void 0;
let l = c?.config ?? s.resolvedConfig;
const u = resolveLegacyAgentRosterOwner(t.sourceConfigBeforeMigrations ?? t.parsed);
const d = listAgentEntries(t.config);
const f = listAgentEntries(l);
const p = new Set(f.map((e) => normalizeAgentId(e.id)));
const m = tryResolveDefaultAgentId(t.config);
const h = d.length <= 1 && f.length > 1;
const g = Boolean(m && p.has(m));
const _ = n.persistCanonicalAgentRoster === true || !isDeepStrictEqual(d, f) || [...n.explicitSetPaths ?? [], ...r].some((e) => e[0] === "agents" && (e.length === 1 || e[1] === "entries" || e[1] === "list" || e[1] === "ownership"));
const v = h || u !== void 0 && _;
const y = f.length > 1 && t.config.agents?.ownership === "explicit";
const b = (v || y) && l.agents?.ownership === void 0;
if (b) l = {
...l,
agents: {
...l.agents,
ownership: "explicit"
}
};
const x = pinSurvivorWorkspaceForRosterCollapse(t.config, l, i);
l = x.config;
const S = prepareAuthInheritanceOwnerForWrite({
currentConfig: t.config,
targetConfig: l,
writesOwnershipTopology: _,
explicitSetPaths: n.explicitSetPaths,
env: i
});
l = S.config;
const C = prepareSessionStoreOwnershipForWrite({
currentConfig: t.config,
currentStore: (t.sourceConfigBeforeMigrations ?? t.config).session?.store,
targetConfig: l,
env: i,
explicitSetPaths: n.explicitSetPaths,
explicitSetValueSource: n.explicitSetValueSource
});
l = C.config;
const { sameFixedSessionStore: w } = C;
const T = u && _ && p.has(normalizeAgentId(u)) ? u : void 0;
const E = (h && g ? m : void 0) ?? T;
const D = E ? await materializeLegacyAgentOwnershipForActiveChannelsResultAsync(l, E, i, o?.manifestRegistry.plugins, {
materializeSessionStore: w,
materializeWorkspace: true,
homedir: a
}) : {
config: l,
insertedPaths: []
};
l = D.config;
const O = [
...D.insertedPaths.concat(x.insertedPaths),
...S.insertedPaths,
...C.ownershipPaths,
...b ? [["agents", "ownership"]] : []
];
const k = l.agents?.defaults?.sessionStore;
if (!E && _ && d.length === 1 && m && !g && w && (k === void 0 || isRecord(k) && !Object.hasOwn(k, "agentId"))) {
l = {
...l,
agents: {
...l.agents,
defaults: {
...l.agents?.defaults,
sessionStore: {
...k,
agentId: m
}
}
}
};
O.push([
"agents",
"defaults",
"sessionStore",
"agentId"
]);
}
const A = [...new Map(O.map((e) => [e.join("\0"), e])).values()];
assertAutomaticBindingsWriteAllowed({
bindingsIncludeOwned: t.bindingsIncludeOwned === true,
ownershipPaths: A
});
const j = [...n.explicitSetPaths ?? [], ...A];
const M = s.explicitSetValueSource;
const N = { ...M };
for (const e of A) {
cloneConfigPathParents(M, N, e);
setConfigValueAtPath(N, e, getConfigValueAtPath(l, e));
}
return {
nextConfig: l,
clearedSessionStoreOwner: C.ownershipPaths.length > 0,
resolutionEnv: s.resolutionEnv,
authoredConfig: l === s.resolvedConfig ? s.authoredConfig : coerceConfig(restoreEnvVarRefsFromResolved(l, s.authoredConfig, s.resolvedConfig)),
authoredSourceConfig: s.authoredSourceConfig,
authoredRuntimeConfig: s.authoredRuntimeConfig,
explicitSetPaths: j,
explicitSetValueSource: N,
persistCanonicalAgentRoster: n.persistCanonicalAgentRoster === true || v || b || (c?.changes.length ?? 0) > 0,
preserveLegacyAgentRoster: Boolean(u) && !_,
cronOwner: v ? u ? { provenOwnerAgentId: u } : {} : void 0
};
}
//#endregion
export { prepareConfigWriteTopology as t };