|
1 | | -import type { SessionState, WithParts } from "./state" |
2 | | -import type { Logger } from "./logger" |
3 | | -import type { PluginConfig } from "./config" |
4 | | -import { syncToolCache } from "./state/tool-cache" |
5 | | -import { deduplicate, supersedeWrites } from "./strategies" |
6 | | -import { prune, insertPruneToolContext } from "./messages" |
7 | | -import { checkSession } from "./state" |
8 | | -import { runOnIdle } from "./strategies/on-idle" |
9 | | - |
| 1 | +import type { SessionState, WithParts } from "./state"; |
| 2 | +import type { Logger } from "./logger"; |
| 3 | +import type { PluginConfig } from "./config"; |
| 4 | +import { syncToolCache } from "./state/tool-cache"; |
| 5 | +import { deduplicate, supersedeWrites } from "./strategies"; |
| 6 | +import { prune, insertPruneToolContext } from "./messages"; |
| 7 | +import { checkSession } from "./state"; |
| 8 | +import { runOnIdle } from "./strategies/on-idle"; |
10 | 9 |
|
11 | 10 | export function createChatMessageTransformHandler( |
12 | | - client: any, |
13 | | - state: SessionState, |
14 | | - logger: Logger, |
15 | | - config: PluginConfig |
| 11 | + client: any, |
| 12 | + state: SessionState, |
| 13 | + logger: Logger, |
| 14 | + config: PluginConfig, |
16 | 15 | ) { |
17 | | - return async ( |
18 | | - input: {}, |
19 | | - output: { messages: WithParts[] } |
20 | | - ) => { |
21 | | - await checkSession(client, state, logger, output.messages) |
| 16 | + return async (input: {}, output: { messages: WithParts[] }) => { |
| 17 | + await checkSession(client, state, logger, output.messages); |
22 | 18 |
|
23 | | - if (state.isSubAgent) { |
24 | | - return |
25 | | - } |
| 19 | + if (state.isSubAgent) { |
| 20 | + return; |
| 21 | + } |
26 | 22 |
|
27 | | - syncToolCache(state, config, logger, output.messages); |
| 23 | + syncToolCache(state, config, logger, output.messages); |
28 | 24 |
|
29 | | - deduplicate(state, logger, config, output.messages) |
30 | | - supersedeWrites(state, logger, config, output.messages) |
| 25 | + deduplicate(state, logger, config, output.messages); |
| 26 | + supersedeWrites(state, logger, config, output.messages); |
31 | 27 |
|
32 | | - prune(state, logger, config, output.messages) |
| 28 | + prune(state, logger, config, output.messages); |
33 | 29 |
|
34 | | - insertPruneToolContext(state, config, logger, output.messages) |
35 | | - } |
| 30 | + insertPruneToolContext(state, config, logger, output.messages); |
| 31 | + }; |
36 | 32 | } |
37 | 33 |
|
38 | 34 | export function createEventHandler( |
39 | | - client: any, |
40 | | - config: PluginConfig, |
41 | | - state: SessionState, |
42 | | - logger: Logger, |
43 | | - workingDirectory?: string |
| 35 | + client: any, |
| 36 | + config: PluginConfig, |
| 37 | + state: SessionState, |
| 38 | + logger: Logger, |
| 39 | + workingDirectory?: string, |
| 40 | + onUserMessage?: () => void, |
44 | 41 | ) { |
45 | | - return async ( |
46 | | - { event }: { event: any } |
47 | | - ) => { |
48 | | - if (state.sessionId === null || state.isSubAgent) { |
49 | | - return |
50 | | - } |
| 42 | + return async ({ event }: { event: any }) => { |
| 43 | + if (state.sessionId === null || state.isSubAgent) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Reset auto-confirm on user message |
| 48 | + if ( |
| 49 | + event.type === "message.part.added" && |
| 50 | + event.properties?.part?.type === "text" |
| 51 | + ) { |
| 52 | + const role = event.properties?.role; |
| 53 | + if (role === "user" && onUserMessage) { |
| 54 | + onUserMessage(); |
| 55 | + } |
| 56 | + } |
51 | 57 |
|
52 | | - if (event.type === "session.status" && event.properties.status.type === "idle") { |
53 | | - if (!config.strategies.onIdle.enabled) { |
54 | | - return |
55 | | - } |
56 | | - if (state.lastToolPrune) { |
57 | | - logger.info("Skipping OnIdle pruning - last tool was prune") |
58 | | - return |
59 | | - } |
| 58 | + if ( |
| 59 | + event.type === "session.status" && |
| 60 | + event.properties.status.type === "idle" |
| 61 | + ) { |
| 62 | + if (!config.strategies.onIdle.enabled) { |
| 63 | + return; |
| 64 | + } |
| 65 | + if (state.lastToolPrune) { |
| 66 | + logger.info("Skipping OnIdle pruning - last tool was prune"); |
| 67 | + return; |
| 68 | + } |
60 | 69 |
|
61 | | - try { |
62 | | - await runOnIdle( |
63 | | - client, |
64 | | - state, |
65 | | - logger, |
66 | | - config, |
67 | | - workingDirectory |
68 | | - ) |
69 | | - } catch (err: any) { |
70 | | - logger.error("OnIdle pruning failed", { error: err.message }) |
71 | | - } |
72 | | - } |
| 70 | + try { |
| 71 | + await runOnIdle(client, state, logger, config, workingDirectory); |
| 72 | + } catch (err: any) { |
| 73 | + logger.error("OnIdle pruning failed", { error: err.message }); |
| 74 | + } |
73 | 75 | } |
| 76 | + }; |
74 | 77 | } |
0 commit comments