// 對話的長度 (R4): 「/context」 says how much of what the model can read this chat uses, and // 「/compact」 folds its older part into a summary now (on a thread, by the quick model), the same // way a turn does it at COMPACT_THRESHOLD_PERCENT (agent::summarize). use super::*; impl App { /// 「/context」: a line in this chat. pub(super) fn context_note(&mut self, bot_id: u64) { let Some(b) = self.state.bot(bot_id) else { return }; let start = crate::model::model_view_start(&b.messages); let used = agent::history_tokens("", &b.messages[start..]); let limit = b.reads(); let text = format!( "這個對話(模型看得到的部分)約 {} token,{} 可讀 {} token,約 {}%(系統指示和工具說明另外大約幾千到一萬多 token)。到 {}% 會自動把較早的內容整理成摘要;要現在整理打 /compact。", used, b.model, limit, used * 100 / limit, agent::COMPACT_THRESHOLD_PERCENT ); self.goal_note(bot_id, &text); self.dirty = true; } /// 「/compact」: the older part of this chat into a summary now (compact_tick takes it in). pub(super) fn compact_now(&mut self, bot_id: u64) { let Some(b) = self.state.bot(bot_id).cloned() else { return }; if self.streams.contains_key(&bot_id) { self.goal_note(bot_id, &format!("{} 正在做事:等它做完再 /compact(太長時它自己也會整理)。", b.name)); return; } let start = crate::model::model_view_start(&b.messages); let view: Vec = b.messages[start..].iter().filter(|m| !m.note).cloned().collect(); let Some(cut) = agent::compaction_cut(&view) else { self.goal_note(bot_id, "對話還很短,不用整理。"); return; }; let ps = self.state.provider(b.provider); let req = ChatRequest { kind: b.provider, base_url: providers::resolve_base(b.provider, &ps.base_url), api_key: providers::resolve_key_for(b.provider, &ps.api_key, &ps.base_url), model: b.model.clone(), system: String::new(), history: vec![], tools: vec![], session: format!("anybot-bot-{bot_id}"), effort: String::new(), }; let quick = match b.step_model.trim() { "off" => None, "" => b.provider.quick_model(&b.model).map(String::from), m if m == b.model => None, m => Some(m.to_string()), }; self.goal_note(bot_id, "正在把較早的對話整理成摘要…"); let (out, repaint) = (self.compact_out.clone(), self.repaint_fn()); std::thread::spawn(move || { agent::set_turn_bot(bot_id); let cancel = AtomicBool::new(false); let got = agent::summarize(&req, quick.as_deref(), &view[..cut], &cancel, &|_| {}); out.lock().unwrap().push((bot_id, got.map(|text| (cut, Message { role: Role::Assistant, text, summary: true, via: b.model.clone(), at: crate::model::now_ms(), ..Default::default() }, view[cut].at)))); repaint(); }); self.dirty = true; } /// A finished 「/compact」: the summary takes the older part's place (as a turn's does). pub(super) fn compact_tick(&mut self) { let done = std::mem::take(&mut *self.compact_out.lock().unwrap()); for (bot_id, got) in done { match got { Some((n, summary, boundary)) => { if let Some(b) = self.state.bot_mut(bot_id) { crate::model::fold_in(&mut b.messages, n, summary, boundary); } self.goal_note(bot_id, &format!("已整理:較早的 {n} 則訊息換成一段摘要(你說過的話照原文留在摘要下面)。")); } None => self.goal_note(bot_id, "沒有整理成:模型沒有回應,稍後再試。"), } self.dirty = true; self.save_now = true; } } }