@howaboua/pi-better-skills-tool 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +52 -0
- package/biome.json +31 -0
- package/changelog.ts +337 -0
- package/index.ts +101 -0
- package/package.json +76 -0
- package/src/catalog.ts +125 -0
- package/src/discovery.ts +246 -0
- package/src/skill-document.ts +133 -0
- package/src/skill-package.ts +253 -0
- package/src/tool.ts +56 -0
- package/tsconfig.json +7 -0
package/src/catalog.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import {
|
|
3
|
+
type CatalogSkill,
|
|
4
|
+
defaultSessionSkillsDir,
|
|
5
|
+
defaultSkillsDir,
|
|
6
|
+
discoverVisibleSkills,
|
|
7
|
+
type LoadedSkill,
|
|
8
|
+
} from "./discovery.js";
|
|
9
|
+
import { readSkillPackage } from "./skill-package.js";
|
|
10
|
+
|
|
11
|
+
export type { LoadedSkill } from "./discovery.js";
|
|
12
|
+
export {
|
|
13
|
+
defaultSessionSkillsDir,
|
|
14
|
+
defaultSkillsDir,
|
|
15
|
+
discoverSkills,
|
|
16
|
+
} from "./discovery.js";
|
|
17
|
+
|
|
18
|
+
const MAX_OUTPUT_BYTES = 48 * 1024;
|
|
19
|
+
|
|
20
|
+
type SkillRequest =
|
|
21
|
+
| { action: "list"; categories: string[] }
|
|
22
|
+
| { action: "read"; name: string; references: string[] };
|
|
23
|
+
|
|
24
|
+
export function parseRequest(input: unknown): SkillRequest {
|
|
25
|
+
if (typeof input !== "string")
|
|
26
|
+
throw new Error("skills expects a string command");
|
|
27
|
+
const parts = input.trim().split(/\s+/).filter(Boolean);
|
|
28
|
+
const [action, ...arguments_] = parts;
|
|
29
|
+
if (!action || action === "list") {
|
|
30
|
+
return { action: "list", categories: [...new Set(arguments_)] };
|
|
31
|
+
}
|
|
32
|
+
if (action === "read" && arguments_.length >= 1) {
|
|
33
|
+
return {
|
|
34
|
+
action,
|
|
35
|
+
name: arguments_[0] ?? "",
|
|
36
|
+
references: [...new Set(arguments_.slice(1))],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (action === "read") {
|
|
40
|
+
throw new Error(
|
|
41
|
+
'read expects one skill name and optional reference names: "read <exact-skill-name> [reference...]"',
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
throw new Error(
|
|
45
|
+
'Expected "list", "list <category>...", or "read <exact-skill-name> [reference...]"',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function formatSkillList(
|
|
50
|
+
skills: CatalogSkill[],
|
|
51
|
+
requestedCategories: string[] = [],
|
|
52
|
+
): string {
|
|
53
|
+
const availableCategories = [
|
|
54
|
+
...new Set(skills.flatMap(({ category }) => (category ? [category] : []))),
|
|
55
|
+
].sort();
|
|
56
|
+
const unknown = requestedCategories.filter(
|
|
57
|
+
(category) => !availableCategories.includes(category),
|
|
58
|
+
);
|
|
59
|
+
if (unknown.length) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
"Unknown categor" +
|
|
62
|
+
(unknown.length === 1 ? "y" : "ies") +
|
|
63
|
+
": " +
|
|
64
|
+
unknown.join(", ") +
|
|
65
|
+
". Available: " +
|
|
66
|
+
(availableCategories.join(", ") || "none"),
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
const selected = requestedCategories.length
|
|
70
|
+
? skills.filter(({ category }) =>
|
|
71
|
+
requestedCategories.includes(category ?? ""),
|
|
72
|
+
)
|
|
73
|
+
: skills;
|
|
74
|
+
if (!selected.length) return "No skills available.";
|
|
75
|
+
|
|
76
|
+
const topLevel = selected.filter(({ category }) => !category);
|
|
77
|
+
const groups = new Map<string, CatalogSkill[]>();
|
|
78
|
+
for (const skill of selected) {
|
|
79
|
+
if (!skill.category) continue;
|
|
80
|
+
const group = groups.get(skill.category) ?? [];
|
|
81
|
+
group.push(skill);
|
|
82
|
+
groups.set(skill.category, group);
|
|
83
|
+
}
|
|
84
|
+
const lines = topLevel.map(
|
|
85
|
+
(skill) =>
|
|
86
|
+
`- ${skill.name}: ${skill.description.replace(/\s+/g, " ").trim()}`,
|
|
87
|
+
);
|
|
88
|
+
if (topLevel.length && groups.size) lines.push("");
|
|
89
|
+
for (const [category, categorySkills] of groups) {
|
|
90
|
+
lines.push(`# ${category.replace(/-/g, " ").toUpperCase()}`);
|
|
91
|
+
for (const skill of categorySkills) {
|
|
92
|
+
lines.push(
|
|
93
|
+
`- ${skill.name}: ${skill.description.replace(/\s+/g, " ").trim()}`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return lines.join("\n");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function enforceOutputLimit(output: string): string {
|
|
101
|
+
const bytes = Buffer.byteLength(output);
|
|
102
|
+
if (bytes > MAX_OUTPUT_BYTES) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`skills output is ${bytes} bytes; maximum is ${MAX_OUTPUT_BYTES} bytes`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
return output;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function runSkills(
|
|
111
|
+
input: unknown,
|
|
112
|
+
globalRoot = defaultSkillsDir(),
|
|
113
|
+
sessionRoot: string | undefined = globalRoot === defaultSkillsDir()
|
|
114
|
+
? defaultSessionSkillsDir()
|
|
115
|
+
: undefined,
|
|
116
|
+
loadedSkills: readonly LoadedSkill[] = [],
|
|
117
|
+
): string {
|
|
118
|
+
const request = parseRequest(input);
|
|
119
|
+
const skills = discoverVisibleSkills(globalRoot, sessionRoot, loadedSkills);
|
|
120
|
+
return enforceOutputLimit(
|
|
121
|
+
request.action === "list"
|
|
122
|
+
? formatSkillList(skills, request.categories)
|
|
123
|
+
: readSkillPackage(skills, request.name, request.references),
|
|
124
|
+
);
|
|
125
|
+
}
|
package/src/discovery.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Dirent,
|
|
3
|
+
existsSync,
|
|
4
|
+
readdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
statSync,
|
|
7
|
+
} from "node:fs";
|
|
8
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
9
|
+
import {
|
|
10
|
+
getAgentDir,
|
|
11
|
+
loadSkillsFromDir,
|
|
12
|
+
} from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { parseSkillDocument, validateSkill } from "./skill-document.js";
|
|
14
|
+
|
|
15
|
+
const SKILL_FILENAME = "SKILL.md";
|
|
16
|
+
type SkillCategory = string | undefined;
|
|
17
|
+
|
|
18
|
+
export interface LoadedSkill {
|
|
19
|
+
name: string;
|
|
20
|
+
description: string;
|
|
21
|
+
filePath: string;
|
|
22
|
+
baseDir: string;
|
|
23
|
+
disableModelInvocation?: boolean;
|
|
24
|
+
sourceInfo?: { scope?: string };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CatalogSkill {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
packageName: string;
|
|
31
|
+
category: SkillCategory;
|
|
32
|
+
directory: string;
|
|
33
|
+
path: string;
|
|
34
|
+
body: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface DirectoryCatalog {
|
|
38
|
+
skills: CatalogSkill[];
|
|
39
|
+
hiddenNames: Set<string>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function defaultSkillsDir(): string {
|
|
43
|
+
return join(getAgentDir(), "skills");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function defaultSessionSkillsDir(cwd = process.cwd()): string {
|
|
47
|
+
return join(cwd, ".pi", "skills");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function skillFileIn(directory: string): string | undefined {
|
|
51
|
+
const path = join(directory, SKILL_FILENAME);
|
|
52
|
+
return existsSync(path) ? path : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function directoryEntries(directory: string): Dirent[] {
|
|
56
|
+
return readdirSync(directory, { withFileTypes: true })
|
|
57
|
+
.sort((left, right) => left.name.localeCompare(right.name))
|
|
58
|
+
.filter((entry) => !entry.name.startsWith("."));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function entryKind(
|
|
62
|
+
entry: Dirent,
|
|
63
|
+
path: string,
|
|
64
|
+
): "directory" | "file" | undefined {
|
|
65
|
+
if (entry.isDirectory()) return "directory";
|
|
66
|
+
if (entry.isFile()) return "file";
|
|
67
|
+
if (!entry.isSymbolicLink()) return undefined;
|
|
68
|
+
try {
|
|
69
|
+
const target = statSync(path);
|
|
70
|
+
if (target.isDirectory()) return "directory";
|
|
71
|
+
if (target.isFile()) return "file";
|
|
72
|
+
} catch {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function isWithin(root: string, path: string): boolean {
|
|
79
|
+
const child = relative(root, path);
|
|
80
|
+
return (
|
|
81
|
+
child === "" ||
|
|
82
|
+
(!child.startsWith(`..${sep}`) && child !== ".." && !isAbsolute(child))
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function loadSkill(
|
|
87
|
+
directory: string,
|
|
88
|
+
packageName: string,
|
|
89
|
+
category: SkillCategory,
|
|
90
|
+
hiddenPaths: ReadonlySet<string>,
|
|
91
|
+
): CatalogSkill | null | undefined {
|
|
92
|
+
const path = skillFileIn(directory);
|
|
93
|
+
if (!path) return undefined;
|
|
94
|
+
if (hiddenPaths.has(resolve(path))) return null;
|
|
95
|
+
const document = parseSkillDocument(readFileSync(path, "utf8"), packageName);
|
|
96
|
+
const skill: CatalogSkill = {
|
|
97
|
+
name: document.frontmatter.name || packageName.split("/").at(-1) || "",
|
|
98
|
+
description: document.frontmatter.description ?? "",
|
|
99
|
+
packageName,
|
|
100
|
+
category,
|
|
101
|
+
directory: resolve(directory),
|
|
102
|
+
path: resolve(path),
|
|
103
|
+
body: document.body,
|
|
104
|
+
};
|
|
105
|
+
validateSkill(skill);
|
|
106
|
+
return skill;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function categoryRank(category: SkillCategory): number {
|
|
110
|
+
if (!category) return 0;
|
|
111
|
+
if (category === "session") return 1;
|
|
112
|
+
return 2;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function sortSkills(skills: CatalogSkill[]): CatalogSkill[] {
|
|
116
|
+
return skills.sort(
|
|
117
|
+
(left, right) =>
|
|
118
|
+
categoryRank(left.category) - categoryRank(right.category) ||
|
|
119
|
+
(left.category ?? "").localeCompare(right.category ?? "") ||
|
|
120
|
+
left.name.localeCompare(right.name),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function discoverDirectoryCatalog(root: string): DirectoryCatalog {
|
|
125
|
+
if (!existsSync(root)) return { skills: [], hiddenNames: new Set() };
|
|
126
|
+
const loaded = loadSkillsFromDir({ dir: root, source: "skills-tool" }).skills;
|
|
127
|
+
const hidden = loaded.filter((skill) => skill.disableModelInvocation);
|
|
128
|
+
const hiddenPaths = new Set(hidden.map((skill) => resolve(skill.filePath)));
|
|
129
|
+
const hiddenNames = new Set(hidden.map((skill) => skill.name));
|
|
130
|
+
const skills: CatalogSkill[] = [];
|
|
131
|
+
for (const entry of directoryEntries(root)) {
|
|
132
|
+
const directory = join(root, entry.name);
|
|
133
|
+
if (entryKind(entry, directory) !== "directory") continue;
|
|
134
|
+
|
|
135
|
+
const directSkill = loadSkill(
|
|
136
|
+
directory,
|
|
137
|
+
entry.name,
|
|
138
|
+
undefined,
|
|
139
|
+
hiddenPaths,
|
|
140
|
+
);
|
|
141
|
+
if (directSkill !== undefined) {
|
|
142
|
+
if (directSkill) skills.push(directSkill);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(entry.name)) {
|
|
146
|
+
throw new Error(
|
|
147
|
+
`${entry.name}: category names must contain only lowercase letters, numbers, and single hyphens`,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
for (const child of directoryEntries(directory)) {
|
|
151
|
+
const childDirectory = join(directory, child.name);
|
|
152
|
+
if (entryKind(child, childDirectory) !== "directory") continue;
|
|
153
|
+
const skill = loadSkill(
|
|
154
|
+
childDirectory,
|
|
155
|
+
`${entry.name}/${child.name}`,
|
|
156
|
+
entry.name,
|
|
157
|
+
hiddenPaths,
|
|
158
|
+
);
|
|
159
|
+
if (skill) skills.push(skill);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const names = new Map<string, string>();
|
|
164
|
+
for (const skill of skills) {
|
|
165
|
+
const existing = names.get(skill.name);
|
|
166
|
+
if (existing) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Duplicate skill name "${skill.name}" in packages ${existing} and ${skill.packageName}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
names.set(skill.name, skill.packageName);
|
|
172
|
+
}
|
|
173
|
+
return { skills: sortSkills(skills), hiddenNames };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function discoverSkills(root = defaultSkillsDir()): CatalogSkill[] {
|
|
177
|
+
return discoverDirectoryCatalog(root).skills;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function loadedSkillCategory(
|
|
181
|
+
skill: LoadedSkill,
|
|
182
|
+
globalRoot: string,
|
|
183
|
+
sessionRoot: string | undefined,
|
|
184
|
+
): SkillCategory {
|
|
185
|
+
const directory = resolve(skill.baseDir);
|
|
186
|
+
if (sessionRoot && isWithin(resolve(sessionRoot), directory))
|
|
187
|
+
return "session";
|
|
188
|
+
if (skill.sourceInfo?.scope === "project") return "session";
|
|
189
|
+
if (!isWithin(resolve(globalRoot), directory)) return undefined;
|
|
190
|
+
const [category, nested] = relative(resolve(globalRoot), directory).split(
|
|
191
|
+
sep,
|
|
192
|
+
);
|
|
193
|
+
return category && nested ? category : undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function catalogLoadedSkill(
|
|
197
|
+
skill: LoadedSkill,
|
|
198
|
+
globalRoot: string,
|
|
199
|
+
sessionRoot: string | undefined,
|
|
200
|
+
): CatalogSkill {
|
|
201
|
+
const document = parseSkillDocument(
|
|
202
|
+
readFileSync(skill.filePath, "utf8"),
|
|
203
|
+
skill.name,
|
|
204
|
+
);
|
|
205
|
+
const catalogSkill: CatalogSkill = {
|
|
206
|
+
name: skill.name,
|
|
207
|
+
description: skill.description,
|
|
208
|
+
packageName: skill.name,
|
|
209
|
+
category: loadedSkillCategory(skill, globalRoot, sessionRoot),
|
|
210
|
+
directory: resolve(skill.baseDir),
|
|
211
|
+
path: resolve(skill.filePath),
|
|
212
|
+
body: document.body,
|
|
213
|
+
};
|
|
214
|
+
validateSkill(catalogSkill);
|
|
215
|
+
return catalogSkill;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function discoverVisibleSkills(
|
|
219
|
+
globalRoot = defaultSkillsDir(),
|
|
220
|
+
sessionRoot: string | undefined = globalRoot === defaultSkillsDir()
|
|
221
|
+
? defaultSessionSkillsDir()
|
|
222
|
+
: undefined,
|
|
223
|
+
loadedSkills: readonly LoadedSkill[] = [],
|
|
224
|
+
): CatalogSkill[] {
|
|
225
|
+
if (loadedSkills.length > 0) {
|
|
226
|
+
return sortSkills(
|
|
227
|
+
loadedSkills
|
|
228
|
+
.filter((skill) => !skill.disableModelInvocation)
|
|
229
|
+
.map((skill) => catalogLoadedSkill(skill, globalRoot, sessionRoot)),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const globalCatalog = discoverDirectoryCatalog(globalRoot);
|
|
234
|
+
const byName = new Map(
|
|
235
|
+
globalCatalog.skills.map((skill) => [skill.name, skill]),
|
|
236
|
+
);
|
|
237
|
+
if (sessionRoot) {
|
|
238
|
+
const sessionCatalog = discoverDirectoryCatalog(sessionRoot);
|
|
239
|
+
for (const hiddenName of sessionCatalog.hiddenNames)
|
|
240
|
+
byName.delete(hiddenName);
|
|
241
|
+
for (const skill of sessionCatalog.skills) {
|
|
242
|
+
byName.set(skill.name, { ...skill, category: "session" });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return sortSkills([...byName.values()]);
|
|
246
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
const MAX_NAME_LENGTH = 64;
|
|
2
|
+
const MAX_DESCRIPTION_LENGTH = 1024;
|
|
3
|
+
|
|
4
|
+
export interface SkillDocument {
|
|
5
|
+
frontmatter: Partial<Record<"name" | "description", string>>;
|
|
6
|
+
body: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface SkillFields {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
packageName: string;
|
|
13
|
+
body: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function decodeQuotedScalar(
|
|
17
|
+
value: string,
|
|
18
|
+
path: string,
|
|
19
|
+
field: string,
|
|
20
|
+
): string {
|
|
21
|
+
if (value.startsWith('"')) {
|
|
22
|
+
try {
|
|
23
|
+
const parsed: unknown = JSON.parse(value);
|
|
24
|
+
if (typeof parsed !== "string") throw new Error();
|
|
25
|
+
return parsed;
|
|
26
|
+
} catch {
|
|
27
|
+
throw new Error(`${path}: ${field} must be a valid quoted YAML string`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (value.startsWith("'")) {
|
|
31
|
+
if (!value.endsWith("'") || value.length < 2) {
|
|
32
|
+
throw new Error(`${path}: ${field} must be a valid quoted YAML string`);
|
|
33
|
+
}
|
|
34
|
+
return value.slice(1, -1).replace(/''/g, "'");
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseBlockScalar(
|
|
40
|
+
lines: string[],
|
|
41
|
+
start: number,
|
|
42
|
+
parentIndent: number,
|
|
43
|
+
style: string,
|
|
44
|
+
): { value: string; nextIndex: number } {
|
|
45
|
+
const values: string[] = [];
|
|
46
|
+
let commonIndent: number | undefined;
|
|
47
|
+
let index = start;
|
|
48
|
+
for (; index < lines.length; index += 1) {
|
|
49
|
+
const line = lines[index] ?? "";
|
|
50
|
+
if (!line.trim()) {
|
|
51
|
+
values.push("");
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const indent = line.match(/^\s*/)?.[0].length ?? 0;
|
|
55
|
+
if (indent <= parentIndent) break;
|
|
56
|
+
commonIndent =
|
|
57
|
+
commonIndent === undefined ? indent : Math.min(commonIndent, indent);
|
|
58
|
+
values.push(line);
|
|
59
|
+
}
|
|
60
|
+
const indent = commonIndent ?? parentIndent + 1;
|
|
61
|
+
const normalized = values.map((line) =>
|
|
62
|
+
line.slice(Math.min(indent, line.length)),
|
|
63
|
+
);
|
|
64
|
+
const value =
|
|
65
|
+
style === ">"
|
|
66
|
+
? normalized.join("\n").replace(/([^\n])\n(?=[^\n])/g, "$1 ")
|
|
67
|
+
: normalized.join("\n");
|
|
68
|
+
return { value: value.trim(), nextIndex: index };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function parseSkillDocument(
|
|
72
|
+
content: string,
|
|
73
|
+
label: string,
|
|
74
|
+
): SkillDocument {
|
|
75
|
+
const normalized = content.replace(/^\uFEFF/, "").replace(/\r\n?/g, "\n");
|
|
76
|
+
const lines = normalized.split("\n");
|
|
77
|
+
if (lines[0] !== "---") throw new Error(`${label}: missing YAML frontmatter`);
|
|
78
|
+
const end = lines.findIndex(
|
|
79
|
+
(line, index) => index > 0 && (line === "---" || line === "..."),
|
|
80
|
+
);
|
|
81
|
+
if (end < 0) throw new Error(`${label}: unterminated YAML frontmatter`);
|
|
82
|
+
|
|
83
|
+
const fields: SkillDocument["frontmatter"] = {};
|
|
84
|
+
for (let index = 1; index < end; index += 1) {
|
|
85
|
+
const line = lines[index] ?? "";
|
|
86
|
+
const match = /^(\s*)(name|description):\s*(.*)$/.exec(line);
|
|
87
|
+
if (!match) continue;
|
|
88
|
+
const whitespace = match[1] ?? "";
|
|
89
|
+
const field = match[2] as "name" | "description";
|
|
90
|
+
const rawValue = match[3] ?? "";
|
|
91
|
+
const block = /^([>|])[-+]?\s*$/.exec(rawValue);
|
|
92
|
+
if (block) {
|
|
93
|
+
const parsed = parseBlockScalar(
|
|
94
|
+
lines.slice(0, end),
|
|
95
|
+
index + 1,
|
|
96
|
+
whitespace.length,
|
|
97
|
+
block[1] ?? "|",
|
|
98
|
+
);
|
|
99
|
+
fields[field] = parsed.value;
|
|
100
|
+
index = parsed.nextIndex - 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
fields[field] = decodeQuotedScalar(rawValue.trim(), label, field);
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
frontmatter: fields,
|
|
107
|
+
body: lines
|
|
108
|
+
.slice(end + 1)
|
|
109
|
+
.join("\n")
|
|
110
|
+
.trim(),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function validateSkill(skill: SkillFields): void {
|
|
115
|
+
const errors: string[] = [];
|
|
116
|
+
if (!skill.name) errors.push("name is required");
|
|
117
|
+
else {
|
|
118
|
+
if (skill.name.length > MAX_NAME_LENGTH)
|
|
119
|
+
errors.push(`name exceeds ${MAX_NAME_LENGTH} characters`);
|
|
120
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(skill.name)) {
|
|
121
|
+
errors.push(
|
|
122
|
+
"name must contain only lowercase letters, numbers, and single hyphens",
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (!skill.description?.trim()) errors.push("description is required");
|
|
127
|
+
else if (skill.description.length > MAX_DESCRIPTION_LENGTH) {
|
|
128
|
+
errors.push(`description exceeds ${MAX_DESCRIPTION_LENGTH} characters`);
|
|
129
|
+
}
|
|
130
|
+
if (!skill.body) errors.push("Markdown body is required");
|
|
131
|
+
if (errors.length)
|
|
132
|
+
throw new Error(`${skill.packageName}: ${errors.join("; ")}`);
|
|
133
|
+
}
|