feat: add microcompact_boundary event handling in web UI

Adds support for Claude Code's microcompact_boundary system message type to display context microcompaction events with token savings information in the chat UI. Shows a package icon with formatted token count (e.g., "Context compacted (saved 32K tokens)"). close #108
This commit is contained in:
weishu
2026-01-26 10:30:27 +08:00
parent e53abfb840
commit 8137ce4f23
3 changed files with 23 additions and 0 deletions
+17
View File
@@ -246,6 +246,23 @@ export function normalizeAgentRecord(
meta meta
} }
} }
if (data.type === 'system' && data.subtype === 'microcompact_boundary') {
const metadata = isObject(data.microcompactMetadata) ? data.microcompactMetadata : null
return {
id: messageId,
localId,
createdAt,
role: 'event',
content: {
type: 'microcompact',
trigger: asString(metadata?.trigger) ?? 'auto',
preTokens: asNumber(metadata?.preTokens) ?? 0,
tokensSaved: asNumber(metadata?.tokensSaved) ?? 0
},
isSidechain: false,
meta
}
}
return null return null
} }
+5
View File
@@ -58,6 +58,11 @@ export function getEventPresentation(event: AgentEvent): EventPresentation {
const ms = typeof event.durationMs === 'number' ? event.durationMs : 0 const ms = typeof event.durationMs === 'number' ? event.durationMs : 0
return { icon: '⏱️', text: `Turn: ${formatDuration(ms)}` } return { icon: '⏱️', text: `Turn: ${formatDuration(ms)}` }
} }
if (event.type === 'microcompact') {
const saved = typeof event.tokensSaved === 'number' ? event.tokensSaved : 0
const formatted = saved >= 1000 ? `${Math.round(saved / 1000)}K` : String(saved)
return { icon: '📦', text: `Context compacted (saved ${formatted} tokens)` }
}
try { try {
return { icon: null, text: JSON.stringify(event) } return { icon: null, text: JSON.stringify(event) }
} catch { } catch {
+1
View File
@@ -16,6 +16,7 @@ export type AgentEvent =
| { type: 'ready' } | { type: 'ready' }
| { type: 'api-error'; retryAttempt: number; maxRetries: number; error: unknown } | { type: 'api-error'; retryAttempt: number; maxRetries: number; error: unknown }
| { type: 'turn-duration'; durationMs: number } | { type: 'turn-duration'; durationMs: number }
| { type: 'microcompact'; trigger: string; preTokens: number; tokensSaved: number }
| ({ type: string } & Record<string, unknown>) | ({ type: string } & Record<string, unknown>)
export type ToolResultPermission = { export type ToolResultPermission = {