openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
207 lines (206 loc) • 7.5 kB
JavaScript
import { _ as resolvePrimaryStringValue, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { n as normalizeAgentId, r as normalizeAgentIdStrict } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs";
import { A as tryResolveLegacyCompatibilityAgentId, E as toAgentEntriesRecord, a as resolveAgentDir, f as resolveAgentWorkspaceDir, r as resolveAgentConfig } from "./agent-scope-config-DmXgFGKP.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { S as readAgentDatabaseAdmissionRefusal } from "./agent-database-admission-CL6wHJ_N.mjs";
import { n as pinLegacyInheritedAuthOwnerForRosterTransition } from "./legacy-inherited-auth-dir-1Y-Cs3kS.mjs";
import { a as resolveAgentAvatarUrlFromSource } from "./identity-avatar-file-CzRs1l0-.mjs";
import { a as loadAgentIdentityFromWorkspaceAsync } from "./identity-file-C4Fvs-OY.mjs";
import { r as pinSurvivorWorkspaceForRosterCollapse } from "./agent-workspace-roster-transition-B9fDuucP.mjs";
import { i as listRouteBindings } from "./bindings-C2C-UuI7.mjs";
import pMap from "p-map";
//#region src/commands/agents.config.ts
/** Find a configured agent entry by normalized id. */
function findAgentEntryIndex(e, t) {
const n = normalizeAgentId(t);
return e.findIndex((e) => normalizeAgentId(e.id) === n);
}
/** Build config-derived summaries for text/JSON agent listing. */
async function buildAgentSummaries(e) {
const t = tryResolveLegacyCompatibilityAgentId(e);
const n = listAgentEntries(e);
const r = n.length > 0 ? n.map((e) => normalizeAgentId(e.id)) : t ? [t] : [];
const i = /* @__PURE__ */ new Map();
for (const t of listRouteBindings(e)) {
const e = normalizeAgentId(t.agentId);
i.set(e, (i.get(e) ?? 0) + 1);
}
const a = uniqueStrings(r);
return pMap(a, async (n) => {
const r = resolveAgentWorkspaceDir(e, n);
const a = await loadAgentIdentityFromWorkspaceAsync(r);
const o = resolveAgentConfig(e, n);
const s = normalizeOptionalString(o?.identity?.name);
const c = normalizeOptionalString(o?.identity?.emoji);
const l = await resolveAgentAvatarUrlFromSource(e, n, o?.identity?.avatar);
const u = l ?? await resolveAgentAvatarUrlFromSource(e, n, a?.avatar);
const d = s || c || l ? "config" : a ? "identity" : void 0;
const f = {
id: n,
name: normalizeOptionalString(o?.name),
identityName: s ?? a?.name,
identityEmoji: c ?? a?.emoji,
identitySource: d,
workspace: r,
agentDir: resolveAgentDir(e, n),
model: resolvePrimaryStringValue(o?.model) ?? resolvePrimaryStringValue(e.agents?.defaults?.model),
bindings: i.get(n) ?? 0,
isDefault: t !== void 0 && n === normalizeAgentId(t)
};
if (u) f.identityAvatarUrl = u;
const p = readAgentDatabaseAdmissionRefusal(n);
if (p) {
f.status = "degraded";
f.admissionRefusal = p;
}
return f;
}, { concurrency: 4 });
}
function applyAgentConfig(e, t) {
const n = normalizeAgentId(t.agentId);
const r = listAgentEntries(e);
const i = findAgentEntryIndex(r, n);
const a = (i >= 0 ? r[i] : void 0) ?? { id: n };
const o = normalizeOptionalString(a.name);
const s = normalizeOptionalString(t.name) ?? (o && o === normalizeOptionalString(a.identity?.name) ? normalizeOptionalString(t.identity?.name) : void 0);
const c = t.identity ? {
...a.identity,
...t.identity
} : void 0;
const l = {
...a,
...s ? { name: s } : {},
...t.workspace ? { workspace: t.workspace } : {},
...t.agentDir ? { agentDir: t.agentDir } : {},
...c ? { identity: c } : {}
};
if (t.model === null) delete l.model;
else if (t.model !== void 0) l.model = t.model;
const u = [...r];
if (i >= 0) u[i] = l;
else u.push(l);
const { ownership: d, ...f } = e.agents ?? {};
const p = {
...e,
agents: {
...f,
...u.length > 1 ? { ownership: "explicit" } : {},
entries: toAgentEntriesRecord(u)
}
};
if (r.length !== 1 || u.length <= 1) return p;
const m = tryResolveLegacyCompatibilityAgentId(e);
const h = m && !normalizeOptionalString(p.agents?.defaults?.systemAgent?.agentId) ? {
...p,
agents: {
...p.agents,
defaults: {
...p.agents?.defaults,
systemAgent: { agentId: m }
}
}
} : p;
return pinLegacyInheritedAuthOwnerForRosterTransition(e, h);
}
/** Remove an agent and any config references that route or allow traffic to it. */
function pruneAgentConfig(e, t) {
const n = normalizeAgentId(t);
const r = [];
const targetsDeletedAgent = (e) => {
const t = normalizeAgentIdStrict(e);
return t.ok && t.value === n;
};
const clearOwnerRef = (e, t) => {
const i = normalizeOptionalString(e?.agentId);
if (!e || !i || normalizeAgentId(i) !== n) return e;
r.push(t);
const { agentId: a, ...o } = e;
return Object.keys(o).length > 0 ? o : void 0;
};
const i = listAgentEntries(e);
const pruneAllowAgents = (e) => e?.filter((e) => {
const t = e.trim();
return !t || !targetsDeletedAgent(t);
});
const a = [];
for (const e of i) {
if (normalizeAgentId(e.id) === n) continue;
const t = { ...e };
if (e.subagents?.allowAgents) t.subagents = {
...e.subagents,
allowAgents: pruneAllowAgents(e.subagents.allowAgents)
};
if (e.tools?.agentToAgent?.send) t.tools = {
...e.tools,
agentToAgent: {
...e.tools.agentToAgent,
send: pruneAllowAgents(e.tools.agentToAgent.send)
}
};
a.push(t);
}
const o = a.length > 0 ? toAgentEntriesRecord(a) : void 0;
const s = e.bindings ?? [];
const c = s.filter((e) => normalizeAgentId(e.agentId) !== n);
const l = e.tools?.agentToAgent?.allow ?? [];
const u = l.filter((e) => e !== n);
const d = e.agents?.defaults?.subagents?.allowAgents ? {
...e.agents.defaults,
subagents: {
...e.agents.defaults.subagents,
allowAgents: pruneAllowAgents(e.agents.defaults.subagents.allowAgents)
}
} : e.agents?.defaults;
const f = d ? {
...d,
systemAgent: clearOwnerRef(d.systemAgent, "agents.defaults.systemAgent.agentId")
} : void 0;
const p = clearOwnerRef(e.talk, "talk.agentId");
const m = e.broadcast ? Object.fromEntries(Object.entries(e.broadcast).map(([e, t]) => [e, Array.isArray(t) ? t.filter((e) => !targetsDeletedAgent(e)) : t && typeof t === "object" ? {
...t,
agents: t.agents.filter((e) => !targetsDeletedAgent(e))
} : t])) : void 0;
const h = e.hooks ? {
...e.hooks,
allowedAgentIds: e.hooks.allowedAgentIds?.filter((e) => !targetsDeletedAgent(e)),
mappings: e.hooks.mappings?.filter((e) => !e.agentId || !targetsDeletedAgent(e.agentId))
} : void 0;
const { ownership: g, ..._ } = e.agents ?? {};
const v = e.agents ? {
..._,
...a.length > 1 ? { ownership: "explicit" } : {},
defaults: f,
entries: o
} : o ? {
...a.length > 1 ? { ownership: "explicit" } : {},
entries: o
} : void 0;
const y = e.tools?.agentToAgent ? {
...e.tools,
agentToAgent: {
...e.tools.agentToAgent,
allow: u.length > 0 ? u : void 0
}
} : e.tools;
const b = {
...e,
agents: v,
bindings: c.length > 0 ? c : void 0,
broadcast: m,
hooks: h,
talk: p,
tools: y
};
const x = pinSurvivorWorkspaceForRosterCollapse(e, b).config;
return {
config: i.length > 1 && a.length === 1 ? pinLegacyInheritedAuthOwnerForRosterTransition(e, x) : x,
removedBindings: s.length - c.length,
removedAllow: l.length - u.length,
clearedOwnerRefs: r
};
}
//#endregion
export { pruneAgentConfig as i, buildAgentSummaries as n, findAgentEntryIndex as r, applyAgentConfig as t };