mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Implement plugins system / skills
This commit is contained in:
23 files changed
+1310
-8
No files matched your search
@@ -63,6 +63,7 @@ export class SessionManager {
|
||||
private getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata };
|
||||
private corsProxy?: string;
|
||||
private projectsPath: string;
|
||||
private pluginsPath?: string;
|
||||
|
||||
constructor(
|
||||
fs: JSRuntimeFS,
|
||||
@@ -71,12 +72,14 @@ export class SessionManager {
|
||||
getCurrentUser?: () => { user?: NUser; metadata?: NostrMetadata },
|
||||
corsProxy?: string,
|
||||
projectsPath = '/projects',
|
||||
pluginsPath?: string,
|
||||
) {
|
||||
this.fs = fs;
|
||||
this.aiSettings = aiSettings;
|
||||
this.getProviderModels = getProviderModels;
|
||||
this.getCurrentUser = getCurrentUser;
|
||||
this.projectsPath = projectsPath;
|
||||
this.pluginsPath = pluginsPath;
|
||||
this.corsProxy = corsProxy;
|
||||
}
|
||||
|
||||
@@ -242,6 +245,7 @@ export class SessionManager {
|
||||
name: "Shakespeare",
|
||||
profession: "software extraordinaire",
|
||||
tools: Object.values(session.tools),
|
||||
pluginsPath: this.pluginsPath,
|
||||
user,
|
||||
metadata,
|
||||
});
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { LightningFSAdapter } from './LightningFSAdapter';
|
||||
import LightningFS from '@isomorphic-git/lightning-fs';
|
||||
import {
|
||||
getPlugins,
|
||||
getPluginSkills,
|
||||
getAllSkills,
|
||||
findSkill,
|
||||
readSkillContent,
|
||||
isValidSkillName,
|
||||
} from './plugins';
|
||||
|
||||
describe('plugins', () => {
|
||||
let fs: LightningFSAdapter;
|
||||
|
||||
beforeEach(() => {
|
||||
const lightningFS = new LightningFS('test-plugins-fs');
|
||||
fs = new LightningFSAdapter(lightningFS.promises);
|
||||
});
|
||||
|
||||
describe('isValidSkillName', () => {
|
||||
it('should validate alphanumeric lowercase names', () => {
|
||||
expect(isValidSkillName('test')).toBe(true);
|
||||
expect(isValidSkillName('test123')).toBe(true);
|
||||
expect(isValidSkillName('abc')).toBe(true);
|
||||
});
|
||||
|
||||
it('should reject invalid names', () => {
|
||||
expect(isValidSkillName('Test')).toBe(false);
|
||||
expect(isValidSkillName('test-name')).toBe(false);
|
||||
expect(isValidSkillName('test_name')).toBe(false);
|
||||
expect(isValidSkillName('test name')).toBe(false);
|
||||
expect(isValidSkillName('test-123')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPlugins', () => {
|
||||
it('should return empty array when plugins directory does not exist', async () => {
|
||||
const plugins = await getPlugins(fs, '/plugins');
|
||||
expect(plugins).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return list of plugin directories', async () => {
|
||||
await fs.mkdir('/plugins/plugin1', { recursive: true });
|
||||
await fs.mkdir('/plugins/plugin2', { recursive: true });
|
||||
await fs.writeFile('/plugins/file.txt', 'not a directory');
|
||||
|
||||
const plugins = await getPlugins(fs, '/plugins');
|
||||
expect(plugins).toContain('plugin1');
|
||||
expect(plugins).toContain('plugin2');
|
||||
expect(plugins).not.toContain('file.txt');
|
||||
});
|
||||
});
|
||||
|
||||
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');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return skills with valid SKILL.md files', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/myskill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: myskill
|
||||
description: A test skill
|
||||
---
|
||||
|
||||
# My Skill
|
||||
|
||||
This is a test skill.`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins', 'testplugin');
|
||||
expect(skills).toHaveLength(1);
|
||||
expect(skills[0]).toEqual({
|
||||
name: 'myskill',
|
||||
description: 'A test skill',
|
||||
path: skillPath,
|
||||
plugin: 'testplugin',
|
||||
});
|
||||
});
|
||||
|
||||
it('should skip skills with invalid names', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/InvalidSkill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: InvalidSkill
|
||||
description: Has uppercase letters
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins', 'testplugin');
|
||||
expect(skills).toEqual([]);
|
||||
});
|
||||
|
||||
it('should skip skills missing required frontmatter', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/incomplete';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: incomplete
|
||||
---
|
||||
|
||||
Missing description`
|
||||
);
|
||||
|
||||
const skills = await getPluginSkills(fs, '/plugins', 'testplugin');
|
||||
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 });
|
||||
await fs.writeFile(
|
||||
`${skill1Path}/SKILL.md`,
|
||||
`---
|
||||
name: skill1
|
||||
description: First skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
// Plugin 2
|
||||
const skill2Path = '/plugins/plugin2/skills/skill2';
|
||||
await fs.mkdir(skill2Path, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skill2Path}/SKILL.md`,
|
||||
`---
|
||||
name: skill2
|
||||
description: Second skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skills = await getAllSkills(fs, '/plugins');
|
||||
expect(skills).toHaveLength(2);
|
||||
expect(skills.map((s) => s.name)).toContain('skill1');
|
||||
expect(skills.map((s) => s.name)).toContain('skill2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findSkill', () => {
|
||||
it('should find a skill by name', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/findme';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
await fs.writeFile(
|
||||
`${skillPath}/SKILL.md`,
|
||||
`---
|
||||
name: findme
|
||||
description: Find this skill
|
||||
---
|
||||
|
||||
Content`
|
||||
);
|
||||
|
||||
const skill = await findSkill(fs, '/plugins', 'findme');
|
||||
expect(skill).not.toBeNull();
|
||||
expect(skill?.name).toBe('findme');
|
||||
});
|
||||
|
||||
it('should return null when skill not found', async () => {
|
||||
const skill = await findSkill(fs, '/plugins', 'nonexistent');
|
||||
expect(skill).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('readSkillContent', () => {
|
||||
it('should read the full SKILL.md content', async () => {
|
||||
const skillPath = '/plugins/testplugin/skills/myskill';
|
||||
await fs.mkdir(skillPath, { recursive: true });
|
||||
const content = `---
|
||||
name: myskill
|
||||
description: A test skill
|
||||
---
|
||||
|
||||
# My Skill
|
||||
|
||||
This is the skill content.`;
|
||||
|
||||
await fs.writeFile(`${skillPath}/SKILL.md`, content);
|
||||
|
||||
const skill = {
|
||||
name: 'myskill',
|
||||
description: 'A test skill',
|
||||
path: skillPath,
|
||||
plugin: 'testplugin',
|
||||
};
|
||||
|
||||
const readContent = await readSkillContent(fs, skill);
|
||||
expect(readContent).toBe(content);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,167 @@
|
||||
import { join } from 'path-browserify';
|
||||
import matter from 'gray-matter';
|
||||
import type { JSRuntimeFS } from './JSRuntime';
|
||||
|
||||
export interface Skill {
|
||||
/** Skill name (alphanumeric lowercase only) */
|
||||
name: string;
|
||||
/** Human-readable description of what the skill does */
|
||||
description: string;
|
||||
/** Full path to the skill directory */
|
||||
path: string;
|
||||
/** Plugin name that contains this skill */
|
||||
plugin: string;
|
||||
}
|
||||
|
||||
export interface SkillFrontmatter {
|
||||
name: string;
|
||||
description: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a skill name (alphanumeric lowercase only)
|
||||
*/
|
||||
export function isValidSkillName(name: string): boolean {
|
||||
return /^[a-z0-9]+$/.test(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all plugins from the plugins directory
|
||||
*/
|
||||
export async function getPlugins(fs: JSRuntimeFS, pluginsPath: string): Promise<string[]> {
|
||||
try {
|
||||
const entries = await fs.readdir(pluginsPath);
|
||||
const plugins: string[] = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
const stat = await fs.stat(join(pluginsPath, entry));
|
||||
if (stat.isDirectory()) {
|
||||
plugins.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
} catch {
|
||||
// Plugins directory doesn't exist yet
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all skills from a plugin
|
||||
*/
|
||||
export async function getPluginSkills(
|
||||
fs: JSRuntimeFS,
|
||||
pluginsPath: string,
|
||||
pluginName: string
|
||||
): Promise<Skill[]> {
|
||||
const skills: Skill[] = [];
|
||||
const skillsDir = join(pluginsPath, pluginName, 'skills');
|
||||
|
||||
try {
|
||||
const skillDirs = await fs.readdir(skillsDir);
|
||||
|
||||
for (const skillDir of skillDirs) {
|
||||
const skillPath = join(skillsDir, skillDir);
|
||||
const stat = await fs.stat(skillPath);
|
||||
|
||||
if (!stat.isDirectory()) continue;
|
||||
|
||||
const skillFilePath = join(skillPath, 'SKILL.md');
|
||||
|
||||
try {
|
||||
const skillContent = await fs.readFile(skillFilePath, 'utf8');
|
||||
const { data } = matter(skillContent);
|
||||
|
||||
const frontmatter = data as SkillFrontmatter;
|
||||
|
||||
// Validate required fields
|
||||
if (!frontmatter.name || !frontmatter.description) {
|
||||
console.warn(`Skill at ${skillPath} missing required frontmatter fields (name, description)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Validate skill name format
|
||||
if (!isValidSkillName(frontmatter.name)) {
|
||||
console.warn(`Skill at ${skillPath} has invalid name "${frontmatter.name}" (must be alphanumeric lowercase only)`);
|
||||
continue;
|
||||
}
|
||||
|
||||
skills.push({
|
||||
name: frontmatter.name,
|
||||
description: frontmatter.description,
|
||||
path: skillPath,
|
||||
plugin: pluginName,
|
||||
});
|
||||
} catch {
|
||||
// SKILL.md not found or invalid, skip this directory
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Skills directory doesn't exist for this plugin
|
||||
return [];
|
||||
}
|
||||
|
||||
return skills;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all skills from all plugins
|
||||
*/
|
||||
export async function getAllSkills(fs: JSRuntimeFS, pluginsPath: string): Promise<Skill[]> {
|
||||
const plugins = await getPlugins(fs, pluginsPath);
|
||||
const allSkills: Skill[] = [];
|
||||
|
||||
for (const plugin of plugins) {
|
||||
const skills = await getPluginSkills(fs, pluginsPath, plugin);
|
||||
allSkills.push(...skills);
|
||||
}
|
||||
|
||||
return allSkills;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a skill by name across all plugins
|
||||
*/
|
||||
export async function findSkill(
|
||||
fs: JSRuntimeFS,
|
||||
pluginsPath: string,
|
||||
skillName: string
|
||||
): Promise<Skill | null> {
|
||||
const skills = await getAllSkills(fs, pluginsPath);
|
||||
return skills.find(skill => skill.name === skillName) || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the SKILL.md content for a skill
|
||||
*/
|
||||
export async function readSkillContent(fs: JSRuntimeFS, skill: Skill): Promise<string> {
|
||||
const skillFilePath = join(skill.path, 'SKILL.md');
|
||||
return await fs.readFile(skillFilePath, 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a plugin directory
|
||||
*/
|
||||
export async function deletePlugin(fs: JSRuntimeFS, pluginsPath: string, pluginName: string): Promise<void> {
|
||||
const pluginPath = join(pluginsPath, pluginName);
|
||||
|
||||
// Recursively delete the plugin directory
|
||||
async function rmrf(path: string): Promise<void> {
|
||||
const stat = await fs.stat(path);
|
||||
|
||||
if (stat.isDirectory()) {
|
||||
const entries = await fs.readdir(path);
|
||||
for (const entry of entries) {
|
||||
await rmrf(join(path, entry));
|
||||
}
|
||||
await fs.rmdir(path);
|
||||
} else {
|
||||
await fs.unlink(path);
|
||||
}
|
||||
}
|
||||
|
||||
await rmrf(pluginPath);
|
||||
}
|
||||
+31
-1
@@ -4,6 +4,7 @@ import { join } from "path-browserify";
|
||||
import OpenAI from "openai";
|
||||
import { nip19 } from "nostr-tools";
|
||||
import { JSRuntimeFS } from "./JSRuntime";
|
||||
import { getAllSkills } from "./plugins";
|
||||
|
||||
export interface MakeSystemPromptOpts {
|
||||
name: string;
|
||||
@@ -12,12 +13,13 @@ export interface MakeSystemPromptOpts {
|
||||
mode: "init" | "agent";
|
||||
fs: JSRuntimeFS;
|
||||
cwd: string;
|
||||
pluginsPath?: string;
|
||||
user?: NUser;
|
||||
metadata?: NostrMetadata;
|
||||
}
|
||||
|
||||
export async function makeSystemPrompt(opts: MakeSystemPromptOpts): Promise<string> {
|
||||
const { name, profession, tools, mode, fs, cwd, user, metadata } = opts;
|
||||
const { name, profession, tools, mode, fs, cwd, pluginsPath, user, metadata } = opts;
|
||||
|
||||
let system = mode === "init"
|
||||
? `You are ${name}, an expert ${profession}. The files in the current directory are a template. Your goal is to transform this template into a working project according to the user's request.`
|
||||
@@ -180,6 +182,34 @@ The user expects you to handle all technical implementation while they focus on
|
||||
}
|
||||
}
|
||||
|
||||
// Add skills section
|
||||
system += "\n\n## Skills\n\n";
|
||||
|
||||
if (pluginsPath) {
|
||||
try {
|
||||
const skills = await getAllSkills(fs, pluginsPath);
|
||||
|
||||
if (skills.length > 0) {
|
||||
system += "You have access to the following skills. **Skills MUST be used whenever applicable** by calling the `skill` tool with the skill name.\n\n";
|
||||
system += "Available skills:\n\n";
|
||||
|
||||
for (const skill of skills) {
|
||||
system += `- **${skill.name}**: ${skill.description}\n - Plugin: ${skill.plugin}\n - Path: ${skill.path}\n`;
|
||||
}
|
||||
|
||||
system += `\n**Important**: When a task matches a skill's description, you MUST use that skill by calling the skill tool. Skills contain specialized workflows and best practices for specific tasks.\n`;
|
||||
} else {
|
||||
system += "No skills are currently configured. Skills are reusable AI workflows that can be added via plugins.\n";
|
||||
}
|
||||
} catch {
|
||||
system += "No skills are currently configured. Skills are reusable AI workflows that can be added via plugins.\n";
|
||||
}
|
||||
} else {
|
||||
system += "No skills are currently configured. Skills are reusable AI workflows that can be added via plugins.\n";
|
||||
}
|
||||
|
||||
system += `\nUsers can configure skills in Settings > AI (${location.origin}/settings/ai) by adding plugins that contain skills.`;
|
||||
|
||||
// Add README.md if it exists
|
||||
try {
|
||||
const readmePath = join(cwd, "README.md");
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { z } from 'zod';
|
||||
import type { Tool } from './Tool';
|
||||
import type { JSRuntimeFS } from '../JSRuntime';
|
||||
import { findSkill, readSkillContent } from '../plugins';
|
||||
|
||||
interface SkillParams {
|
||||
name: string;
|
||||
}
|
||||
|
||||
export class SkillTool implements Tool<SkillParams> {
|
||||
private fs: JSRuntimeFS;
|
||||
private pluginsPath: string;
|
||||
|
||||
readonly description = 'Load and execute a skill from the configured plugins. 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 (alphanumeric lowercase only)'),
|
||||
});
|
||||
|
||||
constructor(fs: JSRuntimeFS, pluginsPath: string) {
|
||||
this.fs = fs;
|
||||
this.pluginsPath = pluginsPath;
|
||||
}
|
||||
|
||||
async execute(args: SkillParams): Promise<string> {
|
||||
const { name } = args;
|
||||
|
||||
try {
|
||||
// Find the skill across all plugins
|
||||
const skill = await findSkill(this.fs, this.pluginsPath, name);
|
||||
|
||||
if (!skill) {
|
||||
throw new Error(`Skill "${name}" not found. Use the skill tool to list available skills or configure plugins in Settings > AI.`);
|
||||
}
|
||||
|
||||
// Read and return the skill content
|
||||
const content = await readSkillContent(this.fs, skill);
|
||||
|
||||
return `# Skill: ${skill.name}
|
||||
|
||||
**Plugin**: ${skill.plugin}
|
||||
**Description**: ${skill.description}
|
||||
**Path**: ${skill.path}
|
||||
|
||||
---
|
||||
|
||||
${content}`;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(`Failed to execute skill "${name}": ${String(error)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user