@tarquinen/opencode-dcp 0.2.8 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ import { z } from "zod";
3
3
  import { buildAnalysisPrompt } from "./prompt";
4
4
  import { selectModel, extractModelFromSession } from "./model-selector";
5
5
  import { estimateTokensBatch, formatTokenCount } from "./tokenizer";
6
+ import { detectDuplicates, extractParameterKey } from "./deduplicator";
6
7
  export class Janitor {
7
8
  client;
8
9
  stateManager;
@@ -12,8 +13,10 @@ export class Janitor {
12
13
  modelCache;
13
14
  configModel;
14
15
  showModelErrorToasts;
16
+ pruningMode;
15
17
  constructor(client, stateManager, logger, toolParametersCache, protectedTools, modelCache, configModel, // Format: "provider/model"
16
- showModelErrorToasts = true // Whether to show toast for model errors
18
+ showModelErrorToasts = true, // Whether to show toast for model errors
19
+ pruningMode = "smart" // Pruning strategy
17
20
  ) {
18
21
  this.client = client;
19
22
  this.stateManager = stateManager;
@@ -23,6 +26,7 @@ export class Janitor {
23
26
  this.modelCache = modelCache;
24
27
  this.configModel = configModel;
25
28
  this.showModelErrorToasts = showModelErrorToasts;
29
+ this.pruningMode = pruningMode;
26
30
  }
27
31
  /**
28
32
  * Sends an ignored message to the session UI (user sees it, AI doesn't)
@@ -174,113 +178,178 @@ export class Janitor {
174
178
  alreadyPrunedIds: alreadyPrunedIds.slice(0, 5), // Show first 5 for brevity
175
179
  unprunedCount: unprunedToolCallIds.length
176
180
  });
177
- // Filter out protected tools from being considered for pruning
178
- const protectedToolCallIds = [];
179
- const prunableToolCallIds = unprunedToolCallIds.filter(id => {
180
- const metadata = toolMetadata.get(id);
181
- if (metadata && this.protectedTools.includes(metadata.tool)) {
182
- protectedToolCallIds.push(id);
183
- return false;
184
- }
185
- return true;
186
- });
187
- if (protectedToolCallIds.length > 0) {
188
- this.logger.debug("janitor", "Protected tools excluded from pruning", {
189
- sessionID,
190
- protectedCount: protectedToolCallIds.length,
191
- protectedTools: protectedToolCallIds.map(id => {
192
- const metadata = toolMetadata.get(id);
193
- return { id, tool: metadata?.tool };
194
- })
195
- });
196
- }
197
181
  // If there are no unpruned tool calls, skip analysis
198
- if (prunableToolCallIds.length === 0) {
199
- this.logger.debug("janitor", "No prunable tool calls found, skipping analysis", {
200
- sessionID,
201
- protectedCount: protectedToolCallIds.length
182
+ if (unprunedToolCallIds.length === 0) {
183
+ this.logger.debug("janitor", "No unpruned tool calls found, skipping analysis", {
184
+ sessionID
202
185
  });
203
186
  return;
204
187
  }
205
- // Select appropriate model with intelligent fallback
206
- // Try to get model from cache first, otherwise extractModelFromSession won't find it
207
- const cachedModelInfo = this.modelCache.get(sessionID);
208
- const sessionModelInfo = extractModelFromSession(sessionInfo, this.logger);
209
- const currentModelInfo = cachedModelInfo || sessionModelInfo;
210
- if (cachedModelInfo) {
211
- this.logger.debug("janitor", "Using cached model info", {
212
- sessionID,
213
- providerID: cachedModelInfo.providerID,
214
- modelID: cachedModelInfo.modelID
215
- });
216
- }
217
- const modelSelection = await selectModel(currentModelInfo, this.logger, this.configModel);
218
- this.logger.info("janitor", "Model selected for analysis", {
188
+ // ============================================================
189
+ // PHASE 1: DUPLICATE DETECTION (runs for both modes)
190
+ // ============================================================
191
+ const dedupeResult = detectDuplicates(toolMetadata, unprunedToolCallIds, this.protectedTools);
192
+ const deduplicatedIds = dedupeResult.duplicateIds;
193
+ const deduplicationDetails = dedupeResult.deduplicationDetails;
194
+ this.logger.info("janitor", "Duplicate detection complete", {
219
195
  sessionID,
220
- modelInfo: modelSelection.modelInfo,
221
- source: modelSelection.source,
222
- reason: modelSelection.reason
196
+ duplicatesFound: deduplicatedIds.length,
197
+ uniqueToolPatterns: deduplicationDetails.size
223
198
  });
224
- // Show toast if we had to fallback from a failed model
225
- if (modelSelection.failedModel && this.showModelErrorToasts) {
226
- try {
227
- await this.client.tui.showToast({
228
- body: {
229
- title: "DCP: Model fallback",
230
- message: `${modelSelection.failedModel.providerID}/${modelSelection.failedModel.modelID} failed\nUsing ${modelSelection.modelInfo.providerID}/${modelSelection.modelInfo.modelID}`,
231
- variant: "info",
232
- duration: 5000
199
+ // ============================================================
200
+ // PHASE 2: LLM ANALYSIS (only runs in "smart" mode)
201
+ // ============================================================
202
+ let llmPrunedIds = [];
203
+ if (this.pruningMode === "smart") {
204
+ // Filter out duplicates and protected tools
205
+ const protectedToolCallIds = [];
206
+ const prunableToolCallIds = unprunedToolCallIds.filter(id => {
207
+ // Skip already deduplicated
208
+ if (deduplicatedIds.includes(id))
209
+ return false;
210
+ // Skip protected tools
211
+ const metadata = toolMetadata.get(id);
212
+ if (metadata && this.protectedTools.includes(metadata.tool)) {
213
+ protectedToolCallIds.push(id);
214
+ return false;
215
+ }
216
+ return true;
217
+ });
218
+ if (protectedToolCallIds.length > 0) {
219
+ this.logger.debug("janitor", "Protected tools excluded from pruning", {
220
+ sessionID,
221
+ protectedCount: protectedToolCallIds.length,
222
+ protectedTools: protectedToolCallIds.map(id => {
223
+ const metadata = toolMetadata.get(id);
224
+ return { id, tool: metadata?.tool };
225
+ })
226
+ });
227
+ }
228
+ // Run LLM analysis only if there are prunable tools
229
+ if (prunableToolCallIds.length > 0) {
230
+ this.logger.info("janitor", "Starting LLM analysis", {
231
+ sessionID,
232
+ candidateCount: prunableToolCallIds.length
233
+ });
234
+ // Select appropriate model with intelligent fallback
235
+ const cachedModelInfo = this.modelCache.get(sessionID);
236
+ const sessionModelInfo = extractModelFromSession(sessionInfo, this.logger);
237
+ const currentModelInfo = cachedModelInfo || sessionModelInfo;
238
+ if (cachedModelInfo) {
239
+ this.logger.debug("janitor", "Using cached model info", {
240
+ sessionID,
241
+ providerID: cachedModelInfo.providerID,
242
+ modelID: cachedModelInfo.modelID
243
+ });
244
+ }
245
+ const modelSelection = await selectModel(currentModelInfo, this.logger, this.configModel);
246
+ this.logger.info("janitor", "Model selected for analysis", {
247
+ sessionID,
248
+ modelInfo: modelSelection.modelInfo,
249
+ source: modelSelection.source,
250
+ reason: modelSelection.reason
251
+ });
252
+ // Show toast if we had to fallback from a failed model
253
+ if (modelSelection.failedModel && this.showModelErrorToasts) {
254
+ try {
255
+ await this.client.tui.showToast({
256
+ body: {
257
+ title: "DCP: Model fallback",
258
+ message: `${modelSelection.failedModel.providerID}/${modelSelection.failedModel.modelID} failed\nUsing ${modelSelection.modelInfo.providerID}/${modelSelection.modelInfo.modelID}`,
259
+ variant: "info",
260
+ duration: 5000
261
+ }
262
+ });
263
+ this.logger.info("janitor", "Toast notification shown for model fallback", {
264
+ failedModel: modelSelection.failedModel,
265
+ selectedModel: modelSelection.modelInfo
266
+ });
233
267
  }
268
+ catch (toastError) {
269
+ this.logger.error("janitor", "Failed to show toast notification", {
270
+ error: toastError.message
271
+ });
272
+ // Don't fail the whole operation if toast fails
273
+ }
274
+ }
275
+ else if (modelSelection.failedModel && !this.showModelErrorToasts) {
276
+ this.logger.info("janitor", "Model fallback occurred but toast disabled by config", {
277
+ failedModel: modelSelection.failedModel,
278
+ selectedModel: modelSelection.modelInfo
279
+ });
280
+ }
281
+ // Log comprehensive stats before AI call
282
+ this.logger.info("janitor", "Preparing AI analysis", {
283
+ sessionID,
284
+ totalToolCallsInSession: toolCallIds.length,
285
+ alreadyPrunedCount: alreadyPrunedIds.length,
286
+ deduplicatedCount: deduplicatedIds.length,
287
+ protectedToolsCount: protectedToolCallIds.length,
288
+ candidatesForPruning: prunableToolCallIds.length,
289
+ candidateTools: prunableToolCallIds.map(id => {
290
+ const meta = toolMetadata.get(id);
291
+ return meta ? `${meta.tool}[${id.substring(0, 12)}...]` : id.substring(0, 12) + '...';
292
+ }).slice(0, 10), // Show first 10 for brevity
293
+ batchToolCount: batchToolChildren.size,
294
+ batchDetails: Array.from(batchToolChildren.entries()).map(([batchId, children]) => ({
295
+ batchId: batchId.substring(0, 20) + '...',
296
+ childCount: children.length
297
+ }))
234
298
  });
235
- this.logger.info("janitor", "Toast notification shown for model fallback", {
236
- failedModel: modelSelection.failedModel,
237
- selectedModel: modelSelection.modelInfo
299
+ this.logger.debug("janitor", "Starting shadow inference", { sessionID });
300
+ // Analyze which tool calls are obsolete
301
+ const result = await generateObject({
302
+ model: modelSelection.model,
303
+ schema: z.object({
304
+ pruned_tool_call_ids: z.array(z.string()),
305
+ reasoning: z.string(),
306
+ }),
307
+ prompt: buildAnalysisPrompt(prunableToolCallIds, messages, this.protectedTools)
308
+ });
309
+ // Filter LLM results to only include IDs that were actually candidates
310
+ // (LLM sometimes returns duplicate IDs that were already filtered out)
311
+ const rawLlmPrunedIds = result.object.pruned_tool_call_ids;
312
+ llmPrunedIds = rawLlmPrunedIds.filter(id => prunableToolCallIds.includes(id.toLowerCase()));
313
+ if (rawLlmPrunedIds.length !== llmPrunedIds.length) {
314
+ this.logger.warn("janitor", "LLM returned non-candidate IDs (filtered out)", {
315
+ sessionID,
316
+ rawCount: rawLlmPrunedIds.length,
317
+ filteredCount: llmPrunedIds.length,
318
+ invalidIds: rawLlmPrunedIds.filter(id => !prunableToolCallIds.includes(id.toLowerCase()))
319
+ });
320
+ }
321
+ this.logger.info("janitor", "LLM analysis complete", {
322
+ sessionID,
323
+ llmPrunedCount: llmPrunedIds.length,
324
+ reasoning: result.object.reasoning
238
325
  });
239
326
  }
240
- catch (toastError) {
241
- this.logger.error("janitor", "Failed to show toast notification", {
242
- error: toastError.message
327
+ else {
328
+ this.logger.info("janitor", "No prunable tools for LLM analysis", {
329
+ sessionID,
330
+ deduplicatedCount: deduplicatedIds.length,
331
+ protectedCount: protectedToolCallIds.length
243
332
  });
244
- // Don't fail the whole operation if toast fails
245
333
  }
246
334
  }
247
- else if (modelSelection.failedModel && !this.showModelErrorToasts) {
248
- this.logger.info("janitor", "Model fallback occurred but toast disabled by config", {
249
- failedModel: modelSelection.failedModel,
250
- selectedModel: modelSelection.modelInfo
335
+ else {
336
+ this.logger.info("janitor", "Skipping LLM analysis (auto mode)", {
337
+ sessionID,
338
+ deduplicatedCount: deduplicatedIds.length
251
339
  });
252
340
  }
253
- // Log comprehensive stats before AI call
254
- this.logger.info("janitor", "Preparing AI analysis", {
255
- sessionID,
256
- totalToolCallsInSession: toolCallIds.length,
257
- alreadyPrunedCount: alreadyPrunedIds.length,
258
- protectedToolsCount: protectedToolCallIds.length,
259
- candidatesForPruning: prunableToolCallIds.length,
260
- candidateTools: prunableToolCallIds.map(id => {
261
- const meta = toolMetadata.get(id);
262
- return meta ? `${meta.tool}[${id.substring(0, 12)}...]` : id.substring(0, 12) + '...';
263
- }).slice(0, 10), // Show first 10 for brevity
264
- batchToolCount: batchToolChildren.size,
265
- batchDetails: Array.from(batchToolChildren.entries()).map(([batchId, children]) => ({
266
- batchId: batchId.substring(0, 20) + '...',
267
- childCount: children.length
268
- }))
269
- });
270
- this.logger.debug("janitor", "Starting shadow inference", { sessionID });
271
- // Analyze which tool calls are obsolete
272
- const result = await generateObject({
273
- model: modelSelection.model,
274
- schema: z.object({
275
- pruned_tool_call_ids: z.array(z.string()),
276
- reasoning: z.string(),
277
- }),
278
- prompt: buildAnalysisPrompt(prunableToolCallIds, messages, this.protectedTools)
279
- });
341
+ // If mode is "auto", llmPrunedIds stays empty
342
+ // ============================================================
343
+ // PHASE 3: COMBINE & EXPAND
344
+ // ============================================================
345
+ const newlyPrunedIds = [...deduplicatedIds, ...llmPrunedIds];
346
+ if (newlyPrunedIds.length === 0) {
347
+ this.logger.info("janitor", "No tools to prune", { sessionID });
348
+ return;
349
+ }
280
350
  // Expand batch tool IDs to include their children
281
- // Note: IDs are already normalized to lowercase when collected from messages
282
351
  const expandedPrunedIds = new Set();
283
- for (const prunedId of result.object.pruned_tool_call_ids) {
352
+ for (const prunedId of newlyPrunedIds) {
284
353
  const normalizedId = prunedId.toLowerCase();
285
354
  expandedPrunedIds.add(normalizedId);
286
355
  // If this is a batch tool, add all its children
@@ -296,15 +365,15 @@ export class Janitor {
296
365
  }
297
366
  }
298
367
  // Calculate which IDs are actually NEW (not already pruned)
299
- const newlyPrunedIds = Array.from(expandedPrunedIds).filter(id => !alreadyPrunedIds.includes(id));
368
+ const finalNewlyPrunedIds = Array.from(expandedPrunedIds).filter(id => !alreadyPrunedIds.includes(id));
300
369
  // finalPrunedIds includes everything (new + already pruned) for logging
301
370
  const finalPrunedIds = Array.from(expandedPrunedIds);
302
371
  this.logger.info("janitor", "Analysis complete", {
303
372
  sessionID,
304
373
  prunedCount: finalPrunedIds.length,
305
- originalPrunedCount: result.object.pruned_tool_call_ids.length,
306
- prunedIds: finalPrunedIds,
307
- reasoning: result.object.reasoning
374
+ deduplicatedCount: deduplicatedIds.length,
375
+ llmPrunedCount: llmPrunedIds.length,
376
+ prunedIds: finalPrunedIds
308
377
  });
309
378
  this.logger.debug("janitor", "Pruning ID details", {
310
379
  sessionID,
@@ -312,158 +381,29 @@ export class Janitor {
312
381
  alreadyPrunedIds: alreadyPrunedIds,
313
382
  finalPrunedCount: finalPrunedIds.length,
314
383
  finalPrunedIds: finalPrunedIds,
315
- newlyPrunedCount: newlyPrunedIds.length,
316
- newlyPrunedIds: newlyPrunedIds
384
+ newlyPrunedCount: finalNewlyPrunedIds.length,
385
+ newlyPrunedIds: finalNewlyPrunedIds
317
386
  });
318
- // Calculate token savings from newly pruned tool outputs
319
- // Use accurate tokenization with gpt-tokenizer
320
- let totalTokensSaved = 0;
321
- const outputsToTokenize = [];
322
- for (const prunedId of newlyPrunedIds) {
323
- const output = toolOutputs.get(prunedId);
324
- if (output) {
325
- outputsToTokenize.push(output);
326
- }
387
+ // ============================================================
388
+ // PHASE 4: NOTIFICATION
389
+ // ============================================================
390
+ if (this.pruningMode === "auto") {
391
+ await this.sendAutoModeNotification(sessionID, deduplicatedIds, deduplicationDetails, toolMetadata, toolOutputs);
327
392
  }
328
- if (outputsToTokenize.length > 0) {
329
- // Use batch tokenization for efficiency
330
- const tokenCounts = estimateTokensBatch(outputsToTokenize, this.logger);
331
- totalTokensSaved = tokenCounts.reduce((sum, count) => sum + count, 0);
332
- this.logger.debug("janitor", "Token estimation complete", {
333
- sessionID,
334
- outputCount: outputsToTokenize.length,
335
- totalTokens: totalTokensSaved,
336
- avgTokensPerOutput: Math.round(totalTokensSaved / outputsToTokenize.length)
337
- });
393
+ else {
394
+ await this.sendSmartModeNotification(sessionID, deduplicatedIds, deduplicationDetails, llmPrunedIds, toolMetadata, toolOutputs);
338
395
  }
339
- const estimatedTokensSaved = totalTokensSaved;
396
+ // ============================================================
397
+ // PHASE 5: STATE UPDATE
398
+ // ============================================================
340
399
  // Merge newly pruned IDs with existing ones (using expanded IDs)
341
400
  const allPrunedIds = [...new Set([...alreadyPrunedIds, ...finalPrunedIds])];
342
401
  await this.stateManager.set(sessionID, allPrunedIds);
343
402
  this.logger.debug("janitor", "Updated state manager", {
344
403
  sessionID,
345
404
  totalPrunedCount: allPrunedIds.length,
346
- newlyPrunedCount: newlyPrunedIds.length
405
+ newlyPrunedCount: finalNewlyPrunedIds.length
347
406
  });
348
- // Show toast notification if we pruned anything NEW
349
- if (newlyPrunedIds.length > 0) {
350
- try {
351
- // Helper function to shorten paths for display
352
- const shortenPath = (path) => {
353
- // Replace home directory with ~
354
- const homeDir = require('os').homedir();
355
- if (path.startsWith(homeDir)) {
356
- path = '~' + path.slice(homeDir.length);
357
- }
358
- // Shorten node_modules paths: show package + file only
359
- const nodeModulesMatch = path.match(/node_modules\/(@[^\/]+\/[^\/]+|[^\/]+)\/(.*)/);
360
- if (nodeModulesMatch) {
361
- return `${nodeModulesMatch[1]}/${nodeModulesMatch[2]}`;
362
- }
363
- return path;
364
- };
365
- // Helper function to truncate long strings
366
- const truncate = (str, maxLen = 60) => {
367
- if (str.length <= maxLen)
368
- return str;
369
- return str.slice(0, maxLen - 3) + '...';
370
- };
371
- // Build a summary of pruned tools by grouping them
372
- const toolsSummary = new Map(); // tool name -> [parameters]
373
- for (const prunedId of newlyPrunedIds) {
374
- const metadata = toolMetadata.get(prunedId);
375
- if (metadata) {
376
- const toolName = metadata.tool;
377
- if (!toolsSummary.has(toolName)) {
378
- toolsSummary.set(toolName, []);
379
- }
380
- this.logger.debug("janitor", "Processing pruned tool metadata", {
381
- sessionID,
382
- prunedId,
383
- toolName,
384
- parameters: metadata.parameters
385
- });
386
- // Extract meaningful parameter info based on tool type
387
- let paramInfo = "";
388
- if (metadata.parameters) {
389
- // For read tool, show filePath
390
- if (toolName === "read" && metadata.parameters.filePath) {
391
- paramInfo = truncate(shortenPath(metadata.parameters.filePath), 80);
392
- }
393
- // For list tool, show path
394
- else if (toolName === "list" && metadata.parameters.path) {
395
- paramInfo = truncate(shortenPath(metadata.parameters.path), 80);
396
- }
397
- // For bash/command tools, prefer description over command
398
- else if (toolName === "bash") {
399
- if (metadata.parameters.description) {
400
- paramInfo = truncate(metadata.parameters.description, 80);
401
- }
402
- else if (metadata.parameters.command) {
403
- paramInfo = truncate(metadata.parameters.command, 80);
404
- }
405
- }
406
- // For other tools, show the first relevant parameter
407
- else if (metadata.parameters.path) {
408
- paramInfo = truncate(shortenPath(metadata.parameters.path), 80);
409
- }
410
- else if (metadata.parameters.pattern) {
411
- paramInfo = truncate(metadata.parameters.pattern, 80);
412
- }
413
- else if (metadata.parameters.command) {
414
- paramInfo = truncate(metadata.parameters.command, 80);
415
- }
416
- }
417
- if (paramInfo) {
418
- toolsSummary.get(toolName).push(paramInfo);
419
- }
420
- }
421
- else {
422
- this.logger.warn("janitor", "No metadata found for pruned tool", {
423
- sessionID,
424
- prunedId
425
- });
426
- }
427
- }
428
- // Format the message with tool details using improved UI
429
- const toolText = newlyPrunedIds.length === 1 ? 'tool' : 'tools';
430
- const tokensFormatted = formatTokenCount(estimatedTokensSaved);
431
- let message = `🧹 DCP: Saved ~${tokensFormatted} tokens (${newlyPrunedIds.length} ${toolText} pruned)\n`;
432
- for (const [toolName, params] of toolsSummary.entries()) {
433
- if (params.length > 0) {
434
- message += `\n${toolName} (${params.length}):\n`;
435
- for (const param of params) {
436
- message += ` ${param}\n`;
437
- }
438
- }
439
- else {
440
- // For tools with no specific params (like batch), just show the tool name and count
441
- const count = newlyPrunedIds.filter(id => {
442
- const m = toolMetadata.get(id);
443
- return m && m.tool === toolName;
444
- }).length;
445
- if (count > 0) {
446
- message += `\n${toolName} (${count})\n`;
447
- }
448
- }
449
- }
450
- // Send as an ignored message (user sees, AI doesn't)
451
- await this.sendIgnoredMessage(sessionID, message.trim());
452
- this.logger.info("janitor", "Pruning notification sent", {
453
- sessionID,
454
- prunedCount: newlyPrunedIds.length,
455
- estimatedTokensSaved,
456
- toolsSummary: Array.from(toolsSummary.entries())
457
- });
458
- }
459
- catch (toastError) {
460
- this.logger.error("janitor", "Failed to show toast notification", {
461
- sessionID,
462
- error: toastError.message
463
- });
464
- // Don't fail the whole pruning operation if toast fails
465
- }
466
- }
467
407
  }
468
408
  catch (error) {
469
409
  this.logger.error("janitor", "Analysis failed", {
@@ -475,5 +415,167 @@ export class Janitor {
475
415
  // Silently fail and try again on next idle event
476
416
  }
477
417
  }
418
+ /**
419
+ * Helper function to shorten paths for display
420
+ */
421
+ shortenPath(path) {
422
+ // Replace home directory with ~
423
+ const homeDir = require('os').homedir();
424
+ if (path.startsWith(homeDir)) {
425
+ path = '~' + path.slice(homeDir.length);
426
+ }
427
+ // Shorten node_modules paths: show package + file only
428
+ const nodeModulesMatch = path.match(/node_modules\/(@[^\/]+\/[^\/]+|[^\/]+)\/(.*)/);
429
+ if (nodeModulesMatch) {
430
+ return `${nodeModulesMatch[1]}/${nodeModulesMatch[2]}`;
431
+ }
432
+ return path;
433
+ }
434
+ /**
435
+ * Helper function to calculate token savings from tool outputs
436
+ */
437
+ calculateTokensSaved(prunedIds, toolOutputs) {
438
+ const outputsToTokenize = [];
439
+ for (const prunedId of prunedIds) {
440
+ const output = toolOutputs.get(prunedId);
441
+ if (output) {
442
+ outputsToTokenize.push(output);
443
+ }
444
+ }
445
+ if (outputsToTokenize.length > 0) {
446
+ // Use batch tokenization for efficiency
447
+ const tokenCounts = estimateTokensBatch(outputsToTokenize, this.logger);
448
+ return tokenCounts.reduce((sum, count) => sum + count, 0);
449
+ }
450
+ return 0;
451
+ }
452
+ /**
453
+ * Build a summary of tools by grouping them
454
+ * Uses shared extractParameterKey logic for consistent parameter extraction
455
+ */
456
+ buildToolsSummary(prunedIds, toolMetadata) {
457
+ const toolsSummary = new Map();
458
+ // Helper function to truncate long strings
459
+ const truncate = (str, maxLen = 60) => {
460
+ if (str.length <= maxLen)
461
+ return str;
462
+ return str.slice(0, maxLen - 3) + '...';
463
+ };
464
+ for (const prunedId of prunedIds) {
465
+ const metadata = toolMetadata.get(prunedId);
466
+ if (metadata) {
467
+ const toolName = metadata.tool;
468
+ if (!toolsSummary.has(toolName)) {
469
+ toolsSummary.set(toolName, []);
470
+ }
471
+ // Use shared parameter extraction logic
472
+ const paramKey = extractParameterKey(metadata);
473
+ if (paramKey) {
474
+ // Apply path shortening and truncation for display
475
+ const displayKey = truncate(this.shortenPath(paramKey), 80);
476
+ toolsSummary.get(toolName).push(displayKey);
477
+ }
478
+ }
479
+ }
480
+ return toolsSummary;
481
+ }
482
+ /**
483
+ * Auto mode notification - shows only deduplication results
484
+ */
485
+ async sendAutoModeNotification(sessionID, deduplicatedIds, deduplicationDetails, toolMetadata, toolOutputs) {
486
+ if (deduplicatedIds.length === 0)
487
+ return;
488
+ // Calculate token savings
489
+ const tokensSaved = this.calculateTokensSaved(deduplicatedIds, toolOutputs);
490
+ const tokensFormatted = formatTokenCount(tokensSaved);
491
+ const toolText = deduplicatedIds.length === 1 ? 'tool' : 'tools';
492
+ let message = `🧹 DCP: Saved ~${tokensFormatted} tokens (${deduplicatedIds.length} duplicate ${toolText} removed)\n`;
493
+ // Group by tool type
494
+ const grouped = new Map();
495
+ for (const [_, details] of deduplicationDetails) {
496
+ const { toolName, parameterKey, duplicateCount } = details;
497
+ if (!grouped.has(toolName)) {
498
+ grouped.set(toolName, []);
499
+ }
500
+ grouped.get(toolName).push({
501
+ count: duplicateCount,
502
+ key: this.shortenPath(parameterKey)
503
+ });
504
+ }
505
+ // Display grouped results
506
+ for (const [toolName, items] of grouped.entries()) {
507
+ const totalDupes = items.reduce((sum, item) => sum + (item.count - 1), 0);
508
+ message += `\n${toolName} (${totalDupes} duplicate${totalDupes > 1 ? 's' : ''}):\n`;
509
+ for (const item of items.slice(0, 5)) {
510
+ const dupeCount = item.count - 1;
511
+ message += ` ${item.key} (${dupeCount}Ɨ duplicate)\n`;
512
+ }
513
+ if (items.length > 5) {
514
+ message += ` ... and ${items.length - 5} more\n`;
515
+ }
516
+ }
517
+ await this.sendIgnoredMessage(sessionID, message.trim());
518
+ }
519
+ /**
520
+ * Smart mode notification - shows both deduplication and LLM analysis results
521
+ */
522
+ async sendSmartModeNotification(sessionID, deduplicatedIds, deduplicationDetails, llmPrunedIds, toolMetadata, toolOutputs) {
523
+ const totalPruned = deduplicatedIds.length + llmPrunedIds.length;
524
+ if (totalPruned === 0)
525
+ return;
526
+ // Calculate token savings
527
+ const allPrunedIds = [...deduplicatedIds, ...llmPrunedIds];
528
+ const tokensSaved = this.calculateTokensSaved(allPrunedIds, toolOutputs);
529
+ const tokensFormatted = formatTokenCount(tokensSaved);
530
+ let message = `🧹 DCP: Saved ~${tokensFormatted} tokens (${totalPruned} tool${totalPruned > 1 ? 's' : ''} pruned)\n`;
531
+ // Section 1: Deduplicated tools
532
+ if (deduplicatedIds.length > 0 && deduplicationDetails) {
533
+ message += `\nšŸ“¦ Duplicates removed (${deduplicatedIds.length}):\n`;
534
+ // Group by tool type
535
+ const grouped = new Map();
536
+ for (const [_, details] of deduplicationDetails) {
537
+ const { toolName, parameterKey, duplicateCount } = details;
538
+ if (!grouped.has(toolName)) {
539
+ grouped.set(toolName, []);
540
+ }
541
+ grouped.get(toolName).push({
542
+ count: duplicateCount,
543
+ key: this.shortenPath(parameterKey)
544
+ });
545
+ }
546
+ for (const [toolName, items] of grouped.entries()) {
547
+ message += ` ${toolName}:\n`;
548
+ for (const item of items) {
549
+ const removedCount = item.count - 1; // Total occurrences minus the one we kept
550
+ message += ` ${item.key} (${removedCount}Ɨ duplicate)\n`;
551
+ }
552
+ }
553
+ }
554
+ // Section 2: LLM-pruned tools
555
+ if (llmPrunedIds.length > 0) {
556
+ message += `\nšŸ¤– LLM analysis (${llmPrunedIds.length}):\n`;
557
+ // Use buildToolsSummary logic
558
+ const toolsSummary = this.buildToolsSummary(llmPrunedIds, toolMetadata);
559
+ for (const [toolName, params] of toolsSummary.entries()) {
560
+ if (params.length > 0) {
561
+ message += ` ${toolName} (${params.length}):\n`;
562
+ for (const param of params) {
563
+ message += ` ${param}\n`;
564
+ }
565
+ }
566
+ else {
567
+ // For tools with no specific params (like batch), just show the tool name and count
568
+ const count = llmPrunedIds.filter(id => {
569
+ const m = toolMetadata.get(id);
570
+ return m && m.tool === toolName;
571
+ }).length;
572
+ if (count > 0) {
573
+ message += ` ${toolName} (${count})\n`;
574
+ }
575
+ }
576
+ }
577
+ }
578
+ await this.sendIgnoredMessage(sessionID, message.trim());
579
+ }
478
580
  }
479
581
  //# sourceMappingURL=janitor.js.map