|
| 1 | +import os from 'node:os' |
| 2 | +import path from 'node:path' |
| 3 | +import { existsSync } from 'node:fs' |
| 4 | +import { readJsonFile } from '../utils/config.js' |
| 5 | + |
| 6 | +/** |
| 7 | + * Native package detection for the Pi Agent harness. |
| 8 | + * |
| 9 | + * Pi is package-owned: `pi install npm:nsolid-pi-plugin` installs skills via a |
| 10 | + * real npm package rather than the shared CLI tracking file. These helpers |
| 11 | + * discover that package on disk (from `~/.pi/agent/settings.json` `packages` |
| 12 | + * entries or the canonical npm install location) so both `doctor` and the |
| 13 | + * Pi adapter can report it without the CLI tracking file. |
| 14 | + * |
| 15 | + * Extracted from `index.ts` so the Pi adapter and `doctor()` share one source |
| 16 | + * of truth. |
| 17 | + */ |
| 18 | + |
| 19 | +export const PI_PLUGIN_PACKAGE_NAME = 'nsolid-pi-plugin' |
| 20 | + |
| 21 | +function readPiPackageSourceEntries (settingsPath: string): string[] { |
| 22 | + let settings: { packages?: Array<string | { source?: string }> } | null = null |
| 23 | + try { |
| 24 | + settings = readJsonFile<{ packages?: Array<string | { source?: string }> }>(settingsPath) |
| 25 | + } catch { |
| 26 | + return [] |
| 27 | + } |
| 28 | + if (!settings || !Array.isArray(settings.packages)) return [] |
| 29 | + |
| 30 | + return settings.packages |
| 31 | + .map((entry) => typeof entry === 'string' ? entry : entry.source) |
| 32 | + .filter((source): source is string => typeof source === 'string' && source.length > 0) |
| 33 | +} |
| 34 | + |
| 35 | +function packageNameFromNpmSource (source: string): string | null { |
| 36 | + if (!source.startsWith('npm:')) return null |
| 37 | + const spec = source.slice('npm:'.length) |
| 38 | + if (spec.startsWith('@')) { |
| 39 | + const [scope, name] = spec.split('/') |
| 40 | + if (!scope || !name) return null |
| 41 | + return `${scope}/${name.split('@')[0]}` |
| 42 | + } |
| 43 | + return spec.split('@')[0] || null |
| 44 | +} |
| 45 | + |
| 46 | +function resolvePiPackageRootFromSource (source: string, settingsDir: string): string | null { |
| 47 | + const npmPackageName = packageNameFromNpmSource(source) |
| 48 | + if (npmPackageName) { |
| 49 | + if (npmPackageName !== PI_PLUGIN_PACKAGE_NAME) return null |
| 50 | + return path.join(settingsDir, 'npm', 'node_modules', PI_PLUGIN_PACKAGE_NAME) |
| 51 | + } |
| 52 | + |
| 53 | + if (source.startsWith('git:') || source.startsWith('http://') || source.startsWith('https://') || source.startsWith('ssh://')) { |
| 54 | + return null |
| 55 | + } |
| 56 | + |
| 57 | + return path.resolve(settingsDir, source) |
| 58 | +} |
| 59 | + |
| 60 | +export function findPiPluginPackageRoots (): string[] { |
| 61 | + const settingsPaths = [ |
| 62 | + path.join(os.homedir(), '.pi', 'agent', 'settings.json'), |
| 63 | + path.resolve('.pi', 'settings.json'), |
| 64 | + ] |
| 65 | + const roots = new Set<string>() |
| 66 | + |
| 67 | + for (const settingsPath of settingsPaths) { |
| 68 | + const settingsDir = path.dirname(settingsPath) |
| 69 | + for (const source of readPiPackageSourceEntries(settingsPath)) { |
| 70 | + const root = resolvePiPackageRootFromSource(source, settingsDir) |
| 71 | + if (root) roots.add(root) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + roots.add(path.join(os.homedir(), '.pi', 'agent', 'npm', 'node_modules', PI_PLUGIN_PACKAGE_NAME)) |
| 76 | + return [...roots] |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Skill roots declared by each installed Pi package. A package without an |
| 81 | + * explicit `pi.skills` array defaults to `./skills` (matching the package |
| 82 | + * generator and the install test). |
| 83 | + */ |
| 84 | +export function findPiPluginSkillRoots (): string[] { |
| 85 | + const skillRoots: string[] = [] |
| 86 | + for (const packageRoot of findPiPluginPackageRoots()) { |
| 87 | + const pkgPath = path.join(packageRoot, 'package.json') |
| 88 | + let pkg: { name?: string; pi?: { skills?: string[] } } | null = null |
| 89 | + try { |
| 90 | + pkg = readJsonFile<{ name?: string; pi?: { skills?: string[] } }>(pkgPath) |
| 91 | + } catch { |
| 92 | + continue |
| 93 | + } |
| 94 | + if (pkg?.name !== PI_PLUGIN_PACKAGE_NAME) continue |
| 95 | + |
| 96 | + const configuredSkillRoots = Array.isArray(pkg.pi?.skills) && pkg.pi!.skills.length > 0 |
| 97 | + ? pkg.pi!.skills |
| 98 | + : ['./skills'] |
| 99 | + for (const skillRoot of configuredSkillRoots) { |
| 100 | + skillRoots.push(path.resolve(packageRoot, skillRoot)) |
| 101 | + } |
| 102 | + } |
| 103 | + return skillRoots |
| 104 | +} |
| 105 | + |
| 106 | +/** True when a nsolid-pi-plugin package root exists on disk. */ |
| 107 | +export function piPluginInstalled (): boolean { |
| 108 | + return findPiPluginPackageRoots().some((root) => { |
| 109 | + try { |
| 110 | + return existsSync(path.join(root, 'package.json')) |
| 111 | + } catch { |
| 112 | + return false |
| 113 | + } |
| 114 | + }) |
| 115 | +} |
0 commit comments