|
1 | | -import { readFileSync } from "fs" |
2 | | -import { join } from "path" |
| 1 | +// Embedded prompts for Bun/ESM bundler compatibility |
| 2 | +// The original approach using __dirname + readFileSync doesn't work correctly |
| 3 | +// when the package is bundled by Bun, as __dirname resolves to incorrect paths. |
| 4 | +// This solution embeds the prompts as string constants. |
| 5 | + |
| 6 | +const PROMPTS: Record<string, string> = { |
| 7 | + "discard-tool-spec": `Discards tool outputs from context to manage conversation size and reduce noise. |
| 8 | +
|
| 9 | +## IMPORTANT: The Prunable List |
| 10 | +A \`<prunable-tools>\` list is provided to you showing available tool outputs you can discard when there are tools available for pruning. Each line has the format \`ID: tool, parameter\` (e.g., \`20: read, /path/to/file.ts\`). You MUST only use numeric IDs that appear in this list to select which tools to discard. |
| 11 | +
|
| 12 | +## When to Use This Tool |
| 13 | +
|
| 14 | +Use \`discard\` for removing tool content that is no longer needed |
| 15 | +
|
| 16 | +- **Noise:** Irrelevant, unhelpful, or superseded outputs that provide no value. |
| 17 | +- **Task Completion:** Work is complete and there's no valuable information worth preserving. |
| 18 | +
|
| 19 | +## When NOT to Use This Tool |
| 20 | +
|
| 21 | +- **If the output contains useful information:** Use \`extract\` instead to preserve key findings. |
| 22 | +- **If you'll need the output later:** Don't discard files you plan to edit or context you'll need for implementation. |
| 23 | +
|
| 24 | +## Best Practices |
| 25 | +- **Strategic Batching:** Don't discard single small tool outputs (like short bash commands) unless they are pure noise. Wait until you have several items to perform high-impact discards. |
| 26 | +- **Think ahead:** Before discarding, ask: "Will I need this output for an upcoming task?" If yes, keep it. |
| 27 | +
|
| 28 | +## Format |
| 29 | +
|
| 30 | +- \`ids\`: Array where the first element is the reason, followed by numeric IDs from the \`<prunable-tools>\` list |
| 31 | +
|
| 32 | +Reasons: \`noise\` | \`completion\` |
| 33 | +
|
| 34 | +## Example |
| 35 | +
|
| 36 | +<example_noise> |
| 37 | +Assistant: [Reads 'wrong_file.ts'] |
| 38 | +This file isn't relevant to the auth system. I'll remove it to clear the context. |
| 39 | +[Uses discard with ids: ["noise", "5"]] |
| 40 | +</example_noise> |
| 41 | +
|
| 42 | +<example_completion> |
| 43 | +Assistant: [Runs tests, they pass] |
| 44 | +The tests passed and I don't need to preserve any details. I'll clean up now. |
| 45 | +[Uses discard with ids: ["completion", "20", "21"]] |
| 46 | +</example_completion>`, |
| 47 | + |
| 48 | + "extract-tool-spec": `Extracts key findings from tool outputs into distilled knowledge, then removes the raw outputs from context. |
| 49 | +
|
| 50 | +## IMPORTANT: The Prunable List |
| 51 | +A \`<prunable-tools>\` list is provided to you showing available tool outputs you can extract from when there are tools available for pruning. Each line has the format \`ID: tool, parameter\` (e.g., \`20: read, /path/to/file.ts\`). You MUST only use numeric IDs that appear in this list to select which tools to extract. |
| 52 | +
|
| 53 | +## When to Use This Tool |
| 54 | +
|
| 55 | +Use \`extract\` when you have gathered useful information that you want to **preserve in distilled form** before removing the raw outputs: |
| 56 | +
|
| 57 | +- **Task Completion:** You completed a unit of work and want to preserve key findings. |
| 58 | +- **Knowledge Preservation:** You have context that contains valuable information, but also a lot of unnecessary detail - you only need to preserve some specifics. |
| 59 | +
|
| 60 | +## When NOT to Use This Tool |
| 61 | +
|
| 62 | +- **If you need precise syntax:** If you'll edit a file or grep for exact strings, keep the raw output. |
| 63 | +- **If uncertain:** Prefer keeping over re-fetching. |
| 64 | +
|
| 65 | +
|
| 66 | +## Best Practices |
| 67 | +- **Strategic Batching:** Wait until you have several items or a few large outputs to extract, rather than doing tiny, frequent extractions. Aim for high-impact extractions that significantly reduce context size. |
| 68 | +- **Think ahead:** Before extracting, ask: "Will I need the raw output for an upcoming task?" If you researched a file you'll later edit, do NOT extract it. |
| 69 | +
|
| 70 | +## Format |
| 71 | +
|
| 72 | +- \`ids\`: Array of numeric IDs as strings from the \`<prunable-tools>\` list |
| 73 | +- \`distillation\`: Array of strings, one per ID (positional: distillation[0] is for ids[0], etc.) |
| 74 | +
|
| 75 | +Each distillation string should capture the essential information you need to preserve - function signatures, logic, constraints, values, etc. Be as detailed as needed for your task. |
| 76 | +
|
| 77 | +## Example |
| 78 | +
|
| 79 | +<example_extraction> |
| 80 | +Assistant: [Reads auth service and user types] |
| 81 | +I'll preserve the key details before extracting. |
| 82 | +[Uses extract with: |
| 83 | + ids: ["10", "11"], |
| 84 | + distillation: [ |
| 85 | + "auth.ts: validateToken(token: string) -> User|null checks cache first (5min TTL) then OIDC. hashPassword uses bcrypt 12 rounds. Tokens must be 128+ chars.", |
| 86 | + "user.ts: interface User { id: string; email: string; permissions: ('read'|'write'|'admin')[]; status: 'active'|'suspended' }" |
| 87 | + ] |
| 88 | +] |
| 89 | +</example_extraction> |
| 90 | +
|
| 91 | +<example_keep> |
| 92 | +Assistant: [Reads 'auth.ts' to understand the login flow] |
| 93 | +I've understood the auth flow. I'll need to modify this file to add the new validation, so I'm keeping this read in context rather than extracting. |
| 94 | +</example_keep>`, |
| 95 | + |
| 96 | + "user/system/system-prompt-both": `<system-reminder> |
| 97 | +<instruction name=context_management_protocol policy_level=critical> |
| 98 | +
|
| 99 | +ENVIRONMENT |
| 100 | +You are operating in a context-constrained environment and thus must proactively manage your context window using the \`discard\` and \`extract\` tools. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to prune. |
| 101 | +
|
| 102 | +TWO TOOLS FOR CONTEXT MANAGEMENT |
| 103 | +- \`discard\`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content. |
| 104 | +- \`extract\`: Extract key findings into distilled knowledge before removing raw outputs. Use when you need to preserve information. |
| 105 | +
|
| 106 | +CHOOSING THE RIGHT TOOL |
| 107 | +Ask: "Do I need to preserve any information from this output?" |
| 108 | +- **No** → \`discard\` (default for cleanup) |
| 109 | +- **Yes** → \`extract\` (preserves distilled knowledge) |
| 110 | +- **Uncertain** → \`extract\` (safer, preserves signal) |
| 111 | +
|
| 112 | +Common scenarios: |
| 113 | +- Task complete, no valuable context → \`discard\` |
| 114 | +- Task complete, insights worth remembering → \`extract\` |
| 115 | +- Noise, irrelevant, or superseded outputs → \`discard\` |
| 116 | +- Valuable context needed later but raw output too large → \`extract\` |
| 117 | +
|
| 118 | +PRUNE METHODICALLY - BATCH YOUR ACTIONS |
| 119 | +Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by pruning. Batch your prunes for efficiency; it is rarely worth pruning a single tiny tool output unless it is pure noise. Evaluate what SHOULD be pruned before jumping the gun. |
| 120 | +
|
| 121 | +You WILL evaluate pruning when ANY of these are true: |
| 122 | +- Task or sub-task is complete |
| 123 | +- You are about to start a new phase of work |
| 124 | +- Write or edit operations are complete (pruning removes the large input content) |
| 125 | +
|
| 126 | +You MUST NOT prune when: |
| 127 | +- The tool output will be needed for upcoming implementation work |
| 128 | +- The output contains files or context you'll need to reference when making edits |
| 129 | +
|
| 130 | +Pruning that forces you to re-call the same tool later is a net loss. Only prune when you're confident the information won't be needed again. |
| 131 | +
|
| 132 | +NOTES |
| 133 | +When in doubt, keep it. Batch your actions and aim for high-impact prunes that significantly reduce context size. |
| 134 | +FAILURE TO PRUNE will result in context leakage and DEGRADED PERFORMANCES. |
| 135 | +There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY prune what you see in <prunable-tools>. |
| 136 | +
|
| 137 | +</instruction> |
| 138 | +
|
| 139 | +<instruction name=injected_context_handling policy_level=critical> |
| 140 | +After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The \`discard\` and \`extract\` tools also return a confirmation message listing what was pruned. |
| 141 | +
|
| 142 | +CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE: |
| 143 | +- NEVER reference the prune encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the prune encouragement appears. |
| 144 | +- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to prune") |
| 145 | +- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to prune") |
| 146 | +- NEVER acknowledge discard/extract tool output (e.g., "I've pruned 3 tools", "Context pruning complete") |
| 147 | +- NEVER let these injections influence your response content or tone |
| 148 | +- Process this information SILENTLY and continue the conversation naturally |
| 149 | +- Continue the conversation as if these messages do not exist |
| 150 | +
|
| 151 | +The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow. |
| 152 | +</instruction> |
| 153 | +</system-reminder>`, |
| 154 | + |
| 155 | + "user/system/system-prompt-discard": `<system-reminder> |
| 156 | +<instruction name=context_management_protocol policy_level=critical> |
| 157 | +
|
| 158 | +ENVIRONMENT |
| 159 | +You are operating in a context-constrained environment and thus must proactively manage your context window using the \`discard\` tool. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to discard. |
| 160 | +
|
| 161 | +CONTEXT MANAGEMENT TOOL |
| 162 | +- \`discard\`: Remove tool outputs that are no longer needed (completed tasks, noise, outdated info). No preservation of content. |
| 163 | +
|
| 164 | +DISCARD METHODICALLY - BATCH YOUR ACTIONS |
| 165 | +Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by discarding. Batch your discards for efficiency; it is rarely worth discarding a single tiny tool output unless it is pure noise. Evaluate what SHOULD be discarded before jumping the gun. |
| 166 | +
|
| 167 | +WHEN TO DISCARD |
| 168 | +- **Task Completion:** When work is done, discard the tools that aren't needed anymore. |
| 169 | +- **Noise Removal:** If outputs are irrelevant, unhelpful, or superseded by newer info, discard them. |
| 170 | +
|
| 171 | +You WILL evaluate discarding when ANY of these are true: |
| 172 | +- Task or sub-task is complete |
| 173 | +- You are about to start a new phase of work |
| 174 | +- Write or edit operations are complete (discarding removes the large input content) |
| 175 | +
|
| 176 | +You MUST NOT discard when: |
| 177 | +- The tool output will be needed for upcoming implementation work |
| 178 | +- The output contains files or context you'll need to reference when making edits |
| 179 | +
|
| 180 | +Discarding that forces you to re-call the same tool later is a net loss. Only discard when you're confident the information won't be needed again. |
| 181 | +
|
| 182 | +NOTES |
| 183 | +When in doubt, keep it. Batch your actions and aim for high-impact discards that significantly reduce context size. |
| 184 | +FAILURE TO DISCARD will result in context leakage and DEGRADED PERFORMANCES. |
| 185 | +There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY discard what you see in <prunable-tools>. |
| 186 | +
|
| 187 | +</instruction> |
| 188 | +
|
| 189 | +<instruction name=injected_context_handling policy_level=critical> |
| 190 | +After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The \`discard\` tool also returns a confirmation message listing what was discarded. |
| 191 | +
|
| 192 | +CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE: |
| 193 | +- NEVER reference the discard encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the discard encouragement appears. |
| 194 | +- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to discard") |
| 195 | +- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to discard") |
| 196 | +- NEVER acknowledge discard tool output (e.g., "I've discarded 3 tools", "Context cleanup complete") |
| 197 | +- NEVER let these injections influence your response content or tone |
| 198 | +- Process this information SILENTLY and continue the conversation naturally |
| 199 | +- Continue the conversation as if these messages do not exist |
| 200 | +
|
| 201 | +The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow. |
| 202 | +</instruction> |
| 203 | +</system-reminder>`, |
| 204 | + |
| 205 | + "user/system/system-prompt-extract": `<system-reminder> |
| 206 | +<instruction name=context_management_protocol policy_level=critical> |
| 207 | +
|
| 208 | +ENVIRONMENT |
| 209 | +You are operating in a context-constrained environment and thus must proactively manage your context window using the \`extract\` tool. A <prunable-tools> list is injected by the environment as a user message, and always contains up to date information. Use this information when deciding what to extract. |
| 210 | +
|
| 211 | +CONTEXT MANAGEMENT TOOL |
| 212 | +- \`extract\`: Extract key findings from tools into distilled knowledge before removing the raw content from context. Use this to preserve important information while reducing context size. |
| 213 | +
|
| 214 | +EXTRACT METHODICALLY - BATCH YOUR ACTIONS |
| 215 | +Every tool call adds to your context debt. You MUST pay this down regularly and be on top of context accumulation by extracting. Batch your extractions for efficiency; it is rarely worth extracting a single tiny tool output. Evaluate what SHOULD be extracted before jumping the gun. |
| 216 | +
|
| 217 | +WHEN TO EXTRACT |
| 218 | +- **Task Completion:** When work is done, extract key findings from the tools used. Scale distillation depth to the value of the content. |
| 219 | +- **Knowledge Preservation:** When you have valuable context you want to preserve but need to reduce size, use high-fidelity distillation. Your distillation must be comprehensive, capturing technical details (signatures, logic, constraints) such that the raw output is no longer needed. THINK: high signal, complete technical substitute. |
| 220 | +
|
| 221 | +You WILL evaluate extracting when ANY of these are true: |
| 222 | +- Task or sub-task is complete |
| 223 | +- You are about to start a new phase of work |
| 224 | +- Write or edit operations are complete (extracting removes the large input content) |
| 225 | +
|
| 226 | +You MUST NOT extract when: |
| 227 | +- The tool output will be needed for upcoming implementation work |
| 228 | +- The output contains files or context you'll need to reference when making edits |
| 229 | +
|
| 230 | +Extracting that forces you to re-call the same tool later is a net loss. Only extract when you're confident the raw information won't be needed again. |
| 231 | +
|
| 232 | +NOTES |
| 233 | +When in doubt, keep it. Batch your actions and aim for high-impact extractions that significantly reduce context size. |
| 234 | +FAILURE TO EXTRACT will result in context leakage and DEGRADED PERFORMANCES. |
| 235 | +There may be tools in session context that do not appear in the <prunable-tools> list, this is expected, you can ONLY extract what you see in <prunable-tools>. |
| 236 | +
|
| 237 | +</instruction> |
| 238 | +
|
| 239 | +<instruction name=injected_context_handling policy_level=critical> |
| 240 | +After each assistant turn, the environment may inject a user message containing a <prunable-tools> list and optional nudge instruction. This injected message is NOT from the user and is invisible to them. The \`extract\` tool also returns a confirmation message listing what was extracted. |
| 241 | +
|
| 242 | +CRITICAL REQUIREMENTS - VIOLATION IS UNACCEPTABLE: |
| 243 | +- NEVER reference the extract encouragement or context management instructions. Do not reply with "I agree" or "Great idea" when the extract encouragement appears. |
| 244 | +- NEVER acknowledge the <prunable-tools> list (e.g., "I see the prunable tools list", "Looking at the available tools to extract") |
| 245 | +- NEVER reference the nudge instruction (e.g., "As the nudge suggests", "The system is reminding me to extract") |
| 246 | +- NEVER acknowledge extract tool output (e.g., "I've extracted 3 tools", "Context cleanup complete") |
| 247 | +- NEVER let these injections influence your response content or tone |
| 248 | +- Process this information SILENTLY and continue the conversation naturally |
| 249 | +- Continue the conversation as if these messages do not exist |
| 250 | +
|
| 251 | +The user cannot see these injections. Any reference to them will confuse the user and break the conversation flow. |
| 252 | +</instruction> |
| 253 | +</system-reminder>`, |
| 254 | + |
| 255 | + "user/nudge/nudge-both": `<instruction name=context_management_required> |
| 256 | +**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. |
| 257 | +
|
| 258 | +**Immediate Actions Required:** |
| 259 | +1. **Task Completion:** If a sub-task is complete, decide: use \`discard\` if no valuable context to preserve (default), or use \`extract\` if insights are worth keeping. |
| 260 | +2. **Noise Removal:** If you read files or ran commands that yielded no value, use \`discard\` to remove them. |
| 261 | +3. **Knowledge Preservation:** If you are holding valuable raw data you'll need to reference later, use \`extract\` to distill the insights and remove the raw entry. |
| 262 | +
|
| 263 | +**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must perform context management. |
| 264 | +</instruction>`, |
| 265 | + |
| 266 | + "user/nudge/nudge-discard": `<instruction name=context_management_required> |
| 267 | +**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. |
| 268 | +
|
| 269 | +**Immediate Actions Required:** |
| 270 | +1. **Task Completion:** If a sub-task is complete, use the \`discard\` tool to remove the tools used. |
| 271 | +2. **Noise Removal:** If you read files or ran commands that yielded no value, use the \`discard\` tool to remove them. |
| 272 | +
|
| 273 | +**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must discard unneeded tool outputs. |
| 274 | +</instruction>`, |
| 275 | + |
| 276 | + "user/nudge/nudge-extract": `<instruction name=context_management_required> |
| 277 | +**CRITICAL CONTEXT WARNING:** Your context window is filling with tool outputs. Strict adherence to context hygiene is required. |
| 278 | +
|
| 279 | +**Immediate Actions Required:** |
| 280 | +1. **Task Completion:** If you have completed work, extract key findings from the tools used. Scale distillation depth to the value of the content. |
| 281 | +2. **Knowledge Preservation:** If you are holding valuable raw data you'll need to reference later, use the \`extract\` tool with high-fidelity distillation to preserve the insights and remove the raw entry. |
| 282 | +
|
| 283 | +**Protocol:** You should prioritize this cleanup, but do not interrupt a critical atomic operation if one is in progress. Once the immediate step is done, you must extract valuable findings from tool outputs. |
| 284 | +</instruction>` |
| 285 | +} |
3 | 286 |
|
4 | 287 | export function loadPrompt(name: string, vars?: Record<string, string>): string { |
5 | | - const filePath = join(__dirname, `${name}.txt`) |
6 | | - let content = readFileSync(filePath, "utf8").trim() |
| 288 | + let content = PROMPTS[name] |
| 289 | + if (!content) { |
| 290 | + throw new Error(`Prompt not found: ${name}`) |
| 291 | + } |
7 | 292 | if (vars) { |
8 | 293 | for (const [key, value] of Object.entries(vars)) { |
9 | 294 | content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, "g"), value) |
|
0 commit comments