-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathhooks.ts
More file actions
73 lines (63 loc) · 1.96 KB
/
hooks.ts
File metadata and controls
73 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { SessionState, WithParts } from "./state"
import type { Logger } from "./logger"
import type { PluginConfig } from "./config"
import { syncToolCache } from "./state/tool-cache"
import { deduplicate } from "./strategies"
import { prune, insertPruneToolContext } from "./messages"
import { checkSession } from "./state"
import { runOnIdle } from "./strategies/on-idle"
export function createChatMessageTransformHandler(
client: any,
state: SessionState,
logger: Logger,
config: PluginConfig
) {
return async (
input: {},
output: { messages: WithParts[] }
) => {
await checkSession(client, state, logger, output.messages)
if (state.isSubAgent) {
return
}
syncToolCache(state, config, logger, output.messages);
deduplicate(state, logger, config, output.messages)
prune(state, logger, config, output.messages)
insertPruneToolContext(state, config, logger, output.messages)
}
}
export function createEventHandler(
client: any,
config: PluginConfig,
state: SessionState,
logger: Logger,
workingDirectory?: string
) {
return async (
{ event }: { event: any }
) => {
if (state.sessionId === null || state.isSubAgent) {
return
}
if (event.type === "session.status" && event.properties.status.type === "idle") {
if (!config.strategies.onIdle.enabled) {
return
}
if (state.lastToolPrune) {
logger.info("Skipping OnIdle pruning - last tool was prune")
return
}
try {
await runOnIdle(
client,
state,
logger,
config,
workingDirectory
)
} catch (err: any) {
logger.error("OnIdle pruning failed", { error: err.message })
}
}
}
}