|
1 | 1 | import { tool } from "@opencode-ai/plugin" |
2 | 2 | import type { SessionState, ToolParameterEntry, WithParts } from "../state" |
3 | 3 | import type { PluginConfig } from "../config" |
4 | | -import { findCurrentAgent, buildToolIdList, getPruneToolIds } from "../messages/utils" |
| 4 | +import { findCurrentAgent, buildToolIdList } from "../messages/utils" |
5 | 5 | import { calculateTokensSaved } from "../utils" |
6 | 6 | import { PruneReason, sendUnifiedNotification } from "../ui/notification" |
7 | 7 | import { formatPruningResultForTool } from "../ui/display-utils" |
@@ -70,12 +70,23 @@ export function createPruneTool( |
70 | 70 |
|
71 | 71 | const currentAgent: string | undefined = findCurrentAgent(messages) |
72 | 72 | const toolIdList: string[] = buildToolIdList(messages) |
73 | | - const pruneToolIds: string[] = getPruneToolIds( |
74 | | - numericToolIds, |
75 | | - toolIdList, |
76 | | - state.toolParameters, |
77 | | - config.strategies.pruneTool.protectedTools |
78 | | - ) |
| 73 | + |
| 74 | + // Validate that all numeric IDs are within bounds |
| 75 | + const invalidIds = numericToolIds.filter(id => id < 0 || id >= toolIdList.length) |
| 76 | + if (invalidIds.length > 0) { |
| 77 | + return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list." |
| 78 | + } |
| 79 | + |
| 80 | + // Check for protected tools (model hallucinated an ID not in the prunable list) |
| 81 | + for (const index of numericToolIds) { |
| 82 | + const id = toolIdList[index] |
| 83 | + const metadata = state.toolParameters.get(id) |
| 84 | + if (metadata && config.strategies.pruneTool.protectedTools.includes(metadata.tool)) { |
| 85 | + return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list." |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + const pruneToolIds: string[] = numericToolIds.map(index => toolIdList[index]) |
79 | 90 | state.prune.toolIds.push(...pruneToolIds) |
80 | 91 |
|
81 | 92 | const toolMetadata = new Map<string, ToolParameterEntry>() |
|
0 commit comments