openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
365 lines (364 loc) • 13.4 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord, r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { o as readObjectPath } from "./dot-path-BJ4tyWKJ.mjs";
import "./utils-C8YECn2Q.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { p as tryResolveSoleAgentId } from "./agent-roster-t1ooUdXp.mjs";
import { V as isPerAgentSessionStoreConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import { t as resolveDefaultAgentWorkspaceDir } from "./workspace-default-path-CWX4WiCH.mjs";
import "./workspace-default-DvctYF3A.mjs";
import { i as copyConfigResolutionFacts } from "./resolution-facts-xaIKuREs.mjs";
import { t as applyImplicitAgentRosterDefaults } from "./implicit-agent-roster-DtoGie9T.mjs";
import { n as isChannelConfigMetadataKey } from "./config-metadata-BVUrt758.mjs";
import { t as normalizeRouteBindingChannelId } from "./binding-scope-C3Xc3l9P.mjs";
import { r as retainLegacyDefaultAgentId } from "./legacy.default-agent-owner-DpmHgK0A.mjs";
import { n as projectLegacyAgentRosterEntries } from "./legacy.roster-BcYJITMZ.mjs";
import { t as isSafeExecutableValue } from "./exec-safety-DkenKsFz.mjs";
import { t as GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA } from "./bundled-channel-config-metadata.generated-BoM67BKX.mjs";
import { n as parseToolsBySenderTypedKey } from "./types.tools-BQVTy9eE.mjs";
import { i as visitAgentConfigScopes } from "./legacy-config-record-shared-D3vIaBh9.mjs";
//#region src/config/legacy.default-agent-roles.ts
function isChannelWideBinding(e, t) {
const n = e.match;
return isRecord(n) && normalizeRouteBindingChannelId(typeof n.channel === "string" ? n.channel : void 0) === t && (typeof n.accountId === "string" ? n.accountId.trim() : void 0) === "*" && n.peer === void 0 && !normalizeOptionalString(n.guildId) && !normalizeOptionalString(n.teamId) && (!Array.isArray(n.roles) || n.roles.length === 0);
}
function listUnboundAmbientChannelIds(e, t) {
if (e.bindings && !Array.isArray(e.bindings)) return [];
const n = (e.bindings ?? []).filter((e) => isRecord(e) && e.type !== "acp");
const r = new Set(t.map(normalizeRouteBindingChannelId).filter((e) => Boolean(e)));
if (isRecord(e.channels)) for (const [t, n] of Object.entries(e.channels)) {
const e = normalizeRouteBindingChannelId(t);
if (e && !isChannelConfigMetadataKey(t) && (!isRecord(n) || n.enabled !== false)) r.add(e);
}
return [...r].toSorted().filter((e) => !n.some((t) => isChannelWideBinding(t, e)));
}
/** Preserve a legacy workspace locator without assigning any runtime ownership. */
function resolveLegacyAgentWorkspacePin(e, t, n = {}) {
return !Object.hasOwn(t, "workspace") || typeof t.workspace === "string" && t.workspace.trim().length === 0 ? normalizeOptionalString(e) ?? resolveDefaultAgentWorkspaceDir(n.env, n.homedir) : void 0;
}
function resolveLegacyFirstAgentWorkspacePin(e, t, n = {}) {
return e.ownership === void 0 && t.length > 1 && t.every((e) => !Object.hasOwn(e, "default") || e.default === false) ? resolveLegacyAgentWorkspacePin(isRecord(e.defaults) ? e.defaults.workspace : void 0, t[0], n) : void 0;
}
function materializeLegacyDefaultAgentRoles(e, t, n = {}) {
const r = normalizeAgentId(t);
let i = e;
const a = [];
if (n.materializeWorkspace) {
const e = { ...i.agents?.entries };
const t = Object.keys(e).find((e) => normalizeAgentId(e) === r);
const o = t ? e[t] : void 0;
const s = o ? resolveLegacyAgentWorkspacePin(i.agents?.defaults?.workspace, o, n) : void 0;
if (t && o && s !== void 0) {
e[t] = {
...o,
workspace: s
};
i = {
...i,
agents: {
...i.agents,
entries: e
}
};
a.push([
"agents",
"entries",
t,
"workspace"
]);
}
}
const o = listUnboundAmbientChannelIds(e, n.ambientChannelIds ?? []);
if (o.length > 0) {
i = {
...i,
bindings: [...Array.isArray(i.bindings) ? i.bindings : [], ...o.map((e) => ({
agentId: r,
match: {
channel: e,
accountId: "*"
}
}))]
};
a.push(["bindings"]);
}
const s = e.agents?.defaults;
const c = isRecord(s) ? s : void 0;
if (s === void 0 || c) {
const t = normalizeAgentId(tryResolveSoleAgentId(e) ?? "main");
const unset = (e) => c?.[e] === void 0 || isRecord(c[e]) && !Object.hasOwn(c[e], "agentId");
const o = { ...c };
let s = false;
const materialize = (e, t) => {
if (!t) return;
o[e] = {
...isRecord(o[e]) ? o[e] : {},
agentId: r
};
a.push([
"agents",
"defaults",
e,
"agentId"
]);
s = true;
};
materialize("systemAgent", unset("systemAgent"));
materialize("authInheritance", r !== t && unset("authInheritance"));
materialize("sessionStore", n.materializeSessionStore !== false && !isPerAgentSessionStoreConfig(e.session?.store) && unset("sessionStore"));
if (s) i = {
...i,
agents: {
...i.agents,
defaults: o
}
};
}
const l = isRecord(e.talk) ? e.talk : void 0;
if ((e.talk === void 0 || l) && (!l || !Object.hasOwn(l, "agentId"))) {
i = {
...i,
talk: {
...l,
agentId: r
}
};
a.push(["talk", "agentId"]);
}
return {
config: i,
insertedPaths: a
};
}
//#endregion
//#region src/config/legacy.shared.ts
/** Evaluate exactly the rules selected by the migration owner. */
function findLegacyConfigRuleIssues(e, t, n) {
if (!e || typeof e !== "object") return [];
const r = e;
const i = n && typeof n === "object" ? n : r;
const a = [];
for (const e of t) {
const t = readObjectPath(r, e.path);
if (t !== void 0 && (!e.match || e.match(t, r))) {
if (e.requireSourceLiteral) {
const t = readObjectPath(i, e.path);
if (t === void 0) continue;
if (e.match && !e.match(t, i)) continue;
}
a.push({
path: e.path.join("."),
message: e.message
});
}
}
return a;
}
const createLegacyConfigRule = (e, t, n) => ({
path: e,
message: `${t} Run "openclaw doctor --fix".`,
...n ? { match: n } : {}
});
const mapLegacyAudioTranscription = (e) => {
const t = asNullableRecord(e);
const n = Array.isArray(t?.command) ? t?.command : null;
if (!n || n.length === 0) return null;
if (typeof n[0] !== "string") return null;
if (!n.every((e) => typeof e === "string")) return null;
const r = n[0].trim();
if (!isSafeExecutableValue(r)) return null;
const i = n.slice(1).map((e) => e.replace(/\{input\}/g, "{{AttachmentPath}}"));
const a = typeof t?.timeoutSeconds === "number" ? t?.timeoutSeconds : void 0;
const o = {
command: r,
type: "cli"
};
if (i.length > 0) o.args = i;
if (a !== void 0) o.timeoutSeconds = a;
return o;
};
//#endregion
//#region src/commands/doctor/shared/legacy-config-migrations.runtime.entries.ts
function migrateAgentEntries(e, t, n) {
const r = asNullableRecord(e.agents);
if (!r || !Object.prototype.propertyIsEnumerable.call(r, "list") || !Array.isArray(r.list)) return;
if (Object.hasOwn(r, "entries")) {
if (asNullableRecord(r.entries)) {
delete r.list;
t.push("Removed agents.list because agents.entries is already set.");
}
return;
}
const i = projectLegacyAgentRosterEntries(r.list);
t.push(...i.diagnostics);
const a = i.entries.map(({ config: e }) => e);
const o = resolveLegacyFirstAgentWorkspacePin(r, a, n);
if (o !== void 0) a[0].workspace = o;
r.entries = Object.fromEntries(i.entries.map(({ id: e, config: t }) => [e, t]));
delete r.list;
Object.assign(e, applyImplicitAgentRosterDefaults(e));
t.push("Moved agents.list → keyed agents.entries.");
}
/** Writers preserve existing responsibilities; Doctor uses the retired marker's original owner. */
function retireLegacyAgentDefaultMarkers(e) {
const t = asNullableRecord(asNullableRecord(e)?.agents);
const n = asNullableRecord(t?.entries);
if (!t || !n) return;
const r = [];
for (const [e, t] of Object.entries(n)) {
const n = asNullableRecord(t);
if (!n || Object.hasOwn(n, "default") && typeof n.default !== "boolean") return;
r.push([e, n]);
}
const i = r.filter(([, e]) => e.default === true);
if (i.length > 1 || i.length > 0 && t.ownership === "explicit") return;
const a = [];
const o = Object.fromEntries(r.map(([e, t]) => {
if (!Object.hasOwn(t, "default")) return [e, t];
const { default: n, ...r } = t;
a.push("Removed retired agents.entries default marker.");
return [e, r];
}));
const s = a.length > 0 ? {
...e,
agents: {
...t,
entries: o
}
} : e;
copyConfigResolutionFacts(e, s);
return {
config: s,
changes: a,
agentCount: r.length,
legacyOwner: r.length > 1 ? i[0]?.[0] : void 0
};
}
const LEGACY_AGENT_ROSTER_RULES = [{
path: ["agents", "list"],
match: (e, t) => Object.prototype.propertyIsEnumerable.call(asNullableRecord(t.agents), "list"),
message: "agents.list moved to keyed agents.entries. Run \"openclaw doctor --fix\"."
}, {
path: ["agents", "entries"],
match: (e) => Object.values(asNullableRecord(e) ?? {}).some((e) => {
const t = asNullableRecord(e);
return t !== null && typeof t.default === "boolean";
}),
message: "Legacy agents.entries default markers need explicit surface owners. Run \"openclaw doctor --fix\"."
}];
const LEGACY_CONFIG_MIGRATIONS_RUNTIME_ENTRIES = [{
id: "runtime.agents-entries",
legacyRules: LEGACY_AGENT_ROSTER_RULES,
apply: migrateAgentEntries
}, {
id: "runtime.agents-explicit-ownership",
apply: (e, t, n) => {
const r = retireLegacyAgentDefaultMarkers(e);
if (!r) return;
Object.assign(e, r.config);
const { legacyOwner: i } = r;
if (i) {
const r = materializeLegacyDefaultAgentRoles(e, i, {
...n,
materializeWorkspace: true
});
Object.assign(e, r.config);
retainLegacyDefaultAgentId(e, i);
t.push("Preserved legacy per-surface agent ownership and workspace.");
}
const a = asNullableRecord(e.agents);
t.push(...r.changes);
if (r.agentCount < 2 || a.ownership !== void 0) return;
a.ownership = "explicit";
t.push("Stamped the multi-agent roster for explicit per-surface ownership.");
}
}];
//#endregion
//#region src/commands/doctor/shared/legacy-tools-by-sender.ts
const migrationMessage = "Untyped toolsBySender keys are retired. Run \"openclaw doctor --fix\" to migrate them to id: entries.";
const channelSchemas = new Map(GENERATED_BUNDLED_CHANNEL_CONFIG_METADATA.map(({ channelId: e, schema: t }) => [e, t]));
function* channelPolicyScopes(e, t, n) {
const r = asNullableRecord(e);
const i = asNullableRecord(t);
if (!r || !i) return;
const a = asNullableRecord(i.properties);
if (asNullableRecord(a?.toolsBySender)) yield {
parent: r,
path: n
};
for (const [e, t] of Object.entries(r)) if (e !== "toolsBySender") yield* channelPolicyScopes(t, a?.[e] ?? i.additionalProperties, `${n}.${e}`);
for (const e of [
i.anyOf,
i.oneOf,
i.allOf
]) for (const t of Array.isArray(e) ? e : []) yield* channelPolicyScopes(r, t, n);
}
function* legacySenderMaps(e) {
const t = asNullableRecord(e);
if (!t) return;
const n = [{
parent: t.tools,
path: "tools"
}];
visitAgentConfigScopes(t, (e, t) => {
n.push({
parent: e.tools,
path: `${t}.tools`
});
});
for (const [e, r] of Object.entries(asNullableRecord(t.channels) ?? {})) for (const t of channelPolicyScopes(r, channelSchemas.get(e), `channels.${e}`)) n.push(t);
const r = /* @__PURE__ */ new Set();
for (const e of n) {
const t = asNullableRecord(e.parent);
const n = asNullableRecord(t?.toolsBySender);
if (!t || !n || r.has(t)) continue;
r.add(t);
const i = Object.keys(n).filter((e) => e.trim() !== "*" && !parseToolsBySenderTypedKey(e));
if (i.length > 0) yield {
parent: t,
policies: n,
path: `${e.path}.toolsBySender`,
keys: i
};
}
}
function collectLegacyToolsBySenderIssues(e) {
return Array.from(legacySenderMaps(e)).flatMap(({ path: e, keys: t }) => t.map((t) => ({
path: `${e}.${t}`,
message: migrationMessage
})));
}
const LEGACY_CONFIG_MIGRATION_TOOLS_BY_SENDER = {
id: "toolsBySender.typed-keys",
legacyRules: [{
path: [],
match: (e) => !legacySenderMaps(e).next().done,
message: migrationMessage
}],
apply(e, t) {
for (const { parent: n, policies: r, path: i, keys: a } of legacySenderMaps(e)) {
const e = /* @__PURE__ */ new Map();
let o = 0;
let s = 0;
for (const [t, n] of Object.entries(r)) {
const r = t.trim();
const i = parseToolsBySenderTypedKey(r);
const a = (i?.type === "id" ? i.value : r.replace(/^@/, "")).trim();
if (!i && !a) {
s++;
continue;
}
const c = i?.type === "id" || !i && r !== "*" ? `id:${a.toLowerCase()}` : t;
const l = !i && r !== "*" ? `id:${a}` : t;
if (e.has(c)) o++;
else e.set(c, [l, n]);
}
n.toolsBySender = Object.fromEntries(e.values());
t.push(`${i}: migrated ${a.length - s} untyped sender key(s) to id: entries.` + (o > 0 ? ` Kept the first matching policy for ${o} shadowed key(s); original entries remain in the config backup.` : ""));
if (s > 0) t.push(`${i}: removed ${s} empty untyped sender key(s) that matched no sender; original entries remain in the config backup.`);
}
}
};
//#endregion
export { retireLegacyAgentDefaultMarkers as a, mapLegacyAudioTranscription as c, LEGACY_CONFIG_MIGRATIONS_RUNTIME_ENTRIES as i, materializeLegacyDefaultAgentRoles as l, collectLegacyToolsBySenderIssues as n, createLegacyConfigRule as o, LEGACY_AGENT_ROSTER_RULES as r, findLegacyConfigRuleIssues as s, LEGACY_CONFIG_MIGRATION_TOOLS_BY_SENDER as t };