|
| 1 | +import crypto from "node:crypto"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { cancel, isCancel } from "@clack/prompts"; |
| 6 | + |
| 7 | +import { |
| 8 | + DEFAULT_AGENT_WORKSPACE_DIR, |
| 9 | + ensureAgentWorkspace, |
| 10 | +} from "../agents/workspace.js"; |
| 11 | +import type { ClawdisConfig } from "../config/config.js"; |
| 12 | +import { CONFIG_PATH_CLAWDIS } from "../config/config.js"; |
| 13 | +import { resolveSessionTranscriptsDir } from "../config/sessions.js"; |
| 14 | +import { runCommandWithTimeout } from "../process/exec.js"; |
| 15 | +import type { RuntimeEnv } from "../runtime.js"; |
| 16 | +import { CONFIG_DIR } from "../utils.js"; |
| 17 | +import type { NodeManagerChoice, ResetScope } from "./onboard-types.js"; |
| 18 | + |
| 19 | +export function guardCancel<T>(value: T, runtime: RuntimeEnv): T { |
| 20 | + if (isCancel(value)) { |
| 21 | + cancel("Setup cancelled."); |
| 22 | + runtime.exit(0); |
| 23 | + } |
| 24 | + return value; |
| 25 | +} |
| 26 | + |
| 27 | +export function summarizeExistingConfig(config: ClawdisConfig): string { |
| 28 | + const rows: string[] = []; |
| 29 | + if (config.agent?.workspace) |
| 30 | + rows.push(`workspace: ${config.agent.workspace}`); |
| 31 | + if (config.agent?.model) rows.push(`model: ${config.agent.model}`); |
| 32 | + if (config.gateway?.mode) rows.push(`gateway.mode: ${config.gateway.mode}`); |
| 33 | + if (config.gateway?.bind) rows.push(`gateway.bind: ${config.gateway.bind}`); |
| 34 | + if (config.skills?.install?.nodeManager) { |
| 35 | + rows.push(`skills.nodeManager: ${config.skills.install.nodeManager}`); |
| 36 | + } |
| 37 | + return rows.length ? rows.join("\n") : "No key settings detected."; |
| 38 | +} |
| 39 | + |
| 40 | +export function randomToken(): string { |
| 41 | + return crypto.randomBytes(24).toString("hex"); |
| 42 | +} |
| 43 | + |
| 44 | +export async function openUrl(url: string): Promise<void> { |
| 45 | + const platform = process.platform; |
| 46 | + const command = |
| 47 | + platform === "darwin" |
| 48 | + ? ["open", url] |
| 49 | + : platform === "win32" |
| 50 | + ? ["cmd", "/c", "start", "", url] |
| 51 | + : ["xdg-open", url]; |
| 52 | + try { |
| 53 | + await runCommandWithTimeout(command, { timeoutMs: 5_000 }); |
| 54 | + } catch { |
| 55 | + // ignore; we still print the URL for manual open |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +export async function ensureWorkspaceAndSessions( |
| 60 | + workspaceDir: string, |
| 61 | + runtime: RuntimeEnv, |
| 62 | +) { |
| 63 | + const ws = await ensureAgentWorkspace({ |
| 64 | + dir: workspaceDir, |
| 65 | + ensureBootstrapFiles: true, |
| 66 | + }); |
| 67 | + runtime.log(`Workspace OK: ${ws.dir}`); |
| 68 | + const sessionsDir = resolveSessionTranscriptsDir(); |
| 69 | + await fs.mkdir(sessionsDir, { recursive: true }); |
| 70 | + runtime.log(`Sessions OK: ${sessionsDir}`); |
| 71 | +} |
| 72 | + |
| 73 | +export function resolveNodeManagerOptions(): Array<{ |
| 74 | + value: NodeManagerChoice; |
| 75 | + label: string; |
| 76 | +}> { |
| 77 | + return [ |
| 78 | + { value: "npm", label: "npm" }, |
| 79 | + { value: "pnpm", label: "pnpm" }, |
| 80 | + { value: "bun", label: "bun" }, |
| 81 | + ]; |
| 82 | +} |
| 83 | + |
| 84 | +export async function moveToTrash( |
| 85 | + pathname: string, |
| 86 | + runtime: RuntimeEnv, |
| 87 | +): Promise<void> { |
| 88 | + if (!pathname) return; |
| 89 | + try { |
| 90 | + await fs.access(pathname); |
| 91 | + } catch { |
| 92 | + return; |
| 93 | + } |
| 94 | + try { |
| 95 | + await runCommandWithTimeout(["trash", pathname], { timeoutMs: 5000 }); |
| 96 | + runtime.log(`Moved to Trash: ${pathname}`); |
| 97 | + } catch { |
| 98 | + runtime.log(`Failed to move to Trash (manual delete): ${pathname}`); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export async function handleReset( |
| 103 | + scope: ResetScope, |
| 104 | + workspaceDir: string, |
| 105 | + runtime: RuntimeEnv, |
| 106 | +) { |
| 107 | + await moveToTrash(CONFIG_PATH_CLAWDIS, runtime); |
| 108 | + if (scope === "config") return; |
| 109 | + await moveToTrash(path.join(CONFIG_DIR, "credentials"), runtime); |
| 110 | + await moveToTrash(resolveSessionTranscriptsDir(), runtime); |
| 111 | + if (scope === "full") { |
| 112 | + await moveToTrash(workspaceDir, runtime); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +export async function detectBinary(name: string): Promise<boolean> { |
| 117 | + const command = |
| 118 | + process.platform === "win32" |
| 119 | + ? ["where", name] |
| 120 | + : ["/usr/bin/env", "sh", "-lc", `command -v ${name}`]; |
| 121 | + try { |
| 122 | + const result = await runCommandWithTimeout(command, { timeoutMs: 2000 }); |
| 123 | + return result.code === 0 && result.stdout.trim().length > 0; |
| 124 | + } catch { |
| 125 | + return false; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +export const DEFAULT_WORKSPACE = DEFAULT_AGENT_WORKSPACE_DIR; |
0 commit comments