|
1 | 1 | import { readFileSync } from "fs" |
2 | 2 | import { join } from "path" |
3 | 3 |
|
4 | | -export function loadPrompt(name: string): string { |
| 4 | +export function loadPrompt(name: string, vars?: Record<string, string>): string { |
5 | 5 | const filePath = join(__dirname, "prompts", `${name}.txt`) |
6 | | - return readFileSync(filePath, "utf8").trim() |
| 6 | + let content = readFileSync(filePath, "utf8").trim() |
| 7 | + if (vars) { |
| 8 | + for (const [key, value] of Object.entries(vars)) { |
| 9 | + content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value) |
| 10 | + } |
| 11 | + } |
| 12 | + return content |
7 | 13 | } |
8 | 14 |
|
9 | 15 | function minimizeMessages(messages: any[], alreadyPrunedIds?: string[], protectedToolCallIds?: string[]): any[] { |
@@ -120,41 +126,15 @@ export function buildAnalysisPrompt( |
120 | 126 | reason?: string |
121 | 127 | ): string { |
122 | 128 | const minimizedMessages = minimizeMessages(messages, alreadyPrunedIds, protectedToolCallIds) |
123 | | - |
124 | 129 | const messagesJson = JSON.stringify(minimizedMessages, null, 2).replace(/\\n/g, '\n') |
125 | 130 |
|
126 | 131 | const reasonContext = reason |
127 | 132 | ? `\nContext: The AI has requested pruning with the following reason: "${reason}"\nUse this context to inform your decisions about what is most relevant to keep.` |
128 | 133 | : '' |
129 | 134 |
|
130 | | - return `You are a conversation analyzer that identifies obsolete tool outputs in a coding session. |
131 | | -${reasonContext} |
132 | | -Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context. |
133 | | -
|
134 | | -Guidelines for identifying obsolete tool calls: |
135 | | -1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained |
136 | | -2. Tool outputs from debugging/fixing an error that has now been resolved |
137 | | -3. Failed or incorrect tool attempts that were immediately corrected (e.g., reading a file from the wrong path, then reading from the correct path) |
138 | | -
|
139 | | -DO NOT prune: |
140 | | -- Tool calls whose outputs are actively being discussed |
141 | | -- Tool calls that produced errors still being debugged |
142 | | -- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use) |
143 | | -
|
144 | | -IMPORTANT: Available tool call IDs for analysis: ${unprunedToolCallIds.join(", ")} |
145 | | -
|
146 | | -The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either: |
147 | | -1. Protected tools (marked with toolCallID "<protected>") |
148 | | -2. Already-pruned tools (marked with toolCallID "<already-pruned>") |
149 | | -
|
150 | | -ONLY return IDs from the available list above. |
151 | | -
|
152 | | -Session history (each tool call has a "toolCallID" field): |
153 | | -${messagesJson} |
154 | | -
|
155 | | -You MUST respond with valid JSON matching this exact schema: |
156 | | -{ |
157 | | - "pruned_tool_call_ids": ["id1", "id2", ...], |
158 | | - "reasoning": "explanation of why these IDs were selected" |
159 | | -}` |
| 135 | + return loadPrompt("pruning", { |
| 136 | + reason_context: reasonContext, |
| 137 | + available_tool_call_ids: unprunedToolCallIds.join(", "), |
| 138 | + session_history: messagesJson |
| 139 | + }) |
160 | 140 | } |
0 commit comments