From 68ca2e4c783bfbd2f2c8c01126ee2daae90f81eb Mon Sep 17 00:00:00 2001 From: EthanMiller0x Date: Fri, 27 Mar 2026 18:16:27 +0700 Subject: [PATCH] fix: community plugin loading, settings path, and onboard init - Load community plugins from registry on startup (main.ts) - Fix path resolution for local and npm plugins (absolute paths, ESM entry point) - Scan node_modules when plugin name differs from npm package name - Align settings basePath to ~/.openacp/plugins/data/ across all CLI commands - Initialize plugin system in onboard command - Add debug logging for settings resolution in lifecycle-manager Co-Authored-By: Claude Opus 4.6 (1M context) --- src/cli/commands/onboard.ts | 10 ++++++ src/cli/commands/plugins.ts | 12 ++++--- src/core/plugin/lifecycle-manager.ts | 3 ++ src/main.ts | 49 ++++++++++++++++++++++++---- 4 files changed, 62 insertions(+), 12 deletions(-) diff --git a/src/cli/commands/onboard.ts b/src/cli/commands/onboard.ts index 704ab13..7432f1e 100644 --- a/src/cli/commands/onboard.ts +++ b/src/cli/commands/onboard.ts @@ -6,8 +6,18 @@ const PLUGINS_DATA_DIR = path.join(OPENACP_DIR, 'plugins', 'data') const REGISTRY_PATH = path.join(OPENACP_DIR, 'plugins.json') export async function cmdOnboard(): Promise { + const os = await import('node:os') + const path = await import('node:path') const { ConfigManager } = await import('../../core/config/config.js') + const { SettingsManager } = await import('../../core/plugin/settings-manager.js') + const { PluginRegistry } = await import('../../core/plugin/plugin-registry.js') + const cm = new ConfigManager() + const pluginsDataDir = path.join(os.homedir(), '.openacp', 'plugins', 'data') + const registryPath = path.join(os.homedir(), '.openacp', 'plugins.json') + const settingsManager = new SettingsManager(pluginsDataDir) + const pluginRegistry = new PluginRegistry(registryPath) + await pluginRegistry.load() if (await cm.exists()) { const { runReconfigure } = await import('../../core/setup/index.js') diff --git a/src/cli/commands/plugins.ts b/src/cli/commands/plugins.ts index 8748c06..ed7fdab 100644 --- a/src/cli/commands/plugins.ts +++ b/src/cli/commands/plugins.ts @@ -184,7 +184,7 @@ async function configurePlugin(name: string): Promise { process.exit(1) } - const basePath = path.join(os.homedir(), '.openacp', 'plugins') + const basePath = path.join(os.homedir(), '.openacp', 'plugins', 'data') const settingsManager = new SettingsManager(basePath) const ctx = createInstallContext({ pluginName: name, settingsManager, basePath }) @@ -261,7 +261,7 @@ async function installPlugin(input: string): Promise { const { corePlugins } = await import('../../plugins/core-plugins.js') const builtinPlugin = corePlugins.find(p => p.name === pkgName) - const basePath = path.join(os.homedir(), '.openacp', 'plugins') + const basePath = path.join(os.homedir(), '.openacp', 'plugins', 'data') const settingsManager = new SettingsManager(basePath) const registryPath = path.join(os.homedir(), '.openacp', 'plugins.json') const pluginRegistry = new PluginRegistry(registryPath) @@ -302,8 +302,10 @@ async function installPlugin(input: string): Promise { // Read installed plugin's package.json for compatibility check const cliVersion = getCurrentVersion() + const isLocalPath = pkgName.startsWith('/') || pkgName.startsWith('.') try { - const installedPkgPath = path.join(nodeModulesDir, pkgName, 'package.json') + const pluginRoot = isLocalPath ? path.resolve(pkgName) : path.join(nodeModulesDir, pkgName) + const installedPkgPath = path.join(pluginRoot, 'package.json') const { readFileSync } = await import('node:fs') const installedPkg = JSON.parse(readFileSync(installedPkgPath, 'utf-8')) @@ -318,7 +320,7 @@ async function installPlugin(input: string): Promise { } // Try to load and run install hook - const pluginModule = await import(path.join(nodeModulesDir, pkgName, installedPkg.main ?? 'dist/index.js')) + const pluginModule = await import(path.join(pluginRoot, installedPkg.main ?? 'dist/index.js')) const plugin = pluginModule.default if (plugin?.install) { @@ -377,7 +379,7 @@ async function uninstallPlugin(name: string, purge: boolean): Promise { if (plugin?.uninstall) { const { SettingsManager } = await import('../../core/plugin/settings-manager.js') const { createInstallContext } = await import('../../core/plugin/install-context.js') - const basePath = path.join(os.homedir(), '.openacp', 'plugins') + const basePath = path.join(os.homedir(), '.openacp', 'plugins', 'data') const settingsManager = new SettingsManager(basePath) const ctx = createInstallContext({ pluginName: name, settingsManager, basePath }) await plugin.uninstall(ctx, { purge }) diff --git a/src/core/plugin/lifecycle-manager.ts b/src/core/plugin/lifecycle-manager.ts index c5e3fff..8b003be 100644 --- a/src/core/plugin/lifecycle-manager.ts +++ b/src/core/plugin/lifecycle-manager.ts @@ -205,11 +205,14 @@ export class LifecycleManager { let pluginConfig: Record if (this.settingsManager) { pluginConfig = await this.settingsManager.loadSettings(plugin.name) + const settingsPath = this.settingsManager.getSettingsPath(plugin.name) + this.getPluginLogger(plugin.name).debug(`Settings loaded from ${settingsPath}: ${Object.keys(pluginConfig).length} keys`) if (Object.keys(pluginConfig).length === 0) { pluginConfig = resolvePluginConfig(plugin.name, this.config) } } else { pluginConfig = resolvePluginConfig(plugin.name, this.config) + this.getPluginLogger(plugin.name).debug('No settingsManager, using legacy config') } // Create context for this plugin diff --git a/src/main.ts b/src/main.ts index a89a514..8cfc07a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -140,14 +140,49 @@ export async function startServer(opts?: StartServerOptions) { try { let modulePath: string - if (entry.source === 'local') { - // Local plugin: name is the filesystem path or resolves directly - modulePath = name.startsWith('/') || name.startsWith('.') - ? path.resolve(name) - : path.join(OPENACP_DIR, 'plugins', 'node_modules', name) + if (name.startsWith('/') || name.startsWith('.')) { + // Absolute or relative path (local install via `plugin add /path/to/plugin`) + const resolved = path.resolve(name) + const pkgPath = path.join(resolved, 'package.json') + const pkg = JSON.parse(await fs.promises.readFile(pkgPath, 'utf-8')) + modulePath = path.join(resolved, pkg.main || 'dist/index.js') } else { - // npm plugin: installed under ~/.openacp/plugins/node_modules/ - modulePath = path.join(OPENACP_DIR, 'plugins', 'node_modules', name) + // npm package: try direct name first, then scan node_modules for matching plugin name + const nodeModulesDir = path.join(OPENACP_DIR, 'plugins', 'node_modules') + let pkgDir = path.join(nodeModulesDir, name) + + if (!fs.existsSync(path.join(pkgDir, 'package.json'))) { + // Plugin name doesn't match npm package name — scan installed packages + let found = false + const scopes = fs.readdirSync(nodeModulesDir).filter(d => d.startsWith('@')) + for (const scope of scopes) { + const scopeDir = path.join(nodeModulesDir, scope) + const pkgs = fs.readdirSync(scopeDir) + for (const pkg of pkgs) { + const candidateDir = path.join(scopeDir, pkg) + const candidatePkgPath = path.join(candidateDir, 'package.json') + if (fs.existsSync(candidatePkgPath)) { + try { + const candidatePkg = JSON.parse(fs.readFileSync(candidatePkgPath, 'utf-8')) + const mainPath = path.join(candidateDir, candidatePkg.main || 'dist/index.js') + if (fs.existsSync(mainPath)) { + const testMod = await import(mainPath) + if (testMod.default?.name === name) { + pkgDir = candidateDir + found = true + break + } + } + } catch { /* skip */ } + } + } + if (found) break + } + } + + const pkgJsonPath = path.join(pkgDir, 'package.json') + const pkg = JSON.parse(await fs.promises.readFile(pkgJsonPath, 'utf-8')) + modulePath = path.join(pkgDir, pkg.main || 'dist/index.js') } log.debug({ plugin: name, modulePath }, 'Loading community plugin')