import type { ExtensionAPI } from "@mariozechner/pi-coding-agent"; import { ensureBundledBinary, resolvePreferredBinary } from "../install"; import { verifyBundledBinary } from "../integrity"; import { BUNDLE_MANIFEST } from "../manifest"; import { getPackageRoot } from "../paths"; import { getRuntimeTarget } from "../platform"; import { checkSystemCommand } from "../process"; import { notify } from "../ui"; import { type EnsureSnapshot, type RenderedReport, type StatusSnapshot, renderEnsureFailure, renderEnsureReport, renderStatusReport, } from "./render"; async function collectStatusSnapshot(): Promise { const target = getRuntimeTarget(); const packageRoot = getPackageRoot(); const [rgPath, fdPath, systemGrep, systemFind] = await Promise.all([ resolvePreferredBinary("rg"), resolvePreferredBinary("fd"), checkSystemCommand("grep"), checkSystemCommand("find"), ]); const rgVerify = verifyBundledBinary("rg", target.key); const fdVerify = verifyBundledBinary("fd", target.key); return { target, manifestGeneratedAt: BUNDLE_MANIFEST.generatedAt, rg: { resolvedPath: rgPath, embedded: !!rgPath && rgPath.startsWith(packageRoot), integrity: rgVerify, }, fd: { resolvedPath: fdPath, embedded: !!fdPath && fdPath.startsWith(packageRoot), integrity: fdVerify, }, systemGrep, systemFind, }; } async function collectEnsureSnapshot(): Promise { const target = getRuntimeTarget(); const rgPath = await ensureBundledBinary("rg", target.key); const fdPath = await ensureBundledBinary("fd", target.key); const rgVerify = verifyBundledBinary("rg", target.key); const fdVerify = verifyBundledBinary("fd", target.key); return { target, rg: { path: rgPath, integrity: rgVerify }, fd: { path: fdPath, integrity: fdVerify }, }; } function emit(ctx: any, rendered: RenderedReport): void { if (ctx?.hasUI) { notify(ctx, rendered.colorText, rendered.severity === "ok" ? "info" : "warning"); return; } console.log(rendered.text); } export function registerSearchToolsCommand(pi: ExtensionAPI): void { pi.registerCommand("search-tools", { description: "Inspect or repair bundled rg/fd. Usage: /search-tools [status|ensure]", handler: async (args, ctx) => { const sub = (args ?? "").trim().toLowerCase() || "status"; if (sub === "status") { const snapshot = await collectStatusSnapshot(); emit(ctx, renderStatusReport(snapshot)); return; } if (sub === "ensure") { try { const snapshot = await collectEnsureSnapshot(); emit(ctx, renderEnsureReport(snapshot)); } catch (error: any) { emit(ctx, renderEnsureFailure(error?.message ?? String(error), getRuntimeTarget())); } return; } notify(ctx, `Unknown subcommand "${sub}". Use: /search-tools [status|ensure]`, "warning"); }, }); }