Skip to content

Commit fb3cb2c

Browse files
committed
Refactor: consolidate parameter extraction logic to eliminate duplication
- Export extractParameterKey from deduplicator module for reuse - Replace buildToolsSummary parameter extraction with shared extractParameterKey function - Remove 40+ lines of duplicate tool-specific extraction logic - Establish single source of truth for parameter extraction across deduplication and notifications - Improves consistency and maintainability - new tool support only requires one code change
1 parent 43add19 commit fb3cb2c

File tree

2 files changed

+15
-41
lines changed

2 files changed

+15
-41
lines changed

lib/deduplicator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function sortObjectKeys(obj: any): any {
134134
* INACTIVE Tools (exist but not registered, skip):
135135
* - multiedit, patch, lsp_diagnostics, lsp_hover
136136
*/
137-
function extractParameterKey(metadata: { tool: string, parameters?: any }): string {
137+
export function extractParameterKey(metadata: { tool: string, parameters?: any }): string {
138138
if (!metadata.parameters) return ''
139139

140140
const { tool, parameters } = metadata

lib/janitor.ts

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { StateManager } from "./state"
55
import { buildAnalysisPrompt } from "./prompt"
66
import { selectModel, extractModelFromSession } from "./model-selector"
77
import { estimateTokensBatch, formatTokenCount } from "./tokenizer"
8-
import { detectDuplicates } from "./deduplicator"
8+
import { detectDuplicates, extractParameterKey } from "./deduplicator"
99

1010
export class Janitor {
1111
constructor(
@@ -505,10 +505,17 @@ export class Janitor {
505505

506506
/**
507507
* Build a summary of tools by grouping them
508+
* Uses shared extractParameterKey logic for consistent parameter extraction
508509
*/
509510
private buildToolsSummary(prunedIds: string[], toolMetadata: Map<string, { tool: string, parameters?: any }>): Map<string, string[]> {
510511
const toolsSummary = new Map<string, string[]>()
511512

513+
// Helper function to truncate long strings
514+
const truncate = (str: string, maxLen: number = 60): string => {
515+
if (str.length <= maxLen) return str
516+
return str.slice(0, maxLen - 3) + '...'
517+
}
518+
512519
for (const prunedId of prunedIds) {
513520
const metadata = toolMetadata.get(prunedId)
514521
if (metadata) {
@@ -517,45 +524,12 @@ export class Janitor {
517524
toolsSummary.set(toolName, [])
518525
}
519526

520-
// Helper function to truncate long strings
521-
const truncate = (str: string, maxLen: number = 60): string => {
522-
if (str.length <= maxLen) return str
523-
return str.slice(0, maxLen - 3) + '...'
524-
}
525-
526-
// Extract meaningful parameter info based on tool type
527-
let paramInfo = ""
528-
if (metadata.parameters) {
529-
// For read tool, show filePath
530-
if (toolName === "read" && metadata.parameters.filePath) {
531-
paramInfo = truncate(this.shortenPath(metadata.parameters.filePath), 80)
532-
}
533-
// For list tool, show path
534-
else if (toolName === "list" && metadata.parameters.path) {
535-
paramInfo = truncate(this.shortenPath(metadata.parameters.path), 80)
536-
}
537-
// For bash/command tools, prefer description over command
538-
else if (toolName === "bash") {
539-
if (metadata.parameters.description) {
540-
paramInfo = truncate(metadata.parameters.description, 80)
541-
} else if (metadata.parameters.command) {
542-
paramInfo = truncate(metadata.parameters.command, 80)
543-
}
544-
}
545-
// For other tools, show the first relevant parameter
546-
else if (metadata.parameters.path) {
547-
paramInfo = truncate(this.shortenPath(metadata.parameters.path), 80)
548-
}
549-
else if (metadata.parameters.pattern) {
550-
paramInfo = truncate(metadata.parameters.pattern, 80)
551-
}
552-
else if (metadata.parameters.command) {
553-
paramInfo = truncate(metadata.parameters.command, 80)
554-
}
555-
}
556-
557-
if (paramInfo) {
558-
toolsSummary.get(toolName)!.push(paramInfo)
527+
// Use shared parameter extraction logic
528+
const paramKey = extractParameterKey(metadata)
529+
if (paramKey) {
530+
// Apply path shortening and truncation for display
531+
const displayKey = truncate(this.shortenPath(paramKey), 80)
532+
toolsSummary.get(toolName)!.push(displayKey)
559533
}
560534
}
561535
}

0 commit comments

Comments
 (0)