feat(agent): add context usage ring indicator and /context command (#2537)
Add a context window usage indicator to the web chat UI and a /context slash command that works across all channels. Backend: - Add computeContextUsage() estimating history + system + tool tokens - Attach ContextUsage to outbound messages via the pico WebSocket protocol - Add /context command showing context stats as formatted text - Add EstimateSystemTokens() on ContextBuilder for system prompt estimation Frontend: - Add ContextUsageRing component (SVG ring + hover/tap popover) - Show usage percentage, token counts, and compression threshold - Hover on desktop (150ms leave delay), tap on mobile - "View Details" sends /context with 1s cooldown - i18n support (en/zh) for popover labels Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c3dc0ee3a
commit
6ca7311273
19 files changed
+462
-35
No files matched your search
@@ -1,7 +1,11 @@
|
||||
import { toast } from "sonner"
|
||||
|
||||
import { normalizeUnixTimestamp } from "@/features/chat/state"
|
||||
import { type AssistantMessageKind, updateChatStore } from "@/store/chat"
|
||||
import {
|
||||
type AssistantMessageKind,
|
||||
type ContextUsage,
|
||||
updateChatStore,
|
||||
} from "@/store/chat"
|
||||
|
||||
export interface PicoMessage {
|
||||
type: string
|
||||
@@ -21,6 +25,24 @@ function hasAssistantKindPayload(payload: Record<string, unknown>): boolean {
|
||||
return typeof payload.thought === "boolean"
|
||||
}
|
||||
|
||||
function parseContextUsage(
|
||||
payload: Record<string, unknown>,
|
||||
): ContextUsage | undefined {
|
||||
const raw = payload.context_usage
|
||||
if (!raw || typeof raw !== "object") return undefined
|
||||
const obj = raw as Record<string, unknown>
|
||||
const used = Number(obj.used_tokens)
|
||||
const total = Number(obj.total_tokens)
|
||||
if (!Number.isFinite(used) || !Number.isFinite(total) || total <= 0)
|
||||
return undefined
|
||||
return {
|
||||
used_tokens: used,
|
||||
total_tokens: total,
|
||||
compress_at_tokens: Number(obj.compress_at_tokens) || 0,
|
||||
used_percent: Number(obj.used_percent) || 0,
|
||||
}
|
||||
}
|
||||
|
||||
export function handlePicoMessage(
|
||||
message: PicoMessage,
|
||||
expectedSessionId: string,
|
||||
@@ -36,6 +58,7 @@ export function handlePicoMessage(
|
||||
const content = (payload.content as string) || ""
|
||||
const messageId = (payload.message_id as string) || `pico-${Date.now()}`
|
||||
const kind = parseAssistantMessageKind(payload)
|
||||
const contextUsage = parseContextUsage(payload)
|
||||
const timestamp =
|
||||
message.timestamp !== undefined &&
|
||||
Number.isFinite(Number(message.timestamp))
|
||||
@@ -54,6 +77,7 @@ export function handlePicoMessage(
|
||||
},
|
||||
],
|
||||
isTyping: false,
|
||||
...(contextUsage ? { contextUsage } : {}),
|
||||
}))
|
||||
break
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user