MCPcopy Index your code
hub / github.com/Opencode-DCP/opencode-dynamic-context-pruning / syncToolCache

Function syncToolCache

lib/state/tool-cache.ts:12–79  ·  view source on GitHub ↗
(
    state: SessionState,
    config: PluginConfig,
    logger: Logger,
    messages: WithParts[],
)

Source from the content-addressed store, hash-verified

10 * Sync tool parameters from session messages.
11 */
12export function syncToolCache(
13 state: SessionState,
14 config: PluginConfig,
15 logger: Logger,
16 messages: WithParts[],
17): void {
18 try {
19 logger.info("Syncing tool parameters from OpenCode messages")
20
21 let turnCounter = 0
22
23 for (const msg of messages) {
24 if (isMessageCompacted(state, msg)) {
25 continue
26 }
27
28 const parts = Array.isArray(msg.parts) ? msg.parts : []
29 for (const part of parts) {
30 if (part.type === "step-start") {
31 turnCounter++
32 continue
33 }
34
35 if (part.type !== "tool" || !part.callID) {
36 continue
37 }
38
39 const turnProtectionEnabled = config.turnProtection.enabled
40 const turnProtectionTurns = config.turnProtection.turns
41 const isProtectedByTurn =
42 turnProtectionEnabled &&
43 turnProtectionTurns > 0 &&
44 state.currentTurn - turnCounter < turnProtectionTurns
45
46 if (state.toolParameters.has(part.callID)) {
47 continue
48 }
49
50 if (isProtectedByTurn) {
51 continue
52 }
53
54 const tokenCount = countToolTokens(part)
55
56 state.toolParameters.set(part.callID, {
57 tool: part.tool,
58 parameters: part.state?.input ?? {},
59 status: part.state.status as ToolStatus | undefined,
60 error: part.state.status === "error" ? part.state.error : undefined,
61 turn: turnCounter,
62 tokenCount,
63 })
64 logger.info(
65 `Cached tool id: ${part.callID} (turn ${turnCounter}${tokenCount !== undefined ? `, ${tokenCount} tokens` : ""})`,
66 )
67 }
68 }
69

Callers 2

handleSweepCommandFunction · 0.90

Calls 5

isMessageCompactedFunction · 0.90
countToolTokensFunction · 0.90
trimToolParametersCacheFunction · 0.85
infoMethod · 0.80
warnMethod · 0.80

Tested by

no test coverage detected