Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
2.9 kB · 92 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293/** * Prompt Customizer Extension * * Demonstrates using systemPromptOptions to make informed, context-aware * modifications to the system prompt without re-discovering resources. * * This extension adds tool-specific guidance based on what tools and skills * are currently active, respecting whatever the user has configured. * * Usage: * 1. Copy this file to ~/.pi/agent/extensions/ or your project's .pi/extensions/ * 2. Use the extension — it automatically adapts to your active tools and skills */
import type { BuildSystemPromptOptions, ExtensionAPI } from "@earendil-works/pi-coding-agent";
/** * Adds tool-specific guidance that adapts to the active tool set. * Instead of appending one-size-fits-all instructions, this reads what's * actually loaded and tailors the guidance accordingly. */function addToolGuidance(options: BuildSystemPromptOptions, basePrompt: string): string { const hasTool = (name: string) => options.selectedTools?.includes(name) ?? false;
const parts: string[] = [];
if (hasTool("ipython")) { parts.push( "• Use the `ipython` tool for Python execution, file access, data inspection, and shell subprocesses.", ); }
if (hasTool("bash")) { parts.push("• Execute shell commands with the `bash` tool when it is explicitly enabled."); }
if (hasTool("edit")) { parts.push( "• Use the `edit` tool for precise text replacements in files. Match exact content including whitespace.", ); }
if (options.skills && options.skills.length > 0) { const skillNames = options.skills.map((s) => s.name).join(", "); parts.push(`\nAvailable skills: ${skillNames}`, "Use skill documentation for best practices on specific tools."); }
if (parts.length === 0) { return basePrompt; }
return `${basePrompt}
## Tool Guidance
${parts.join("\n")}`;}
/** * Merges extension instructions with user-provided append prompts. * This respects whatever the user configured via --append-system-prompt * flags or files, rather than duplicating that work. */function mergeWithUserAppend(options: BuildSystemPromptOptions): string { const userAppend = options.appendSystemPrompt; const extensionSpecific = `## Extension-Added Context
This prompt includes tool guidance and skill information loaded dynamically.If you have additional requirements, configure them via --append-system-prompt or project context files.`;
if (userAppend) { return `${userAppend}\n\n${extensionSpecific}`; }
return extensionSpecific;}
export default function promptCustomizer(pi: ExtensionAPI) { pi.on("before_agent_start", async (event) => { const { systemPrompt, systemPromptOptions } = event;
const customPrompt = addToolGuidance(systemPromptOptions, systemPrompt); const appendSection = mergeWithUserAppend(systemPromptOptions);
return { systemPrompt: `${customPrompt}${appendSection}`, }; });}