Skip to content

Commit aa60c0d

Browse files
remath proportional bargraph display
1 parent 5c8e9b7 commit aa60c0d

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

lib/tools/compress.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ export function createCompressTool(ctx: ToolContext): ReturnType<typeof tool> {
406406
compressedToolIds: compressedToolIds.length,
407407
})
408408

409+
// Build token weight map for all messages (for proportional bar graph)
410+
const weights = new Map<string, number>()
411+
for (const msg of messages) {
412+
weights.set(msg.info.id, countAllMessageTokens(msg))
413+
}
414+
409415
await sendCompressNotification(
410416
client,
411417
logger,
@@ -420,6 +426,7 @@ export function createCompressTool(ctx: ToolContext): ReturnType<typeof tool> {
420426
totalSessionTokens,
421427
estimatedCompressedTokens,
422428
messages.map((m) => m.info.id),
429+
weights,
423430
messages.length,
424431
currentParams,
425432
)

lib/ui/notification.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export async function sendCompressNotification(
142142
totalSessionTokens: number,
143143
compressedTokens: number,
144144
sessionMessageIds: string[],
145+
weights: Map<string, number>,
145146
totalMessages: number,
146147
params: any,
147148
): Promise<boolean> {
@@ -168,7 +169,13 @@ export async function sendCompressNotification(
168169
})
169170

170171
const newIds = new Set(messageIds)
171-
const progressBar = formatSessionMap(sessionMessageIds, state.prune.messages, newIds, 50)
172+
const progressBar = formatSessionMap(
173+
sessionMessageIds,
174+
state.prune.messages,
175+
newIds,
176+
weights,
177+
50,
178+
)
172179
const reduction =
173180
totalSessionTokens > 0 ? Math.round((compressedTokens / totalSessionTokens) * 100) : 0
174181
message += `\n\n${progressBar}`

lib/ui/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,25 @@ export function formatSessionMap(
6363
messageIds: string[],
6464
prunedMessages: Map<string, number>,
6565
newPrunedIds: Set<string>,
66+
weights: Map<string, number>,
6667
width: number = 50,
6768
): string {
6869
const total = messageIds.length
6970
if (total === 0) return `│${"░".repeat(width)}│`
7071

72+
// Build cumulative token weights for proportional positioning
73+
const cum = [0]
74+
for (const id of messageIds) {
75+
cum.push(cum[cum.length - 1] + (weights.get(id) || 1))
76+
}
77+
const totalWeight = cum[cum.length - 1]
78+
7179
const bar = new Array(width).fill("█")
7280

7381
for (let m = 0; m < total; m++) {
7482
if (prunedMessages.has(messageIds[m])) {
75-
const start = Math.floor((m / total) * width)
76-
const end = Math.floor(((m + 1) / total) * width)
83+
const start = Math.floor((cum[m] / totalWeight) * width)
84+
const end = Math.floor((cum[m + 1] / totalWeight) * width)
7785
const char = newPrunedIds.has(messageIds[m]) ? "▓" : "░"
7886
for (let i = start; i < end; i++) {
7987
bar[i] = char

0 commit comments

Comments
 (0)