diff --git a/FEATURES.md b/FEATURES.md index 355f86f..969ad10 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -310,7 +310,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - 打字停 300 ms,就在這個 Bot 的記憶裡找跟草稿有關的,在輸入框上方排成一列小卡片,最多 8 張。左邊是「帶入記憶」,點了打開記憶頁。 - 分數 0.5 以上的預先勾好。點一下卡片就加入或拿掉。滑過去看全文、種類、日期、相關度,以及「點一下:這次帶上」或「點一下:這次不帶」。 -- 最右邊小字「帶 N 條,約 T token」,一條都沒勾時是「這次不帶」。 +- 「帶入記憶」旁邊小字「帶 N 條,約 T token」(窄的時候只寫「N 條」),一條都沒勾時是「這次不帶」。卡片只佔一行,放不下的左右捲(字打很多、配到很多條時,不會把輸入框擠出視窗)。 - 送出時把勾的那幾條一起送出,存在這則訊息上。沒等到卡片就送出時,由 Bot 那邊自己挑(分數 0.55 以上,最多 3 條)。 - 釘選的記憶不會出現在這裡,因為每次都會帶。分數怎麼算見 [docs/memory.md](docs/memory.md)。 diff --git a/docs/feature-map/chat.md b/docs/feature-map/chat.md index db63af0..ac7b40d 100644 --- a/docs/feature-map/chat.md +++ b/docs/feature-map/chat.md @@ -5,16 +5,16 @@ ## 子功能 - `chat-layout` 版面:左邊 Bot 清單(拖到比 210 px 窄收成頭像直欄),右邊聊天;點頂部 Bot 名稱開右側面板(「動態」「資料庫」)。 -- `chat-input` 輸入框:Enter 送出、Shift+Enter 換行(在「1. 」清單裡自動接下一項);↑ 找回上一則;草稿每個 Bot 各存一份;打字停一下,上方出現「帶入記憶」小卡片([memory](memory.md))。 +- `chat-input` 輸入框:Enter 送出、Shift+Enter 換行(「1. 」清單裡自動接下一項);↑ 找回上一則;草稿每個 Bot 各存一份;框最高長到這欄放得下(聊天留一段),再多就捲;打字停一下,上方出現「帶入記憶」小卡片([memory](memory.md))。 - `chat-commands` 打「/」列出 /goal、/plan、/btw、/always-approve(一律放行,見 [autonomy](autonomy.md))、/usage(最近一天、一週的 token 用量)、/doctor(診斷,見 [garden](garden.md))、/context(模型可讀用量)、/compact(現在就把較早的整理成摘要)、/fork(分岔:這段對話到這裡存一份進「過去的對話」);打「@」列出其他 Bot。 - `chat-steer` 插話:Bot 工作中照樣送出,下一步就讀到(附檔一起),下方寫「已排入「…」」;來不及讀的變成下一輪。它在做例行任務、工作流或回外面的訊息時,你的話等那輪做完才回,不混進去。 - `chat-stop` 停止:工作中、輸入框空著時,送出鈕變成「停止」。 - `chat-followthrough` 說了就做:回答說了要做什麼(「我把它整理成一頁」)卻沒動手,或問「要我現在就開始嗎?」,這一輪不停:上方閃過「說了要做還沒動手,提醒它接著做…」,它接著做。其實做完、或接下來要你決定(刪除、付款、寄出、斷線),就停在原來那句;連兩次沒動手就停。 - `chat-actions` 回答旁的操作:滑過 Bot 的回答出現「新增表情回應」「回覆」(引用這則)「更多訊息操作」;⋯ 裡有「複製」「重新產生」(只有最後一則)「還原上一個答案」「顯示步驟(N)」,最下面寫是哪個模型寫的。出錯的回答下面直接有「重試」。 - `chat-select` 選取一段文字(手機頁面和網頁版):按住一則訊息(或回答下的 ⋯)開訊息選單,「複製」是整則;「選取文字」把這則放進下方面板,在那裡按住開始選、拖兩端調整、按系統的「拷貝」(電腦上拖曳後按 ⌘C/Ctrl+C),「完成」關掉。用電腦的瀏覽器能在聊天裡拖曳選字;選好的字上右鍵(長按)是瀏覽器選單,別處右鍵才開訊息選單。 -- `chat-steps` 步驟和思考:預設收起。「顯示步驟」一行一個工具(等你允許/執行中/完成/被拒絕或停止/失敗),滑過那一行看結果;「思考過程」也在這裡。工作中,輸入框上方的臉旁邊寫它正在做什麼。臉和「正在…」最多每秒畫 20 次(CPU 畫圖 4 次),沒在動不重畫;你不在(視窗不在前、90 秒沒動)頭像不眨眼。 +- `chat-steps` 步驟和思考:預設收起。「顯示步驟」一行一個工具(等你允許/執行中/完成/被拒絕或停止/失敗),滑過那一行看結果;「思考過程」也在這裡。工作中,輸入框上方的臉旁邊寫它正在做什麼。臉和「正在…」最多每秒畫 20 次(CPU 畫圖 4 次),沒在動不重畫;你不在(視窗不在前、90 秒沒動)不眨眼。 - `chat-attach` 附件:「附加檔案」、拖進視窗、⌘V 貼圖;每檔最多 50 MB,文字檔最多讀 20,000 字給模型。 -- `chat-compact` 長對話:舊步驟的長結果(連同長指令)累積到可讀長度 15% 就整批收起(聊天裡照樣完整);用到 6 成才寫可展開的「較早的對話已整理成摘要」。一個要求做很多步時也寫。 +- `chat-compact` 長對話:舊步驟的長結果(連同長指令)累積到可讀長度 15% 就整批收起(聊天裡照樣完整);用到 6 成才寫可展開的「較早的對話已整理成摘要」。很多步時也寫。 - `chat-window` 開聊天分頁(手機頁面):打開給最新 30 條,頂上「載入更早的對話」點一次從大腦上整段聊天往前補 60 條,到最開頭說「已是對話最開頭」;回答中手機每秒最多更新一次,打完的整句補齊。電腦和聊底指令照舊拿整段。 - `chat-stall` 卡住偵測:送得多的請求等得久(120 秒起,最多 10 分鐘),等不到第一個字先收小對話再問,還不行才顯示「模型未能及時開始回應」;開始後靜 35 秒是「回應中斷…」。等你允許、工具在跑時不算。 diff --git a/docs/feature-map/memory.md b/docs/feature-map/memory.md index a99ab75..82b6de6 100644 --- a/docs/feature-map/memory.md +++ b/docs/feature-map/memory.md @@ -6,7 +6,7 @@ - `memory-kinds` 三種:「記得的事」(Bot 用 `remember` 記,或在記憶頁新增;不會自動刪)、「做過的事」(有動手做事的回合做完自動記一行)、「整理過的」(每晚把 30 天前做過的事壓成每週一行,半年前的再壓成每月一行)。 - `memory-pin` 釘選(核心記憶):每次聊天都放進 Bot 的說明裡。 -- `memory-carry` 跟著訊息帶的記憶:打字停 300 毫秒,輸入框上方出現「帶入記憶」一列小卡片(最多 8 張,相關度高的先勾好),右邊寫「帶 N 條,約 T token」或「這次不帶」,點卡片就加入或拿掉。送出後訊息下面標「帶了 N 條記憶」,點開看是哪幾條。沒等到卡片就送出、例行任務:AnyBot 自己挑,最多 3 條。同一段聊天帶過的不再重帶。 +- `memory-carry` 跟著訊息帶的記憶:打字停 300 毫秒,輸入框上方出現「帶入記憶」一列小卡片(最多 8 張,相關度高的先勾好;只佔一行,放不下的左右捲),旁邊寫「帶 N 條,約 T token」(窄的時候「N 條」)或「這次不帶」,點卡片就加入或拿掉。送出後訊息下面標「帶了 N 條記憶」,點開看是哪幾條。沒等到卡片就送出、例行任務:AnyBot 自己挑,最多 3 條。同一段聊天帶過的不再重帶。 - `memory-about` 「關於你」:關於使用者本人、每個 Bot 都該知道的事(名字、家人、住哪、工作、喜歡怎麼被回答),記一次,每個 Bot 的說明都帶著最新的 40 條。 - `memory-search` 搜尋:`search_memory` 用意思加字詞找自己的記憶和「關於你」,每條附日期。 - `memory-forget` 忘記:`forget` 把那條放進「最近刪除」,30 天內可以還原。 diff --git a/src/app.rs b/src/app.rs index f50a097..834f4f2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -780,6 +780,17 @@ impl App { if let Some(id) = std::env::var("ANYBOT_SHOT_TAB").ok().and_then(|v| v.parse::().ok()) { app.wf.current = id; } + // ANYBOT_SHOT_DRAFT: a long draft in the chat beside it, with memories it may carry. + if let Ok(d) = std::env::var("ANYBOT_SHOT_DRAFT") { + app.wf.chat_hidden = false; + // (The first bot: not busy, so the memories it may carry show.) + app.state.selected = 1; + app.wf.chat_was = 1; + let bot = 1; + let hits = (0..9).map(|i| serde_json::json!({"id": i, "text": format!("11-21:你是一個很猛的社群小編,你看一下第 {} 則", i + 1), "kind": "chat", "pick": i < 5, "tokens": 140})).collect(); + app.drafts.insert(bot, d.clone()); + app.mem_suggest.insert(bot, memory_ui::Suggest::answered(&d, hits)); + } } // 日程: a sample week (ANYBOT_SHOT_TAB=day|month for the other views). Ok("schedule") => { @@ -4284,10 +4295,15 @@ impl App { }; self.header(ui, &bot); self.mirror_banner(ui); - // The composer grows with a long draft: keep room for last frame's height. + // The composer grows with a long draft: keep room for last frame's height — but never all + // of it: the conversation keeps a strip, and the input stops growing (it scrolls) where + // the column ends, even in a short window or the narrow chat beside a workflow. let h_id = egui::Id::unique("composer-height"); let composer_h: f32 = ui.data(|d| d.get_temp(h_id)).unwrap_or(116.0); - let avail = ui.available_height() - composer_h; + let room = ui.available_height(); + let cap = composer_cap(room, self.approvals.get(&id).is_some_and(|a| !matches!(a.kind, ApprovalKind::Question | ApprovalKind::Handoff))); + ui.data_mut(|d| d.insert_temp(egui::Id::unique("composer-cap"), cap)); + let avail = room - composer_h.min(cap); let w = ui.available_width(); let col_w = (w - 48.0).min(TRANSCRIPT_MAX_WIDTH); let pad = ((w - col_w) / 2.0).max(0.0); @@ -4296,6 +4312,7 @@ impl App { .id_salt(("transcript", id)) .auto_shrink([false, false]) .max_height(avail) + .min_scrolled_height(avail.clamp(0.0, 64.0)) .stick_to_bottom(self.stick_bottom) .show(ui, |ui| { ui.horizontal(|ui| { @@ -4594,7 +4611,8 @@ impl App { /// and Esc refuses while the input is empty — once the card has been up for a moment, so an /// Enter meant for something else can't answer a request that just appeared. /// `enter`: the composer took a plain Enter that is this card's (the box is empty). - fn approval_dock(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot, draft_empty: bool, typing: bool, enter: bool) { + #[allow(clippy::too_many_arguments)] + fn approval_dock(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot, draft_empty: bool, typing: bool, enter: bool, room: f32) { let p = self.pal; // (A question's card is in the chat: see question_card.) if self.approvals.get(&bot.id).is_some_and(|a| a.kind == ApprovalKind::Question) { @@ -4611,7 +4629,8 @@ impl App { // (長理由不許把輸入框頂出螢幕:卡片照內容估高、照視窗高度封頂,裡面的東西在卡面裡往下捲。 // 下方的列問不到剩餘高度,所以捲動區用估出來的固定高度。) let reason = self.approvals.get(&bot.id).map(|a| a.reason.clone()).unwrap_or_default(); - let cap = (ui.ctx().content_rect().height() * 0.45).max(160.0); + // (And within what the chat column has left for it: room, less the card's edges and the space after.) + let cap = (ui.ctx().content_rect().height() * 0.45).max(160.0).min(room - 48.0).max(72.0); let body = ask_dock_height(&reason, ui.available_width(), cap); needs_you_card(ui, p, |ui| { ui.set_width(ui.available_width()); @@ -5121,6 +5140,7 @@ impl App { fn composer(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot) { let p = self.pal; + let composer_top = ui.cursor().top(); let streaming = self.streams.contains_key(&bot.id); let composer_id = egui::Id::unique(("composer", bot.id)); let had_focus = ui.memory(|m| m.has_focus(composer_id)); @@ -5229,12 +5249,21 @@ impl App { s.store(ctx, composer_id); ui.memory_mut(|m| m.request_focus(composer_id)); } - // 需要你允許, then the bot's face peeking over the input with what it is doing. - self.approval_dock(ui, ctx, bot, draft.trim().is_empty(), composing, enter_for_approval); + // The column's share for all of this (see chat_column), and what goes under the box (the + // frame's bottom, the lines under it: as tall as last time). + let cap: f32 = ui.data(|d| d.get_temp(egui::Id::unique("composer-cap"))).unwrap_or(f32::INFINITY); + let below_id = egui::Id::unique(("composer-below", bot.id)); + let below: f32 = ui.data(|d| d.get_temp(below_id)).unwrap_or(COMPOSER_BELOW); + // 需要你允許, then the bot's face peeking over the input with what it is doing. (The card + // leaves room for the face and a one-line box under it.) + let card_room = cap - (ui.cursor().top() - composer_top) - below - COMPOSER_FACE - 44.0; + self.approval_dock(ui, ctx, bot, draft.trim().is_empty(), composing, enter_for_approval, card_room); self.face_dock(ui, bot); + // What is left for the box: past it, the words scroll inside it. + let box_room = cap - (ui.cursor().top() - composer_top) - below - 16.0; // The input: one box with an ink edge — +, the words, the mic, and a button in the // bot's colour (voice chat while empty, send ↑ once there is something to send). - Frame::new() + let input = Frame::new() .fill(p.elevated) .stroke(Stroke::new(1.5, if had_focus { p.text } else { p.text.gamma_multiply(0.45) })) .corner_radius(R_L) @@ -5268,7 +5297,7 @@ impl App { ui.fonts_mut(|f| f.layout_job(job)) }; let text_h = layouter(ui, &draft, text_w).size().y.max(COMPOSER_LINE_H); - let max_h = (ctx.content_rect().height() * 0.4).clamp(120.0, 360.0); + let max_h = (ctx.content_rect().height() * 0.4).clamp(120.0, 360.0).min(box_room); let box_h = composer_box_h(text_h, max_h); // +: 附加檔案 / 教它一項任務 / the model. In a slot as tall as the words, at its // bottom (the row grows only after it, so Align::Max alone left it at the top). @@ -5368,7 +5397,9 @@ impl App { } }); }); - }); + }) + .response + .rect; ui.add_space(4.0); let queued: Vec = self.streams.get(&bot.id).map(|l| l.steer.lock().unwrap().iter().map(|q| q.text.clone()).collect()).unwrap_or_default(); let notice = self.notice.clone(); @@ -5400,6 +5431,11 @@ impl App { } }); ui.add_space(10.0); + let now_below = ui.cursor().top() - input.bottom() + 8.0 + 1.5; + if (now_below - below).abs() > 0.5 { + ui.data_mut(|d| d.insert_temp(below_id, now_below)); + ui.ctx().request_repaint(); + } // 草稿 rides along to disk: a restart or a bot switch mustn't erase half-written words. // (Kept as typed; saved once typing rests — see update.) if !self.mir.on && draft != bot.draft { @@ -8367,6 +8403,21 @@ fn ask_dock_height(reason: &str, width: f32, cap: f32) -> f32 { (wrapped_lines(detail, (width - 92.0).max(120.0), cw) * 17.0 + 168.0).min(cap) } +/// Under the words in the composer before it has been measured: the frame's bottom (8 + its +/// edge), and the line and space under the box (see composer). +const COMPOSER_BELOW: f32 = 8.0 + 1.5 + 4.0 + 20.0 + 10.0; + +/// The bot's face over the input, with the line of what it is doing (see face_dock). +const COMPOSER_FACE: f32 = 46.0 + 28.0; + +/// How tall the composer may get in a chat column `room` tall: the conversation above keeps +/// about a third (at least 60, at most 200) — only a strip while a step waits for a yes (the +/// card comes first). +fn composer_cap(room: f32, asking: bool) -> f32 { + let keep = if asking { 60.0 } else { (room * 0.3).clamp(60.0, 200.0) }; + (room - keep).max(140.0) +} + fn composer_box_h(text_h: f32, max_h: f32) -> f32 { (text_h + COMPOSER_TOP + 3.0).clamp(28.0, max_h.max(28.0)) } @@ -10613,4 +10664,49 @@ mod tests { let many = wrapped_lines(&"abc ".repeat(500), 300.0, 7.0); assert!(one >= 1.0 && many > one); } + + /// A long draft in the narrow chat beside a workflow, with many memories it may carry: the + /// input stays inside the window and the conversation keeps a strip. (The memory chips went + /// one a line and the box grew with the window's height, not the column's: the input fell + /// off the bottom.) + #[test] + fn a_long_draft_never_pushes_the_input_off_the_chat() { + let ctx = egui::Context::default(); + let mut app = super::App::new(&ctx, Some(std::env::temp_dir().join("anybot-long-draft.png"))); + let id = app.state.bots[0].id; + app.state.selected = id; + let draft = "我要做一個發文流水線,最後要給我審批,你幫我想一下怎麼做最好。".repeat(30); + app.drafts.insert(id, draft.clone()); + let hits: Vec = (0..9).map(|i| serde_json::json!({"id": i, "text": format!("11-21:你是一個很猛的社群小編,你看一下第 {i} 則"), "kind": "chat", "pick": i < 5, "tokens": 140})).collect(); + app.mem_suggest.insert(id, memory_ui::Suggest::answered(&draft, hits)); + let input = egui::Id::unique(("composer", id)); + for asking in [false, true] { + // (And with a step waiting for a yes: its card, the face and the box all fit.) + if asking { + let (reply, _got) = std::sync::mpsc::channel(); + app.approvals.insert(id, Approval { call_id: "c1".into(), tool: "shell".into(), reason: format!("執行:{}", "mkdir -p 2026-04 2026-05 && mv IMG_04*.jpg 2026-04/ ".repeat(6)), grant: String::new(), reply, kind: ApprovalKind::Action }); + std::mem::forget(_got); + } + for (w, h) in [(300.0, 560.0), (300.0, 420.0), (760.0, 900.0)] { + let column = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), vec2(w, h)); + let mut bottom = 0.0; + let mut cap = 0.0; + for _ in 0..8 { + let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(1400.0, h))), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + let mut col = ui.new_child(egui::UiBuilder::new().max_rect(column)); + col.set_clip_rect(column); + app.chat_column(&mut col); + bottom = col.min_rect().bottom(); + }); + out.textures_delta.clear(); + cap = ctx.data(|d| d.get_temp::(egui::Id::unique("composer-cap"))).unwrap(); + } + assert!(bottom <= column.bottom() + 1.0, "{w}×{h} asking {asking}: the chat ran to {bottom}, past {}", column.bottom()); + assert!(cap < h - 50.0, "{w}×{h}: the conversation keeps a strip (cap {cap})"); + let words = ctx.read_response(input).expect("the input was drawn").rect; + assert!(words.top() + 20.0 < column.bottom(), "{w}×{h} asking {asking}: the input starts at {}, off the column", words.top()); + } + } + } } diff --git a/src/app/memory_ui.rs b/src/app/memory_ui.rs index 39fd3bf..2f9d401 100644 --- a/src/app/memory_ui.rs +++ b/src/app/memory_ui.rs @@ -236,24 +236,35 @@ impl App { let mut open = false; let tokens: i64 = hits.iter().filter(|h| st.on(h)).map(|h| h["tokens"].as_i64().unwrap_or(0)).sum(); let n_on = hits.iter().filter(|h| st.on(h)).count(); - ui.horizontal_wrapped(|ui| { + // One row, whatever the width: the label and how many it carries, then the chips, which + // scroll sideways. (Wrapped, a long draft's many hits — one a line in a narrow chat + // beside a workflow — pushed the input off the bottom of the window.) + let narrow = ui.available_width() < 520.0; + let cut = if narrow { 12 } else { 22 }; + ui.horizontal(|ui| { ui.add_space(12.0); ui.spacing_mut().item_spacing = vec2(6.0, 6.0); let r = crate::icons::label(ui, crate::icons::Icon::Bookmark, &t("帶入記憶"), 12.0, p.text3); if r.on_hover_text(t("打開這個 Bot 的記憶頁")).on_hover_cursor(egui::CursorIcon::PointingHand).interact(egui::Sense::click()).clicked() { open = true; } - for h in &hits { - let on = st.on(h); - let text = h["text"].as_str().unwrap_or(""); - let short: String = text.chars().take(22).collect(); - let short = if text.chars().count() > 22 { format!("{short}…") } else { short }; - let tip = format!("{text}\n\n{} · {} · 相關度 {:.2}{}", kind_label(h["kind"].as_str().unwrap_or("")), day(h["created"].as_i64().unwrap_or(0)), h["score"].as_f64().unwrap_or(0.0), if on { "\n點一下:這次不帶" } else { "\n點一下:這次帶上" }); - if chip(ui, &format!("{}{short}", if on { "✓ " } else { "" }), on, p).on_hover_text(tip).clicked() { - flip = h["id"].as_i64().map(|id| (id, !on)); - } - } - ui.label(RichText::new(if n_on == 0 { t("這次不帶") } else { format!("帶 {n_on} 條,約 {tokens} token") }).size(FONT_XS).color(p.text3)); + let summary = if n_on == 0 { t("這次不帶") } else if narrow { format!("{n_on} 條") } else { format!("帶 {n_on} 條,約 {tokens} token") }; + ui.label(RichText::new(summary).size(FONT_XS).color(p.text3)).on_hover_text(format!("帶 {n_on} 條,約 {tokens} token")); + egui::ScrollArea::horizontal().id_salt(("mem-chips", bot)).auto_shrink([false, true]).show(ui, |ui| { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing = vec2(6.0, 6.0); + for h in &hits { + let on = st.on(h); + let text = h["text"].as_str().unwrap_or(""); + let short: String = text.chars().take(cut).collect(); + let short = if text.chars().count() > cut { format!("{short}…") } else { short }; + let tip = format!("{text}\n\n{} · {} · 相關度 {:.2}{}", kind_label(h["kind"].as_str().unwrap_or("")), day(h["created"].as_i64().unwrap_or(0)), h["score"].as_f64().unwrap_or(0.0), if on { "\n點一下:這次不帶" } else { "\n點一下:這次帶上" }); + if chip(ui, &format!("{}{short}", if on { "✓ " } else { "" }), on, p).on_hover_text(tip).clicked() { + flip = h["id"].as_i64().map(|id| (id, !on)); + } + } + }); + }); }); ui.add_space(4.0); if let (Some((id, on)), Some(st)) = (flip, self.mem_suggest.get_mut(&bot)) {