openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
122 lines (121 loc) • 5.3 kB
JavaScript
import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { t as WorkerProviderError } from "./capability-provider.types-qOxhG-PT.mjs";
import { n as hasEffectivePairedDeviceRole } from "./device-pairing-identity-BqPJxbIo.mjs";
import { s as formatNodeRunnerInventoryIssue } from "./node-runner-inventory-CixlIhG-.mjs";
import "./device-pairing-D--Fzrek.mjs";
import { t as DEVICE_WORKER_PROVIDER_ID } from "./device-provider-identity-v6nXqNq_.mjs";
import { n as workerInferencePlacement } from "./inference-placement-DTrb-mxJ.mjs";
import { n as createNodeWorkerLaunchAdapter } from "./node-launch-adapter-D-g1brFW.mjs";
import { createHash } from "node:crypto";
//#region src/gateway/worker-environments/device-provider.ts
const DEVICE_WORKER_DORMANCY_MS = 12096e5;
const DEVICE_WORKER_AVAILABILITY = /* @__PURE__ */ new WeakMap();
const DEVICE_WORKER_RECONCILIATION = /* @__PURE__ */ new WeakMap();
function bindDeviceWorkerAvailability(e, t) {
DEVICE_WORKER_AVAILABILITY.set(e, t);
}
async function resolveDeviceWorkerAvailability(e, t) {
const n = e ? DEVICE_WORKER_AVAILABILITY.get(e) : void 0;
return n ? await n(t) : { available: false };
}
function deviceUnavailableText(e, t) {
if (t.issue) return formatNodeRunnerInventoryIssue(e, t.issue);
switch (t.unavailableReason) {
case "unpaired": return `device worker is not a paired node host: ${e}`;
case "disconnected": return `device worker node is not connected: ${e}; reconnect it before retrying`;
case "hosting-unavailable": return `device node ${e} is connected but cannot host sessions; enable session hosting (nodeHost.workerRuns.enabled), update the node if needed, then reconnect it`;
case "at-capacity": return `device worker is at capacity (all worker slots in use): ${e}; stop an existing worker environment or retry when a slot is free`;
default: return `device worker availability is unknown: ${e}; verify the node host is paired and connected, then retry`;
}
}
function bindDeviceWorkerReconciliation(e, t) {
DEVICE_WORKER_RECONCILIATION.set(e, t);
}
async function reconcileDeviceWorker(e, t) {
const n = e ? DEVICE_WORKER_RECONCILIATION.get(e) : void 0;
return n ? await n(t) : [];
}
function requireDeviceId(e) {
workerInferencePlacement({
providerId: DEVICE_WORKER_PROVIDER_ID,
profileSnapshot: { settings: e }
});
const t = e.device;
if (typeof t !== "string" || !t.trim()) throw new WorkerProviderError("device worker profile requires a device setting");
return t.trim();
}
function hasPairedNodeRole(e) {
return Boolean(e && hasEffectivePairedDeviceRole(e, "node"));
}
function isWithinDeviceDormancy(e, t) {
const n = e.nodeSurface?.lastDisconnectedAtMs;
return n === void 0 || t - n < DEVICE_WORKER_DORMANCY_MS;
}
function deviceLeaseId(e, t) {
return `device:${createHash("sha256").update(e).digest("hex")}:${createHash("sha256").update(t).digest("hex").slice(0, 32)}`;
}
/** Core runtime for already-paired node hosts; pairing remains the durable trust owner. */
function createDeviceWorkerRuntime(e) {
const t = e.now ?? Date.now;
let n;
const r = createNodeWorkerLaunchAdapter({ getTransport: () => n });
const resolveAvailability = async (t) => {
const [r, i] = await Promise.all([e.getPairedDevice(t), n?.getCurrentNode(t)]);
const a = i && n?.isCurrent(i) ? i : void 0;
const o = !hasPairedNodeRole(r) ? "unpaired" : !a ? n?.isConnected?.(t) ? "hosting-unavailable" : "disconnected" : void 0;
const s = n?.getIssue?.(t);
return {
available: o === void 0,
...o === void 0 && a ? { node: a } : {},
...s ? { issue: s } : {},
...o ? { unavailableReason: o } : {}
};
};
const i = {
id: DEVICE_WORKER_PROVIDER_ID,
supportedExecutionModes: ["worker-turn", "remote-exec"],
provisionBeforeInstallation: true,
resolveAllocation: async (e, t) => ({
leaseId: deviceLeaseId(requireDeviceId(e), t),
sharedHost: true
}),
provision: async (e, t, n) => {
if (!n?.assertCurrent) throw new WorkerProviderError("Device provisioning requires current Gateway allocation authority");
n.assertCurrent();
const r = requireDeviceId(e);
const a = await resolveAvailability(r);
n.assertCurrent();
if (!a.available) throw new WorkerProviderError(deviceUnavailableText(r, a));
const o = await i.resolveAllocation(e, t);
n.assertCurrent();
return {
...o,
node: { deviceId: r }
};
},
inspect: async ({ profile: r, signal: i }) => {
i?.throwIfAborted();
const a = requireDeviceId(r);
const o = await racePromiseWithAbortSignal(e.getPairedDevice(a), i);
if (!hasPairedNodeRole(o)) return { status: "unknown" };
if (await racePromiseWithAbortSignal(Promise.resolve(n?.getCurrentNode(a)), i)) return {
status: "active",
sharedHost: true
};
return isWithinDeviceDormancy(o, t()) ? { status: "dormant" } : { status: "unknown" };
},
destroy: async () => {}
};
return {
provider: i,
resolveAvailability,
launchNodeWorker: r.launch,
getNodeTransport: () => n,
bindNodeTransport: (e) => {
n = e;
}
};
}
//#endregion
export { reconcileDeviceWorker as a, deviceUnavailableText as i, bindDeviceWorkerReconciliation as n, resolveDeviceWorkerAvailability as o, createDeviceWorkerRuntime as r, bindDeviceWorkerAvailability as t };