openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
173 lines (172 loc) • 6.13 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { t as runtimeConfigPublication } from "./runtime-config-publication-VikzJQm1.mjs";
import { c as normalizeOptionalAgentId, n as LEGACY_IMPLICIT_AGENT_ID } from "./session-key-CqAyZeVk.mjs";
import { r as isDeeplyFrozenPlainData } from "./immutable-data-Y7xVjZoy.mjs";
//#region src/system-agent/agent-id.ts
const SYSTEM_AGENT_ID = "openclaw";
const SYSTEM_AGENT_ROSTER_ENTRIES = [{
id: SYSTEM_AGENT_ID,
kind: "system"
}, {
id: "crestodian",
kind: "system"
}];
const RESERVED_SYSTEM_AGENT_IDS = new Set(SYSTEM_AGENT_ROSTER_ENTRIES.map((e) => normalizeAgentId(e.id)));
function isReservedSystemAgentId(e) {
return RESERVED_SYSTEM_AGENT_IDS.has(normalizeAgentId(e));
}
//#endregion
//#region src/agents/agent-roster.ts
let activeAgentRosterFactsBatch;
const agentRosterFacts = /* @__PURE__ */ new WeakMap();
/** Share roster reads synchronously; unbound mutable configs expire when the callback returns. */
function withAgentRosterFactsBatch(e, t) {
const n = activeAgentRosterFactsBatch;
activeAgentRosterFactsBatch = n?.config === e ? n : {
config: e,
facts: readAgentRosterFacts(e) ?? {}
};
try {
return t();
} finally {
activeAgentRosterFactsBatch = n;
}
}
function readAgentRosterFacts(e) {
if (activeAgentRosterFactsBatch?.config === e) return activeAgentRosterFactsBatch.facts;
const t = runtimeConfigPublication.current;
const n = t?.config === e ? t.revision : void 0;
if (n === void 0 && !isDeeplyFrozenPlainData(e)) return;
let r = agentRosterFacts.get(e);
if (!r || r.publication !== n) {
r = {
publication: n,
facts: {}
};
agentRosterFacts.set(e, r);
}
return r.facts;
}
function collectAgentEntries(e, t, n) {
const r = readAgentRosterProperty(e);
if (r?.kind === "entries" && isRecord(r.value)) {
const e = [];
for (const i in r.value) {
if (!Object.hasOwn(r.value, i)) continue;
const a = r.value[i];
if (isRecord(a)) {
const r = {
...a,
id: i
};
e.push(t ? {
entry: r,
source: {
kind: "entries",
key: i
}
} : r);
if (e.length === n) break;
}
}
return e;
}
if (r?.kind !== "list" || !Array.isArray(r.value)) return [];
const i = [];
r.value.some((e, t) => {
if (e !== null && typeof e === "object") i.push({
entry: e,
source: {
kind: "list",
index: t
}
});
return i.length === n;
});
return t ? i : i.map(({ entry: e }) => e);
}
/** Lists valid configured agent entries from config. */
function listAgentEntriesWithSource(e) {
return collectAgentEntries(e, true);
}
/** Lists valid configured agent entries from either supported representation. */
function listAgentEntries(e) {
return collectAgentEntries(e, false);
}
/** Reads the explicitly owned raw roster without normalizing malformed values. */
function readAgentRosterProperty(e) {
if (!isRecord(e)) return;
const t = e.agents;
if (!isRecord(t)) return;
for (const e of ["entries", "list"]) {
const n = t[e];
if (Object.hasOwn(t, e) && n !== void 0) return {
kind: e,
value: n
};
}
}
/** True when raw config explicitly owns either supported roster representation. */
function hasAgentRosterProperty(e) {
return readAgentRosterProperty(e) !== void 0;
}
/** Lists unique configured agent ids. */
function listAgentIds(e) {
const t = readAgentRosterFacts(e);
if (t?.agentIds) return t.agentIds;
const n = listAgentEntries(e);
const r = n.length === 0 && !hasAgentRosterProperty(e) ? [LEGACY_IMPLICIT_AGENT_ID] : [...new Set(n.map((e) => normalizeAgentId(e?.id)))];
if (t) {
Object.freeze(r);
t.agentIds = r;
}
return r;
}
/** Lists ACP harness ids and ACP-runtime roster agents named by config; native agents are excluded. */
function resolveConfiguredAcpHarnessIds(e) {
const t = /* @__PURE__ */ new Set();
for (const n of listAgentEntries(e)) {
if (n.runtime?.type !== "acp") continue;
t.add(normalizeAgentId(n.id));
const e = normalizeOptionalAgentId(n.runtime.acp?.agent);
if (e) t.add(e);
}
const n = [e.acp?.defaultAgent, ...(e.acp?.allowedAgents ?? []).filter((e) => e.trim() !== "*")];
for (const e of n) {
const n = normalizeOptionalAgentId(e);
if (n) t.add(n);
}
return t;
}
/** Lists configured roster and ACP harness targets for subagent policy. */
function resolveConfiguredAcpSubagentTargetIds(e) {
return /* @__PURE__ */ new Set([...listAgentIds(e), ...resolveConfiguredAcpHarnessIds(e)]);
}
/** Reserved system agents belong to the Gateway without authored roster entries. */
function listGatewayOwnedAgentIds(e) {
return [.../* @__PURE__ */ new Set([...listAgentIds(e), ...SYSTEM_AGENT_ROSTER_ENTRIES.map(({ id: e }) => e)])];
}
function tryResolveSoleAgentId(e) {
const t = collectAgentEntries(e, false, 2);
if (t.length === 0) {
if (!hasAgentRosterProperty(e)) return LEGACY_IMPLICIT_AGENT_ID;
return;
}
return t.length === 1 ? normalizeAgentId(t[0].id) : void 0;
}
function tryResolveRawLegacyDefaultAgentId(e) {
if (e.agents?.ownership === "explicit") return;
const t = collectAgentEntries(e, false).filter((e) => e.default === true);
return t.length === 1 ? normalizeAgentId(t[0].id) : void 0;
}
/** @deprecated Use tryResolveSoleAgentId; accepts raw shipped markers only for input compatibility. */
function tryResolveDefaultAgentId(e) {
return tryResolveRawLegacyDefaultAgentId(e) ?? tryResolveSoleAgentId(e);
}
/** Preserves legacy data locators independently of the configured runtime owner. */
function tryResolveLegacyDataOwner(e) {
return tryResolveDefaultAgentId(e);
}
//#endregion
export { listGatewayOwnedAgentIds as a, resolveConfiguredAcpHarnessIds as c, tryResolveLegacyDataOwner as d, tryResolveRawLegacyDefaultAgentId as f, isReservedSystemAgentId as g, SYSTEM_AGENT_ID as h, listAgentIds as i, resolveConfiguredAcpSubagentTargetIds as l, withAgentRosterFactsBatch as m, listAgentEntries as n, readAgentRosterFacts as o, tryResolveSoleAgentId as p, listAgentEntriesWithSource as r, readAgentRosterProperty as s, hasAgentRosterProperty as t, tryResolveDefaultAgentId as u };