|
| 1 | +import { |
| 2 | + ConfigValidationError, |
| 3 | + parseMarkdownRule, |
| 4 | +} from "@continuedev/config-yaml"; |
| 5 | +import z from "zod"; |
| 6 | +import { IDE, Skill } from "../.."; |
| 7 | +import { walkDir } from "../../indexing/walkDir"; |
| 8 | +import { localPathToUri } from "../../util/pathToUri"; |
| 9 | +import { getGlobalFolderWithName } from "../../util/paths"; |
| 10 | +import { findUriInDirs, joinPathsToUri } from "../../util/uri"; |
| 11 | +import { getAllDotContinueDefinitionFiles } from "../loadLocalAssistants"; |
| 12 | + |
| 13 | +const skillFrontmatterSchema = z.object({ |
| 14 | + name: z.string().min(1), |
| 15 | + description: z.string().min(1), |
| 16 | +}); |
| 17 | + |
| 18 | +const SKILLS_DIR = "skills"; |
| 19 | + |
| 20 | +/** |
| 21 | + * Get skills from .claude/skills directory |
| 22 | + */ |
| 23 | +async function getClaudeSkillsDir(ide: IDE) { |
| 24 | + const fullDirs = (await ide.getWorkspaceDirs()).map((dir) => |
| 25 | + joinPathsToUri(dir, ".claude", SKILLS_DIR), |
| 26 | + ); |
| 27 | + |
| 28 | + fullDirs.push(localPathToUri(getGlobalFolderWithName(SKILLS_DIR))); |
| 29 | + |
| 30 | + return ( |
| 31 | + await Promise.all( |
| 32 | + fullDirs.map(async (dir) => { |
| 33 | + const exists = await ide.fileExists(dir); |
| 34 | + if (!exists) return []; |
| 35 | + const uris = await walkDir(dir, ide, { |
| 36 | + source: "get .claude skills files", |
| 37 | + }); |
| 38 | + // filter markdown files only |
| 39 | + return uris.filter((uri) => uri.endsWith(".md")); |
| 40 | + }), |
| 41 | + ) |
| 42 | + ).flat(); |
| 43 | +} |
| 44 | + |
| 45 | +export async function loadMarkdownSkills(ide: IDE) { |
| 46 | + const errors: ConfigValidationError[] = []; |
| 47 | + const skills: Skill[] = []; |
| 48 | + |
| 49 | + try { |
| 50 | + const yamlAndMarkdownFileUris = [ |
| 51 | + ...( |
| 52 | + await getAllDotContinueDefinitionFiles( |
| 53 | + ide, |
| 54 | + { |
| 55 | + includeGlobal: true, |
| 56 | + includeWorkspace: true, |
| 57 | + fileExtType: "markdown", |
| 58 | + }, |
| 59 | + SKILLS_DIR, |
| 60 | + ) |
| 61 | + ).map((file) => file.path), |
| 62 | + ...(await getClaudeSkillsDir(ide)), |
| 63 | + ]; |
| 64 | + |
| 65 | + const skillFiles = yamlAndMarkdownFileUris.filter((path) => |
| 66 | + path.endsWith("SKILL.md"), |
| 67 | + ); |
| 68 | + |
| 69 | + const workspaceDirs = await ide.getWorkspaceDirs(); |
| 70 | + for (const fileUri of skillFiles) { |
| 71 | + try { |
| 72 | + const content = await ide.readFile(fileUri); |
| 73 | + const { frontmatter, markdown } = parseMarkdownRule( |
| 74 | + content, |
| 75 | + ) as unknown as { frontmatter: Skill; markdown: string }; |
| 76 | + |
| 77 | + const validatedFrontmatter = skillFrontmatterSchema.parse(frontmatter); |
| 78 | + |
| 79 | + const filesInSkillsDirectory = ( |
| 80 | + await walkDir(fileUri.substring(0, fileUri.lastIndexOf("/")), ide, { |
| 81 | + source: "get skill files", |
| 82 | + }) |
| 83 | + ) |
| 84 | + // do not include SKILL.md as it is already in content |
| 85 | + .filter((file) => !file.endsWith("SKILL.md")); |
| 86 | + |
| 87 | + const foundRelativeUri = findUriInDirs(fileUri, workspaceDirs); |
| 88 | + |
| 89 | + skills.push({ |
| 90 | + ...validatedFrontmatter, |
| 91 | + content: markdown, |
| 92 | + path: foundRelativeUri.foundInDir |
| 93 | + ? foundRelativeUri.relativePathOrBasename |
| 94 | + : fileUri, |
| 95 | + files: filesInSkillsDirectory, |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + errors.push({ |
| 99 | + fatal: false, |
| 100 | + message: `Failed to parse markdown skill file: ${error instanceof Error ? error.message : error}`, |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + } catch (err) { |
| 105 | + errors.push({ |
| 106 | + fatal: false, |
| 107 | + message: `Error loading markdown skill files: ${err instanceof Error ? err.message : err}`, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + return { skills, errors }; |
| 112 | +} |
0 commit comments