Files
oh-my-pi/packages/coding-agent/scripts/format-prompts.ts
T
can1357 7de4c831e6 refactor(coding-agent): extracted prompt formatting into reusable utility
- Extracted prompt formatting logic into reusable `formatPromptContent()` utility function with configurable render phases.
- Removed 166 lines of duplicate formatting code from scripts and config modules by centralizing regex patterns and helper functions.
- Updated prompt template rendering to use unified `formatPromptContent()` instead of inline `optimizePromptLayout()` implementation.
- Added comprehensive test coverage for prompt formatting with pre-render and post-render mode validation.
2026-02-28 05:27:46 +01:00

69 lines
2.1 KiB
TypeScript

#!/usr/bin/env bun
/**
* 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. No trailing newline at EOF
* 9. Bold RFC 2119 keywords (MUST, SHOULD, MAY, etc.) in prompt content
*/
import { Glob } from "bun";
import { formatPromptContent } from "../src/utils/prompt-format";
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 = formatPromptContent(original, PROMPT_FORMAT_OPTIONS);
if (original !== formatted) {
if (check) {
console.log(`Would format: ${fullPath}`);
} else {
await Bun.write(fullPath, formatted);
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();