openclaw/src/commands/setup.ts

92 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-02-18 01:29:02 +00:00
import fs from "node:fs/promises";
import JSON5 from "json5";
import { DEFAULT_AGENT_WORKSPACE_DIR, ensureAgentWorkspace } from "../agents/workspace.js";
2026-01-30 03:15:10 +01:00
import { type OpenClawConfig, createConfigIO, writeConfigFile } from "../config/config.js";
import { formatConfigPath, logConfigUpdated } from "../config/logging.js";
2025-12-17 11:29:04 +01:00
import { resolveSessionTranscriptsDir } from "../config/sessions.js";
import type { RuntimeEnv } from "../runtime.js";
2025-12-17 11:29:04 +01:00
import { defaultRuntime } from "../runtime.js";
2026-01-23 03:43:32 +00:00
import { shortenHomePath } from "../utils.js";
2025-12-17 11:29:04 +01:00
2026-01-28 00:15:54 +00:00
async function readConfigFileRaw(configPath: string): Promise<{
2025-12-17 11:29:04 +01:00
exists: boolean;
2026-01-30 03:15:10 +01:00
parsed: OpenClawConfig;
2025-12-17 11:29:04 +01:00
}> {
try {
2026-01-28 00:15:54 +00:00
const raw = await fs.readFile(configPath, "utf-8");
2025-12-17 11:29:04 +01:00
const parsed = JSON5.parse(raw);
if (parsed && typeof parsed === "object") {
2026-01-30 03:15:10 +01:00
return { exists: true, parsed: parsed as OpenClawConfig };
2025-12-17 11:29:04 +01:00
}
return { exists: true, parsed: {} };
} catch {
return { exists: false, parsed: {} };
}
}
export async function setupCommand(
opts?: { workspace?: string },
runtime: RuntimeEnv = defaultRuntime,
) {
const desiredWorkspace =
typeof opts?.workspace === "string" && opts.workspace.trim()
? opts.workspace.trim()
: undefined;
2026-01-28 00:15:54 +00:00
const io = createConfigIO();
const configPath = io.configPath;
const existingRaw = await readConfigFileRaw(configPath);
2025-12-17 11:29:04 +01:00
const cfg = existingRaw.parsed;
const defaults = cfg.agents?.defaults ?? {};
2025-12-17 11:29:04 +01:00
const workspace = desiredWorkspace ?? defaults.workspace ?? DEFAULT_AGENT_WORKSPACE_DIR;
2025-12-17 11:29:04 +01:00
2026-01-30 03:15:10 +01:00
const next: OpenClawConfig = {
2025-12-17 11:29:04 +01:00
...cfg,
agents: {
...cfg.agents,
defaults: {
...defaults,
workspace,
},
2025-12-17 11:29:04 +01:00
},
2026-03-08 11:17:15 -07:00
gateway: {
...cfg.gateway,
mode: cfg.gateway?.mode ?? "local",
},
2025-12-17 11:29:04 +01:00
};
2026-03-08 11:17:15 -07:00
if (
!existingRaw.exists ||
defaults.workspace !== workspace ||
cfg.gateway?.mode !== next.gateway?.mode
) {
2025-12-17 11:29:04 +01:00
await writeConfigFile(next);
if (!existingRaw.exists) {
2026-01-28 00:15:54 +00:00
runtime.log(`Wrote ${formatConfigPath(configPath)}`);
} else {
2026-03-08 11:17:15 -07:00
const updates: string[] = [];
if (defaults.workspace !== workspace) {
updates.push("set agents.defaults.workspace");
}
if (cfg.gateway?.mode !== next.gateway?.mode) {
updates.push("set gateway.mode");
}
const suffix = updates.length > 0 ? `(${updates.join(", ")})` : undefined;
logConfigUpdated(runtime, { path: configPath, suffix });
}
2025-12-17 11:29:04 +01:00
} else {
2026-01-28 00:15:54 +00:00
runtime.log(`Config OK: ${formatConfigPath(configPath)}`);
2025-12-17 11:29:04 +01:00
}
const ws = await ensureAgentWorkspace({
dir: workspace,
ensureBootstrapFiles: !next.agents?.defaults?.skipBootstrap,
2025-12-17 11:29:04 +01:00
});
2026-01-23 03:43:32 +00:00
runtime.log(`Workspace OK: ${shortenHomePath(ws.dir)}`);
2025-12-17 11:29:04 +01:00
const sessionsDir = resolveSessionTranscriptsDir();
await fs.mkdir(sessionsDir, { recursive: true });
2026-01-23 03:43:32 +00:00
runtime.log(`Sessions OK: ${shortenHomePath(sessionsDir)}`);
2025-12-17 11:29:04 +01:00
}