|
| 1 | +import { existsSync, readFileSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { |
| 4 | + detectOmpBinary, |
| 5 | + listOmpPlugins, |
| 6 | + OMP_PLUGIN_PACKAGE, |
| 7 | + runOmpCommand, |
| 8 | +} from "../lib/omp-helpers"; |
| 9 | +import { |
| 10 | + dirSizeBytes, |
| 11 | + getMagicContextLogPath, |
| 12 | + getOmpAgentDir, |
| 13 | + getOmpPluginsDir, |
| 14 | + getOmpPluginsLockPath, |
| 15 | + getOmpUserConfigPath, |
| 16 | +} from "../lib/paths"; |
| 17 | +import type { |
| 18 | + HarnessAdapter, |
| 19 | + HarnessConfigPaths, |
| 20 | + PluginCacheInfo, |
| 21 | + PluginEntryResult, |
| 22 | +} from "./types"; |
| 23 | + |
| 24 | +export class OmpAdapter implements HarnessAdapter { |
| 25 | + readonly kind = "omp" as const; |
| 26 | + readonly displayName = "Oh My Pi (OMP)"; |
| 27 | + readonly pluginPackageName = OMP_PLUGIN_PACKAGE; |
| 28 | + |
| 29 | + isInstalled(): boolean { |
| 30 | + return detectOmpBinary() !== null; |
| 31 | + } |
| 32 | + |
| 33 | + hasPluginEntry(): boolean { |
| 34 | + const omp = detectOmpBinary(); |
| 35 | + if (!omp) return false; |
| 36 | + return ( |
| 37 | + listOmpPlugins(omp.path)?.some( |
| 38 | + (plugin) => plugin.name === OMP_PLUGIN_PACKAGE && plugin.enabled, |
| 39 | + ) ?? false |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + getConfigPaths(): HarnessConfigPaths { |
| 44 | + return { |
| 45 | + configDir: getOmpAgentDir(), |
| 46 | + pluginConfigPath: getOmpPluginsLockPath(), |
| 47 | + magicContextConfigPath: getOmpUserConfigPath(), |
| 48 | + secondaryConfigPath: null, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + async ensurePluginEntry(): Promise<PluginEntryResult> { |
| 53 | + const configPath = getOmpPluginsLockPath(); |
| 54 | + const omp = detectOmpBinary(); |
| 55 | + if (!omp) return this.errorResult(configPath, "OMP binary not found"); |
| 56 | + const plugins = listOmpPlugins(omp.path); |
| 57 | + if (plugins === null) { |
| 58 | + return this.errorResult(configPath, "`omp plugin list --json` failed"); |
| 59 | + } |
| 60 | + const installed = plugins.find((plugin) => plugin.name === OMP_PLUGIN_PACKAGE); |
| 61 | + if (installed?.enabled) { |
| 62 | + return { |
| 63 | + ok: true, |
| 64 | + action: "already_present", |
| 65 | + message: `${OMP_PLUGIN_PACKAGE} is already enabled in OMP.`, |
| 66 | + configPath, |
| 67 | + }; |
| 68 | + } |
| 69 | + const originalRuntimeEnabled = this.readRuntimeEnabled(configPath); |
| 70 | + |
| 71 | + const args = installed |
| 72 | + ? ["plugin", "enable", OMP_PLUGIN_PACKAGE] |
| 73 | + : ["plugin", "install", OMP_PLUGIN_PACKAGE]; |
| 74 | + const result = runOmpCommand(omp.path, args, 120_000); |
| 75 | + if (!result.ok) { |
| 76 | + return this.errorResult( |
| 77 | + configPath, |
| 78 | + result.stderr || result.stdout || `omp ${args.join(" ")} failed`, |
| 79 | + ); |
| 80 | + } |
| 81 | + const enabledAfter = listOmpPlugins(omp.path)?.some( |
| 82 | + (plugin) => plugin.name === OMP_PLUGIN_PACKAGE && plugin.enabled, |
| 83 | + ); |
| 84 | + if (!enabledAfter) { |
| 85 | + // A project override can keep the plugin disabled even when the |
| 86 | + // global install/enable command exits 0. New installs are removed. |
| 87 | + // Existing installs restore the exact lockfile enable state; never |
| 88 | + // infer global state from the project-effective plugin list. |
| 89 | + if (!installed) { |
| 90 | + runOmpCommand(omp.path, ["plugin", "uninstall", OMP_PLUGIN_PACKAGE], 120_000); |
| 91 | + } else if (originalRuntimeEnabled !== undefined) { |
| 92 | + runOmpCommand( |
| 93 | + omp.path, |
| 94 | + ["plugin", originalRuntimeEnabled ? "enable" : "disable", OMP_PLUGIN_PACKAGE], |
| 95 | + 120_000, |
| 96 | + ); |
| 97 | + } |
| 98 | + return this.errorResult( |
| 99 | + configPath, |
| 100 | + `${OMP_PLUGIN_PACKAGE} is still disabled in the current project after \`omp ${args.join(" ")}\``, |
| 101 | + ); |
| 102 | + } |
| 103 | + return { |
| 104 | + ok: true, |
| 105 | + action: installed ? "updated" : "added", |
| 106 | + message: installed |
| 107 | + ? `Enabled ${OMP_PLUGIN_PACKAGE} in OMP.` |
| 108 | + : `Installed ${OMP_PLUGIN_PACKAGE} in OMP.`, |
| 109 | + configPath, |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + async removePluginEntry(): Promise<PluginEntryResult> { |
| 114 | + const configPath = getOmpPluginsLockPath(); |
| 115 | + const omp = detectOmpBinary(); |
| 116 | + if (!omp) return this.errorResult(configPath, "OMP binary not found"); |
| 117 | + const plugins = listOmpPlugins(omp.path); |
| 118 | + if (plugins === null) { |
| 119 | + return this.errorResult(configPath, "`omp plugin list --json` failed"); |
| 120 | + } |
| 121 | + const installed = plugins.some((plugin) => plugin.name === OMP_PLUGIN_PACKAGE); |
| 122 | + if (!installed) { |
| 123 | + return { |
| 124 | + ok: true, |
| 125 | + action: "already_present", |
| 126 | + message: `${OMP_PLUGIN_PACKAGE} is not installed in OMP.`, |
| 127 | + configPath, |
| 128 | + }; |
| 129 | + } |
| 130 | + const result = runOmpCommand( |
| 131 | + omp.path, |
| 132 | + ["plugin", "uninstall", OMP_PLUGIN_PACKAGE], |
| 133 | + 120_000, |
| 134 | + ); |
| 135 | + if (!result.ok) { |
| 136 | + return this.errorResult( |
| 137 | + configPath, |
| 138 | + result.stderr || result.stdout || "OMP plugin uninstall failed", |
| 139 | + ); |
| 140 | + } |
| 141 | + return { |
| 142 | + ok: true, |
| 143 | + action: "updated", |
| 144 | + message: `Uninstalled ${OMP_PLUGIN_PACKAGE} from OMP.`, |
| 145 | + configPath, |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + getInstallHint(): string { |
| 150 | + return "Install OMP: https://omp.sh (npm: @oh-my-pi/pi-coding-agent)"; |
| 151 | + } |
| 152 | + |
| 153 | + getPluginCacheInfo(): PluginCacheInfo { |
| 154 | + const path = join(getOmpPluginsDir(), "cache"); |
| 155 | + return { path, exists: existsSync(path), sizeBytes: dirSizeBytes(path) }; |
| 156 | + } |
| 157 | + |
| 158 | + getLogPath(): string { |
| 159 | + // OMP executes the Pi-compatible runtime, which intentionally keeps the |
| 160 | + // existing `pi` DB/log discriminator for cross-host session semantics. |
| 161 | + return getMagicContextLogPath("pi"); |
| 162 | + } |
| 163 | + |
| 164 | + getInstalledPluginVersion(): string | null { |
| 165 | + const omp = detectOmpBinary(); |
| 166 | + if (!omp) return null; |
| 167 | + return ( |
| 168 | + listOmpPlugins(omp.path)?.find((plugin) => plugin.name === OMP_PLUGIN_PACKAGE) |
| 169 | + ?.version ?? null |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + private readRuntimeEnabled(configPath: string): boolean | undefined { |
| 174 | + try { |
| 175 | + const lock = JSON.parse(readFileSync(configPath, "utf-8")) as { |
| 176 | + plugins?: Record<string, { enabled?: unknown }>; |
| 177 | + }; |
| 178 | + const enabled = lock.plugins?.[OMP_PLUGIN_PACKAGE]?.enabled; |
| 179 | + return typeof enabled === "boolean" ? enabled : undefined; |
| 180 | + } catch { |
| 181 | + return undefined; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private errorResult(configPath: string, message: string): PluginEntryResult { |
| 186 | + return { |
| 187 | + ok: false, |
| 188 | + action: "error", |
| 189 | + message: `Failed to configure OMP: ${message}`, |
| 190 | + configPath, |
| 191 | + }; |
| 192 | + } |
| 193 | +} |
0 commit comments