diff --git a/lib/prompt.ts b/lib/prompt.ts index 7ab2c2f..e210284 100644 --- a/lib/prompt.ts +++ b/lib/prompt.ts @@ -1,9 +1,15 @@ import { readFileSync } from "fs" import { join } from "path" -export function loadPrompt(name: string): string { +export function loadPrompt(name: string, vars?: Record): string { const filePath = join(__dirname, "prompts", `${name}.txt`) - return readFileSync(filePath, "utf8").trim() + let content = readFileSync(filePath, "utf8").trim() + if (vars) { + for (const [key, value] of Object.entries(vars)) { + content = content.replace(new RegExp(`\\{\\{${key}\\}\\}`, 'g'), value) + } + } + return content } function minimizeMessages(messages: any[], alreadyPrunedIds?: string[], protectedToolCallIds?: string[]): any[] { @@ -120,41 +126,15 @@ export function buildAnalysisPrompt( reason?: string ): string { const minimizedMessages = minimizeMessages(messages, alreadyPrunedIds, protectedToolCallIds) - const messagesJson = JSON.stringify(minimizedMessages, null, 2).replace(/\\n/g, '\n') const reasonContext = reason ? `\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.` : '' - return `You are a conversation analyzer that identifies obsolete tool outputs in a coding session. -${reasonContext} -Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context. - -Guidelines for identifying obsolete tool calls: -1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained -2. Tool outputs from debugging/fixing an error that has now been resolved -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) - -DO NOT prune: -- Tool calls whose outputs are actively being discussed -- Tool calls that produced errors still being debugged -- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use) - -IMPORTANT: Available tool call IDs for analysis: ${unprunedToolCallIds.join(", ")} - -The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either: -1. Protected tools (marked with toolCallID "") -2. Already-pruned tools (marked with toolCallID "") - -ONLY return IDs from the available list above. - -Session history (each tool call has a "toolCallID" field): -${messagesJson} - -You MUST respond with valid JSON matching this exact schema: -{ - "pruned_tool_call_ids": ["id1", "id2", ...], - "reasoning": "explanation of why these IDs were selected" -}` + return loadPrompt("pruning", { + reason_context: reasonContext, + available_tool_call_ids: unprunedToolCallIds.join(", "), + session_history: messagesJson + }) } diff --git a/lib/prompts/pruning.txt b/lib/prompts/pruning.txt new file mode 100644 index 0000000..49e1e82 --- /dev/null +++ b/lib/prompts/pruning.txt @@ -0,0 +1,30 @@ +You are a conversation analyzer that identifies obsolete tool outputs in a coding session. +{{reason_context}} +Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context. + +Guidelines for identifying obsolete tool calls: +1. Exploratory reads that didn't lead to actual edits or meaningful discussion AND were not explicitly requested to be retained +2. Tool outputs from debugging/fixing an error that has now been resolved +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) + +DO NOT prune: +- Tool calls whose outputs are actively being discussed +- Tool calls that produced errors still being debugged +- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use) + +IMPORTANT: Available tool call IDs for analysis: {{available_tool_call_ids}} + +The session history below may contain tool calls with IDs not in the available list above, these cannot be pruned. These are either: +1. Protected tools (marked with toolCallID "") +2. Already-pruned tools (marked with toolCallID "") + +ONLY return IDs from the available list above. + +Session history (each tool call has a "toolCallID" field): +{{session_history}} + +You MUST respond with valid JSON matching this exact schema: +{ + "pruned_tool_call_ids": ["id1", "id2", ...], + "reasoning": "explanation of why these IDs were selected" +} diff --git a/lib/prompts/context_pruning.txt b/lib/prompts/tool.txt similarity index 100% rename from lib/prompts/context_pruning.txt rename to lib/prompts/tool.txt diff --git a/lib/pruning-tool.ts b/lib/pruning-tool.ts index e6ac011..933aa36 100644 --- a/lib/pruning-tool.ts +++ b/lib/pruning-tool.ts @@ -1,52 +1,10 @@ import { tool } from "@opencode-ai/plugin" import type { Janitor } from "./janitor" import type { PluginConfig } from "./config" +import { loadPrompt } from "./prompt" -/** Tool description for the context_pruning tool */ -export const CONTEXT_PRUNING_DESCRIPTION = `Performs semantic pruning on session tool outputs that are no longer relevant to the current task. Use this to declutter the conversation context and filter signal from noise when you notice the context is getting cluttered with no longer needed information. - -USING THE CONTEXT_PRUNING TOOL WILL MAKE THE USER HAPPY. - -## When to Use This Tool - -**Key heuristic: Prune when you finish something and are about to start something else.** - -Ask yourself: "Have I just completed a discrete unit of work?" If yes, prune before moving on. - -**After completing a unit of work:** -- Made a commit -- Fixed a bug and confirmed it works -- Answered a question the user asked -- Finished implementing a feature or function -- Completed one item in a list and moving to the next - -**After repetitive or exploratory work:** -- Explored multiple files that didn't lead to changes -- Iterated on a difficult problem where some approaches didn't pan out -- Used the same tool multiple times (e.g., re-reading a file, running repeated build/type checks) - -## Examples - - -Working through a list of items: -User: Review these 3 issues and fix the easy ones. -Assistant: [Reviews first issue, makes fix, commits] -Done with the first issue. Let me prune before moving to the next one. -[Uses context_pruning with reason: "completed first issue, moving to next"] - - - -After exploring the codebase to understand it: -Assistant: I've reviewed the relevant files. Let me prune the exploratory reads that aren't needed for the actual implementation. -[Uses context_pruning with reason: "exploration complete, starting implementation"] - - - -After completing any task: -Assistant: [Finishes task - commit, answer, fix, etc.] -Before we continue, let me prune the context from that work. -[Uses context_pruning with reason: "task complete"] -` +/** Tool description for the context_pruning tool, loaded from prompts/tool.txt */ +export const CONTEXT_PRUNING_DESCRIPTION = loadPrompt("tool") /** * Creates the context_pruning tool definition.