Skip to content

Commit 9d64c42

Browse files
committed
on-idle re-implemented
1 parent ebf8151 commit 9d64c42

File tree

5 files changed

+345
-5
lines changed

5 files changed

+345
-5
lines changed

index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Plugin } from "@opencode-ai/plugin"
22
import { getConfig } from "./lib/config"
33
import { Logger } from "./lib/logger"
44
import { createSessionState } from "./lib/state"
5-
import { createPruneTool } from "./lib/strategies/prune-tool"
5+
import { createPruneTool } from "./lib/strategies"
66
import { createChatMessageTransformHandler, createEventHandler } from "./lib/hooks"
77

88
const plugin: Plugin = (async (ctx) => {
@@ -54,7 +54,7 @@ const plugin: Plugin = (async (ctx) => {
5454
logger.info("Added 'prune' to experimental.primary_tools via config mutation")
5555
}
5656
},
57-
event: createEventHandler(ctx.client, config, state, logger),
57+
event: createEventHandler(ctx.client, config, state, logger, ctx.directory),
5858
}
5959
}) satisfies Plugin
6060

lib/hooks.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { syncToolCache } from "./state/tool-cache"
55
import { deduplicate } from "./strategies"
66
import { prune, insertPruneToolContext } from "./messages"
77
import { checkSession } from "./state"
8+
import { runOnIdle } from "./strategies/on-idle"
89

910

1011
export function createChatMessageTransformHandler(
@@ -36,7 +37,8 @@ export function createEventHandler(
3637
client: any,
3738
config: PluginConfig,
3839
state: SessionState,
39-
logger: Logger
40+
logger: Logger,
41+
workingDirectory?: string
4042
) {
4143
return async (
4244
{ event }: { event: any }
@@ -49,6 +51,22 @@ export function createEventHandler(
4951
if (!config.strategies.onIdle.enabled) {
5052
return
5153
}
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+
}
5270
}
5371
}
5472
}

lib/messages/prune.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const insertPruneToolContext = (
4040
logger: Logger,
4141
messages: WithParts[]
4242
): void => {
43+
if (!config.strategies.pruneTool.enabled) {
44+
return
45+
}
46+
4347
const lastUserMessage = getLastUserMessage(messages)
4448
if (!lastUserMessage || lastUserMessage.info.role !== 'user') {
4549
return
@@ -48,7 +52,7 @@ export const insertPruneToolContext = (
4852
const prunableToolsList = buildPrunableToolsList(state, config, logger, messages)
4953

5054
let nudgeString = ""
51-
if (config.strategies.pruneTool.nudge.enabled && state.nudgeCounter >= config.strategies.pruneTool.nudge.frequency) {
55+
if (state.nudgeCounter >= config.strategies.pruneTool.nudge.frequency) {
5256
logger.info("Inserting prune nudge message")
5357
nudgeString = "\n" + NUDGE_STRING
5458
}

lib/strategies/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export { deduplicate } from "./deduplication"
2-
2+
export { runOnIdle } from "./on-idle"
3+
export { createPruneTool } from "./prune-tool"

0 commit comments

Comments
 (0)