openclaw/src/commands/setup.ts

85 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-12-17 11:29:04 +01:00
import fs from "node:fs/promises";
import path from "node:path";
import JSON5 from "json5";
import {
DEFAULT_AGENT_WORKSPACE_DIR,
ensureAgentWorkspace,
} from "../agents/workspace.js";
2026-01-04 14:32:47 +00:00
import { type ClawdbotConfig, CONFIG_PATH_CLAWDBOT } from "../config/config.js";
import { applyModelDefaults } from "../config/defaults.js";
2025-12-17 11:29:04 +01:00
import { resolveSessionTranscriptsDir } from "../config/sessions.js";
import type { RuntimeEnv } from "../runtime.js";
import { defaultRuntime } from "../runtime.js";
async function readConfigFileRaw(): Promise<{
exists: boolean;
2026-01-04 14:32:47 +00:00
parsed: ClawdbotConfig;
2025-12-17 11:29:04 +01:00
}> {
try {
2026-01-04 14:32:47 +00:00
const raw = await fs.readFile(CONFIG_PATH_CLAWDBOT, "utf-8");
2025-12-17 11:29:04 +01:00
const parsed = JSON5.parse(raw);
if (parsed && typeof parsed === "object") {
2026-01-04 14:32:47 +00:00
return { exists: true, parsed: parsed as ClawdbotConfig };
2025-12-17 11:29:04 +01:00
}
return { exists: true, parsed: {} };
} catch {
return { exists: false, parsed: {} };
}
}
2026-01-04 14:32:47 +00:00
async function writeConfigFile(cfg: ClawdbotConfig) {
await fs.mkdir(path.dirname(CONFIG_PATH_CLAWDBOT), { recursive: true });
const json = JSON.stringify(applyModelDefaults(cfg), null, 2)
.trimEnd()
.concat("\n");
2026-01-04 14:32:47 +00:00
await fs.writeFile(CONFIG_PATH_CLAWDBOT, json, "utf-8");
2025-12-17 11:29:04 +01:00
}
export async function setupCommand(
opts?: { workspace?: string },
runtime: RuntimeEnv = defaultRuntime,
) {
const desiredWorkspace =
typeof opts?.workspace === "string" && opts.workspace.trim()
? opts.workspace.trim()
: undefined;
const existingRaw = await readConfigFileRaw();
const cfg = existingRaw.parsed;
2025-12-23 23:45:20 +00:00
const agent = cfg.agent ?? {};
2025-12-17 11:29:04 +01:00
const workspace =
2025-12-23 23:45:20 +00:00
desiredWorkspace ?? agent.workspace ?? DEFAULT_AGENT_WORKSPACE_DIR;
2025-12-17 11:29:04 +01:00
2026-01-04 14:32:47 +00:00
const next: ClawdbotConfig = {
2025-12-17 11:29:04 +01:00
...cfg,
2025-12-23 23:45:20 +00:00
agent: {
...agent,
2025-12-17 11:29:04 +01:00
workspace,
},
};
2025-12-23 23:45:20 +00:00
if (!existingRaw.exists || agent.workspace !== workspace) {
2025-12-17 11:29:04 +01:00
await writeConfigFile(next);
runtime.log(
!existingRaw.exists
2026-01-04 14:32:47 +00:00
? `Wrote ${CONFIG_PATH_CLAWDBOT}`
: `Updated ${CONFIG_PATH_CLAWDBOT} (set agent.workspace)`,
2025-12-17 11:29:04 +01:00
);
} else {
2026-01-04 14:32:47 +00:00
runtime.log(`Config OK: ${CONFIG_PATH_CLAWDBOT}`);
2025-12-17 11:29:04 +01:00
}
const ws = await ensureAgentWorkspace({
dir: workspace,
ensureBootstrapFiles: !next.agent?.skipBootstrap,
2025-12-17 11:29:04 +01:00
});
runtime.log(`Workspace OK: ${ws.dir}`);
const sessionsDir = resolveSessionTranscriptsDir();
await fs.mkdir(sessionsDir, { recursive: true });
runtime.log(`Sessions OK: ${sessionsDir}`);
}