prompt-customizer.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Prompt Customizer Extension
  3. *
  4. * Demonstrates using systemPromptOptions to make informed, context-aware
  5. * modifications to the system prompt without re-discovering resources.
  6. *
  7. * This extension adds tool-specific guidance based on what tools and skills
  8. * are currently active, respecting whatever the user has configured.
  9. *
  10. * Usage:
  11. * 1. Copy this file to ~/.pi/agent/extensions/ or your project's .pi/extensions/
  12. * 2. Use the extension — it automatically adapts to your active tools and skills
  13. */
  14. import type { BuildSystemPromptOptions, ExtensionAPI } from "@earendil-works/pi-coding-agent";
  15. /**
  16. * Adds tool-specific guidance that adapts to the active tool set.
  17. * Instead of appending one-size-fits-all instructions, this reads what's
  18. * actually loaded and tailors the guidance accordingly.
  19. */
  20. function addToolGuidance(options: BuildSystemPromptOptions, basePrompt: string): string {
  21. const hasTool = (name: string) => options.selectedTools?.includes(name) ?? false;
  22. const parts: string[] = [];
  23. if (hasTool("read")) {
  24. parts.push(
  25. "• Use the `read` tool for file contents (supports text and images).",
  26. " - For large files, use `offset` and `limit` to read in chunks.",
  27. );
  28. }
  29. if (hasTool("bash")) {
  30. parts.push("• Execute commands with the `bash` tool. Use it for file operations like `ls`, `find`, `grep`.");
  31. }
  32. if (hasTool("edit")) {
  33. parts.push(
  34. "• Use the `edit` tool for precise text replacements in files. Match exact content including whitespace.",
  35. );
  36. }
  37. if (hasTool("write")) {
  38. parts.push("• Use the `write` tool to create new files or overwrite existing ones completely.");
  39. }
  40. if (options.skills && options.skills.length > 0) {
  41. const skillNames = options.skills.map((s) => s.name).join(", ");
  42. parts.push(`\nAvailable skills: ${skillNames}`, "Use skill documentation for best practices on specific tools.");
  43. }
  44. if (parts.length === 0) {
  45. return basePrompt;
  46. }
  47. return `${basePrompt}
  48. ## Tool Guidance
  49. ${parts.join("\n")}
  50. `;
  51. }
  52. /**
  53. * Merges extension instructions with user-provided append prompts.
  54. * This respects whatever the user configured via --append-system-prompt
  55. * flags or files, rather than duplicating that work.
  56. */
  57. function mergeWithUserAppend(options: BuildSystemPromptOptions): string {
  58. const userAppend = options.appendSystemPrompt;
  59. const extensionSpecific = `
  60. ## Extension-Added Context
  61. This prompt includes tool guidance and skill information loaded dynamically.
  62. If you have additional requirements, configure them via --append-system-prompt or project context files.
  63. `;
  64. if (userAppend) {
  65. return `${userAppend}\n\n${extensionSpecific}`;
  66. }
  67. return extensionSpecific;
  68. }
  69. export default function promptCustomizer(pi: ExtensionAPI) {
  70. pi.on("before_agent_start", async (event) => {
  71. const { systemPrompt, systemPromptOptions } = event;
  72. const customPrompt = addToolGuidance(systemPromptOptions, systemPrompt);
  73. const appendSection = mergeWithUserAppend(systemPromptOptions);
  74. return {
  75. systemPrompt: `${customPrompt}${appendSection}`,
  76. };
  77. });
  78. }