/** Reads the name and description from a SKILL.md file's frontmatter, and the instructions after it. */
function parseSkill(text: string) {
const frontmatter = /^---\n([\s\S]*?)\n---\n/.exec(text);
const field = (key: string) => new RegExp("^" + key + ":\\s*(.+)", "m").exec(frontmatter?.[1] ?? "")?.[1]?.trim() ?? "";
return {name: field("name"), description: field("description"), body: text.slice(frontmatter?.[0].length ?? 0).trim()};
}
/** Every folder in `root` with a SKILL.md file is a skill. */
export function folderSkills(root: string): SkillSource {
return {
async list() {
const entries = await readdir(root, {withFileTypes: true});
const folders = entries.filter(
(entry) => entry.isDirectory() && existsSync(join(root, entry.name, "SKILL.md")),
);
return Promise.all(
folders.map(async (folder) => {
const location = join(root, folder.name);
const {name, description} = parseSkill(await readFile(join(location, "SKILL.md"), "utf8"));
return {name, description, location};
}),
);
},
async load({location}) {
return parseSkill(await readFile(join(location!, "SKILL.md"), "utf8")).body;
},
async read({location}, path) {
return readFile(join(location!, path), "utf8");
},
};
}
const writer = agent({
model,
instructions: "You write for a software team.",
skills: folderSkills("skills"),
});