|
1 | 1 | import * as fs from "fs/promises" |
2 | 2 | import * as path from "path" |
| 3 | +import * as os from "os" |
3 | 4 | import * as vscode from "vscode" |
4 | 5 | import matter from "gray-matter" |
5 | 6 |
|
@@ -239,6 +240,142 @@ export class SkillsManager { |
239 | 240 | } |
240 | 241 | } |
241 | 242 |
|
| 243 | + /** |
| 244 | + * Get all skills metadata (for UI display) |
| 245 | + * Returns skills from all sources without content |
| 246 | + */ |
| 247 | + getSkillsMetadata(): SkillMetadata[] { |
| 248 | + return Array.from(this.skills.values()) |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Get a skill by name, source, and optionally mode |
| 253 | + */ |
| 254 | + getSkill(name: string, source: "global" | "project", mode?: string): SkillMetadata | undefined { |
| 255 | + const skillKey = this.getSkillKey(name, source, mode) |
| 256 | + return this.skills.get(skillKey) |
| 257 | + } |
| 258 | + |
| 259 | + /** |
| 260 | + * Validate skill name per agentskills.io spec |
| 261 | + * - 1-64 chars |
| 262 | + * - lowercase letters/numbers/hyphens only |
| 263 | + * - must not start/end with hyphen |
| 264 | + * - must not contain consecutive hyphens |
| 265 | + */ |
| 266 | + private validateSkillName(name: string): { valid: boolean; error?: string } { |
| 267 | + if (name.length < 1 || name.length > 64) { |
| 268 | + return { valid: false, error: `Skill name must be 1-64 characters (got ${name.length})` } |
| 269 | + } |
| 270 | + |
| 271 | + const nameFormat = /^[a-z0-9]+(?:-[a-z0-9]+)*$/ |
| 272 | + if (!nameFormat.test(name)) { |
| 273 | + return { |
| 274 | + valid: false, |
| 275 | + error: "Skill name must be lowercase letters/numbers/hyphens only (no leading/trailing hyphen, no consecutive hyphens)", |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + return { valid: true } |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Create a new skill |
| 284 | + * @param name - Skill name (must be valid per agentskills.io spec) |
| 285 | + * @param source - "global" or "project" |
| 286 | + * @param description - Skill description |
| 287 | + * @param mode - Optional mode restriction (creates in skills-{mode}/ directory) |
| 288 | + * @returns Path to created SKILL.md file |
| 289 | + */ |
| 290 | + async createSkill(name: string, source: "global" | "project", description: string, mode?: string): Promise<string> { |
| 291 | + // Validate skill name |
| 292 | + const validation = this.validateSkillName(name) |
| 293 | + if (!validation.valid) { |
| 294 | + throw new Error(validation.error) |
| 295 | + } |
| 296 | + |
| 297 | + // Validate description |
| 298 | + const trimmedDescription = description.trim() |
| 299 | + if (trimmedDescription.length < 1 || trimmedDescription.length > 1024) { |
| 300 | + throw new Error(`Skill description must be 1-1024 characters (got ${trimmedDescription.length})`) |
| 301 | + } |
| 302 | + |
| 303 | + // Determine base directory |
| 304 | + let baseDir: string |
| 305 | + if (source === "global") { |
| 306 | + baseDir = getGlobalRooDirectory() |
| 307 | + } else { |
| 308 | + const provider = this.providerRef.deref() |
| 309 | + if (!provider?.cwd) { |
| 310 | + throw new Error("Cannot create project skill: no workspace folder is open") |
| 311 | + } |
| 312 | + baseDir = path.join(provider.cwd, ".roo") |
| 313 | + } |
| 314 | + |
| 315 | + // Determine skills directory (with optional mode suffix) |
| 316 | + const skillsDirName = mode ? `skills-${mode}` : "skills" |
| 317 | + const skillsDir = path.join(baseDir, skillsDirName) |
| 318 | + const skillDir = path.join(skillsDir, name) |
| 319 | + const skillMdPath = path.join(skillDir, "SKILL.md") |
| 320 | + |
| 321 | + // Check if skill already exists |
| 322 | + if (await fileExists(skillMdPath)) { |
| 323 | + throw new Error(`Skill "${name}" already exists at ${skillMdPath}`) |
| 324 | + } |
| 325 | + |
| 326 | + // Create the skill directory |
| 327 | + await fs.mkdir(skillDir, { recursive: true }) |
| 328 | + |
| 329 | + // Generate SKILL.md content with frontmatter |
| 330 | + const titleName = name |
| 331 | + .split("-") |
| 332 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 333 | + .join(" ") |
| 334 | + |
| 335 | + const skillContent = `--- |
| 336 | +name: ${name} |
| 337 | +description: ${trimmedDescription} |
| 338 | +--- |
| 339 | +
|
| 340 | +# ${titleName} |
| 341 | +
|
| 342 | +## Instructions |
| 343 | +
|
| 344 | +Add your skill instructions here. |
| 345 | +` |
| 346 | + |
| 347 | + // Write the SKILL.md file |
| 348 | + await fs.writeFile(skillMdPath, skillContent, "utf-8") |
| 349 | + |
| 350 | + // Refresh skills list |
| 351 | + await this.discoverSkills() |
| 352 | + |
| 353 | + return skillMdPath |
| 354 | + } |
| 355 | + |
| 356 | + /** |
| 357 | + * Delete a skill |
| 358 | + * @param name - Skill name to delete |
| 359 | + * @param source - Where the skill is located |
| 360 | + * @param mode - Optional mode (to locate in skills-{mode}/ directory) |
| 361 | + */ |
| 362 | + async deleteSkill(name: string, source: "global" | "project", mode?: string): Promise<void> { |
| 363 | + // Find the skill |
| 364 | + const skill = this.getSkill(name, source, mode) |
| 365 | + if (!skill) { |
| 366 | + throw new Error(`Skill "${name}" not found in ${source} ${mode ? `(mode: ${mode})` : ""}`) |
| 367 | + } |
| 368 | + |
| 369 | + // Get the skill directory (parent of SKILL.md) |
| 370 | + const skillDir = path.dirname(skill.path) |
| 371 | + |
| 372 | + // Delete the entire skill directory |
| 373 | + await fs.rm(skillDir, { recursive: true, force: true }) |
| 374 | + |
| 375 | + // Refresh skills list |
| 376 | + await this.discoverSkills() |
| 377 | + } |
| 378 | + |
242 | 379 | /** |
243 | 380 | * Get all skills directories to scan, including mode-specific directories. |
244 | 381 | */ |
|
0 commit comments