openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
129 lines (128 loc) • 4.69 kB
JavaScript
import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs";
import "./config-CMA-J084.mjs";
import { r as execContainer } from "./container-engine-CscrTTHa.mjs";
import { p as removeSandboxContainerRuntime, s as validateSandboxContainerEngineTarget } from "./podman-runtime-CUqzctoD.mjs";
import "./docker-DFBRw_yx.mjs";
import { n as resolveSandboxAgentId } from "./shared-BfMYtWUR.mjs";
import { a as readBrowserRegistry, d as removeSandboxRegistryGeneration, f as removeSandboxRegistryRuntime, l as removeBrowserRegistryEntry, s as readRegistry, u as removeRegistryEntry } from "./registry-kRRGa9R8.mjs";
import { c as usesSandboxRuntimeReservations, i as getSandboxBackendManager, l as dockerSandboxBackendManager } from "./backend-CR3qLOqu.mjs";
import { n as stopCachedBrowserBridge, r as stopCachedBrowserBridgesForContainer } from "./browser-bridges-B4Tr1hQM.mjs";
//#region src/agents/sandbox/manage.ts
function toBrowserDockerRuntimeEntry(e) {
return {
...e,
backendId: "docker",
runtimeLabel: e.containerName,
configLabelKind: "BrowserImage"
};
}
async function listSandboxRuntimes(e, t, n) {
const r = readRuntimeConfig();
const i = await e();
const a = [];
for (const e of i.entries) {
if (n && !n(e)) continue;
const i = t(e);
const o = i.manager ? await i.manager.describeRuntime({
entry: i.entry,
config: r,
agentId: resolveSandboxAgentId(e.sessionKey)
}) : {
running: false,
configLabelMatch: true
};
a.push({
...e,
image: o.actualConfigLabel ?? e.image,
running: o.running,
imageMatch: o.configLabelMatch
});
}
return a;
}
async function listSandboxContainers(e) {
return listSandboxRuntimes(readRegistry, (e) => ({
manager: getSandboxBackendManager(e.backendId ?? "docker"),
entry: e
}), e);
}
async function listSandboxBrowsers(e) {
return listSandboxRuntimes(readBrowserRegistry, (e) => ({
manager: dockerSandboxBackendManager,
entry: toBrowserDockerRuntimeEntry(e)
}), e);
}
/** Retire only the physical generation fenced by local workspace settlement. */
async function removeSandboxRuntimeGeneration(e) {
const { runtime: t, engine: n, id: r } = e;
const revalidateGeneration = async () => {
await e.assertGenerationCurrent?.();
e.assertCurrent();
};
const assertCurrent = () => {
e.database.assertCurrent();
e.assertCurrent();
};
if (r !== null && !/^[a-f0-9]{64}$/u.test(r)) throw new Error("Invalid sandbox runtime generation");
await revalidateGeneration();
assertCurrent();
await validateSandboxContainerEngineTarget(n, t.kind === "container" ? t.entry.backendTarget : void 0);
await revalidateGeneration();
assertCurrent();
await removeSandboxContainerRuntime(n, t.entry.containerName, {
id: r,
assertCurrent,
signal: e.signal
});
const assertAbsent = async () => {
const e = await execContainer(n, [
"inspect",
"-f",
"{{.Id}}",
t.entry.containerName
], {
allowFailure: true,
signal: AbortSignal.timeout(3e4)
});
await revalidateGeneration();
assertCurrent();
if (e.code === 0 || !/no such (?:container|object)|does not exist/iu.test(e.stderr)) throw new Error("Sandbox runtime generation changed or removal is unconfirmed; custody retained");
};
await assertAbsent();
for (const [t, n] of e.bridges) {
assertCurrent();
await stopCachedBrowserBridge(t, n, assertCurrent);
await revalidateGeneration();
assertCurrent();
}
if (e.bridges.length) await assertAbsent();
await removeSandboxRegistryGeneration(t.kind, t.entry, assertCurrent, e.database);
}
async function removeSandboxContainer(e) {
const t = readRuntimeConfig();
const n = (await readRegistry()).entries.find((t) => t.containerName === e);
if (n) {
const e = n.backendId ?? "docker";
const r = getSandboxBackendManager(e);
if (!r) throw new Error(`Sandbox backend "${e}" is unavailable; enable its plugin before removing this runtime.`);
await removeSandboxRegistryRuntime(n, (e) => r.removeRuntime({
entry: e,
config: t,
agentId: resolveSandboxAgentId(e.sessionKey)
}), { reserveRuntime: usesSandboxRuntimeReservations(e) });
return;
}
await removeRegistryEntry(e);
}
async function removeSandboxBrowserContainer(e) {
const t = readRuntimeConfig();
const n = (await readBrowserRegistry()).entries.find((t) => t.containerName === e);
await stopCachedBrowserBridgesForContainer(e);
if (n) await dockerSandboxBackendManager.removeRuntime({
entry: toBrowserDockerRuntimeEntry(n),
config: t
});
await removeBrowserRegistryEntry(e);
}
//#endregion
export { removeSandboxRuntimeGeneration as a, removeSandboxContainer as i, listSandboxContainers as n, removeSandboxBrowserContainer as r, listSandboxBrowsers as t };