|
| 1 | +import { homedir } from 'node:os'; |
| 2 | + |
| 3 | +import { registerBuiltinSkills } from './builtin'; |
| 4 | +import { SessionSkillRegistry } from './registry'; |
| 5 | +import { resolveSkillRoots } from './scanner'; |
| 6 | +import { summarizeSkill, type SkillRoot, type SkillSummary } from './types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Field-for-field the resolution inputs a session uses, so a caller can pass the |
| 10 | + * config it already assembles for sessions. Declared here rather than imported |
| 11 | + * from the session layer, which depends on this one. |
| 12 | + */ |
| 13 | +export interface ListWorkspaceSkillsOptions { |
| 14 | + /** Directory the skills are resolved for, as a session's cwd would be. */ |
| 15 | + readonly workDir: string; |
| 16 | + readonly userHomeDir?: string; |
| 17 | + /** Brand data dir (PYTHINKER_CODE_HOME); user brand skills live under `<brandHomeDir>/skills`. */ |
| 18 | + readonly brandHomeDir?: string; |
| 19 | + readonly explicitDirs?: readonly string[]; |
| 20 | + readonly extraDirs?: readonly string[]; |
| 21 | + readonly pluginSkillRoots?: readonly SkillRoot[]; |
| 22 | + readonly mergeAllAvailableSkills?: boolean; |
| 23 | + readonly builtinDir?: string; |
| 24 | + readonly onWarning?: (message: string, cause?: unknown) => void; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * The skill catalog a workspace resolves to, without opening a session. |
| 29 | + * |
| 30 | + * A session's own `listSkills` additionally reports the prompts of its MCP |
| 31 | + * connections; those belong to a live session and are absent here. |
| 32 | + */ |
| 33 | +export async function listWorkspaceSkills( |
| 34 | + options: ListWorkspaceSkillsOptions, |
| 35 | +): Promise<readonly SkillSummary[]> { |
| 36 | + const registry = new SessionSkillRegistry({ onWarning: options.onWarning }); |
| 37 | + const roots = await resolveSkillRoots({ |
| 38 | + paths: { |
| 39 | + userHomeDir: options.userHomeDir ?? homedir(), |
| 40 | + brandHomeDir: options.brandHomeDir, |
| 41 | + workDir: options.workDir, |
| 42 | + }, |
| 43 | + explicitDirs: options.explicitDirs, |
| 44 | + extraDirs: options.extraDirs, |
| 45 | + pluginSkillRoots: options.pluginSkillRoots, |
| 46 | + mergeAllAvailableSkills: options.mergeAllAvailableSkills, |
| 47 | + builtinDir: options.builtinDir, |
| 48 | + }); |
| 49 | + await registry.loadRoots(roots); |
| 50 | + registerBuiltinSkills(registry); |
| 51 | + return registry.listSkills().map(summarizeSkill); |
| 52 | +} |
0 commit comments