From 25bf3f5bd4e297924123e553fcbb8b2542173eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Tue, 17 Nov 2026 12:28:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20/fork=20=E5=88=86=E5=B2=94=EF=BC=9A?= =?UTF-8?q?=E9=80=99=E6=AE=B5=E5=B0=8D=E8=A9=B1=E5=AD=98=E4=B8=80=E4=BB=BD?= =?UTF-8?q?=E5=88=B0=E3=80=8C=E9=81=8E=E5=8E=BB=E7=9A=84=E5=B0=8D=E8=A9=B1?= =?UTF-8?q?=E3=80=8D=EF=BC=8C=E4=B9=8B=E5=BE=8C=E5=90=84=E8=B5=B0=E5=90=84?= =?UTF-8?q?=E7=9A=84=EF=BC=9BAnyBot=20=E7=9A=84=E6=8C=87=E4=BB=A4=E5=8F=AA?= =?UTF-8?q?=E5=9C=A8=E4=B8=80=E5=80=8B=E5=9C=B0=E6=96=B9=E8=AA=8D=E3=80=81?= =?UTF-8?q?=E4=B8=80=E5=80=8B=E5=9C=B0=E6=96=B9=E5=81=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - /fork 或 /fork 名字:目前的對話照常繼續,一份「分岔:名字」放進過去的對話最上面;打開它時目前這段自動收進去,兩條路互不影響。Bot 在做事或對話是空的時說原因。 - 不複製整個 Bot(例行任務、訊息管道會重複)。 - 電腦的 send 和手機/螢幕的那條路本來各列一次 /btw、/goal、/plan、/always-approve、/usage、/context、/compact、/doctor,現在都走 app/commands.rs 的 app_command + run_app_command。 - / 清單、手機的指令清單加 /fork。 已驗證:cargo test 590 通過(指令認得出來、不把 /forkx 和路徑當指令;分岔留一份、之後說的不進分岔、打開分岔回到那一點、空的和正在做事時說原因)、clippy -D warnings 乾淨。 未驗證:在真的 App、手機上打 /fork 的樣子。 Co-Authored-By: Claude Opus 5.5 --- FEATURES.md | 9 +- docs/feature-map/chat.md | 4 +- docs/plan-2026-11-engineering.md | 2 +- mobile/index.html | 4 +- src/app.rs | 46 ++------- src/app/chat_ui.rs | 2 +- src/app/commands.rs | 155 +++++++++++++++++++++++++++++++ src/app/remote_ui.rs | 41 +------- 8 files changed, 183 insertions(+), 80 deletions(-) create mode 100644 src/app/commands.rs diff --git a/FEATURES.md b/FEATURES.md index b9323d8..9472438 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -487,7 +487,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 ### 「/」清單只放指令 -- 輸入框只打「/」時,清單只列 AnyBot 自己的指令(/goal、/plan、/btw、/always-approve、/usage、/doctor、/context、/compact),標題「指令」。技能已經多到一整牆,而且 Bot 本來就讀得到自己會的技能,自己會用。 +- 輸入框只打「/」時,清單只列 AnyBot 自己的指令(/goal、/plan、/btw、/always-approve、/usage、/doctor、/context、/compact、/fork),標題「指令」。技能已經多到一整牆,而且 Bot 本來就讀得到自己會的技能,自己會用。 - 「/」後面打字,才會再找**這個 Bot 自己會的**技能(名稱開頭符合的先,最多 6 個),想指定它用某個技能時還是可以。手機一樣。 ### 上線前補上的防護(安全和資源) @@ -1517,6 +1517,13 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) - 「無需詢問直接允許」「一律詢問」除了用一般的話寫,也可以寫固定樣式:`shell(git *)`、`file(~/Projects/**)`、`connector(gitlab__*)`、`web(domain:example.com)`、`tool(browser_click)`(* 任何字,? 一個字;檔案的 * 只在一層、** 跨資料夾)。 - 樣式在模型之前判斷,每次都一樣,不花模型的錢也不用等:符合「一律詢問」樣式的一定問,模型說允許也沒用(網頁上的字騙不了它);符合允許樣式的直接允許,但如果還有用話寫的「一律詢問」,先讓模型看那幾條;都不符合就照舊把用話寫的規則交給模型。指令要每一段(&&、|、; 分開的)都符合才算允許,有 $()、反引號藏指令的不會被樣式允許。直接操作模式下,「一律詢問」的樣式一樣擋得住。(參考 grok-build 的 Bash(git *) 規則。) +### 分岔(/fork) + +- 「/fork」或「/fork 名字」:把這段對話到現在為止存一份到「過去的對話」最上面,標題「分岔:名字」(沒給名字就用第一句話),聊天裡留一行「已分岔:…」。接著照常在這裡做;想回到分岔那一點走另一條路,從 Bot 選單 › 過去的對話 打開它,目前這段會自動收進過去的對話,兩條路之後互不影響、可以來回切換。參考 grok-build 的 /fork、Claude Code 的 /branch。 +- 不複製整個 Bot:複製的 Bot 會把例行任務跑兩次、訊息管道回兩次。目標、計畫、記憶屬於 Bot,不跟著分岔。 +- Bot 正在做事時不行(「等它做完(或按停止)再 /fork」);對話是空的也不行。電腦、大腦的螢幕、手機打的都算,由大腦做;訊息管道和團隊成員打的不算指令。 +- AnyBot 自己的指令(/btw、/goal、/plan、/always-approve、/usage、/doctor、/context、/compact、/fork)在電腦和手機/螢幕兩條路上由同一個地方認出來、同一個地方做,新增指令只要加一次。 + ### 摘要(/context、/compact) - 對話太長時(到模型可讀的 70%)把較早的內容整理成摘要。摘要照段落寫:使用者要的、已經決定的、做過的事和結果(指令、路徑、網址、數字照原樣)、還沒完成的、注意(試過不行的)。整理得太短(一大段對話不到 200 字)會再要一次。摘要下面由 AnyBot 自己(不是模型)附上使用者最近 12 句原話(每句最多 400 字)和還在跑的背景指令(#編號和指令),這些不會被摘要弄丟。參考 grok-build 的分段摘要和摘要後的提醒。 diff --git a/docs/feature-map/chat.md b/docs/feature-map/chat.md index 302457e..25e6d2c 100644 --- a/docs/feature-map/chat.md +++ b/docs/feature-map/chat.md @@ -5,7 +5,7 @@ ## 子功能 - 版面:左邊 Bot 清單(側邊欄拖到比 210 px 窄會收成頭像直欄),右邊聊天。點聊天頂部的 Bot 名稱(「查看對話詳細資訊」)打開右側面板,有「動態」「資料庫」兩頁。 -- 輸入框:Enter 送出、Shift+Enter 換行(在「1. 」「- 」這種清單裡會自動接下一項);輸入框空著按 ↑ 找回上一則;每個 Bot 的草稿各存一份,重開也在。打「/」列出 /goal、/plan、/btw、/always-approve(一律放行,見 [safety](safety.md))、/usage(用量:最近一天和一週、各 Bot 和各模型用了多少 token,模型服務有回報才有;有回報花費的才寫花費)、/doctor(診斷,見 [garden](garden.md))、/context(這個對話用了模型可讀的多少)、/compact(現在就把較早的對話整理成摘要),打「@」列出其他 Bot。 +- 輸入框:Enter 送出、Shift+Enter 換行(在「1. 」「- 」這種清單裡會自動接下一項);輸入框空著按 ↑ 找回上一則;每個 Bot 的草稿各存一份,重開也在。打「/」列出 /goal、/plan、/btw、/always-approve(一律放行,見 [safety](safety.md))、/usage(用量:最近一天和一週、各 Bot 和各模型用了多少 token,模型服務有回報才有;有回報花費的才寫花費)、/doctor(診斷,見 [garden](garden.md))、/context(這個對話用了模型可讀的多少)、/compact(現在就把較早的對話整理成摘要)、/fork(分岔:這段對話到這裡為止存一份到「過去的對話」,標題「分岔:…」,之後各走各的),打「@」列出其他 Bot。 - 記憶小卡片:打字停一下,輸入框上方出現「帶入記憶」一列,見 [memory](memory.md)。 - 插話:Bot 工作中照樣打字送出,不另開一輪,Bot 下一步就讀到(附檔一起進去);輸入框下方寫「已排入「…」,Bot 會在下一步看到」。這一輪剛好結束、來不及讀的,自動變成下一輪。 - 停止:工作中、輸入框空著時,送出鈕變成「停止」。 @@ -23,7 +23,7 @@ - 點側邊欄的 Bot;⌘1–⌘9 切到前九個。 - 輸入框左邊的 +(「輸入框操作」):「附加檔案」「教它一項任務」,下面是換模型(見 [bots](bots.md))。右邊是「開始語音輸入」和送出鈕。 -- Bot 選單(右鍵 Bot,或聊天右上角「更多」):「開始新對話」把目前的對話收進「過去的對話」;「清除聊天記錄」不會再問;「重新啟動這個 Bot」卡住時用,聊天記錄和檔案都保留。 +- Bot 選單(右鍵 Bot,或聊天右上角「更多」):「開始新對話」把目前的對話收進「過去的對話」;/fork 留一份分岔在那裡、目前的照常繼續,打開分岔時目前的會自動收進去;「清除聊天記錄」不會再問;「重新啟動這個 Bot」卡住時用,聊天記錄和檔案都保留。 - 你的最後一則沒有回覆時,下面有「還沒有回覆」和「重新傳送」;被 AnyBot 關掉打斷的回合有「繼續」。 ## 怎麼操作 diff --git a/docs/plan-2026-11-engineering.md b/docs/plan-2026-11-engineering.md index 6cb62c7..1ca1da8 100644 --- a/docs/plan-2026-11-engineering.md +++ b/docs/plan-2026-11-engineering.md @@ -113,7 +113,7 @@ - 做法:SKILL.md frontmatter:`arguments`(`$ARGUMENTS`、`$1`…)、`allowed-tools`(只能用哪些工具,執行層擋)、`context: fork`(丟給支線做,不佔主對話)。「/技能名 參數」直接帶進去。 - 做完:測試涵蓋三種。 -### M5. /fork 帶著上下文分岔 ⬜ +### M5. /fork 帶著上下文分岔 ✅(分岔存在「過去的對話」,不複製整個 Bot:例行任務、訊息管道會重複;順便把兩份重複的指令分派合成 app/commands.rs 一份) - 做法:`/fork` 把這段對話複製成一個新對話(或新 Bot),之後各走各的。 - 做完:分岔後兩邊互不影響。 diff --git a/mobile/index.html b/mobile/index.html index 75bd540..20a0630 100644 --- a/mobile/index.html +++ b/mobile/index.html @@ -1656,7 +1656,7 @@ input.addEventListener("keydown", (e) => { input.addEventListener("keydown", (e) => { if (e.key === "Enter" && !e.shiftKey && !e.isComposing && matchMedia("(pointer:fine)").matches) { e.preventDefault(); send(); } }); const SL = {hits: [], lit: 0, q: null, moved: false, closed: false, loading: false, fresh: false, at: false}; /** AnyBot's own / commands (goal.rs on the brain). */ -const COMMANDS = [{name: "goal", description: "設一個目標,一直做到達成為止(/goal clear 清除)"}, {name: "plan", description: "先規劃再動手:它先查清楚、寫出計畫給你核准,核准後才照計畫做(/plan clear 取消)"}, {name: "always-approve", description: "一律放行:Bot 做事不再一個個問你(/always-approve off 關閉)"}, {name: "usage", description: "用量:最近一天和一週,各 Bot、各模型用了多少 token"}, {name: "doctor", description: "診斷:檢查大腦的連線、模型服務、Bot 的電腦、連接器、磁碟"}, {name: "context", description: "這個對話用了模型可讀的多少"}, {name: "compact", description: "現在就把較早的對話整理成摘要"}, {name: "btw", description: "開一條支線:帶著這段對話和記憶,同時去做另一件事,做完回報到這裡"}]; +const COMMANDS = [{name: "goal", description: "設一個目標,一直做到達成為止(/goal clear 清除)"}, {name: "plan", description: "先規劃再動手:它先查清楚、寫出計畫給你核准,核准後才照計畫做(/plan clear 取消)"}, {name: "always-approve", description: "一律放行:Bot 做事不再一個個問你(/always-approve off 關閉)"}, {name: "usage", description: "用量:最近一天和一週,各 Bot、各模型用了多少 token"}, {name: "doctor", description: "診斷:檢查大腦的連線、模型服務、Bot 的電腦、連接器、磁碟"}, {name: "context", description: "這個對話用了模型可讀的多少"}, {name: "compact", description: "現在就把較早的對話整理成摘要"}, {name: "fork", description: "分岔:這段對話存一份到過去的對話,之後各走各的"}, {name: "btw", description: "開一條支線:帶著這段對話和記憶,同時去做另一件事,做完回報到這裡"}]; const GOAL_TAG = "[目標]"; /** The skills a draft like 「/pp」 asks for, as the brain ranks them: name starts with it, name * contains it, then description. None once a space ends the name, or while the bot works. */ @@ -1754,7 +1754,7 @@ async function send() { const memories = b.busy ? undefined : memPicks(); memReset(); input.value = ""; input.style.height = "auto"; $("composer").classList.remove("tall"); S.pending = []; renderChips(); renderSlash(); - const quiet = /^\/(goal|plan)(\s+(clear|stop|off|reset|none|cancel|清除|取消|停止))?\s*$/i.test(text) || /^\/(always-approve|yolo)(\s|$)/i.test(text) || /^\/(usage|doctor|context|compact)\s*$/i.test(text); + const quiet = /^\/(goal|plan)(\s+(clear|stop|off|reset|none|cancel|清除|取消|停止))?\s*$/i.test(text) || /^\/(always-approve|yolo)(\s|$)/i.test(text) || /^\/(usage|doctor|context|compact)\s*$/i.test(text) || /^\/fork(\s|$)/.test(text); if (!b.busy && !quiet) { S.msgs.push({role: "user", text, attachments: [], memories: memories || []}); renderMsgs(); $("scroller").scrollTop = 1e9; } // 先顯示: while it works the words aren't in the chat yet — shown at once all the same. const sent = b.busy && text ? {bot: S.open, text, at: Date.now(), failed: false} : null; diff --git a/src/app.rs b/src/app.rs index 1fc793d..38676fb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -44,6 +44,8 @@ mod drops_ui; mod memory_ui; mod goal_ui; mod context_ui; +mod commands; +use commands::app_command; mod doctor_ui; mod plan_ui; mod mood_ui; @@ -1426,7 +1428,7 @@ impl App { } // 先顯示: while it works (or the words travel to the brain) they aren't in the chat // yet — the person sees them at once all the same. (A command here draws its own line.) - let local_command = !self.mir.on && (branch::command(&text).is_some() || crate::goal::command(&text).is_some() || crate::plan::command(&text).is_some() || crate::always::command(&text).is_some() || crate::usage::command(&text) || matches!(text.trim(), "/context" | "/compact")); + let local_command = !self.mir.on && app_command(&text).is_some(); if self.streams.contains_key(&bot_id) && !text.trim().is_empty() && !local_command { self.sending.entry(bot_id).or_default().push((text.trim().to_string(), Instant::now())); } @@ -1456,43 +1458,13 @@ impl App { } // Typed here: this computer is the person's. self.typed_on = Some(String::new()); - // 支線: 「/btw …」 branches the bot off (even while it works: it isn't steering). - if let Some(task) = branch::command(&text) { - self.btw_command(ctx, bot_id, &task, attachments); + // AnyBot's own commands (/btw even while it works: it isn't steering; /goal, /plan, + // /always-approve, /usage, /context, /compact, /fork — commands.rs); anything else takes a + // paused goal up again. + if let Some(cmd) = app_command(&text) { + self.run_app_command(ctx, bot_id, cmd, &text, attachments, picks); return; } - // 目標: 「/goal …」 is AnyBot's, not the bot's; anything else takes a paused goal up again. - if let Some(cmd) = crate::goal::command(&text) { - self.goal_command(ctx, bot_id, cmd, &text, attachments, picks); - return; - } - // 計畫: 「/plan …」 is AnyBot's too. - if let Some(cmd) = crate::plan::command(&text) { - self.plan_command(ctx, bot_id, cmd, &text, attachments, picks); - return; - } - // 一律放行: 「/always-approve」 changes the person's own permissions. - if let Some(cmd) = crate::always::command(&text) { - self.always_command(ctx, bot_id, cmd); - return; - } - // 用量: 「/usage」 — what the models used, a line in this chat. - if crate::usage::command(&text) { - self.usage_note(bot_id); - return; - } - // 對話的長度: 「/context」 how full it is, 「/compact」 fold the older part now. - match text.trim() { - "/context" => { - self.context_note(bot_id); - return; - } - "/compact" => { - self.compact_now(bot_id); - return; - } - _ => {} - } self.goal_heard(bot_id); // While it works, a new message steers the running turn (the original's `turnSteer`: // it doesn't count as a new turn) and reaches the model at its next step. @@ -8560,7 +8532,7 @@ mod tests { let mut bot = app.state.bots[0].clone(); bot.skills = Some(vec!["週報".into()]); let names = |l: Vec<(String, String)>| l.into_iter().map(|x| x.0).collect::>(); - assert_eq!(names(app.slash_list("/", &bot)), vec!["goal", "plan", "btw", "always-approve", "usage", "doctor", "context", "compact"]); + assert_eq!(names(app.slash_list("/", &bot)), vec!["goal", "plan", "btw", "always-approve", "usage", "doctor", "context", "compact", "fork"]); assert_eq!(names(app.slash_list("/週", &bot)), vec!["週報"], "its own, by the word"); assert_eq!(names(app.slash_list("/b", &bot)), vec!["btw"]); } diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs index 24a3c3b..28eb00f 100644 --- a/src/app/chat_ui.rs +++ b/src/app/chat_ui.rs @@ -1134,7 +1134,7 @@ fn jump_shows(from_bottom: f32, jumping: bool) -> bool { } /// AnyBot's own / commands: (name, what it does). The brain acts on them itself (goal.rs). -pub const SLASH_COMMANDS: [(&str, &str); 8] = [("goal", "設一個目標,一直做到達成為止(/goal clear 清除)"), ("plan", "先規劃再動手:它先查清楚、寫出計畫給你核准,核准後才照計畫做(/plan clear 取消)"), ("btw", "開一條支線:帶著這段對話和記憶,同時去做另一件事,做完回報到這裡"), ("always-approve", "一律放行:Bot 做事不再一個個問你(/always-approve off 關閉)"), ("usage", "用量:最近一天和一週,各 Bot、各模型用了多少 token"), ("doctor", "診斷:檢查這台電腦的連線、大腦、模型服務、Bot 的電腦、連接器、磁碟"), ("context", "這個對話用了模型可讀的多少"), ("compact", "現在就把較早的對話整理成摘要")]; +pub const SLASH_COMMANDS: [(&str, &str); 9] = [("goal", "設一個目標,一直做到達成為止(/goal clear 清除)"), ("plan", "先規劃再動手:它先查清楚、寫出計畫給你核准,核准後才照計畫做(/plan clear 取消)"), ("btw", "開一條支線:帶著這段對話和記憶,同時去做另一件事,做完回報到這裡"), ("always-approve", "一律放行:Bot 做事不再一個個問你(/always-approve off 關閉)"), ("usage", "用量:最近一天和一週,各 Bot、各模型用了多少 token"), ("doctor", "診斷:檢查這台電腦的連線、大腦、模型服務、Bot 的電腦、連接器、磁碟"), ("context", "這個對話用了模型可讀的多少"), ("compact", "現在就把較早的對話整理成摘要"), ("fork", "分岔:這段對話存一份到過去的對話,之後各走各的")]; /// What the bot is doing, in the one line beside its face (and on a colleague's card, the phone). /// A status of several lines — a retry: the error, what it means, when it tries again — used to diff --git a/src/app/commands.rs b/src/app/commands.rs new file mode 100644 index 0000000..0cd1965 --- /dev/null +++ b/src/app/commands.rs @@ -0,0 +1,155 @@ +// AnyBot's own slash commands (not the bot's): told apart and carried out in one place, whether +// typed here, on a screen of a brain (it sends the words; the brain carries them out) or on the +// phone. They used to be listed twice (send and the remote path), and a new one had to be added +// to both. +// +// 分岔 (/fork, M5): a copy of this conversation goes to 過去的對話 as 「分岔:…」 and the person +// goes on here; opening it later puts this one aside, so the two go their own ways from that point +// (like grok-build's /fork, Claude Code's /branch). A copy of the bot instead would run its +// routines and answer its channels twice. + +use super::*; + +/// One of AnyBot's commands. +pub(super) enum AppCommand { + Btw(String), + Goal(crate::goal::Command), + Plan(crate::plan::Command), + Always(crate::always::Command), + Usage, + Context, + Compact, + Doctor, + Fork(String), +} + +/// A branch's title: 「分岔:」 and the name given, or the conversation's first words. +pub(super) const FORK_PREFIX: &str = "分岔:"; + +/// 「/fork」 or 「/fork 名字」: the name (empty when none), or None. +pub(super) fn fork_command(text: &str) -> Option { + let rest = text.trim_start().strip_prefix("/fork")?; + (rest.is_empty() || rest.starts_with(char::is_whitespace)).then(|| rest.trim().chars().take(40).collect()) +} + +/// The command these words are, if they are one. +pub(super) fn app_command(text: &str) -> Option { + if let Some(task) = branch::command(text) { + return Some(AppCommand::Btw(task)); + } + if let Some(c) = crate::goal::command(text) { + return Some(AppCommand::Goal(c)); + } + if let Some(c) = crate::plan::command(text) { + return Some(AppCommand::Plan(c)); + } + if let Some(c) = crate::always::command(text) { + return Some(AppCommand::Always(c)); + } + if crate::usage::command(text) { + return Some(AppCommand::Usage); + } + if crate::doctor::command(text) { + return Some(AppCommand::Doctor); + } + if let Some(name) = fork_command(text) { + return Some(AppCommand::Fork(name)); + } + match text.trim() { + "/context" => Some(AppCommand::Context), + "/compact" => Some(AppCommand::Compact), + _ => None, + } +} + +impl App { + /// Carry out one of AnyBot's commands for this bot. + pub(super) fn run_app_command(&mut self, ctx: &egui::Context, bot_id: u64, cmd: AppCommand, text: &str, attachments: Vec, picks: Option>) { + match cmd { + AppCommand::Btw(task) => self.btw_command(ctx, bot_id, &task, attachments), + AppCommand::Goal(c) => self.goal_command(ctx, bot_id, c, text, attachments, picks), + AppCommand::Plan(c) => self.plan_command(ctx, bot_id, c, text, attachments, picks), + AppCommand::Always(c) => self.always_command(ctx, bot_id, c), + AppCommand::Usage => self.usage_note(bot_id), + AppCommand::Context => self.context_note(bot_id), + AppCommand::Compact => self.compact_now(bot_id), + AppCommand::Doctor => self.doctor_start(bot_id), + AppCommand::Fork(name) => self.fork_now(bot_id, &name), + } + } + + /// 「/fork」: this conversation, as it is now, kept in 過去的對話 too. + pub(super) fn fork_now(&mut self, bot_id: u64, name: &str) { + let Some(b) = self.state.bot(bot_id) else { return }; + if self.streams.contains_key(&bot_id) { + let note = format!("{} 正在做事:等它做完(或按停止)再 /fork。", b.name); + self.goal_note(bot_id, ¬e); + return; + } + let words: Vec<&Message> = b.messages.iter().filter(|m| !m.note).collect(); + if words.is_empty() { + self.goal_note(bot_id, "對話還是空的,沒有東西可以分岔。"); + return; + } + let what = if name.is_empty() { words.iter().find(|m| m.role == Role::User).map(|m| first_line(&m.text, 24)).unwrap_or_else(|| "對話".into()) } else { name.to_string() }; + let title = format!("{FORK_PREFIX}{what}"); + let Some(b) = self.state.bot_mut(bot_id) else { return }; + let mut copy = b.messages.clone(); + // (Like any conversation put aside: its words, not its screenshots in memory.) + copy.iter_mut().for_each(|m| m.images.clear()); + b.threads.insert(0, (title.clone(), copy)); + b.threads.truncate(50); + self.goal_note(bot_id, &format!("已分岔:這段對話到這裡為止存了一份「{title}」。接著在這裡做;要回到這一點走另一條路,從 Bot 的選單 › 過去的對話 打開它(這邊會自動收進過去的對話,不會不見)。")); + self.mark(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn commands_are_told_apart() { + assert!(matches!(app_command("/fork"), Some(AppCommand::Fork(n)) if n.is_empty())); + assert!(matches!(app_command("/fork 試另一個做法"), Some(AppCommand::Fork(n)) if n == "試另一個做法")); + assert!(app_command("/forkx").is_none() && app_command("請 /fork").is_none()); + assert!(matches!(app_command("/btw 查天氣"), Some(AppCommand::Btw(t)) if t == "查天氣")); + assert!(matches!(app_command("/goal 測試通過"), Some(AppCommand::Goal(_)))); + assert!(matches!(app_command(" /compact "), Some(AppCommand::Compact))); + assert!(matches!(app_command("/doctor"), Some(AppCommand::Doctor))); + assert!(app_command("你好").is_none() && app_command("/Users/me/a.txt").is_none()); + } + + #[test] + fn a_fork_keeps_a_copy_and_goes_on_here() { + let ctx = egui::Context::default(); + let mut app = App::new(&ctx, Some(std::env::temp_dir().join("anybot-fork.png"))); + // (A bot not shown as working in the sample.) + let id = app.state.bots.iter().find(|b| b.members.is_empty() && !app.streams.contains_key(&b.id)).unwrap().id; + app.state.bot_mut(id).unwrap().messages = vec![Message::user("幫我寫一份報告"), Message::assistant("好,第一版在 report.md", "m")]; + app.run_app_command(&ctx, id, AppCommand::Fork("改寫成表格".into()), "/fork 改寫成表格", vec![], None); + let b = app.state.bot(id).unwrap(); + assert_eq!(b.threads[0].0, "分岔:改寫成表格"); + assert_eq!(b.threads[0].1.len(), 2); + assert!(b.messages.len() == 3 && b.messages[2].note && b.messages[2].text.contains("已分岔"), "{:?}", b.messages.last()); + // The two go their own ways: what is said here doesn't reach the copy. + app.state.bot_mut(id).unwrap().messages.push(Message::user("改用圖表")); + assert_eq!(app.state.bot(id).unwrap().threads[0].1.len(), 2); + // Opening the branch puts this one aside, and comes back at the point it was made. + app.open_thread(id, 0); + let b = app.state.bot(id).unwrap(); + assert_eq!(b.messages.len(), 2); + assert!(b.threads[0].1.iter().any(|m| m.text == "改用圖表")); + // Nothing to fork, or busy: a line says why. + app.state.bot_mut(id).unwrap().messages.clear(); + app.fork_now(id, ""); + assert!(app.state.bot(id).unwrap().messages[0].text.contains("空的")); + app.state.bot_mut(id).unwrap().messages = vec![Message::user("x")]; + let busy = app.state.bots.iter().find(|b| app.streams.contains_key(&b.id)).map(|b| b.id); + if let Some(busy) = busy { + app.state.bot_mut(busy).unwrap().messages.push(Message::user("在做")); + app.fork_now(busy, ""); + assert!(app.state.bot(busy).unwrap().messages.iter().any(|m| m.note && m.text.contains("正在做事"))); + } + } +} diff --git a/src/app/remote_ui.rs b/src/app/remote_ui.rs index 1efc019..67d4014 100644 --- a/src/app/remote_ui.rs +++ b/src/app/remote_ui.rs @@ -535,42 +535,11 @@ impl App { .collect(); // 目標: 「/goal …」 is AnyBot's, not the bot's (owner/admin only); anything else takes a paused goal up again. if !member { - // 支線: 「/btw …」 from the phone or a screen. - if let Some(task) = super::branch::command(&text) { - self.btw_command(ctx, bot, &task, atts); - return; - } - if let Some(cmd) = crate::goal::command(&text) { - self.goal_command(ctx, bot, cmd, &text, atts, memories); - return; - } - if let Some(cmd) = crate::plan::command(&text) { - self.plan_command(ctx, bot, cmd, &text, atts, memories); - return; - } - // 一律放行: the owner's (a teammate's words arrive as 「【名字】…」, never a command). - if let Some(cmd) = crate::always::command(&text) { - self.always_command(ctx, bot, cmd); - return; - } - if crate::usage::command(&text) { - self.usage_note(bot); - return; - } - match text.trim() { - "/context" => { - self.context_note(bot); - return; - } - "/compact" => { - self.compact_now(bot); - return; - } - _ => {} - } - // (From the phone or a screen: the brain checks itself.) - if crate::doctor::command(&text) { - self.doctor_start(bot); + // AnyBot's own commands from the phone or a screen (commands.rs): the owner's + // only (a teammate's words arrive as 「【名字】…」, never a command). A + // /doctor from there: the brain checks itself. + if let Some(cmd) = super::app_command(&text) { + self.run_app_command(ctx, bot, cmd, &text, atts, memories); return; } self.goal_heard(bot);