import { diagnoseSystem, formatDoctorDashboard } from "../../core/doctor/diagnose.js"; import { getEmbeddedGuideMarkdown } from "../../core/doctor/knowledge.js"; import { booleanOption } from "../parser.js"; import { printJson } from "../printer.js"; import type { ParsedArgs } from "../types.js"; export async function handleDoctorCommand( action: string | undefined, maybeName: string | undefined, args: ParsedArgs ) { const subAction = (action ?? maybeName ?? "").toLowerCase(); // 1. Output embedded guide / documentation if (subAction === "guide" || subAction === "doc" || subAction === "knowledge" || subAction === "book") { console.error("📖 [Seal Use Doctor] 正在读取内嵌系统规范白皮书(纯内存只读)...\n"); console.log(getEmbeddedGuideMarkdown()); return; } // 2. Output compact directory specifications if (subAction === "paths" || subAction === "dirs" || subAction === "directories") { const diagnosis = diagnoseSystem(); printJson(diagnosis.directories); return; } // 2.5. Output auth session diagnostics (seal-use doctor auth) if (subAction === "auth") { const { selectCorp } = await import("../helpers.js"); const { stringOption } = await import("../parser.js"); const { handleAuthCommand } = await import("./auth.js"); const corpSelector = stringOption(args.options.corp) ?? (maybeName && maybeName !== subAction ? maybeName : undefined) ?? (args.command[2] && args.command[2] !== subAction ? args.command[2] : undefined); const corp = selectCorp(corpSelector); await handleAuthCommand("check", corp, args); return; } // 3. Output tenant-level health contract and quota diagnosis if (subAction === "tenant" || subAction === "corp" || subAction === "enterprise") { const { selectCorp } = await import("../helpers.js"); const { createSealClient } = await import("../../core/http/factory.js"); const { stringOption } = await import("../parser.js"); const { diagnoseTenantContract, formatTenantDoctorDashboard } = await import("../../core/doctor/tenant-diagnose.js"); const corpSelector = stringOption(args.options.corp) ?? (maybeName && maybeName !== subAction ? maybeName : undefined) ?? (args.command[2] && args.command[2] !== subAction ? args.command[2] : undefined); const corp = selectCorp(corpSelector); const client = await createSealClient(corp); const diagnosis = await diagnoseTenantContract(client, corp); const isQuiet = booleanOption(args.options.quiet) || booleanOption(args.options.silent); if (!isQuiet) { console.error(formatTenantDoctorDashboard(diagnosis)); } printJson(diagnosis); return; } // 3. Default: Full system diagnostic inspection const diagnosis = diagnoseSystem(); // If quiet or json-only is not explicitly disabled, output formatted dashboard on stderr const isQuiet = booleanOption(args.options.quiet) || booleanOption(args.options.silent); if (!isQuiet) { console.error(formatDoctorDashboard(diagnosis)); } // Machine channel (stdout): clean, structured JSON printJson(diagnosis); }