11import { tool } from "@opencode-ai/plugin"
2- import type { SessionState , ToolParameterEntry } from "./state"
3- import type { PluginConfig } from "./config"
4- import { findCurrentAgent , buildToolIdList , getPrunedIds } from "./utils"
5- import { PruneReason , sendUnifiedNotification } from "./ui/notification"
6- import { formatPruningResultForTool } from "./ui/display-utils"
7- import { ensureSessionInitialized } from "./state"
8- import { saveSessionState } from "./state/persistence"
9- import type { Logger } from "./logger"
10- import { estimateTokensBatch } from "./tokenizer"
11- import { loadPrompt } from "./prompt"
2+ import type { SessionState , ToolParameterEntry } from ".. /state"
3+ import type { PluginConfig } from ".. /config"
4+ import { findCurrentAgent , buildToolIdList , getPruneToolIds } from ". ./utils"
5+ import { PruneReason , sendUnifiedNotification } from ".. /ui/notification"
6+ import { formatPruningResultForTool } from ".. /ui/display-utils"
7+ import { ensureSessionInitialized } from ".. /state"
8+ import { saveSessionState } from ".. /state/persistence"
9+ import type { Logger } from ".. /logger"
10+ import { estimateTokensBatch } from ".. /tokenizer"
11+ import { loadPrompt } from ".. /prompt"
1212
1313/** Tool description loaded from prompts/tool.txt */
1414const TOOL_DESCRIPTION = loadPrompt ( "tool" )
@@ -56,8 +56,8 @@ export function createPruningTool(
5656 return "No valid pruning reason found. Use 'completion', 'noise', or 'consolidation' as the first element."
5757 }
5858
59- const numericIds : number [ ] = args . ids . slice ( 1 ) . filter ( ( id ) : id is number => typeof id === "number" )
60- if ( numericIds . length === 0 ) {
59+ const numericToolIds : number [ ] = args . ids . slice ( 1 ) . filter ( ( id ) : id is number => typeof id === "number" )
60+ if ( numericToolIds . length === 0 ) {
6161 return "No numeric IDs provided. Format: [reason, id1, id2, ...] where reason is 'completion', 'noise', or 'consolidation'."
6262 }
6363
@@ -67,22 +67,20 @@ export function createPruningTool(
6767 const messages = await client . session . messages ( {
6868 path : { id : sessionId }
6969 } )
70- // const messages = messagesResponse.data || messagesResponse // Need this?
7170
7271 const currentAgent : string | undefined = findCurrentAgent ( messages )
7372 const toolIdList : string [ ] = buildToolIdList ( messages )
74- const prunedIds : string [ ] = getPrunedIds ( numericIds , toolIdList )
75- const tokensSaved = await calculateTokensSavedFromMessages ( messages , prunedIds )
73+ const pruneToolIds : string [ ] = getPruneToolIds ( numericToolIds , toolIdList )
74+ const tokensSaved = await calculateTokensSavedFromMessages ( messages , pruneToolIds )
7675
77- state . stats . totalTokensSaved += tokensSaved
78- state . stats . totalToolsPruned += prunedIds . length
79- state . prunedIds . push ( ...prunedIds )
76+ state . stats . pruneTokenCounter += tokensSaved
77+ state . prune . toolIds . push ( ...pruneToolIds )
8078
8179 saveSessionState ( state , logger )
8280 . catch ( err => logger . error ( "prune-tool" , "Failed to persist state" , { error : err . message } ) )
8381
8482 const toolMetadata = new Map < string , ToolParameterEntry > ( )
85- for ( const id of prunedIds ) {
83+ for ( const id of pruneToolIds ) {
8684 const toolParameters = state . toolParameters . get ( id )
8785 if ( toolParameters ) {
8886 toolMetadata . set ( id , toolParameters )
@@ -95,20 +93,17 @@ export function createPruningTool(
9593 client ,
9694 logger ,
9795 config ,
96+ state ,
9897 sessionId ,
99- prunedIds . length ,
100- tokensSaved ,
101- prunedIds ,
98+ pruneToolIds ,
10299 toolMetadata ,
103- null ,
104- state . stats ,
105100 reason as PruneReason ,
106101 currentAgent ,
107102 workingDirectory
108103 )
109104
110105 return formatPruningResultForTool (
111- prunedIds ,
106+ pruneToolIds ,
112107 toolMetadata ,
113108 workingDirectory
114109 )
0 commit comments