@@ -5,6 +5,7 @@ import { syncToolCache } from "./state/tool-cache"
55import { deduplicate } from "./strategies"
66import { prune , insertPruneToolContext } from "./messages"
77import { checkSession } from "./state"
8+ import { runOnIdle } from "./strategies/on-idle"
89
910
1011export function createChatMessageTransformHandler (
@@ -24,11 +25,48 @@ export function createChatMessageTransformHandler(
2425
2526 syncToolCache ( state , config , logger , output . messages ) ;
2627
27-
28- deduplicate ( client , state , logger , config , output . messages )
28+ deduplicate ( state , logger , config , output . messages )
2929
3030 prune ( state , logger , config , output . messages )
3131
3232 insertPruneToolContext ( state , config , logger , output . messages )
3333 }
3434}
35+
36+ export function createEventHandler (
37+ client : any ,
38+ config : PluginConfig ,
39+ state : SessionState ,
40+ logger : Logger ,
41+ workingDirectory ?: string
42+ ) {
43+ return async (
44+ { event } : { event : any }
45+ ) => {
46+ if ( state . sessionId === null || state . isSubAgent ) {
47+ return
48+ }
49+
50+ if ( event . type === "session.status" && event . properties . status . type === "idle" ) {
51+ if ( ! config . strategies . onIdle . enabled ) {
52+ return
53+ }
54+ if ( state . lastToolPrune ) {
55+ logger . info ( "Skipping OnIdle pruning - last tool was prune" )
56+ return
57+ }
58+
59+ try {
60+ await runOnIdle (
61+ client ,
62+ state ,
63+ logger ,
64+ config ,
65+ workingDirectory
66+ )
67+ } catch ( err : any ) {
68+ logger . error ( "OnIdle pruning failed" , { error : err . message } )
69+ }
70+ }
71+ }
72+ }
0 commit comments