Files
pi-auto-reinject/src/expand.ts
T
grayhook 9d32cdffb1 Phase 4: add expandSkill — skill meta to injectable user text.
Compose readBody, formatBlock, and appendSuffix for reinject and /skill-reinject now (SPEC §5.3).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-17 10:31:45 +07:00

37 lines
1.4 KiB
TypeScript

import { readFileSync } from "node:fs";
import { stripFrontmatter } from "@earendil-works/pi-coding-agent";
/** Skill fields needed to build the injected block (SPEC §5.3). */
export type SkillBlockMeta = {
name: string;
filePath: string;
baseDir: string;
};
/** mirror agent-session `_expandSkillCommand` — SKILL.md body without YAML frontmatter (SPEC §5.3, §10). */
export function readSkillBody(filePath: string): string {
const content = readFileSync(filePath, "utf-8");
return stripFrontmatter(content).trim();
}
/** mirror agent-session `_expandSkillCommand` — XML skill block with baseDir hint (SPEC §5.3). */
export function formatBlock(meta: SkillBlockMeta, body: string): string {
return `<skill name="${meta.name}" location="${meta.filePath}">\nReferences are relative to ${meta.baseDir}.\n\n${body}\n</skill>`;
}
/** Append optional reinject suffix after skill block (SPEC §5.3). */
export function appendSuffix(block: string, suffix: string | undefined): string {
const trimmed = suffix?.trim();
if (!trimmed) {
return block;
}
return `${block}\n\n${trimmed}`;
}
/** Read SKILL.md and format full user message text for re-inject (SPEC §5.3). */
export function expandSkill(meta: SkillBlockMeta, suffix?: string): string {
const body = readSkillBody(meta.filePath);
const block = formatBlock(meta, body);
return appendSuffix(block, suffix);
}