mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 06:24:34 +00:00
refactor(sandbox): dedupe sandbox list helpers
This commit is contained in:
@@ -23,14 +23,18 @@ export type SandboxBrowserInfo = SandboxBrowserRegistryEntry & {
|
|||||||
imageMatch: boolean;
|
imageMatch: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function listSandboxContainers(): Promise<SandboxContainerInfo[]> {
|
async function listSandboxRegistryItems<
|
||||||
const config = loadConfig();
|
TEntry extends { containerName: string; image: string; sessionKey: string },
|
||||||
const registry = await readRegistry();
|
>(params: {
|
||||||
const results: SandboxContainerInfo[] = [];
|
read: () => Promise<{ entries: TEntry[] }>;
|
||||||
|
resolveConfiguredImage: (agentId: string) => string;
|
||||||
|
}): Promise<Array<TEntry & { running: boolean; imageMatch: boolean }>> {
|
||||||
|
const registry = await params.read();
|
||||||
|
const results: Array<TEntry & { running: boolean; imageMatch: boolean }> = [];
|
||||||
|
|
||||||
for (const entry of registry.entries) {
|
for (const entry of registry.entries) {
|
||||||
const state = await dockerContainerState(entry.containerName);
|
const state = await dockerContainerState(entry.containerName);
|
||||||
// Get actual image from container
|
// Get actual image from container.
|
||||||
let actualImage = entry.image;
|
let actualImage = entry.image;
|
||||||
if (state.exists) {
|
if (state.exists) {
|
||||||
try {
|
try {
|
||||||
@@ -46,7 +50,7 @@ export async function listSandboxContainers(): Promise<SandboxContainerInfo[]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const agentId = resolveSandboxAgentId(entry.sessionKey);
|
const agentId = resolveSandboxAgentId(entry.sessionKey);
|
||||||
const configuredImage = resolveSandboxConfigForAgent(config, agentId).docker.image;
|
const configuredImage = params.resolveConfiguredImage(agentId);
|
||||||
results.push({
|
results.push({
|
||||||
...entry,
|
...entry,
|
||||||
image: actualImage,
|
image: actualImage,
|
||||||
@@ -58,38 +62,21 @@ export async function listSandboxContainers(): Promise<SandboxContainerInfo[]> {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function listSandboxContainers(): Promise<SandboxContainerInfo[]> {
|
||||||
|
const config = loadConfig();
|
||||||
|
return listSandboxRegistryItems<SandboxRegistryEntry>({
|
||||||
|
read: readRegistry,
|
||||||
|
resolveConfiguredImage: (agentId) => resolveSandboxConfigForAgent(config, agentId).docker.image,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export async function listSandboxBrowsers(): Promise<SandboxBrowserInfo[]> {
|
export async function listSandboxBrowsers(): Promise<SandboxBrowserInfo[]> {
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
const registry = await readBrowserRegistry();
|
return listSandboxRegistryItems<SandboxBrowserRegistryEntry>({
|
||||||
const results: SandboxBrowserInfo[] = [];
|
read: readBrowserRegistry,
|
||||||
|
resolveConfiguredImage: (agentId) =>
|
||||||
for (const entry of registry.entries) {
|
resolveSandboxConfigForAgent(config, agentId).browser.image,
|
||||||
const state = await dockerContainerState(entry.containerName);
|
});
|
||||||
let actualImage = entry.image;
|
|
||||||
if (state.exists) {
|
|
||||||
try {
|
|
||||||
const result = await execDocker(
|
|
||||||
["inspect", "-f", "{{.Config.Image}}", entry.containerName],
|
|
||||||
{ allowFailure: true },
|
|
||||||
);
|
|
||||||
if (result.code === 0) {
|
|
||||||
actualImage = result.stdout.trim();
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const agentId = resolveSandboxAgentId(entry.sessionKey);
|
|
||||||
const configuredImage = resolveSandboxConfigForAgent(config, agentId).browser.image;
|
|
||||||
results.push({
|
|
||||||
...entry,
|
|
||||||
image: actualImage,
|
|
||||||
running: state.running,
|
|
||||||
imageMatch: actualImage === configuredImage,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeSandboxContainer(containerName: string): Promise<void> {
|
export async function removeSandboxContainer(containerName: string): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user