Abeautifulsnow
c300336281
feat(ai): context window management and token usage tracking
* @
feat(ai): add conversation context management and token usage tracking (Phase 4)
## Background
Phase 4 of AI agent loop upgrade: token usage extraction from LLM responses,
cost estimation display, and automatic context window management (summarization
when approaching limits).
## Changes
### Backend (Rust)
- New token_usage::TokenUsage — cross-turn token accumulation
- New agent_loop::maybe_compact — proactive context compaction: token estimation
→ cut-point selection → LLM summarization → fallback summary
→ tool-pair integrity protection → drain/insert
- New agent_loop::context_window_for_model — auto-detect context window by model
- New agent_loop::is_context_length_error — detect upstream context-length errors
- New agent_loop::compact_tool_result_for_context — JSON/text sampling for large results
- Modified agent_loop::run_agent_loop — pre-turn compaction check, retry on context error
- Modified agent_events::AgentEnd — split total_tokens into input_tokens/output_tokens
- New agent_events::ContextCompacted — compaction event with metrics
- New AiConfig.context_window — user-configurable context window size
- New AiChatMessage.kind — context summary message marker
- Modified stream_with_tools & 3 provider stream functions — extract TokenUsage
- Claude: message_start(input) + message_delta(output)
- OpenAI: stream_options { include_usage } → final usage chunk
- Gemini: usageMetadata per chunk, keep last
### Frontend (TypeScript/Vue)
- AiAssistant.vue: token + cost display row, estimateCost() with 7 model families,
contextCompacted UI step, pendingCompaction deferred apply,
messagesForAgentHistory post-compaction history reconstruction
- EditorSettingsDialog.vue: contextWindow input (min=1000, auto-detect)
- settingsStore.ts: AiConfig.contextWindow field
- tauri.ts: AgentEvent type updates, AiChatMessage.kind
- i18n: 6 new keys in en.ts and zh-CN.ts
## Fixed Issues
- messages[0] dropped from LLM context after compaction
- estimate_text_tokens hard threshold causing 2.8x estimate jump
- context_compacted event field semantics mismatch with frontend insert position
- validate_summary English-only keywords causing fallback on non-English LLMs
- Stale agentTokens after conversation switch
@
* remove cost computation logic
2026-06-15 18:55:22 +08:00
..
2026-06-15 17:44:00 +08:00
2026-06-10 18:30:14 +08:00
2026-06-14 22:58:05 +08:00
2026-06-14 22:58:05 +08:00
2026-06-14 22:58:05 +08:00
2026-06-14 22:58:05 +08:00
2026-06-15 14:07:31 +08:00
2026-06-15 14:07:31 +08:00
2026-06-15 18:55:22 +08:00
2026-06-08 00:58:42 +08:00
2026-06-15 18:55:22 +08:00
2026-06-14 12:22:08 +08:00
2026-06-12 16:34:42 +08:00
2026-06-13 01:56:29 +08:00
2026-06-15 13:14:59 +08:00
2026-06-15 18:55:22 +08:00
2026-06-14 10:16:24 +08:00
2026-06-14 10:16:24 +08:00
2026-06-15 17:43:44 +08:00
2026-06-06 23:22:29 +08:00
2026-06-11 11:57:22 +08:00
2026-05-25 23:21:47 +08:00
2026-06-15 17:43:44 +08:00
2026-05-25 23:21:47 +08:00
2026-06-13 16:00:47 +08:00
2026-06-15 16:59:01 +08:00
2026-05-23 01:28:52 +08:00
2026-06-15 14:23:40 +08:00
2026-06-04 23:20:15 +08:00
2026-05-10 14:18:20 +08:00
2026-06-11 21:41:37 +08:00
2026-06-15 18:55:22 +08:00
2026-06-11 18:56:38 +08:00
2026-06-14 22:58:05 +08:00
2026-06-08 20:09:27 +08:00
2026-06-11 17:29:40 +08:00
2026-06-04 17:20:13 +08:00
2026-06-15 13:14:59 +08:00
2026-06-14 22:58:05 +08:00
2026-06-15 15:14:38 +08:00
2026-06-15 15:01:13 +08:00
2026-06-11 22:35:00 +08:00
2026-06-12 17:14:05 +08:00
2026-06-14 22:58:05 +08:00
2026-06-14 22:58:05 +08:00
2026-06-10 19:53:36 +08:00
2026-06-06 23:22:29 +08:00
2026-06-06 23:22:29 +08:00
2026-06-13 20:23:36 +08:00
2026-06-15 14:18:40 +08:00
2026-06-13 18:39:12 +08:00
2026-06-13 17:27:58 +08:00
2026-06-06 23:22:29 +08:00
2026-06-08 17:07:17 +08:00
2026-06-12 12:29:46 +08:00
2026-05-22 15:41:33 +08:00
2026-06-15 18:55:22 +08:00
2026-06-14 22:58:05 +08:00
2026-06-12 17:14:05 +08:00
2026-05-30 19:15:37 +08:00
2026-05-29 17:25:52 +08:00