| 1 | import { OpenRouter, tool } from '@openrouter/sdk'; |
| 2 | import { readFileSync, existsSync, readdirSync } from 'fs'; |
| 3 | import path from 'path'; |
| 4 | import { z } from 'zod'; |
| 5 | |
| 6 | const openrouter = new OpenRouter({ |
| 7 | apiKey: process.env.OPENROUTER_API_KEY, |
| 8 | }); |
| 9 | |
| 10 | const SKILLS_DIR = path.join(process.env.HOME || '~', '.claude', 'skills'); |
| 11 | |
| 12 | // List available skills |
| 13 | const listAvailableSkills = (): string[] => { |
| 14 | if (!existsSync(SKILLS_DIR)) return []; |
| 15 | return readdirSync(SKILLS_DIR, { withFileTypes: true }) |
| 16 | .filter((dirent) => dirent.isDirectory()) |
| 17 | .filter((dirent) => existsSync(path.join(SKILLS_DIR, dirent.name, 'SKILL.md'))) |
| 18 | .map((dirent) => dirent.name); |
| 19 | }; |
| 20 | |
| 21 | const skillsTool = tool({ |
| 22 | name: 'Skill', |
| 23 | description: `Load a specialized skill to enhance the assistant's capabilities. |
| 24 | Available skills: ${listAvailableSkills().join(', ') || 'none configured'} |
| 25 | Each skill provides domain-specific instructions and capabilities.`, |
| 26 | |
| 27 | inputSchema: z.object({ |
| 28 | type: z.string().describe("The skill type to load (e.g., 'pdf-processing')"), |
| 29 | }), |
| 30 | |
| 31 | outputSchema: z.string(), |
| 32 | |
| 33 | // This is where the magic happens - modify context for next turn |
| 34 | nextTurnParams: { |
| 35 | input: (params, context) => { |
| 36 | // Prevent duplicate skill loading |
| 37 | const skillMarker = `[Skill: ${params.type}]`; |
| 38 | if (JSON.stringify(context.input).includes(skillMarker)) { |
| 39 | return context.input; |
| 40 | } |
| 41 | |
| 42 | // Load the skill's instructions |
| 43 | const skillPath = path.join(SKILLS_DIR, params.type, 'SKILL.md'); |
| 44 | if (!existsSync(skillPath)) { |
| 45 | return context.input; |
| 46 | } |
| 47 | |
| 48 | const skill = readFileSync(skillPath, 'utf-8'); |
| 49 | const skillDir = path.join(SKILLS_DIR, params.type); |
| 50 | |
| 51 | // Inject skill context into the conversation |
| 52 | const currentInput = Array.isArray(context.input) ? context.input : [context.input]; |
| 53 | |
| 54 | return [ |
| 55 | ...currentInput, |
| 56 | { |
| 57 | role: 'user', |
| 58 | content: `${skillMarker} |
| 59 | Base directory for this skill: ${skillDir} |
| 60 | |
| 61 | ${skill}`, |
| 62 | }, |
| 63 | ]; |
| 64 | }, |
| 65 | }, |
| 66 | |
| 67 | execute: async (params, context) => { |
| 68 | const skillMarker = `[Skill: ${params.type}]`; |
| 69 | |
| 70 | // Check if already loaded |
| 71 | if (JSON.stringify(context?.turnRequest?.input || []).includes(skillMarker)) { |
| 72 | return `Skill ${params.type} is already loaded`; |
| 73 | } |
| 74 | |
| 75 | const skillPath = path.join(SKILLS_DIR, params.type, 'SKILL.md'); |
| 76 | if (!existsSync(skillPath)) { |
| 77 | const available = listAvailableSkills(); |
| 78 | return `Skill "${params.type}" not found. Available skills: ${available.join(', ') || 'none'}`; |
| 79 | } |
| 80 | |
| 81 | return `Launching skill ${params.type}`; |
| 82 | }, |
| 83 | }); |