diff --git a/README.md b/README.md index 1d30d08..4b317d0 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Add to your OpenCode config: ```jsonc // opencode.jsonc { - "plugin": ["@tarquinen/opencode-dcp@0.4.1"], + "plugin": ["@tarquinen/opencode-dcp@0.4.2"], "experimental": { "primary_tools": ["prune"] } diff --git a/lib/pruning-tool.ts b/lib/pruning-tool.ts index 957361d..13605e8 100644 --- a/lib/pruning-tool.ts +++ b/lib/pruning-tool.ts @@ -62,7 +62,15 @@ export function createPruningTool( return "None of the provided IDs were valid. Check the list for available IDs." } - const tokensSaved = await calculateTokensSaved(client, sessionId, prunedIds) + // Fetch messages to calculate tokens and find current agent + const messagesResponse = await client.session.messages({ + path: { id: sessionId }, + query: { limit: 200 } + }) + const messages = messagesResponse.data || messagesResponse + + const currentAgent = findCurrentAgent(messages) + const tokensSaved = await calculateTokensSavedFromMessages(messages, prunedIds) const currentStats = state.stats.get(sessionId) ?? { totalToolsPruned: 0, @@ -105,7 +113,7 @@ export function createPruningTool( toolMetadata, gcPending: null, sessionStats - }) + }, currentAgent) toolTracker.skipNextIdle = true @@ -128,21 +136,29 @@ export function createPruningTool( }) } +/** + * Finds the current agent from messages (same logic as janitor.ts). + */ +function findCurrentAgent(messages: any[]): string | undefined { + for (let i = messages.length - 1; i >= 0; i--) { + const msg = messages[i] + const info = msg.info + if (info?.role === 'user') { + return info.agent || 'build' + } + } + return undefined +} + /** * Calculates approximate tokens saved by pruning the given tool call IDs. + * Uses pre-fetched messages to avoid duplicate API calls. */ -async function calculateTokensSaved( - client: any, - sessionId: string, +async function calculateTokensSavedFromMessages( + messages: any[], prunedIds: string[] ): Promise { try { - const messagesResponse = await client.session.messages({ - path: { id: sessionId }, - query: { limit: 200 } - }) - const messages = messagesResponse.data || messagesResponse - const toolOutputs = new Map() for (const msg of messages) { if (msg.role === 'tool' && msg.tool_call_id) { diff --git a/package-lock.json b/package-lock.json index 2abfa14..b527e8a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tarquinen/opencode-dcp", - "version": "0.4.1", + "version": "0.4.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@tarquinen/opencode-dcp", - "version": "0.4.1", + "version": "0.4.2", "license": "MIT", "dependencies": { "@ai-sdk/openai-compatible": "^1.0.28", diff --git a/package.json b/package.json index 54a309c..658e80b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package.json", "name": "@tarquinen/opencode-dcp", - "version": "0.4.1", + "version": "0.4.2", "type": "module", "description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context", "main": "./dist/index.js",