-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathtool-cache.ts
More file actions
105 lines (88 loc) · 3.47 KB
/
tool-cache.ts
File metadata and controls
105 lines (88 loc) · 3.47 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { SessionState, ToolStatus, WithParts } from "./index"
import type { Logger } from "../logger"
import { PluginConfig } from "../config"
import { isMessageCompacted } from "../shared-utils"
const MAX_TOOL_CACHE_SIZE = 1000
/**
* Sync tool parameters from OpenCode's session.messages() API.
*/
export async function syncToolCache(
state: SessionState,
config: PluginConfig,
logger: Logger,
messages: WithParts[],
): Promise<void> {
try {
logger.info("Syncing tool parameters from OpenCode messages")
state.nudgeCounter = 0
let turnCounter = 0
for (const msg of messages) {
if (isMessageCompacted(state, msg)) {
continue
}
const parts = Array.isArray(msg.parts) ? msg.parts : []
for (const part of parts) {
if (part.type === "step-start") {
turnCounter++
continue
}
if (part.type !== "tool" || !part.callID) {
continue
}
const turnProtectionEnabled = config.turnProtection.enabled
const turnProtectionTurns = config.turnProtection.turns
const isProtectedByTurn =
turnProtectionEnabled &&
turnProtectionTurns > 0 &&
state.currentTurn - turnCounter < turnProtectionTurns
state.lastToolPrune =
(part.tool === "discard" || part.tool === "extract") &&
part.state.status === "completed"
const allProtectedTools = config.tools.settings.protectedTools
if (part.tool === "discard" || part.tool === "extract") {
state.nudgeCounter = 0
} else if (!allProtectedTools.includes(part.tool) && !isProtectedByTurn) {
state.nudgeCounter++
}
if (state.toolParameters.has(part.callID)) {
continue
}
if (isProtectedByTurn) {
continue
}
state.toolParameters.set(part.callID, {
tool: part.tool,
parameters: part.state?.input ?? {},
status: part.state.status as ToolStatus | undefined,
error: part.state.status === "error" ? part.state.error : undefined,
turn: turnCounter,
})
logger.info(`Cached tool id: ${part.callID} (created on turn ${turnCounter})`)
}
}
logger.info(
`Synced cache - size: ${state.toolParameters.size}, currentTurn: ${state.currentTurn}, nudgeCounter: ${state.nudgeCounter}`,
)
trimToolParametersCache(state)
} catch (error) {
logger.warn("Failed to sync tool parameters from OpenCode", {
error: error instanceof Error ? error.message : String(error),
})
}
}
/**
* Trim the tool parameters cache to prevent unbounded memory growth.
* Uses FIFO eviction - removes oldest entries first.
*/
export function trimToolParametersCache(state: SessionState): void {
if (state.toolParameters.size <= MAX_TOOL_CACHE_SIZE) {
return
}
const keysToRemove = Array.from(state.toolParameters.keys()).slice(
0,
state.toolParameters.size - MAX_TOOL_CACHE_SIZE,
)
for (const key of keysToRemove) {
state.toolParameters.delete(key)
}
}