156 lines
7.6 KiB
Rust
156 lines
7.6 KiB
Rust
|
|
// 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<String> {
|
|||
|
|
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<AppCommand> {
|
|||
|
|
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<crate::attach::Attachment>, picks: Option<Vec<String>>) {
|
|||
|
|
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("正在做事")));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|