mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
6ca7311273
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>
43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package commands
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
func contextCommand() Definition {
|
|
return Definition{
|
|
Name: "context",
|
|
Description: "Show current session context and token usage",
|
|
Usage: "/context",
|
|
Handler: func(_ context.Context, req Request, rt *Runtime) error {
|
|
if rt == nil || rt.GetContextStats == nil {
|
|
return req.Reply(unavailableMsg)
|
|
}
|
|
stats := rt.GetContextStats()
|
|
if stats == nil {
|
|
return req.Reply("No active session context.")
|
|
}
|
|
return req.Reply(formatContextStats(stats))
|
|
},
|
|
}
|
|
}
|
|
|
|
func formatContextStats(s *ContextStats) string {
|
|
remaining := s.CompressAtTokens - s.UsedTokens
|
|
if remaining < 0 {
|
|
remaining = 0
|
|
}
|
|
usedWindowPercent := s.UsedTokens * 100 / max(s.TotalTokens, 1)
|
|
return fmt.Sprintf(
|
|
"Context usage \nMessages: %d \nUsed: ~%d / %d tokens (%d%%) \nCompress at: %d tokens \nCompression progress: %d%% \nRemaining: ~%d tokens",
|
|
s.MessageCount,
|
|
s.UsedTokens,
|
|
s.TotalTokens,
|
|
usedWindowPercent,
|
|
s.CompressAtTokens,
|
|
s.UsedPercent,
|
|
remaining,
|
|
)
|
|
}
|