* 解析 SKILL.md 文件的 frontmatter
(filePath: string)
| 34 | * 解析 SKILL.md 文件的 frontmatter |
| 35 | */ |
| 36 | function parseSkillFile(filePath: string): Skill | null { |
| 37 | try { |
| 38 | const content = fs.readFileSync(filePath, 'utf8'); |
| 39 | |
| 40 | // 解析 frontmatter |
| 41 | const frontmatterMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/); |
| 42 | if (!frontmatterMatch) { |
| 43 | console.warn(`[Skill] 无法解析 frontmatter: ${filePath}`); |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | const [, frontmatter, body] = frontmatterMatch; |
| 48 | |
| 49 | // 解析 name 和 description |
| 50 | const nameMatch = frontmatter.match(/^name:\s*(.+)$/m); |
| 51 | const descMatch = frontmatter.match(/^description:\s*(.+)$/m); |
| 52 | |
| 53 | if (!nameMatch || !descMatch) { |
| 54 | console.warn(`[Skill] 缺少 name 或 description: ${filePath}`); |
| 55 | return null; |
| 56 | } |
| 57 | |
| 58 | const name = nameMatch[1].trim(); |
| 59 | const description = descMatch[1].trim(); |
| 60 | |
| 61 | // 验证 name 格式 |
| 62 | if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name)) { |
| 63 | console.warn(`[Skill] name 格式无效: ${name}`); |
| 64 | } |
| 65 | |
| 66 | const baseDir = path.dirname(filePath); |
| 67 | |
| 68 | // 列出资源文件 |
| 69 | const resources = listResources(baseDir); |
| 70 | |
| 71 | return { |
| 72 | name, |
| 73 | description, |
| 74 | location: filePath, |
| 75 | content: body.trim(), |
| 76 | baseDir, |
| 77 | resources, |
| 78 | }; |
| 79 | } catch (error) { |
| 80 | console.error(`[Skill] 加载失败: ${filePath}`, error); |
| 81 | return null; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * 列出目录下的资源文件 |
no test coverage detected