mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 08:22:47 +00:00
refactor(sandbox): share workspace layout setup
This commit is contained in:
@@ -17,6 +17,53 @@ import { resolveSandboxRuntimeStatus } from "./runtime-status.js";
|
|||||||
import { resolveSandboxScopeKey, resolveSandboxWorkspaceDir } from "./shared.js";
|
import { resolveSandboxScopeKey, resolveSandboxWorkspaceDir } from "./shared.js";
|
||||||
import { ensureSandboxWorkspace } from "./workspace.js";
|
import { ensureSandboxWorkspace } from "./workspace.js";
|
||||||
|
|
||||||
|
async function ensureSandboxWorkspaceLayout(params: {
|
||||||
|
cfg: ReturnType<typeof resolveSandboxConfigForAgent>;
|
||||||
|
rawSessionKey: string;
|
||||||
|
config?: OpenClawConfig;
|
||||||
|
workspaceDir?: string;
|
||||||
|
}): Promise<{
|
||||||
|
agentWorkspaceDir: string;
|
||||||
|
scopeKey: string;
|
||||||
|
sandboxWorkspaceDir: string;
|
||||||
|
workspaceDir: string;
|
||||||
|
}> {
|
||||||
|
const { cfg, rawSessionKey } = params;
|
||||||
|
|
||||||
|
const agentWorkspaceDir = resolveUserPath(
|
||||||
|
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
||||||
|
);
|
||||||
|
const workspaceRoot = resolveUserPath(cfg.workspaceRoot);
|
||||||
|
const scopeKey = resolveSandboxScopeKey(cfg.scope, rawSessionKey);
|
||||||
|
const sandboxWorkspaceDir =
|
||||||
|
cfg.scope === "shared" ? workspaceRoot : resolveSandboxWorkspaceDir(workspaceRoot, scopeKey);
|
||||||
|
const workspaceDir = cfg.workspaceAccess === "rw" ? agentWorkspaceDir : sandboxWorkspaceDir;
|
||||||
|
|
||||||
|
if (workspaceDir === sandboxWorkspaceDir) {
|
||||||
|
await ensureSandboxWorkspace(
|
||||||
|
sandboxWorkspaceDir,
|
||||||
|
agentWorkspaceDir,
|
||||||
|
params.config?.agents?.defaults?.skipBootstrap,
|
||||||
|
);
|
||||||
|
if (cfg.workspaceAccess !== "rw") {
|
||||||
|
try {
|
||||||
|
await syncSkillsToWorkspace({
|
||||||
|
sourceWorkspaceDir: agentWorkspaceDir,
|
||||||
|
targetWorkspaceDir: sandboxWorkspaceDir,
|
||||||
|
config: params.config,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
const message = error instanceof Error ? error.message : JSON.stringify(error);
|
||||||
|
defaultRuntime.error?.(`Sandbox skill sync failed: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await fs.mkdir(workspaceDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
return { agentWorkspaceDir, scopeKey, sandboxWorkspaceDir, workspaceDir };
|
||||||
|
}
|
||||||
|
|
||||||
export async function resolveSandboxContext(params: {
|
export async function resolveSandboxContext(params: {
|
||||||
config?: OpenClawConfig;
|
config?: OpenClawConfig;
|
||||||
sessionKey?: string;
|
sessionKey?: string;
|
||||||
@@ -39,35 +86,12 @@ export async function resolveSandboxContext(params: {
|
|||||||
|
|
||||||
await maybePruneSandboxes(cfg);
|
await maybePruneSandboxes(cfg);
|
||||||
|
|
||||||
const agentWorkspaceDir = resolveUserPath(
|
const { agentWorkspaceDir, scopeKey, workspaceDir } = await ensureSandboxWorkspaceLayout({
|
||||||
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
cfg,
|
||||||
);
|
rawSessionKey,
|
||||||
const workspaceRoot = resolveUserPath(cfg.workspaceRoot);
|
config: params.config,
|
||||||
const scopeKey = resolveSandboxScopeKey(cfg.scope, rawSessionKey);
|
workspaceDir: params.workspaceDir,
|
||||||
const sandboxWorkspaceDir =
|
});
|
||||||
cfg.scope === "shared" ? workspaceRoot : resolveSandboxWorkspaceDir(workspaceRoot, scopeKey);
|
|
||||||
const workspaceDir = cfg.workspaceAccess === "rw" ? agentWorkspaceDir : sandboxWorkspaceDir;
|
|
||||||
if (workspaceDir === sandboxWorkspaceDir) {
|
|
||||||
await ensureSandboxWorkspace(
|
|
||||||
sandboxWorkspaceDir,
|
|
||||||
agentWorkspaceDir,
|
|
||||||
params.config?.agents?.defaults?.skipBootstrap,
|
|
||||||
);
|
|
||||||
if (cfg.workspaceAccess !== "rw") {
|
|
||||||
try {
|
|
||||||
await syncSkillsToWorkspace({
|
|
||||||
sourceWorkspaceDir: agentWorkspaceDir,
|
|
||||||
targetWorkspaceDir: sandboxWorkspaceDir,
|
|
||||||
config: params.config,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const message = error instanceof Error ? error.message : JSON.stringify(error);
|
|
||||||
defaultRuntime.error?.(`Sandbox skill sync failed: ${message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
const containerName = await ensureSandboxContainer({
|
const containerName = await ensureSandboxContainer({
|
||||||
sessionKey: rawSessionKey,
|
sessionKey: rawSessionKey,
|
||||||
@@ -143,35 +167,12 @@ export async function ensureSandboxWorkspaceForSession(params: {
|
|||||||
|
|
||||||
const cfg = resolveSandboxConfigForAgent(params.config, runtime.agentId);
|
const cfg = resolveSandboxConfigForAgent(params.config, runtime.agentId);
|
||||||
|
|
||||||
const agentWorkspaceDir = resolveUserPath(
|
const { workspaceDir } = await ensureSandboxWorkspaceLayout({
|
||||||
params.workspaceDir?.trim() || DEFAULT_AGENT_WORKSPACE_DIR,
|
cfg,
|
||||||
);
|
rawSessionKey,
|
||||||
const workspaceRoot = resolveUserPath(cfg.workspaceRoot);
|
config: params.config,
|
||||||
const scopeKey = resolveSandboxScopeKey(cfg.scope, rawSessionKey);
|
workspaceDir: params.workspaceDir,
|
||||||
const sandboxWorkspaceDir =
|
});
|
||||||
cfg.scope === "shared" ? workspaceRoot : resolveSandboxWorkspaceDir(workspaceRoot, scopeKey);
|
|
||||||
const workspaceDir = cfg.workspaceAccess === "rw" ? agentWorkspaceDir : sandboxWorkspaceDir;
|
|
||||||
if (workspaceDir === sandboxWorkspaceDir) {
|
|
||||||
await ensureSandboxWorkspace(
|
|
||||||
sandboxWorkspaceDir,
|
|
||||||
agentWorkspaceDir,
|
|
||||||
params.config?.agents?.defaults?.skipBootstrap,
|
|
||||||
);
|
|
||||||
if (cfg.workspaceAccess !== "rw") {
|
|
||||||
try {
|
|
||||||
await syncSkillsToWorkspace({
|
|
||||||
sourceWorkspaceDir: agentWorkspaceDir,
|
|
||||||
targetWorkspaceDir: sandboxWorkspaceDir,
|
|
||||||
config: params.config,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
const message = error instanceof Error ? error.message : JSON.stringify(error);
|
|
||||||
defaultRuntime.error?.(`Sandbox skill sync failed: ${message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
await fs.mkdir(workspaceDir, { recursive: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
workspaceDir,
|
workspaceDir,
|
||||||
|
|||||||
Reference in New Issue
Block a user