UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

125 lines (124 loc) • 5.92 kB
import { s as resolveOpenClawCliInvocation } from "./cli-invocation-ZSPsKRWa.mjs"; import { t as resolveRuntimeArgs } from "./runtime-worker-url-DRDkoS4J.mjs"; import { r as resolveExternalPackageRuntimeEntrypointUrl } from "./runtime-process-url-CoHTemu-.mjs"; import { n as resolveOpenClawPackageRoot } from "./openclaw-root-C-ayo09z.mjs"; import { a as formatCompletionReloadCommand, d as resolveCompletionProfileHint, f as resolveCompletionProfilePath, i as findOptionalCompletionInstallFailure, m as usesSlowDynamicCompletion, n as COMPLETION_SKIP_PLUGIN_COMMANDS_ENV, o as installCompletion, p as resolveShellFromEnv, r as completionCacheExists, s as isCompletionInstalled, u as resolveCompletionCachePath } from "./completion-runtime-DQNa6Jzl.mjs"; import { t as note } from "./note-CnLgHvug.mjs"; import { t as CLI_NAME } from "./cli-name-Dp_huZBR.mjs"; import { fileURLToPath } from "node:url"; import { spawnSync } from "node:child_process"; //#region src/commands/doctor-completion.ts const COMPLETION_CACHE_WRITE_TIMEOUT_MS = 3e4; async function installCompletionForDoctor({ shell: e, cachePath: t }, n) { try { await installCompletion(e, true, CLI_NAME); const t = formatCompletionReloadCommand(e, resolveCompletionProfileHint(e)); note(`Shell completion ${n}. Restart your shell or run: ${t}`, "Shell completion"); } catch (r) { const i = findOptionalCompletionInstallFailure(r); if (!i) throw r; const a = formatCompletionReloadCommand(e, t); if (i.kind === "write") { const t = i.error.path ?? resolveCompletionProfilePath(e); note(`Shell completion could not be ${n} (permission or read-only error at ${t}). For this ${e} session only, run:\n${a}`, "Shell completion"); return; } note(`Shell completion could not be ${n} (profile is not valid UTF-8 at ${i.profilePath}). The file was left untouched. For this ${e} session only, run:\n${a}`, "Shell completion"); } } async function generateCompletionCache(e) { const t = await resolveOpenClawPackageRoot({ moduleUrl: import.meta.url, argv1: process.argv[1], cwd: process.cwd() }); if (!t) return false; const n = fileURLToPath(resolveExternalPackageRuntimeEntrypointUrl("cliLauncher", t)); const r = ["completion", "--write-state"]; if (e.shell) r.push("--shell", e.shell); const i = { ...process.env }; if (e.generationMode === "core-only") i[COMPLETION_SKIP_PLUGIN_COMMANDS_ENV] = "1"; else delete i[COMPLETION_SKIP_PLUGIN_COMMANDS_ENV]; const a = resolveOpenClawCliInvocation({ entryPath: n, execArgv: resolveRuntimeArgs(), args: r }); return spawnSync(a.command, a.args, { cwd: t, env: i, encoding: "utf-8", timeout: COMPLETION_CACHE_WRITE_TIMEOUT_MS }).status === 0; } async function checkShellCompletionStatus(e = "openclaw", t = {}) { const n = t.shell ?? resolveShellFromEnv(); return { shell: n, profileInstalled: await isCompletionInstalled(n, e), cacheExists: await completionCacheExists(n, e), cachePath: resolveCompletionCachePath(n, e), usesSlowPattern: await usesSlowDynamicCompletion(n, e) }; } function shellCompletionStatusToHealthFindings(e) { if (!e.usesSlowPattern && (!e.profileInstalled || e.cacheExists)) return []; return [{ checkId: "core/doctor/shell-completion", severity: "info", message: e.usesSlowPattern ? `Your ${e.shell} profile uses slow dynamic completion (source <(...)).` : `Shell completion is configured in your ${e.shell} profile but the cache is missing.`, path: `shellCompletion.${e.shell}`, fixHint: e.usesSlowPattern ? "Run `openclaw doctor --fix` to upgrade to cached completion." : `Run \`openclaw completion --write-state\` or \`openclaw doctor --fix\` to regenerate ${e.cachePath}.` }]; } function shellCompletionStatusToRepairEffects(e) { const t = []; if (!e.cacheExists && (e.usesSlowPattern || e.profileInstalled)) t.push({ kind: "state", action: e.usesSlowPattern ? "would-generate-completion-cache" : "would-regenerate-completion-cache", target: e.cachePath, dryRunSafe: true }); if (e.usesSlowPattern) t.push({ kind: "file", action: "would-upgrade-shell-profile-completion", target: e.shell, dryRunSafe: false }); return t; } /** * Repairs shell completion setup when doctor runs interactively. * * Slow dynamic profiles are upgraded to cached completion; configured profiles with a missing * cache regenerate it; missing completion prompts unless non-interactive mode is active. */ async function doctorShellCompletion(e, t = {}) { const n = await checkShellCompletionStatus(CLI_NAME); const r = !n.usesSlowPattern && n.profileInstalled; const [i] = shellCompletionStatusToHealthFindings(n); if (i) note(`${i.message}\n${n.usesSlowPattern ? "Upgrading to cached completion for faster shell startup..." : "Regenerating cache..."}`, "Shell completion"); else if (n.profileInstalled || t.nonInteractive || !await e.confirm({ message: `Enable ${n.shell} shell completion for ${"openclaw"}?`, initialValue: true })) return; if (!n.usesSlowPattern || !n.cacheExists) { if (!await generateCompletionCache({ generationMode: "core-only" })) { note(`Failed to ${r ? "regenerate" : "generate"} completion cache. Run \`${CLI_NAME} completion --write-state\` manually.`, "Shell completion"); return; } } if (r) { note(`Completion cache regenerated at ${n.cachePath}`, "Shell completion"); return; } await installCompletionForDoctor(n, n.usesSlowPattern ? "upgraded" : "installed"); } /** Ensures the shell completion cache exists without prompting during setup/update flows. */ async function ensureCompletionCacheExists(e, t) { const n = t.shell ?? resolveShellFromEnv(); if (await completionCacheExists(n, e)) return true; return generateCompletionCache(t); } //#endregion export { shellCompletionStatusToRepairEffects as a, shellCompletionStatusToHealthFindings as i, doctorShellCompletion as n, ensureCompletionCacheExists as r, checkShellCompletionStatus as t };