|
| 1 | +import { extractParameterKey } from "../../ui/display-utils" |
| 2 | +import type { PruningStrategy, StrategyResult, ToolMetadata } from "./types" |
| 3 | + |
| 4 | +/** |
| 5 | + * Deduplication strategy - prunes older tool calls that have identical |
| 6 | + * tool name and parameters, keeping only the most recent occurrence. |
| 7 | + */ |
| 8 | +export const deduplicationStrategy: PruningStrategy = { |
| 9 | + name: "deduplication", |
| 10 | + |
| 11 | + detect( |
| 12 | + toolMetadata: Map<string, ToolMetadata>, |
| 13 | + unprunedIds: string[], |
| 14 | + protectedTools: string[] |
| 15 | + ): StrategyResult { |
| 16 | + const signatureMap = new Map<string, string[]>() |
| 17 | + |
| 18 | + const deduplicatableIds = unprunedIds.filter(id => { |
| 19 | + const metadata = toolMetadata.get(id) |
| 20 | + return !metadata || !protectedTools.includes(metadata.tool) |
| 21 | + }) |
| 22 | + |
| 23 | + for (const id of deduplicatableIds) { |
| 24 | + const metadata = toolMetadata.get(id) |
| 25 | + if (!metadata) continue |
| 26 | + |
| 27 | + const signature = createToolSignature(metadata.tool, metadata.parameters) |
| 28 | + if (!signatureMap.has(signature)) { |
| 29 | + signatureMap.set(signature, []) |
| 30 | + } |
| 31 | + signatureMap.get(signature)!.push(id) |
| 32 | + } |
| 33 | + |
| 34 | + const prunedIds: string[] = [] |
| 35 | + const details = new Map() |
| 36 | + |
| 37 | + for (const [signature, ids] of signatureMap.entries()) { |
| 38 | + if (ids.length > 1) { |
| 39 | + const metadata = toolMetadata.get(ids[0])! |
| 40 | + const idsToRemove = ids.slice(0, -1) // All except last |
| 41 | + prunedIds.push(...idsToRemove) |
| 42 | + |
| 43 | + details.set(signature, { |
| 44 | + toolName: metadata.tool, |
| 45 | + parameterKey: extractParameterKey(metadata), |
| 46 | + reason: `duplicate (${ids.length} occurrences, kept most recent)`, |
| 47 | + duplicateCount: ids.length, |
| 48 | + prunedIds: idsToRemove, |
| 49 | + keptId: ids[ids.length - 1] |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return { prunedIds, details } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function createToolSignature(tool: string, parameters?: any): string { |
| 59 | + if (!parameters) return tool |
| 60 | + |
| 61 | + const normalized = normalizeParameters(parameters) |
| 62 | + const sorted = sortObjectKeys(normalized) |
| 63 | + return `${tool}::${JSON.stringify(sorted)}` |
| 64 | +} |
| 65 | + |
| 66 | +function normalizeParameters(params: any): any { |
| 67 | + if (typeof params !== 'object' || params === null) return params |
| 68 | + if (Array.isArray(params)) return params |
| 69 | + |
| 70 | + const normalized: any = {} |
| 71 | + for (const [key, value] of Object.entries(params)) { |
| 72 | + if (value !== undefined && value !== null) { |
| 73 | + normalized[key] = value |
| 74 | + } |
| 75 | + } |
| 76 | + return normalized |
| 77 | +} |
| 78 | + |
| 79 | +function sortObjectKeys(obj: any): any { |
| 80 | + if (typeof obj !== 'object' || obj === null) return obj |
| 81 | + if (Array.isArray(obj)) return obj.map(sortObjectKeys) |
| 82 | + |
| 83 | + const sorted: any = {} |
| 84 | + for (const key of Object.keys(obj).sort()) { |
| 85 | + sorted[key] = sortObjectKeys(obj[key]) |
| 86 | + } |
| 87 | + return sorted |
| 88 | +} |
0 commit comments