diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs index afd5853..c7e979e 100644 --- a/src/app/chat_ui.rs +++ b/src/app/chat_ui.rs @@ -847,7 +847,7 @@ impl App { // 頁面 saved in this reply: the card (the page as it is now) — once a reply, under the step // that saved it last. (Worked out only for a message that saved one: few do.) if !crate::pages::ids_in(&m.tool_calls).is_empty() { - for id in crate::pages::cards_by_message(&bot.messages).get(idx).cloned().unwrap_or_default() { + for id in crate::pages::cards_at(&bot.messages, idx) { ui.add_space(8.0); self.page_card(ui, id); } diff --git a/src/app/layout_audit.rs b/src/app/layout_audit.rs index 762f49f..b809535 100644 --- a/src/app/layout_audit.rs +++ b/src/app/layout_audit.rs @@ -1002,6 +1002,11 @@ fn perf_long_chat() { a.thinking = think.clone(); a.at = 1_700_000_000_000 + i * 2_000; a.tool_calls = vec![crate::model::ToolCall { id: id.clone(), name: "browser_open".into(), args, status: crate::model::ToolStatus::Done }]; + // (jenny-Host's bot keeps a page of what it sent: a write_page every so often.) + if i % 70 == 0 { + let page = serde_json::json!({"id": 16, "content": think.repeat(10), "note": "更新"}).to_string(); + a.tool_calls = vec![crate::model::ToolCall { id: id.clone(), name: "write_page".into(), args: page, status: crate::model::ToolStatus::Done }]; + } let mut r = Message::tool_result(&id, "browser_open", &long); r.at = a.at + 1_000; r.folded = i < 9_000; @@ -1014,7 +1019,8 @@ fn perf_long_chat() { let size = egui::vec2(1280.0, 800.0); let mut frame = eframe::Frame::_new_kittest(); let mut times = vec![]; - for f in 0..12 { + let frames = std::env::var("PERF_FRAMES").ok().and_then(|f| f.parse().ok()).unwrap_or(12); + for f in 0..frames { if f >= 6 { app.state.bot_mut(sel).unwrap().messages.push(Message::tool_result("x", "browser_open", "新的一步")); } @@ -1023,12 +1029,20 @@ fn perf_long_chat() { let mut out = ctx.run_ui(input, |ui| app.ui(ui, &mut frame)); times.push(t.elapsed().as_millis()); let texts = out.shapes.iter().filter(|c| matches!(c.shape, egui::epaint::Shape::Text(_))).count(); - if times.len() == 12 { + if times.len() == frames { println!("PERF last frame shapes {} texts {texts}", out.shapes.len()); } out.textures_delta.clear(); } println!("PERF frames ms: {times:?}"); + let all = app.state.bot(sel).unwrap().messages.clone(); + let t = std::time::Instant::now(); + let _ = crate::pages::cards_by_message(&all); + println!("PERF cards_by_message (the old way, once per page-writing message drawn) ms: {}", t.elapsed().as_millis()); + let t = std::time::Instant::now(); + let last = all.iter().rposition(|m| m.tool_calls.iter().any(|c| c.name == "write_page")).unwrap(); + let _ = crate::pages::cards_at(&all, last); + println!("PERF cards_at µs: {}", t.elapsed().as_micros()); let dir = std::env::temp_dir().join(format!("anybot-perf-{}", std::process::id())); let p = dir.join("state.json"); for k in 0..3 { diff --git a/src/pages.rs b/src/pages.rs index 278927d..0905284 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -477,6 +477,29 @@ fn written_id(c: &crate::model::ToolCall) -> Option { serde_json::from_str::(&c.args).ok().and_then(|v| v["id"].as_u64()) } +/// The pages whose card shows under message `idx` (what cards_by_message gives it), reading only +/// its own reply. The chat drew each message that saved a page by working out the whole chat: +/// on jenny-Host (36 000 messages, a page written a few hundred times) that was most of the +/// window's time every frame, parsing every write_page in the chat once per such message shown. +pub fn cards_at(messages: &[crate::model::Message], idx: usize) -> Vec { + let Some(m) = messages.get(idx) else { return vec![] }; + if m.role == crate::model::Role::User { + return vec![]; + } + let mine = ids_in(&m.tool_calls); + if mine.is_empty() { + return mine; + } + let mut later: Vec = vec![]; + for n in &messages[idx + 1..] { + if n.role == crate::model::Role::User { + break; + } + later.extend(ids_in(&n.tool_calls)); + } + mine.into_iter().filter(|id| !later.contains(id)).collect() +} + /// For each message, the pages whose card shows under it: each page a reply saved, once, under the /// step that saved it last (a reply that wrote its page three times — sent back, fixed, updated — /// showed three cards of the same 第 3 版). A reply runs from one message of yours to the next. @@ -896,6 +919,8 @@ mod tests { ]; let cards = cards_by_message(&msgs); assert_eq!(cards, vec![vec![], vec![], vec![], vec![], vec![7], vec![], vec![], vec![7]]); + // One message at a time (what the chat draws) says the same, reading only its reply. + assert_eq!((0..msgs.len() + 1).map(|i| cards_at(&msgs, i)).collect::>(), [cards, vec![vec![]]].concat()); } #[test]