openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
126 lines (125 loc) • 5.33 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as stableStringify } from "./stable-stringify-vqde_3tT.mjs";
import { o as getGatewayToolCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs";
import { f as COMPUTER_USE_V1_ACTION_NAMES, p as COMPUTER_USE_V2_ACTION_NAMES, r as parseComputerUseCapabilityDescriptor } from "./computer-use-contract-DrB9JL-Y.mjs";
import { s as shouldUseInProcessGatewayTool, t as callGatewayTool } from "./gateway-DL_Nxi-v.mjs";
import { t as createAgentRuntimeIdentity } from "./agent-runtime-identity-token-BK52eBqW.mjs";
import { i as SCREEN_SNAPSHOT_COMMAND, t as COMPUTER_ACT_COMMAND } from "./computer-tool-shared-DjAanHYO.mjs";
import { n as listNodes } from "./nodes-utils-B4u6gtyR.mjs";
//#region src/agents/tools/computer-tool-gateway.ts
async function loadGatewayComputerStatus(e, t, n = false) {
const r = await callGatewayTool("computer.status", e, { probe: n }, { signal: t });
if (!isRecord(r) || typeof r.configured !== "boolean") throw new Error("COMPUTER_CONTRACT_MISMATCH: invalid Gateway computer status");
if (!r.configured) return {
configured: false,
available: false
};
if (r.available !== true) return {
configured: true,
available: false,
...r.computerUse ? { computerUse: parseComputerUseCapabilityDescriptor(r.computerUse) } : {},
...typeof r.error === "string" ? { error: r.error } : {}
};
return {
configured: true,
available: true,
computerUse: parseComputerUseCapabilityDescriptor(r.computerUse)
};
}
/** Capture a fixed native close while its run is admitted; cleanup cannot open another execution. */
async function bindGatewayComputerCleanup(e) {
const t = getGatewayToolCallerIdentity();
if (!shouldUseInProcessGatewayTool(e.options) || !t?.operationalRunInstance) return;
const n = await createAgentRuntimeIdentity({
...t,
operationalRunInstance: t.operationalRunInstance
});
if (!n) throw new Error("Gateway computer cleanup requires the admitted run identity");
const { bindAgentToolGatewayRequest: r, runWithGatewayToolCleanupContext: i, withAgentToolGatewayRuntimeIdentity: a } = await import("./in-process-gateway-uUYBiTqp.mjs");
const o = i(() => r({ resolveGatewayContext: t.gatewayContextResolver }), t.gatewayContextResolver);
return async (t) => {
return (await o(a({
method: "computer.invoke",
params: {
generation: e.generation,
command: "computer.act",
params: {
action: "__close_execution",
executionId: e.executionId,
reason: t
},
idempotencyKey: `computer.close:${e.executionId}:gateway`
},
timeoutMs: e.options.timeoutMs
}, n))).payload;
};
}
//#endregion
//#region src/agents/computer-use-node-capabilities.ts
async function readLocalComputerNodes(e) {
if (!e) throw new Error("Gateway context is no longer available");
const { readKnownNodeCatalog: t } = await import("./node-catalog-read-BuA-oJdZ.mjs");
return (await t(e.nodeRegistry)).nodes;
}
async function readLocalComputerStatus(e) {
if (!e) throw new Error("Gateway context is no longer available");
const t = await e.gatewayComputerService?.status({ probe: false });
if (!t?.configured) return {
configured: false,
available: false
};
return t.available && t.computerUse ? {
configured: true,
available: true,
computerUse: t.computerUse
} : {
configured: true,
available: false,
computerUse: t.computerUse,
error: t.error
};
}
/** Loads host and node inventory only when an ordinary computer tool can reach the model. */
async function loadPairedComputerUseAvailabilityForSurface(e) {
if (!e.computerAllowed || e.modelHasVision === false || e.embeddedMode === true || e.computerTransport !== void 0) return;
const { signal: t } = e;
const n = shouldUseInProcessGatewayTool({});
const r = n ? getGatewayToolCallerIdentity()?.gatewayContextResolver?.() : void 0;
const [i, a] = await Promise.all([(n ? readLocalComputerNodes(r) : listNodes({}, t)).catch(() => {
t?.throwIfAborted();
return [];
}), (n ? readLocalComputerStatus(r) : loadGatewayComputerStatus({}, t)).catch((e) => {
t?.throwIfAborted();
return {
configured: true,
available: false,
error: e instanceof Error ? e.message : "Gateway computer discovery failed"
};
})]);
t?.throwIfAborted();
const o = i.filter(isEligibleComputerNode);
const s = stableStringify({
gateway: a,
nodes: o.sort((e, t) => e.nodeId.localeCompare(t.nodeId)).map(({ nodeId: e, computerUse: t }) => ({
nodeId: e,
computerUse: t
}))
});
const c = [...o.map((e) => e.computerUse), ...a.configured && a.computerUse ? [a.computerUse] : []];
const l = new Set(c.flatMap((e) => e?.actions ?? COMPUTER_USE_V1_ACTION_NAMES));
return {
cacheKey: s,
prepared: {
actions: COMPUTER_USE_V2_ACTION_NAMES.filter((e) => l.has(e)),
gateway: a,
guidanceCapabilities: c.length === 1 ? c[0] : void 0
}
};
}
/** A paired target must support both observation and input to enter the computer tool pool. */
function isEligibleComputerNode(e) {
const t = Array.isArray(e.commands) ? e.commands : [];
return e.connected === true && t.includes("computer.act") && t.includes("screen.snapshot");
}
//#endregion
export { loadGatewayComputerStatus as i, loadPairedComputerUseAvailabilityForSurface as n, bindGatewayComputerCleanup as r, isEligibleComputerNode as t };