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
26 changes: 16 additions & 10 deletions lib/state/tool-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SessionState, ToolStatus, WithParts } from "./index"
import type { Logger } from "../logger"
import { PluginConfig } from "../config"

const MAX_TOOL_CACHE_SIZE = 500
const MAX_TOOL_CACHE_SIZE = 1000

/**
* Sync tool parameters from OpenCode's session.messages() API.
Expand All @@ -16,13 +16,24 @@ export async function syncToolCache(
try {
logger.info("Syncing tool parameters from OpenCode messages")

state.nudgeCounter = 0

for (const msg of messages) {
for (const part of msg.parts) {
if (part.type !== "tool" || !part.callID || state.toolParameters.has(part.callID)) {
if (part.type !== "tool" || !part.callID) {
continue
}

const alreadyPruned = state.prune.toolIds.includes(part.callID)
if (part.tool === "prune") {
state.nudgeCounter = 0
} else if (!config.strategies.pruneTool.protectedTools.includes(part.tool)) {
state.nudgeCounter++
}
state.lastToolPrune = part.tool === "prune"

if (state.toolParameters.has(part.callID)) {
continue
}

state.toolParameters.set(
part.callID,
Expand All @@ -34,16 +45,11 @@ export async function syncToolCache(
compacted: part.state.status === "completed" && !!part.state.time.compacted,
}
)

if (!alreadyPruned && !config.strategies.pruneTool.protectedTools.includes(part.tool)) {
state.nudgeCounter++
}

state.lastToolPrune = part.tool === "prune"
logger.info("lastToolPrune=" + String(state.lastToolPrune))
}
}

// logger.info(`nudgeCounter=${state.nudgeCounter}, lastToolPrune=${state.lastToolPrune}`)

trimToolParametersCache(state)
} catch (error) {
logger.warn("Failed to sync tool parameters from OpenCode", {
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.17",
"version": "1.0.0-beta.1",
"type": "module",
"description": "OpenCode plugin that optimizes token usage by pruning obsolete tool outputs from conversation context",
"main": "./dist/index.js",
Expand Down