diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index c48903d3..884d8e67 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -177,7 +177,7 @@ export const ChatPane = forwardRef(({ nostr_publish_events: new NostrPublishEventsTool(), shell: new ShellTool(fs, cwd, git, config.corsProxy, user?.signer), read_console_messages: new ReadConsoleMessagesTool(), - skill: new SkillTool(fs, pluginsPath), + skill: new SkillTool(fs, pluginsPath, cwd), }; // Add deploy tool only if user is logged in diff --git a/src/hooks/usePlugins.ts b/src/hooks/usePlugins.ts index 79648793..ff6a16f0 100644 --- a/src/hooks/usePlugins.ts +++ b/src/hooks/usePlugins.ts @@ -1,7 +1,7 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useFS } from './useFS'; import { useFSPaths } from './useFSPaths'; -import { getPlugins, getAllSkills, deletePlugin as deletePluginFn } from '@/lib/plugins'; +import { getPlugins, getPluginSkills, deletePlugin as deletePluginFn } from '@/lib/plugins'; import { useGit } from './useGit'; import { join } from 'path-browserify'; @@ -22,11 +22,11 @@ export function usePlugins() { }, }); - // Query to get all skills + // Query to get all plugin skills (not including project skills) const skillsQuery = useQuery({ queryKey: ['skills', pluginsPath], queryFn: async () => { - return await getAllSkills(fs, pluginsPath); + return await getPluginSkills(fs, pluginsPath); }, }); diff --git a/src/lib/plugins.test.ts b/src/lib/plugins.test.ts index 94a18e3f..6597d061 100644 --- a/src/lib/plugins.test.ts +++ b/src/lib/plugins.test.ts @@ -3,6 +3,7 @@ import type { JSRuntimeFS } from './JSRuntime'; import { getPlugins, getPluginSkills, + getProjectSkills, getAllSkills, findSkill, readSkillContent, @@ -200,14 +201,12 @@ describe('plugins', () => { }); describe('getPluginSkills', () => { - it('should return empty array when plugin has no skills', async () => { - await fs.mkdir('/plugins/plugin1', { recursive: true }); - - const skills = await getPluginSkills(fs, '/plugins', 'plugin1'); + it('should return empty array when no plugins exist', async () => { + const skills = await getPluginSkills(fs, '/plugins'); expect(skills).toEqual([]); }); - it('should return skills with valid SKILL.md files', async () => { + it('should return skills from all plugins', async () => { const skillPath = '/plugins/testplugin/skills/myskill'; await fs.mkdir(skillPath, { recursive: true }); await fs.writeFile( @@ -222,7 +221,7 @@ description: A test skill This is a test skill.` ); - const skills = await getPluginSkills(fs, '/plugins', 'testplugin'); + const skills = await getPluginSkills(fs, '/plugins'); expect(skills).toHaveLength(1); expect(skills[0]).toEqual({ name: 'myskill', @@ -245,7 +244,7 @@ description: Has uppercase letters Content` ); - const skills = await getPluginSkills(fs, '/plugins', 'testplugin'); + const skills = await getPluginSkills(fs, '/plugins'); expect(skills).toEqual([]); }); @@ -261,48 +260,131 @@ name: incomplete Missing description` ); - const skills = await getPluginSkills(fs, '/plugins', 'testplugin'); + const skills = await getPluginSkills(fs, '/plugins'); expect(skills).toEqual([]); }); }); describe('getAllSkills', () => { - it('should return skills from all plugins', async () => { - // Plugin 1 - const skill1Path = '/plugins/plugin1/skills/skill1'; - await fs.mkdir(skill1Path, { recursive: true }); + it('should combine plugin and project skills', async () => { + const pluginPath = '/plugins/testplugin/skills/pluginskill'; + const projectPath = '/projects/myproject'; + const projectSkillPath = `${projectPath}/skills/projectskill`; + + // Create plugin skill + await fs.mkdir(pluginPath, { recursive: true }); await fs.writeFile( - `${skill1Path}/SKILL.md`, + `${pluginPath}/SKILL.md`, `--- -name: skill1 -description: First skill +name: plugin-skill +description: A plugin skill --- Content` ); - // Plugin 2 - const skill2Path = '/plugins/plugin2/skills/skill2'; - await fs.mkdir(skill2Path, { recursive: true }); + // Create project skill + await fs.mkdir(projectSkillPath, { recursive: true }); await fs.writeFile( - `${skill2Path}/SKILL.md`, + `${projectSkillPath}/SKILL.md`, `--- -name: skill2 -description: Second skill +name: project-skill +description: A project skill --- Content` ); - const skills = await getAllSkills(fs, '/plugins'); + const skills = await getAllSkills(fs, '/plugins', projectPath); + expect(skills).toHaveLength(2); - expect(skills.map((s) => s.name)).toContain('skill1'); - expect(skills.map((s) => s.name)).toContain('skill2'); + expect(skills.map((s) => s.name)).toContain('plugin-skill'); + expect(skills.map((s) => s.name)).toContain('project-skill'); + }); + + it('should give precedence to project skills over plugin skills with same name', async () => { + const pluginPath = '/plugins/testplugin/skills/samename'; + const projectPath = '/projects/myproject'; + const projectSkillPath = `${projectPath}/skills/samename`; + + // Create plugin skill + await fs.mkdir(pluginPath, { recursive: true }); + await fs.writeFile( + `${pluginPath}/SKILL.md`, + `--- +name: same-name +description: Plugin version +--- + +Plugin content` + ); + + // Create project skill with same name + await fs.mkdir(projectSkillPath, { recursive: true }); + await fs.writeFile( + `${projectSkillPath}/SKILL.md`, + `--- +name: same-name +description: Project version +--- + +Project content` + ); + + const skills = await getAllSkills(fs, '/plugins', projectPath); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe('same-name'); + expect(skills[0].description).toBe('Project version'); + expect(skills[0].plugin).toBe('project'); + }); + + it('should return only plugin skills when project has no skills', async () => { + const pluginPath = '/plugins/testplugin/skills/pluginskill'; + + await fs.mkdir(pluginPath, { recursive: true }); + await fs.writeFile( + `${pluginPath}/SKILL.md`, + `--- +name: plugin-skill +description: A plugin skill +--- + +Content` + ); + + const skills = await getAllSkills(fs, '/plugins', '/projects/emptyproject'); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe('plugin-skill'); + expect(skills[0].plugin).toBe('testplugin'); + }); + + it('should return only project skills when there are no plugins', async () => { + const projectPath = '/projects/myproject'; + const projectSkillPath = `${projectPath}/skills/projectskill`; + + await fs.mkdir(projectSkillPath, { recursive: true }); + await fs.writeFile( + `${projectSkillPath}/SKILL.md`, + `--- +name: project-skill +description: A project skill +--- + +Content` + ); + + const skills = await getAllSkills(fs, '/empty-plugins', projectPath); + + expect(skills).toHaveLength(1); + expect(skills[0].name).toBe('project-skill'); + expect(skills[0].plugin).toBe('project'); }); }); describe('findSkill', () => { - it('should find a skill by name', async () => { + it('should find a plugin skill by name', async () => { const skillPath = '/plugins/testplugin/skills/findme'; await fs.mkdir(skillPath, { recursive: true }); await fs.writeFile( @@ -315,17 +397,76 @@ description: Find this skill Content` ); - const skill = await findSkill(fs, '/plugins', 'findme'); + const skill = await findSkill(fs, '/plugins', '/projects/myproject', 'findme'); expect(skill).not.toBeNull(); expect(skill?.name).toBe('findme'); + expect(skill?.plugin).toBe('testplugin'); }); - it('should return null when skill not found', async () => { - const skill = await findSkill(fs, '/plugins', 'nonexistent'); + it('should find a project skill by name', async () => { + const projectPath = '/projects/myproject'; + const skillPath = `${projectPath}/skills/projectskill`; + await fs.mkdir(skillPath, { recursive: true }); + await fs.writeFile( + `${skillPath}/SKILL.md`, + `--- +name: projectskill +description: A project skill +--- + +Content` + ); + + const skill = await findSkill(fs, '/plugins', projectPath, 'projectskill'); + expect(skill).not.toBeNull(); + expect(skill?.name).toBe('projectskill'); + expect(skill?.plugin).toBe('project'); + }); + + it('should prefer project skill over plugin skill with same name', async () => { + const pluginPath = '/plugins/testplugin/skills/samename'; + const projectPath = '/projects/myproject'; + const projectSkillPath = `${projectPath}/skills/samename`; + + // Create plugin skill + await fs.mkdir(pluginPath, { recursive: true }); + await fs.writeFile( + `${pluginPath}/SKILL.md`, + `--- +name: samename +description: Plugin version +--- + +Plugin content` + ); + + // Create project skill with same name + await fs.mkdir(projectSkillPath, { recursive: true }); + await fs.writeFile( + `${projectSkillPath}/SKILL.md`, + `--- +name: samename +description: Project version +--- + +Project content` + ); + + const skill = await findSkill(fs, '/plugins', projectPath, 'samename'); + expect(skill).not.toBeNull(); + expect(skill?.name).toBe('samename'); + expect(skill?.description).toBe('Project version'); + expect(skill?.plugin).toBe('project'); + }); + + it('should return null when skill not found in either plugins or project', async () => { + const skill = await findSkill(fs, '/plugins', '/projects/myproject', 'nonexistent'); expect(skill).toBeNull(); }); }); + + describe('readSkillContent', () => { it('should read the full SKILL.md content', async () => { const skillPath = '/plugins/testplugin/skills/myskill'; @@ -352,4 +493,69 @@ This is the skill content.`; expect(readContent).toBe(content); }); }); + + describe('getProjectSkills', () => { + it('should get skills from project skills directory', async () => { + const projectPath = '/projects/myproject'; + const skillPath1 = `${projectPath}/skills/skill1`; + const skillPath2 = `${projectPath}/skills/skill2`; + + await fs.mkdir(skillPath1, { recursive: true }); + await fs.mkdir(skillPath2, { recursive: true }); + + await fs.writeFile( + `${skillPath1}/SKILL.md`, + `--- +name: project-skill-1 +description: First project skill +--- + +Content` + ); + + await fs.writeFile( + `${skillPath2}/SKILL.md`, + `--- +name: project-skill-2 +description: Second project skill +--- + +Content` + ); + + const skills = await getProjectSkills(fs, projectPath); + + expect(skills).toHaveLength(2); + expect(skills[0].name).toBe('project-skill-1'); + expect(skills[0].plugin).toBe('project'); + expect(skills[1].name).toBe('project-skill-2'); + expect(skills[1].plugin).toBe('project'); + }); + + it('should return empty array when project has no skills directory', async () => { + const skills = await getProjectSkills(fs, '/projects/emptyproject'); + expect(skills).toEqual([]); + }); + + it('should skip skills with invalid names', async () => { + const projectPath = '/projects/myproject'; + const skillPath = `${projectPath}/skills/badskill`; + + await fs.mkdir(skillPath, { recursive: true }); + await fs.writeFile( + `${skillPath}/SKILL.md`, + `--- +name: Invalid_Skill_Name +description: This has uppercase and underscores +--- + +Content` + ); + + const skills = await getProjectSkills(fs, projectPath); + expect(skills).toEqual([]); + }); + }); + + }); diff --git a/src/lib/plugins.ts b/src/lib/plugins.ts index fc9ca46f..da02d672 100644 --- a/src/lib/plugins.ts +++ b/src/lib/plugins.ts @@ -56,15 +56,25 @@ export async function getPlugins(fs: JSRuntimeFS, pluginsPath: string): Promise< } /** - * Get all skills from a plugin + * Get all skills from a single plugin */ -export async function getPluginSkills( +function getSkillsFromPlugin( fs: JSRuntimeFS, pluginsPath: string, pluginName: string +): Promise { + return getSkillsFromDirectory(fs, join(pluginsPath, pluginName, 'skills'), pluginName); +} + +/** + * Get skills from a directory with a given plugin/source name + */ +async function getSkillsFromDirectory( + fs: JSRuntimeFS, + skillsDir: string, + sourceName: string ): Promise { const skills: Skill[] = []; - const skillsDir = join(pluginsPath, pluginName, 'skills'); try { const skillDirs = await fs.readdir(skillsDir); @@ -99,7 +109,7 @@ export async function getPluginSkills( name: frontmatter.name, description: frontmatter.description, path: skillPath, - plugin: pluginName, + plugin: sourceName, }); } catch { // SKILL.md not found or invalid, skip this directory @@ -107,7 +117,7 @@ export async function getPluginSkills( } } } catch { - // Skills directory doesn't exist for this plugin + // Skills directory doesn't exist return []; } @@ -115,14 +125,24 @@ export async function getPluginSkills( } /** - * Get all skills from all plugins + * Get all skills from a project's skills directory */ -export async function getAllSkills(fs: JSRuntimeFS, pluginsPath: string): Promise { +export async function getProjectSkills( + fs: JSRuntimeFS, + projectPath: string +): Promise { + return getSkillsFromDirectory(fs, join(projectPath, 'skills'), 'project'); +} + +/** + * Get all skills from all plugins (without project skills) + */ +export async function getPluginSkills(fs: JSRuntimeFS, pluginsPath: string): Promise { const plugins = await getPlugins(fs, pluginsPath); const allSkills: Skill[] = []; for (const plugin of plugins) { - const skills = await getPluginSkills(fs, pluginsPath, plugin); + const skills = await getSkillsFromPlugin(fs, pluginsPath, plugin); allSkills.push(...skills); } @@ -130,14 +150,44 @@ export async function getAllSkills(fs: JSRuntimeFS, pluginsPath: string): Promis } /** - * Find a skill by name across all plugins + * Get all skills from both plugins and project, with project skills taking precedence + */ +export async function getAllSkills( + fs: JSRuntimeFS, + pluginsPath: string, + projectPath: string +): Promise { + // Get skills from both sources + const pluginSkills = await getPluginSkills(fs, pluginsPath); + const projectSkills = await getProjectSkills(fs, projectPath); + + // Create a map to handle name collisions, with project skills taking precedence + const skillsMap = new Map(); + + // Add plugin skills first + for (const skill of pluginSkills) { + skillsMap.set(skill.name, skill); + } + + // Add project skills, overwriting any plugin skills with the same name + for (const skill of projectSkills) { + skillsMap.set(skill.name, skill); + } + + // Return all skills as an array + return Array.from(skillsMap.values()); +} + +/** + * Find a skill by name across both plugins and project, with project skills taking precedence */ export async function findSkill( fs: JSRuntimeFS, pluginsPath: string, + projectPath: string, skillName: string ): Promise { - const skills = await getAllSkills(fs, pluginsPath); + const skills = await getAllSkills(fs, pluginsPath, projectPath); return skills.find(skill => skill.name === skillName) || null; } diff --git a/src/lib/system.test.ts b/src/lib/system.test.ts index cb08ae5c..c6f7cfd4 100644 --- a/src/lib/system.test.ts +++ b/src/lib/system.test.ts @@ -253,7 +253,7 @@ describe('makeSystemPrompt', () => { const result = await makeSystemPrompt(baseOpts); expect(result).toContain('No skills are currently configured'); - expect(getAllSkills).toHaveBeenCalledWith(mockFs, '/plugins'); + expect(getAllSkills).toHaveBeenCalledWith(mockFs, '/plugins', '/projects/test-project'); }); it('should list available skills when they exist', async () => { diff --git a/src/lib/system.ts b/src/lib/system.ts index 3a0be858..eecc7c74 100644 --- a/src/lib/system.ts +++ b/src/lib/system.ts @@ -232,11 +232,11 @@ export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise = []; if (config.fsPathPlugins) { try { - skills = await getAllSkills(fs, config.fsPathPlugins); + skills = await getAllSkills(fs, config.fsPathPlugins, cwd); } catch { // Skills not available, use empty array skills = []; diff --git a/src/lib/tools/SkillTool.ts b/src/lib/tools/SkillTool.ts index c07e868c..a24ac6fb 100644 --- a/src/lib/tools/SkillTool.ts +++ b/src/lib/tools/SkillTool.ts @@ -10,24 +10,26 @@ interface SkillParams { export class SkillTool implements Tool { private fs: JSRuntimeFS; private pluginsPath: string; + private projectPath: string; - readonly description = 'Load and execute a skill from the configured plugins. Skills are reusable AI workflows that can be invoked by name.'; + readonly description = 'Load and execute a skill from the configured plugins or project. Skills are reusable AI workflows that can be invoked by name.'; readonly inputSchema = z.object({ name: z.string().describe('The name of the skill to execute (lowercase alphanumeric with hyphens)'), }); - constructor(fs: JSRuntimeFS, pluginsPath: string) { + constructor(fs: JSRuntimeFS, pluginsPath: string, projectPath: string) { this.fs = fs; this.pluginsPath = pluginsPath; + this.projectPath = projectPath; } async execute(args: SkillParams): Promise { const { name } = args; try { - // Find the skill across all plugins - const skill = await findSkill(this.fs, this.pluginsPath, name); + // Find the skill across all plugins and project + const skill = await findSkill(this.fs, this.pluginsPath, this.projectPath, name); if (!skill) { throw new Error(`Skill "${name}" not found. Use the skill tool to list available skills or configure plugins in Settings > AI.`); @@ -38,7 +40,7 @@ export class SkillTool implements Tool { return `# Skill: ${skill.name} -**Plugin**: ${skill.plugin} +**Source**: ${skill.plugin === 'project' ? 'Project' : `Plugin: ${skill.plugin}`} **Description**: ${skill.description} **Path**: ${skill.path}