Skip to content

Commit 3d2869d

Browse files
authored
Merge pull request #309 from Opencode-DCP/feature/extraction-token-count
feat: show extracted token count in prune notifications
2 parents 57dfd50 + f5bb89c commit 3d2869d

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

lib/ui/notification.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Logger } from "../logger"
22
import type { SessionState } from "../state"
33
import {
4+
countDistillationTokens,
45
formatExtracted,
56
formatPrunedItemsList,
67
formatStatsHeader,
@@ -19,36 +20,46 @@ export const PRUNE_REASON_LABELS: Record<PruneReason, string> = {
1920
function buildMinimalMessage(
2021
state: SessionState,
2122
reason: PruneReason | undefined,
22-
distillation?: string[],
23+
distillation: string[] | undefined,
24+
showDistillation: boolean,
2325
): string {
24-
const reasonSuffix = reason ? ` — ${PRUNE_REASON_LABELS[reason]}` : ""
26+
const extractedTokens = countDistillationTokens(distillation)
27+
const extractedSuffix =
28+
extractedTokens > 0 ? ` (extracted ${formatTokenCount(extractedTokens)})` : ""
29+
const reasonSuffix = reason && extractedTokens === 0 ? ` — ${PRUNE_REASON_LABELS[reason]}` : ""
2530
let message =
2631
formatStatsHeader(state.stats.totalPruneTokens, state.stats.pruneTokenCounter) +
27-
reasonSuffix
32+
reasonSuffix +
33+
extractedSuffix
2834

29-
return message + formatExtracted(distillation)
35+
return message + formatExtracted(showDistillation ? distillation : undefined)
3036
}
3137

3238
function buildDetailedMessage(
3339
state: SessionState,
3440
reason: PruneReason | undefined,
3541
pruneToolIds: string[],
3642
toolMetadata: Map<string, ToolParameterEntry>,
37-
workingDirectory?: string,
38-
distillation?: string[],
43+
workingDirectory: string,
44+
distillation: string[] | undefined,
45+
showDistillation: boolean,
3946
): string {
4047
let message = formatStatsHeader(state.stats.totalPruneTokens, state.stats.pruneTokenCounter)
4148

4249
if (pruneToolIds.length > 0) {
4350
const pruneTokenCounterStr = `~${formatTokenCount(state.stats.pruneTokenCounter)}`
44-
const reasonLabel = reason ? ` — ${PRUNE_REASON_LABELS[reason]}` : ""
45-
message += `\n\n▣ Pruning (${pruneTokenCounterStr})${reasonLabel}`
51+
const extractedTokens = countDistillationTokens(distillation)
52+
const extractedSuffix =
53+
extractedTokens > 0 ? `, extracted ${formatTokenCount(extractedTokens)}` : ""
54+
const reasonLabel =
55+
reason && extractedTokens === 0 ? ` — ${PRUNE_REASON_LABELS[reason]}` : ""
56+
message += `\n\n▣ Pruning (${pruneTokenCounterStr}${extractedSuffix})${reasonLabel}`
4657

4758
const itemLines = formatPrunedItemsList(pruneToolIds, toolMetadata, workingDirectory)
4859
message += "\n" + itemLines.join("\n")
4960
}
5061

51-
return (message + formatExtracted(distillation)).trim()
62+
return (message + formatExtracted(showDistillation ? distillation : undefined)).trim()
5263
}
5364

5465
export async function sendUnifiedNotification(
@@ -73,18 +84,19 @@ export async function sendUnifiedNotification(
7384
return false
7485
}
7586

76-
const showExtraction = config.tools.extract.showDistillation ? distillation : undefined
87+
const showDistillation = config.tools.extract.showDistillation
7788

7889
const message =
7990
config.pruneNotification === "minimal"
80-
? buildMinimalMessage(state, reason, showExtraction)
91+
? buildMinimalMessage(state, reason, distillation, showDistillation)
8192
: buildDetailedMessage(
8293
state,
8394
reason,
8495
pruneToolIds,
8596
toolMetadata,
8697
workingDirectory,
87-
showExtraction,
98+
distillation,
99+
showDistillation,
88100
)
89101

90102
await sendIgnoredMessage(client, sessionId, message, params, logger)

lib/ui/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { ToolParameterEntry } from "../state"
22
import { extractParameterKey } from "../messages/utils"
3+
import { countTokens } from "../strategies/utils"
4+
5+
export function countDistillationTokens(distillation?: string[]): number {
6+
if (!distillation || distillation.length === 0) return 0
7+
return countTokens(distillation.join("\n"))
8+
}
39

410
export function formatExtracted(distillation?: string[]): string {
511
if (!distillation || distillation.length === 0) {

0 commit comments

Comments
 (0)