Skip to content

Commit 7c90034

Browse files
committed
sync
1 parent 0914d82 commit 7c90034

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

lib/hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export function createChatMessageTransformHandler(
1818
output: { messages: WithParts[] }
1919
) => {
2020
checkSession(state, logger, output.messages);
21-
syncToolCache(state, logger, output.messages);
21+
syncToolCache(state, config, logger, output.messages);
22+
2223

2324
deduplicate(state, logger, config, output.messages)
2425

lib/messages/prune.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import type { Logger } from "../logger"
33
import type { PluginConfig } from "../config"
44
import { buildToolIdList } from "../utils"
55
import { getLastUserMessage, extractParameterKey } from "./utils"
6+
import { loadPrompt } from "../prompt"
67

78
const PRUNED_TOOL_OUTPUT_REPLACEMENT = '[Output removed to save context - information superseded or no longer needed]'
9+
const NUDGE_STRING = loadPrompt("nudge")
810

911
const buildPrunableToolsList = (
1012
state: SessionState,
@@ -45,6 +47,12 @@ export const insertPruneToolContext = (
4547

4648
const prunableToolsList = buildPrunableToolsList(state, config, logger, messages)
4749

50+
let nudgeString = ""
51+
if (state.nudgeCounter >= config.strategies.pruneTool.nudgeFrequency) {
52+
logger.info("Inserting prune nudge message")
53+
nudgeString = "\n" + NUDGE_STRING
54+
}
55+
4856
const userMessage: WithParts = {
4957
info: {
5058
id: "msg_01234567890123456789012345",
@@ -63,7 +71,7 @@ export const insertPruneToolContext = (
6371
sessionID: lastUserMessage.info.sessionID,
6472
messageID: "msg_01234567890123456789012345",
6573
type: "text",
66-
text: prunableToolsList,
74+
text: prunableToolsList + nudgeString,
6775
}
6876
]
6977
}

lib/state/state.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ export function createSessionState(): SessionState {
3838
pruneTokenCounter: 0,
3939
totalPruneTokens: 0,
4040
},
41-
toolParameters: new Map<string, ToolParameterEntry>()
41+
toolParameters: new Map<string, ToolParameterEntry>(),
42+
nudgeCounter: 0
4243
}
4344
}
4445

@@ -52,6 +53,7 @@ export function resetSessionState(state: SessionState): void {
5253
totalPruneTokens: 0,
5354
}
5455
state.toolParameters.clear()
56+
state.nudgeCounter = 0
5557
}
5658

5759
export async function ensureSessionInitialized(

lib/state/tool-cache.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SessionState, ToolStatus, WithParts } from "./index"
22
import type { Logger } from "../logger"
3+
import { PluginConfig } from "../config"
34

45
const MAX_TOOL_CACHE_SIZE = 500
56

@@ -10,6 +11,7 @@ const MAX_TOOL_CACHE_SIZE = 500
1011
*/
1112
export async function syncToolCache(
1213
state: SessionState,
14+
config: PluginConfig,
1315
logger: Logger,
1416
messages: WithParts[],
1517
): Promise<void> {
@@ -30,6 +32,10 @@ export async function syncToolCache(
3032
error: part.state.status === "error" ? part.state.error : undefined,
3133
}
3234
)
35+
36+
if (!config.strategies.pruneTool.protectedTools.includes(part.tool)) {
37+
state.nudgeCounter++
38+
}
3339
}
3440
}
3541

lib/state/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ export interface SessionState {
2828
prune: Prune
2929
stats: SessionStats
3030
toolParameters: Map<string, ToolParameterEntry>
31+
nudgeCounter: number
3132
}

lib/strategies/prune-tool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export function createPruneTool(
102102
)
103103
state.stats.totalPruneTokens += state.stats.pruneTokenCounter
104104
state.stats.pruneTokenCounter = 0
105+
state.nudgeCounter = 0
105106

106107
return formatPruningResultForTool(
107108
pruneToolIds,

0 commit comments

Comments
 (0)