|
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,7 +70,22 @@ 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(numericToolIds, toolIdList) |
| 73 | + |
| 74 | + // Validate that all numeric IDs are within bounds |
| 75 | + if (numericToolIds.some(id => id < 0 || id >= toolIdList.length)) { |
| 76 | + return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list." |
| 77 | + } |
| 78 | + |
| 79 | + // Check for protected tools (model hallucinated an ID not in the prunable list) |
| 80 | + for (const index of numericToolIds) { |
| 81 | + const id = toolIdList[index] |
| 82 | + const metadata = state.toolParameters.get(id) |
| 83 | + if (metadata && config.strategies.pruneTool.protectedTools.includes(metadata.tool)) { |
| 84 | + return "Invalid IDs provided. Only use numeric IDs from the <prunable-tools> list." |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const pruneToolIds: string[] = numericToolIds.map(index => toolIdList[index]) |
74 | 89 | state.prune.toolIds.push(...pruneToolIds) |
75 | 90 |
|
76 | 91 | const toolMetadata = new Map<string, ToolParameterEntry>() |
|
0 commit comments