/** * Copyright 2026 aphrody-code * SPDX-License-Identifier: Apache-2.0 */ import { existsSync, statSync } from "node:fs"; import { AssetSearch } from "../assets/index.ts"; import { BXC_BRIDGE_ABI_VERSION, bridgeAbiVersion, bridgeLibraryPath, } from "../rust/bridge.ts"; import { resolveCookiePath } from "../utils/paths.ts"; import { type CommonOptions, EXIT } from "./shared.ts"; export interface DoctorCheck { name: string; ok: boolean; required: boolean; detail: string; } export interface DoctorReport { ok: boolean; runtime: string; platform: string; checks: DoctorCheck[]; } function cookieCheck(name: string): DoctorCheck { const path = resolveCookiePath(name); if (!existsSync(path)) return { name: `cookie:${name}`, ok: false, required: false, detail: "not configured", }; const mode = statSync(path).mode & 0o777; return { name: `cookie:${name}`, ok: mode === 0o600, required: false, detail: mode === 0o600 ? "configured (private)" : `configured with unsafe mode ${mode.toString(8)}`, }; } export function collectDoctorReport(): DoctorReport { const checks: DoctorCheck[] = []; try { // Loading already enforces ABI equality; report both sides explicitly. const abi = bridgeAbiVersion(); checks.push({ name: "rust-bridge", ok: abi === BXC_BRIDGE_ABI_VERSION, required: true, detail: `ABI ${abi} (bindings expect ${BXC_BRIDGE_ABI_VERSION}) at ${bridgeLibraryPath() ?? "?"}`, }); } catch (error) { checks.push({ name: "rust-bridge", ok: false, required: true, detail: error instanceof Error ? error.message : "load failed", }); } for (const binary of ["ffmpeg", "ffprobe"] as const) { const path = Bun.which(binary); checks.push({ name: binary, ok: Boolean(path), required: binary === "ffmpeg", detail: path ?? "not installed", }); } checks.push( cookieCheck("google"), cookieCheck("gemini"), cookieCheck("xcom"), ); const assets = new AssetSearch(); for (const kind of ["image", "gif"] as const) { const providers = assets.available(kind); checks.push({ name: `assets:${kind}`, ok: providers.length > 0, required: kind === "image", detail: providers.join(", ") || "none configured", }); } return { ok: checks.every((check) => !check.required || check.ok), runtime: `Bun ${Bun.version}`, platform: `${process.platform}-${process.arch}`, checks, }; } export async function main( _argv: readonly string[], options: CommonOptions, ): Promise { const report = collectDoctorReport(); if (options.json) Bun.stdout.write(`${JSON.stringify(report, null, 2)}\n`); else { Bun.stdout.write(`bxc doctor — ${report.ok ? "READY" : "NOT READY"}\n`); for (const check of report.checks) Bun.stdout.write( `${check.ok ? "ok" : check.required ? "FAIL" : "optional"} ${check.name} — ${check.detail}\n`, ); } if (!report.ok) process.exit(EXIT.SOFTWARE); }