|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync, mkdtempSync, mkdirSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +export const WORKSPACE_TEMPLATE_PACK_PATHS = [ |
| 7 | + "docs/reference/templates/AGENTS.md", |
| 8 | + "docs/reference/templates/SOUL.md", |
| 9 | + "docs/reference/templates/TOOLS.md", |
| 10 | + "docs/reference/templates/IDENTITY.md", |
| 11 | + "docs/reference/templates/USER.md", |
| 12 | + "docs/reference/templates/HEARTBEAT.md", |
| 13 | + "docs/reference/templates/BOOTSTRAP.md", |
| 14 | +]; |
| 15 | + |
| 16 | +const REQUIRED_BOOTSTRAP_WORKSPACE_FILES = [ |
| 17 | + "AGENTS.md", |
| 18 | + "SOUL.md", |
| 19 | + "TOOLS.md", |
| 20 | + "IDENTITY.md", |
| 21 | + "USER.md", |
| 22 | + "HEARTBEAT.md", |
| 23 | + "BOOTSTRAP.md", |
| 24 | +]; |
| 25 | + |
| 26 | +function collectMissingBootstrapWorkspaceFiles(workspaceDir) { |
| 27 | + return REQUIRED_BOOTSTRAP_WORKSPACE_FILES.filter( |
| 28 | + (filename) => !existsSync(join(workspaceDir, filename)), |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +function describeExecFailure(error) { |
| 33 | + if (!(error instanceof Error)) { |
| 34 | + return String(error); |
| 35 | + } |
| 36 | + const stdout = |
| 37 | + typeof error.stdout === "string" |
| 38 | + ? error.stdout.trim() |
| 39 | + : error.stdout instanceof Uint8Array |
| 40 | + ? Buffer.from(error.stdout).toString("utf8").trim() |
| 41 | + : ""; |
| 42 | + const stderr = |
| 43 | + typeof error.stderr === "string" |
| 44 | + ? error.stderr.trim() |
| 45 | + : error.stderr instanceof Uint8Array |
| 46 | + ? Buffer.from(error.stderr).toString("utf8").trim() |
| 47 | + : ""; |
| 48 | + return [error.message, stdout, stderr].filter(Boolean).join(" | "); |
| 49 | +} |
| 50 | + |
| 51 | +export function runInstalledWorkspaceBootstrapSmoke(params) { |
| 52 | + const tempRoot = mkdtempSync(join(tmpdir(), "openclaw-workspace-bootstrap-smoke-")); |
| 53 | + const homeDir = join(tempRoot, "home"); |
| 54 | + const cwd = join(tempRoot, "cwd"); |
| 55 | + mkdirSync(homeDir, { recursive: true }); |
| 56 | + mkdirSync(cwd, { recursive: true }); |
| 57 | + |
| 58 | + let combinedOutput = ""; |
| 59 | + try { |
| 60 | + try { |
| 61 | + execFileSync( |
| 62 | + process.execPath, |
| 63 | + [ |
| 64 | + join(params.packageRoot, "openclaw.mjs"), |
| 65 | + "agent", |
| 66 | + "--message", |
| 67 | + "workspace bootstrap smoke", |
| 68 | + "--session-id", |
| 69 | + "workspace-bootstrap-smoke", |
| 70 | + "--local", |
| 71 | + "--timeout", |
| 72 | + "1", |
| 73 | + "--json", |
| 74 | + ], |
| 75 | + { |
| 76 | + cwd, |
| 77 | + encoding: "utf8", |
| 78 | + maxBuffer: 1024 * 1024 * 16, |
| 79 | + stdio: ["ignore", "pipe", "pipe"], |
| 80 | + env: { |
| 81 | + ...process.env, |
| 82 | + HOME: homeDir, |
| 83 | + OPENCLAW_HOME: homeDir, |
| 84 | + OPENCLAW_SUPPRESS_NOTES: "1", |
| 85 | + }, |
| 86 | + }, |
| 87 | + ); |
| 88 | + } catch (error) { |
| 89 | + combinedOutput = describeExecFailure(error); |
| 90 | + } |
| 91 | + |
| 92 | + if (combinedOutput.includes("Missing workspace template:")) { |
| 93 | + throw new Error( |
| 94 | + `installed workspace bootstrap failed before agent execution: ${combinedOutput}`, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + const workspaceDir = join(homeDir, ".openclaw", "workspace"); |
| 99 | + const missingFiles = collectMissingBootstrapWorkspaceFiles(workspaceDir); |
| 100 | + if (missingFiles.length > 0) { |
| 101 | + throw new Error( |
| 102 | + `installed workspace bootstrap did not create required files in ${workspaceDir}: ${missingFiles.join(", ")}`, |
| 103 | + ); |
| 104 | + } |
| 105 | + } finally { |
| 106 | + try { |
| 107 | + rmSync(tempRoot, { force: true, recursive: true }); |
| 108 | + } catch { |
| 109 | + // best effort cleanup only |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments