Skip to content

Commit 5cbc483

Browse files
committed
fix: only register enabled prune tools
1 parent a3eb386 commit 5cbc483

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

index.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,40 @@ const plugin: Plugin = (async (ctx) => {
5050
logger,
5151
config
5252
),
53-
tool: (config.strategies.discardTool.enabled || config.strategies.extractTool.enabled) ? {
54-
discard: createDiscardTool({
55-
client: ctx.client,
56-
state,
57-
logger,
58-
config,
59-
workingDirectory: ctx.directory
53+
tool: {
54+
...(config.strategies.discardTool.enabled && {
55+
discard: createDiscardTool({
56+
client: ctx.client,
57+
state,
58+
logger,
59+
config,
60+
workingDirectory: ctx.directory
61+
}),
6062
}),
61-
extract: createExtractTool({
62-
client: ctx.client,
63-
state,
64-
logger,
65-
config,
66-
workingDirectory: ctx.directory
63+
...(config.strategies.extractTool.enabled && {
64+
extract: createExtractTool({
65+
client: ctx.client,
66+
state,
67+
logger,
68+
config,
69+
workingDirectory: ctx.directory
70+
}),
6771
}),
68-
} : undefined,
72+
},
6973
config: async (opencodeConfig) => {
70-
// Add discard and extract to primary_tools by mutating the opencode config
74+
// Add enabled tools to primary_tools by mutating the opencode config
7175
// This works because config is cached and passed by reference
72-
if (config.strategies.discardTool.enabled || config.strategies.extractTool.enabled) {
76+
const toolsToAdd: string[] = []
77+
if (config.strategies.discardTool.enabled) toolsToAdd.push("discard")
78+
if (config.strategies.extractTool.enabled) toolsToAdd.push("extract")
79+
80+
if (toolsToAdd.length > 0) {
7381
const existingPrimaryTools = opencodeConfig.experimental?.primary_tools ?? []
7482
opencodeConfig.experimental = {
7583
...opencodeConfig.experimental,
76-
primary_tools: [...existingPrimaryTools, "discard", "extract"],
84+
primary_tools: [...existingPrimaryTools, ...toolsToAdd],
7785
}
78-
logger.info("Added 'discard' and 'extract' to experimental.primary_tools via config mutation")
86+
logger.info(`Added ${toolsToAdd.map(t => `'${t}'`).join(" and ")} to experimental.primary_tools via config mutation`)
7987
}
8088
},
8189
event: createEventHandler(ctx.client, config, state, logger, ctx.directory),

lib/messages/prune.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const PRUNED_TOOL_OUTPUT_REPLACEMENT = '[Output removed to save context - inform
1111
const getNudgeString = (config: PluginConfig): string => {
1212
const discardEnabled = config.strategies.discardTool.enabled
1313
const extractEnabled = config.strategies.extractTool.enabled
14-
14+
1515
if (discardEnabled && extractEnabled) {
1616
return loadPrompt("nudge/nudge-both")
1717
} else if (discardEnabled) {
@@ -30,7 +30,7 @@ ${content}
3030
const getCooldownMessage = (config: PluginConfig): string => {
3131
const discardEnabled = config.strategies.discardTool.enabled
3232
const extractEnabled = config.strategies.extractTool.enabled
33-
33+
3434
let toolName: string
3535
if (discardEnabled && extractEnabled) {
3636
toolName = "discard or extract tools"
@@ -39,7 +39,7 @@ const getCooldownMessage = (config: PluginConfig): string => {
3939
} else {
4040
toolName = "extract tool"
4141
}
42-
42+
4343
return `<prunable-tools>
4444
Context management was just performed. Do not use the ${toolName} again. A fresh list will be available after your next tool use.
4545
</prunable-tools>`
@@ -115,6 +115,8 @@ export const insertPruneToolContext = (
115115
logger.debug("prunable-tools: \n" + prunableToolsList)
116116

117117
let nudgeString = ""
118+
// TODO: Using Math.min() means the lower frequency dominates when both tools are enabled.
119+
// Consider using separate counters for each tool's nudge, or documenting this behavior.
118120
const nudgeFrequency = Math.min(
119121
config.strategies.discardTool.nudge.frequency,
120122
config.strategies.extractTool.nudge.frequency

lib/strategies/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function estimateTokensBatch(texts: string[]): number[] {
3737

3838
/**
3939
* Calculates approximate tokens saved by pruning the given tool call IDs.
40-
* TODO: Make it count message content that are not tool outputs. Currently it ONLY covers tool outputs and errors
4140
*/
4241
export const calculateTokensSaved = (
4342
state: SessionState,

0 commit comments

Comments
 (0)