openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
165 lines (164 loc) • 6.86 kB
JavaScript
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty, t as hasNonEmptyString } from "./string-coerce-CCIiB4H7.mjs";
import { d as normalizeSortedUniqueTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { t as freezeJsonSnapshot } from "./immutable-data-Y7xVjZoy.mjs";
import { M as filterPublicNodeCommands } from "./node-commands-BfMJIlXP.mjs";
import { o as readDevicePairingNodeSnapshot } from "./device-pairing-store-readonly-CIEEZowO.mjs";
import { i as sameNodePermissionSurface, r as sameNodeApprovalSurfaceSet } from "./node-pairing-surface-0CH0V-VR.mjs";
import { n as projectNodePairing } from "./device-pairing-node.records-Bltm-ujI.mjs";
import { l as readNodeCatalogRevision, t as collectNodeCatalogRuntimeState } from "./node-registry-private-Bz6rjvDD.mjs";
//#region src/gateway/node-catalog-read.ts
function uniqueSortedStrings(...e) {
return normalizeSortedUniqueTrimmedStringList(e.flatMap((e) => e ?? []));
}
function firstNormalizedString(...e) {
return normalizeOptionalString(e.find(hasNonEmptyString));
}
function maxDefinedTimestamp(...e) {
const t = e.filter((e) => e !== void 0);
return t.length > 0 ? Math.max(...t) : void 0;
}
function resolveEffectiveLastSeen(e, t, n) {
const r = [
e?.connectedAtMs ? {
atMs: e.connectedAtMs,
reason: "connect"
} : void 0,
n?.lastSeenAtMs ? {
atMs: n.lastSeenAtMs,
reason: n.lastSeenReason
} : void 0,
n?.lastConnectedAtMs ? {
atMs: n.lastConnectedAtMs,
reason: "connect"
} : void 0,
t?.lastSeenAtMs ? {
atMs: t.lastSeenAtMs,
reason: t.lastSeenReason
} : void 0
].filter((e) => e !== void 0);
let i;
for (const e of r) if (!i || e.atMs > i.atMs) i = e;
if (!i) return {};
return {
lastSeenAtMs: i.atMs,
lastSeenReason: normalizeOptionalString(i.reason)
};
}
function compareKnownNodes(e, t) {
if (e.connected !== t.connected) return e.connected ? -1 : 1;
const n = normalizeLowercaseStringOrEmpty(e.displayName ?? e.nodeId);
const r = normalizeLowercaseStringOrEmpty(t.displayName ?? t.nodeId);
if (n < r) return -1;
if (n > r) return 1;
return e.nodeId.localeCompare(t.nodeId);
}
const preparedCatalogs = /* @__PURE__ */ new WeakMap();
/** Pairing publications and registry mutations own the lifetime of these read-only projections. */
async function readKnownNodeCatalog(e, t = "nodes") {
const n = await readDevicePairingNodeSnapshot();
const r = readNodeCatalogRevision(e);
let i = preparedCatalogs.get(e);
if (!i || i.pairing !== n || i.revision !== r || i.connectedNodes.some((e) => e.client.invalidated === true)) {
i = {
pairing: n,
revision: r,
connectedNodes: e.listConnectedForPairingStates(n.bindings),
views: /* @__PURE__ */ new Map()
};
preparedCatalogs.set(e, i);
}
let a = i.views.get(t);
if (!a) {
const r = projectNodePairing(n.paired);
const o = collectNodeCatalogRuntimeState(e, i.connectedNodes, t === "worker-environments");
const s = new Map(n.paired.filter((e) => n.bindings.has(e.deviceId)).filter((e) => hasNonEmptyString(e.deviceId)).map((e) => [e.deviceId, e]));
const c = new Map(r.paired.filter((e) => hasNonEmptyString(e.nodeId)).map((e) => [e.nodeId, {
node: e,
commands: filterPublicNodeCommands(e.commands ?? [])
}]));
const l = /* @__PURE__ */ new Map();
for (const e of t === "nodes" ? r.pending : []) {
if (!hasNonEmptyString(e.nodeId)) continue;
if (!l.has(e.nodeId)) l.set(e.nodeId, {
...e,
caps: uniqueSortedStrings(e.caps),
commands: filterPublicNodeCommands(uniqueSortedStrings(e.commands))
});
}
const u = new Map(i.connectedNodes.map((e) => [e.nodeId, e]));
const d = /* @__PURE__ */ new Set([
...s.keys(),
...c.keys(),
...l.keys(),
...u.keys()
]);
const f = [];
for (const e of d) {
const t = s.get(e);
const n = c.get(e);
const r = n?.node;
const i = u.get(e);
let a = l.get(e);
if (a && i) {
const e = !r && i.declaredPermissions === void 0 ? a.permissions : i.declaredPermissions;
if (!sameNodeApprovalSurfaceSet(a.caps, i.declaredCaps) || !sameNodeApprovalSurfaceSet(a.commands, i.declaredCommands) || !sameNodePermissionSurface(a.permissions, e)) a = void 0;
}
const d = o.workerSlotsByNodeId.get(e);
const p = o.workerBundleByNodeId.get(e);
const m = o.issuesByNodeId.get(e);
const h = resolveEffectiveLastSeen(i, t, r);
const g = maxDefinedTimestamp(r?.lastConnectedAtMs, i?.connectedAtMs);
const _ = i ? void 0 : r?.lastDisconnectedAtMs;
const v = i ? i.hostStats : r?.lastHostStats;
const nodeMetadata = (e) => firstNormalizedString(i?.[e], r?.[e], a?.[e]);
f.push({
nodeId: e,
displayName: firstNormalizedString(r?.displayName, i?.displayName, t?.displayName, a?.displayName),
platform: firstNormalizedString(i?.platform, r?.platform, t?.platform, a?.platform),
version: nodeMetadata("version"),
coreVersion: nodeMetadata("coreVersion"),
uiVersion: nodeMetadata("uiVersion"),
clientId: firstNormalizedString(i?.clientId, t?.clientId, a?.clientId),
clientMode: firstNormalizedString(i?.clientMode, t?.clientMode, a?.clientMode),
deviceFamily: nodeMetadata("deviceFamily"),
modelIdentifier: nodeMetadata("modelIdentifier"),
remoteIp: firstNormalizedString(i?.remoteIp, r?.remoteIp, t?.remoteIp, a?.remoteIp),
caps: i ? uniqueSortedStrings(i.caps) : uniqueSortedStrings(r?.caps),
commands: filterPublicNodeCommands(i ? uniqueSortedStrings(i.commands) : uniqueSortedStrings(n?.commands)),
computerUse: i?.computerUse,
sessionHost: i ? o.sessionHostNodeIds.has(e) : r?.sessionHost === true,
...i && o.deviceFolderNodeIds.has(e) ? { workerHost: { deviceFolders: true } } : {},
...v ? { hostStats: v } : {},
...i && d ? { workerSlots: d } : {},
...i && p ? { workerBundle: p } : {},
...m?.length ? { issues: m } : {},
nodePluginTools: i?.nodePluginTools,
pathEnv: i?.pathEnv,
permissions: i?.permissions ?? r?.permissions,
approvalState: a ? r ? "pending-reapproval" : "pending-approval" : r ? "approved" : "unapproved",
pendingRequestId: a?.requestId,
pendingDeclaredCaps: a?.caps,
pendingDeclaredCommands: a?.commands,
pendingDeclaredPermissions: a?.permissions,
connectedAtMs: i?.connectedAtMs,
lastConnectedAtMs: g,
lastDisconnectedAtMs: _,
lastActiveAtMs: i?.lastActiveAtMs,
presenceUpdatedAtMs: i?.presenceUpdatedAtMs,
lastSeenAtMs: h.lastSeenAtMs,
lastSeenReason: h.lastSeenReason,
approvedAtMs: r?.approvedAtMs ?? t?.approvedAtMs,
paired: Boolean(t ?? r),
connected: Boolean(i)
});
}
a = Object.freeze({
nodes: freezeJsonSnapshot(structuredClone(f.toSorted(compareKnownNodes))),
connectedNodes: Object.freeze(i.connectedNodes)
});
i.views.set(t, a);
}
return a;
}
//#endregion
export { readKnownNodeCatalog as t };