openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
91 lines (90 loc) • 3.52 kB
JavaScript
import { c as normalizeOptionalLowercaseString } from "./string-coerce-CCIiB4H7.mjs";
import { n as sha256Hex } from "./node-crypto-B6yuTjmm.mjs";
import { n as resolveNodeFromNodeList, r as resolveNodeIdFromNodeList } from "./node-resolve-CkGq9hyv.mjs";
import { t as callGatewayTool } from "./gateway-DL_Nxi-v.mjs";
import { t as SYSTEM_RUN_EXECUTION_CONTEXT_CAPABILITY } from "./system-run-execution-context-UpbI778p.mjs";
import { n as getAgentToolAssistantTurnId } from "./tool-execution-context-D0eUSg2-.mjs";
import { t as parseNodeList } from "./node-list-parse-DT6NmnCE.mjs";
import crypto from "node:crypto";
//#region src/agents/tools/nodes-utils.ts
function nodeToolIdempotencyKey(e) {
const t = e.scope?.trim();
const n = e.toolCallId.trim();
if (!t || !n) return crypto.randomUUID();
const r = [
t,
getAgentToolAssistantTurnId() ?? "",
n,
e.command
];
if (e.purpose) r.push(e.purpose);
return `${e.purpose ? "computer.observation" : e.command}:v2:${sha256Hex(JSON.stringify(r))}`;
}
function isLocalMacNode(e) {
return normalizeOptionalLowercaseString(e.platform)?.startsWith("mac") === true && typeof e.nodeId === "string" && e.nodeId.startsWith("mac-");
}
function compareNewestTimestamp(e, t) {
const n = Number.isFinite(e) ? e ?? 0 : -1;
return (Number.isFinite(t) ? t ?? 0 : -1) - n;
}
function selectDefaultNodeFromList(e, t = {}) {
const n = t.capability?.trim();
const r = n ? e.filter((e) => Array.isArray(e.caps) ? e.caps.includes(n) : true) : e;
if (r.length === 0) return null;
const i = r.filter((e) => e.connected);
const a = i.length > 0 ? i : r;
if (a.length === 1) return a.at(0) ?? null;
if (t.preferLocalMac ?? true) {
const e = a.filter(isLocalMacNode);
if (e.length === 1) return e.at(0) ?? null;
}
if ((t.fallback ?? "none") === "none") return null;
const o = i.length > 0 ? "connectedAtMs" : "lastSeenAtMs";
return a.reduce((e, t) => {
return (e ? compareNewestTimestamp(t[o], e[o]) || t.nodeId.localeCompare(e.nodeId) : -1) < 0 ? t : e;
}, null);
}
function pickDefaultNode(e) {
return selectDefaultNodeFromList(e, {
capability: "canvas",
fallback: "first",
preferLocalMac: true
});
}
async function listNodes(e, t) {
let n = true;
const r = await callGatewayTool("node.list", e, {}, {
signal: t,
onHelloOk: (e) => {
n = e.features.capabilities?.includes(SYSTEM_RUN_EXECUTION_CONTEXT_CAPABILITY) === true;
}
});
const i = parseNodeList(r);
if (!n) for (const e of i) e.caps = e.caps?.filter((e) => e !== SYSTEM_RUN_EXECUTION_CONTEXT_CAPABILITY);
return i;
}
function resolveNodeIdFromList(e, t, n = false, r = {}) {
return resolveNodeIdFromNodeList(e, t, {
allowDefault: n,
allowCompactDisplayName: r.allowCompactDisplayName,
pickDefaultNode
});
}
async function resolveAgentNodeId(e, t) {
return (await resolveAgentNode(e, t)).nodeId;
}
async function resolveAgentNode(e, t) {
return resolveNodeFromNodeList(await listNodes(e), t);
}
async function invokeAgentNodeCommand(e) {
const t = await callGatewayTool("node.invoke", e.gatewayOpts, {
nodeId: e.nodeId,
command: e.command,
params: e.commandParams,
timeoutMs: e.timeoutMs,
idempotencyKey: e.idempotencyKey ?? crypto.randomUUID()
}, { signal: e.signal });
return t && typeof t === "object" && Object.hasOwn(t, "payload") ? t.payload : t;
}
//#endregion
export { resolveAgentNodeId as a, resolveAgentNode as i, listNodes as n, resolveNodeIdFromList as o, nodeToolIdempotencyKey as r, selectDefaultNodeFromList as s, invokeAgentNodeCommand as t };