* Ensure the opencode runtime can resolve `@opencode-ai/plugin` from our * isolated config dir. Since opencode 1.14.x the plugin loader is a separate * npm package, and ` /plugins/*.ts` files are only evaluated when * that package is resolvable from ` `. HAPI's per-session *
(rootPath: string)
| 189 | * degraded but not fatal. |
| 190 | */ |
| 191 | function ensurePluginRuntime(rootPath: string): void { |
| 192 | if (!isHapiManagedDir(rootPath)) { |
| 193 | logger.debug(`[opencode-hook] Skipping plugin runtime materialization for non-HAPI dir: ${rootPath}`); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | const packageJsonPath = join(rootPath, PACKAGE_JSON_FILENAME); |
| 198 | if (existsSync(packageJsonPath)) { |
| 199 | if (hasDeclaredPluginPackage(packageJsonPath)) { |
| 200 | // Existing file already declares the plugin (likely opencode- |
| 201 | // installed with a matching lock file). Leave it untouched. |
| 202 | return; |
| 203 | } |
| 204 | logger.debug(`[opencode-hook] package.json exists at ${packageJsonPath} but does not declare ${PLUGIN_PACKAGE}; overwriting placeholder.`); |
| 205 | } |
| 206 | |
| 207 | try { |
| 208 | writeFileSync(packageJsonPath, buildMinimalPackageJson(), 'utf-8'); |
| 209 | } catch (error) { |
| 210 | logger.warn(`[opencode-hook] Failed to materialize ${packageJsonPath}; the hook plugin channel may stay inert. Storage scanner remains as fallback. Error: ${(error as Error).message}`); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | export function ensureOpencodeHookPlugin(rootPath: string, hookUrl: string, token: string): string { |
| 215 | const pluginDir = resolvePluginDir(rootPath); |
no test coverage detected