openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
93 lines (92 loc) • 3.87 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import "./paths-DAcAg5aV.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { i as normalizeMainKey } from "./session-key-DW1jcHbM.mjs";
import "./session-key-CqAyZeVk.mjs";
import { a as listGatewayOwnedAgentIds, g as isReservedSystemAgentId, n as listAgentEntries, u as tryResolveDefaultAgentId } from "./agent-roster-t1ooUdXp.mjs";
import { A as tryResolveLegacyCompatibilityAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import "./legacy.default-agent-owner-DpmHgK0A.mjs";
import { S as readAgentDatabaseAdmissionRefusal } from "./agent-database-admission-CL6wHJ_N.mjs";
import { r as prepareAgentDatabaseDeletionSnapshotRead } from "./agent-deletion-journal.read-DjOO2RZM.mjs";
import { i as getPreparedAgentDeletionDiscovery } from "./agent-deletion-journal-status-CZ5XyH3P.mjs";
import path from "node:path";
import fs from "node:fs/promises";
//#region src/gateway/agent-list.ts
async function listExistingAgentIdsFromDisk() {
const e = path.join(resolveStateDir(), "agents");
try {
return (await fs.readdir(e, { withFileTypes: true })).filter((e) => e.isDirectory()).map((e) => normalizeAgentId(e.name));
} catch {
return [];
}
}
function resolveGatewayAgentSelectionState(e) {
const t = listAgentEntries(e).map((e) => normalizeAgentId(e.id));
const n = tryResolveDefaultAgentId(e);
if (n) return {
defaultId: normalizeAgentId(n),
ownership: "sole",
selectionRequired: false
};
const r = tryResolveLegacyCompatibilityAgentId(e);
const i = r ?? t[0];
if (!i) throw new Error("Cannot project gateway agent ownership without a configured agent.");
return {
defaultId: normalizeAgentId(i),
ownership: r && e.agents?.ownership !== "explicit" ? "legacy" : "explicit",
selectionRequired: !r
};
}
/** Lists gateway-visible agents with canonical membership, ordering, and semantic kind. */
async function listGatewayAgentsBasic(e, t) {
t?.throwIfAborted();
const n = resolveGatewayAgentSelectionState(e);
const r = n.defaultId;
const i = normalizeMainKey(e.session?.mainKey);
const a = e.session?.scope ?? "per-sender";
const o = /* @__PURE__ */ new Map();
const s = new Set(await listExistingAgentIdsFromDisk());
for (const t of listAgentEntries(e)) {
if (!t?.id) continue;
const e = normalizeAgentId(t.id);
const n = normalizeOptionalString(t.name);
const r = normalizeOptionalString(t.identity?.name);
o.set(e, n ?? r);
}
const c = o.size > 0 ? new Set(listGatewayOwnedAgentIds(e)) : null;
const l = [.../* @__PURE__ */ new Set([
r,
...o.keys(),
...s
])].filter((e) => !c || c.has(e));
l.sort((e, t) => e.localeCompare(t));
const u = r && l.includes(r) ? [r, ...l.filter((e) => e !== r)] : l;
if (i && !u.includes(i) && (!c || o.has(i))) u.push(i);
const d = getPreparedAgentDeletionDiscovery(resolveOpenClawStateSqlitePath()) ?? (await prepareAgentDatabaseDeletionSnapshotRead({}, "runtime-discovery", t).read()).snapshot?.retainedDeletions;
const f = d?.status === "present" ? d : d?.status === "unavailable" ? d.known : void 0;
t?.throwIfAborted();
const p = u.filter((e) => !f?.entries.some((t) => t.agentId === e) && !f?.held.some((t) => t.agentId === e)).map((e) => {
const t = readAgentDatabaseAdmissionRefusal(e);
const n = {
id: e,
kind: !o.has(e) && s.has(e) && isReservedSystemAgentId(e) ? "system" : "agent",
name: o.get(e)
};
if (t) {
n.status = "degraded";
n.admissionRefusal = t;
}
return n;
});
return {
...n,
mainKey: i,
scope: a,
agents: p
};
}
//#endregion
export { resolveGatewayAgentSelectionState as n, listGatewayAgentsBasic as t };