(code: string)
| 2 | |
| 3 | // 解析 ==SkillScript== 元数据头 |
| 4 | export function parseSkillScriptMetadata(code: string): SkillScriptMetadata | null { |
| 5 | const match = code.match(/\/\/\s*==SkillScript==([\s\S]*?)\/\/\s*==\/SkillScript==/); |
| 6 | if (!match) return null; |
| 7 | |
| 8 | const block = match[1]; |
| 9 | const lines = block.split("\n"); |
| 10 | |
| 11 | let name = ""; |
| 12 | let description = ""; |
| 13 | const params: SkillScriptParam[] = []; |
| 14 | const grants: string[] = []; |
| 15 | const requires: string[] = []; |
| 16 | let timeout: number | undefined; |
| 17 | |
| 18 | for (const line of lines) { |
| 19 | const trimmed = line.replace(/^\/\/\s*/, "").trim(); |
| 20 | if (!trimmed.startsWith("@")) continue; |
| 21 | |
| 22 | const atMatch = trimmed.match(/^@(\w+)\s*(.*)/); |
| 23 | if (!atMatch) continue; |
| 24 | |
| 25 | const [, key, value] = atMatch; |
| 26 | const val = value.trim(); |
| 27 | |
| 28 | switch (key) { |
| 29 | case "name": |
| 30 | name = val; |
| 31 | break; |
| 32 | case "description": |
| 33 | description = val; |
| 34 | break; |
| 35 | case "param": |
| 36 | { |
| 37 | const param = parseParam(val); |
| 38 | if (param) params.push(param); |
| 39 | } |
| 40 | break; |
| 41 | case "grant": |
| 42 | if (val) grants.push(val); |
| 43 | break; |
| 44 | case "require": |
| 45 | if (val) requires.push(val); |
| 46 | break; |
| 47 | case "timeout": { |
| 48 | const n = Number(val); |
| 49 | if (Number.isFinite(n) && n > 0) timeout = n; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (!name) return null; |
| 56 | |
| 57 | return { name, description, params, grants, requires, ...(timeout !== undefined ? { timeout } : {}) }; |
| 58 | } |
| 59 | |
| 60 | // 解析 @param 行: name type [required] description |
| 61 | // type 支持 string|number|boolean,支持 enum [val1,val2] |
no test coverage detected