-
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathtool-cache.ts
More file actions
65 lines (57 loc) · 2.1 KB
/
tool-cache.ts
File metadata and controls
65 lines (57 loc) · 2.1 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
import type { SessionState, ToolStatus, WithParts } from "./index"
import type { Logger } from "../logger"
import { PluginConfig } from "../config"
const MAX_TOOL_CACHE_SIZE = 500
/**
* Sync tool parameters from OpenCode's session.messages() API.
* This is the single source of truth for tool parameters, replacing
* format-specific parsing from LLM API requests.
*/
export async function syncToolCache(
state: SessionState,
config: PluginConfig,
logger: Logger,
messages: WithParts[],
): Promise<void> {
try {
logger.info("Syncing tool parameters from OpenCode messages")
for (const msg of messages) {
for (const part of msg.parts) {
if (part.type !== "tool" || !part.callID || state.toolParameters.has(part.callID)) {
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,
}
)
if (!config.strategies.pruneTool.protectedTools.includes(part.tool)) {
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)
}
}