Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(tui): add context window tracking and auto-compaction
tikikun8fe052d1fe5f
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex 2a81e409b..557e15499 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -287,6 +287,10 @@ struct App {/// `None` in unit tests, set by `run` for the live session.args: Option<Arc<OrchestrationArgs>>,max_turns: u32,+ /// Context window limit for the current model (default 128K).+ context_window: u64,+ /// Tokens to reserve for the model's response (compaction trigger at limit - reserve).+ reserve_tokens: u64,/// Repo top-level when the project is a git repo; enables workspace snapshots./// Cleared if git setup fails, permanently disabling snapshots this session.repo_root: Option<PathBuf>,@@ -507,6 +511,8 @@ impl App {path_hint_dismissed: false,status: Status::Idle,turn: (0, 0),+ context_window: 128_000,+ reserve_tokens: 16_384,tokens: 0,detail: String::new(),pending_queue: std::collections::VecDeque::new(),@@ -2455,6 +2461,39 @@ async fn chat_loop<B: Backend>(Some(StreamEvent::Done { stop_reason, usage }) => {app.on_done(stop_reason, usage);current = None;+ // Auto-compact when approaching the context limit.+ let limit = app.context_window.saturating_sub(app.reserve_tokens);+ if app.tokens > limit && app.tokens > 0 && app.history.len() > 4 {+ let model = app.model.clone();+ let mut history = std::mem::take(&mut app.history);+ let before = history.len();+ let compacted = crate::core::agent::r#loop::compact_history(+ args, &model, &history,+ crate::core::agent::compaction::DEFAULT_KEEP_RECENT,+ )+ .await;+ match compacted {+ Ok(c) if c.len() < before => {+ history = c;+ app.history = history;+ app.persist();+ app.note(&format!(+ "auto-compacted {} -> {} messages (ctx {}K/{}K)",+ before,+ app.history.len(),+ app.tokens / 1000,+ app.context_window / 1000,+ ));+ }+ Ok(_) => {+ app.history = history;+ }+ Err(e) => {+ app.history = history;+ app.note(&format!("auto-compaction failed: {e}"));+ }+ }+ }}Some(StreamEvent::Error { code, message }) => {app.on_error(code, message);@@ -3015,7 +3054,14 @@ async fn compact_command(app: &mut App) {Ok(compacted) if compacted.len() < before => {app.history = compacted;app.persist();- app.note(&format!("compacted {before} -> {} messages", app.history.len()));+ // Rough token estimate after compaction (assume ~30% of original).+ app.tokens = (app.tokens as f64 * 0.3) as u64;+ app.note(&format!(+ "compacted {before} -> {} messages (ctx {}K/{}K)",+ app.history.len(),+ app.tokens / 1000,+ app.context_window / 1000,+ ));}Ok(_) => app.note("nothing to compact yet"),Err(e) => app.note(&format!("compaction failed: {e}")),@@ -4252,7 +4298,16 @@ fn header(app: &App) -> Paragraph<'static> {Span::styled(" jan agent ", Style::new().on_blue().white().bold()),Span::raw(format!(" {} ", app.model)),];- spans.push(Span::raw(format!(" {turn}tokens {}", app.tokens)));+ spans.push(Span::raw(format!(" {turn}")));+ if app.tokens > 0 {+ spans.push(Span::raw(format!(+ "ctx {}K/{}K ",+ app.tokens / 1000,+ app.context_window / 1000+ )));+ } else {+ spans.push(Span::raw(format!("ctx 0/{}K ", app.context_window / 1000)));+ }spans.push(Span::styled(elapsed, Style::new().dim()));// Active-goal indicator: `◎ /goal active <duration>` (cyan while running,// green once achieved), so an unattended run shows the goal is still live.