|
| 1 | +import { accessSync, constants, existsSync, readFileSync } from "node:fs"; |
| 2 | +import { homedir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { CredentialsStore } from "../../auth/credentials.js"; |
| 5 | +import { VERSION } from "../../version.js"; |
| 6 | +import type { Command } from "../types.js"; |
| 7 | + |
| 8 | +/** |
| 9 | + * /doctor — diagnose the install: runtime, credentials, config files, |
| 10 | + * MCP servers, search keys, storage. Each check is one ✓/✗/– line so a |
| 11 | + * support request can start with a paste of this output. |
| 12 | + */ |
| 13 | +export const doctor: Command = { |
| 14 | + name: "doctor", |
| 15 | + description: "Diagnose the installation: runtime, auth, config, MCP, storage.", |
| 16 | + handler: (_args, ctx) => { |
| 17 | + const lines: string[] = [`codebase ${VERSION} · doctor`]; |
| 18 | + const home = join(homedir(), ".codebase"); |
| 19 | + |
| 20 | + // Runtime |
| 21 | + const major = Number.parseInt(process.versions.node.split(".")[0] ?? "0", 10); |
| 22 | + lines.push(check(major >= 20, `node ${process.versions.node}`, "node ≥ 20 required")); |
| 23 | + |
| 24 | + // Credentials |
| 25 | + const credStore = new CredentialsStore(); |
| 26 | + const creds = credStore.load(); |
| 27 | + if (!creds) { |
| 28 | + lines.push( |
| 29 | + info( |
| 30 | + credStore.exists() |
| 31 | + ? "credentials file present but unreadable/invalid" |
| 32 | + : "not signed in (env-key or BYOK mode)", |
| 33 | + ), |
| 34 | + ); |
| 35 | + } else if (credStore.isExpired(creds)) { |
| 36 | + lines.push( |
| 37 | + check( |
| 38 | + false, |
| 39 | + "", |
| 40 | + `credentials expired${creds.refreshToken ? " (will auto-refresh on next call)" : " — run codebase auth login"}`, |
| 41 | + ), |
| 42 | + ); |
| 43 | + } else { |
| 44 | + const until = creds.expiresAt ? ` until ${new Date(creds.expiresAt).toLocaleString()}` : ""; |
| 45 | + lines.push(check(true, `signed in (${creds.source})${until}`, "")); |
| 46 | + } |
| 47 | + |
| 48 | + // Model resolution for this session |
| 49 | + lines.push( |
| 50 | + check( |
| 51 | + true, |
| 52 | + `model: ${ctx.state.model.name} (${ctx.state.model.provider}/${ctx.state.model.id}) via ${ctx.bundle.source}`, |
| 53 | + "", |
| 54 | + ), |
| 55 | + ); |
| 56 | + |
| 57 | + // Config files parse |
| 58 | + for (const path of [join(home, "config.json"), join(ctx.bundle.toolContext.cwd, ".codebase", "config.json")]) { |
| 59 | + if (!existsSync(path)) continue; |
| 60 | + lines.push(check(parses(path), `config ${path}`, `config ${path} is not valid JSON`)); |
| 61 | + } |
| 62 | + for (const path of [join(home, "mcp.json"), join(ctx.bundle.toolContext.cwd, ".codebase", "mcp.json")]) { |
| 63 | + if (!existsSync(path)) continue; |
| 64 | + lines.push(check(parses(path), `mcp config ${path}`, `mcp config ${path} is not valid JSON`)); |
| 65 | + } |
| 66 | + |
| 67 | + // MCP server status (live) |
| 68 | + for (const s of ctx.bundle.mcp.status()) { |
| 69 | + lines.push(check(s.connected, `mcp ${s.name}: ${s.toolCount} tools`, `mcp ${s.name}: ${s.error ?? "failed"}`)); |
| 70 | + } |
| 71 | + |
| 72 | + // Web search keys |
| 73 | + const hasSearch = Boolean(process.env.TAVILY_API_KEY || process.env.BRAVE_API_KEY || process.env.SEARXNG_URL); |
| 74 | + lines.push( |
| 75 | + hasSearch |
| 76 | + ? check(true, "web_search configured", "") |
| 77 | + : info("web_search unconfigured — set TAVILY_API_KEY, BRAVE_API_KEY, or SEARXNG_URL to enable"), |
| 78 | + ); |
| 79 | + |
| 80 | + // Storage writable |
| 81 | + lines.push( |
| 82 | + check(writable(home), `${home} writable`, `${home} is not writable — sessions/credentials can't persist`), |
| 83 | + ); |
| 84 | + |
| 85 | + // Sessions + skills + agents on disk |
| 86 | + lines.push(info(`sessions for this directory: ${ctx.bundle.sessions.list().length}`)); |
| 87 | + const subagents = ctx.bundle.toolContext.subagentTypes ?? []; |
| 88 | + lines.push(info(`subagent types: ${subagents.map((t) => t.name).join(", ") || "none"}`)); |
| 89 | + |
| 90 | + ctx.emit(lines.join("\n")); |
| 91 | + return { handled: true }; |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +function check(ok: boolean, okText: string, failText: string): string { |
| 96 | + return ok ? ` ✓ ${okText}` : ` ✗ ${failText}`; |
| 97 | +} |
| 98 | + |
| 99 | +function info(text: string): string { |
| 100 | + return ` – ${text}`; |
| 101 | +} |
| 102 | + |
| 103 | +function parses(path: string): boolean { |
| 104 | + try { |
| 105 | + JSON.parse(readFileSync(path, "utf8")); |
| 106 | + return true; |
| 107 | + } catch { |
| 108 | + return false; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function writable(dir: string): boolean { |
| 113 | + try { |
| 114 | + accessSync(dir, constants.W_OK); |
| 115 | + return true; |
| 116 | + } catch { |
| 117 | + return false; |
| 118 | + } |
| 119 | +} |
0 commit comments