Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add to your OpenCode config:
```jsonc
// opencode.jsonc
{
"plugin": ["@tarquinen/[email protected].1"],
"plugin": ["@tarquinen/[email protected].2"],
"experimental": {
"primary_tools": ["prune"]
}
Expand Down
38 changes: 27 additions & 11 deletions lib/pruning-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ export function createPruningTool(
return "None of the provided IDs were valid. Check the <prunable-tools> 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,
Expand Down Expand Up @@ -105,7 +113,7 @@ export function createPruningTool(
toolMetadata,
gcPending: null,
sessionStats
})
}, currentAgent)

toolTracker.skipNextIdle = true

Expand All @@ -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<number> {
try {
const messagesResponse = await client.session.messages({
path: { id: sessionId },
query: { limit: 200 }
})
const messages = messagesResponse.data || messagesResponse

const toolOutputs = new Map<string, string>()
for (const msg of messages) {
if (msg.role === 'tool' && msg.tool_call_id) {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down