Files
oh-my-pi/packages/coding-agent/scripts/format-prompts.ts
T
can1357 a21a542afd refactor(prompt-templates): migrated prompt utilities to pi-utils package
- Extracted prompt rendering and formatting utilities from coding-agent to centralized pi-utils package with new API surface (prompt.render, prompt.format, prompt.registerHelper).
- Migrated parseFrontmatter utility from coding-agent to pi-utils package; updated 8 files to import from @oh-my-pi/pi-utils.
- Removed 170-line prompt-format.ts module and consolidated 192 lines of Handlebars helper registrations into pi-utils prompt module.
- Updated 60+ files across coding-agent and typescript-edit-benchmark to use new prompt.render() and prompt.format() API from pi-utils.
- Simplified prompt-templates.ts by delegating core functionality to pi-utils while retaining custom helper registrations (jtdToTypeScript, jsonStringify, etc.).
2026-04-08 05:47:35 +02:00

69 lines
2.1 KiB
TypeScript

#!/usr/bin/env bun
import { prompt } from "@oh-my-pi/pi-utils";
/**
* Format prompt files (mixed XML + Markdown + Handlebars).
*
* Rules:
* 1. No blank line before list items
* 2. No blank line after opening XML tag or Handlebars block
* 3. No blank line before closing XML tag or Handlebars block
* 4. Strip leading whitespace from top-level closing XML tags (opened at col 0) and Handlebars (lines starting with {{)
* 5. Compact markdown tables (remove padding)
* 6. Collapse 2+ blank lines to single blank line
* 7. Trim trailing whitespace (preserve indentation)
* 8. Trailing newline at EOF (disk only; runtime omits it)
* 9. Bold RFC 2119 keywords (MUST, SHOULD, MAY, etc.) in prompt content
*/
import { Glob } from "bun";
const PROMPTS_DIR = new URL("../src/prompts/", import.meta.url).pathname;
const COMMIT_PROMPTS_DIR = new URL("../src/commit/prompts/", import.meta.url).pathname;
const AGENTIC_PROMPTS_DIR = new URL("../src/commit/agentic/prompts/", import.meta.url).pathname;
const PROMPT_DIRS = [PROMPTS_DIR, COMMIT_PROMPTS_DIR, AGENTIC_PROMPTS_DIR];
const PROMPT_FORMAT_OPTIONS = {
renderPhase: "pre-render",
replaceAsciiSymbols: true,
boldRfc2119Keywords: true,
} as const;
async function main() {
const glob = new Glob("**/*.md");
const files: string[] = [];
let changed = 0;
const check = process.argv.includes("--check");
for (const dir of PROMPT_DIRS) {
for await (const path of glob.scan(dir)) {
files.push(`${dir}${path}`);
}
}
for (const fullPath of files) {
const original = await Bun.file(fullPath).text();
const formatted = prompt.format(original, PROMPT_FORMAT_OPTIONS);
if (original !== `${formatted}\n`) {
if (check) {
console.log(`Would format: ${fullPath}`);
} else {
await Bun.write(fullPath, `${formatted}\n`);
console.log(`Formatted: ${fullPath}`);
}
changed++;
}
}
if (check && changed > 0) {
console.log(`\n${changed} file(s) need formatting. Run 'bun run format-prompts' to fix.`);
process.exit(1);
} else if (changed === 0) {
console.log("All prompt files are formatted.");
} else {
console.log(`\nFormatted ${changed} file(s).`);
}
}
main();