UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

99 lines (98 loc) • 3.82 kB
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; //#region src/shared/node-match.ts function normalizeNodeKey(e) { return normalizeLowercaseStringOrEmpty(e.normalize("NFC")).replace(/(?=\p{M})\p{Emoji_Component}/gu, "").replace(/(?<![\p{L}\p{M}\p{N}])\p{M}+/gu, "").replace(/[^\p{L}\p{M}\p{N}]+/gu, "-").replace(/^-+/, "").replace(/-+$/, ""); } function formatNodeCandidateLabel(e) { const t = e.displayName || e.remoteIp || e.nodeId; const n = [`node=${e.nodeId}`]; const r = normalizeOptionalString(e.clientId); if (r) n.push(`client=${r}`); return `${t} [${n.join(", ")}]`; } function resolveNodeIdFromCandidates(e, t, n = false) { const r = t.trim(); if (!r) throw new Error("node required"); let i = e.filter((e) => e.nodeId === r); if (i.length === 0) i = e.filter((e) => e.remoteIp === r); if (i.length === 0) { const t = normalizeNodeKey(r); const a = n ? t.replace(/-/g, "") : void 0; let o = 0; e.forEach((e) => { const n = typeof e.displayName === "string" ? e.displayName : ""; const s = n ? normalizeNodeKey(n) : ""; let c = 0; if (s && s === t) c = 2e3; else if (a !== void 0 && s && s.replace(/-/g, "") === a) c = 1900; else if (r.length >= 6 && e.nodeId.startsWith(r)) c = 1e3; if (c > o) { o = c; i.length = 0; } if (c > 0 && c === o) i.push(e); }); } if (i.length === 0) { const t = e.map((e) => e.displayName || e.remoteIp || e.nodeId).filter(Boolean).join(", "); throw new Error(`unknown node: ${r}${t ? ` (known: ${t})` : ""}`); } const a = i.filter((e) => e.connected === true); const o = a.length > 0 ? a : i; if (o.length === 1) return o[0]?.nodeId ?? ""; throw new Error(`ambiguous node: ${r} (matches: ${o.map(formatNodeCandidateLabel).join(", ")})`); } //#endregion //#region src/shared/node-resolve.ts /** Resolves a user query to a node id, optionally using a caller-defined blank-query default. */ function resolveNodeIdFromNodeList(e, t, n = {}) { const r = normalizeOptionalString(t) ?? ""; if (!r) { if (n.allowDefault === true && n.pickDefaultNode) { const t = n.pickDefaultNode(e); if (t) return t.nodeId; } throw new Error("node required"); } return resolveNodeIdFromCandidates(e, r, n.allowCompactDisplayName); } /** Resolves a full node entry, preserving synthetic defaults returned by the picker. */ function resolveNodeFromNodeList(e, t, n = {}) { const r = resolveNodeIdFromNodeList(e, t, n); return e.find((e) => e.nodeId === r) ?? { nodeId: r }; } function formatNodeIdList(e) { return e.length > 0 ? e.map((e) => e.nodeId).toSorted().join(", ") : "none"; } /** * Resolves a capability-gated node from the full node list. Exact ids are * checked before eligible-name resolution so an ineligible id cannot redirect * to an eligible node that shares its display name. */ function resolveEligibleNodeFromList(e, t, n, r) { const i = e.filter(n); const a = t?.trim(); if (a) { const t = a.toLowerCase(); const o = e.find((e) => e.nodeId === a) ?? e.find((e) => e.nodeId.toLowerCase() === t); if (o) { if (!n(o)) throw new Error(r.ineligibleExact(a, formatNodeIdList(i))); return o; } try { const e = resolveNodeIdFromNodeList(i, a); const t = i.find((t) => t.nodeId === e); if (t) return t; } catch (e) { throw new Error(r.nameResolveFailed(formatErrorMessage(e), formatNodeIdList(i)), { cause: e }); } throw new Error(`node not found: ${a}`); } const o = i.length === 1 ? i.at(0) : void 0; if (o) return o; if (i.length === 0) throw new Error(r.noneEligible()); throw new Error(r.multipleEligible(i)); } //#endregion export { resolveNodeFromNodeList as n, resolveNodeIdFromNodeList as r, resolveEligibleNodeFromList as t };