diff --git a/FEATURES.md b/FEATURES.md index 969ad10..a3e52d6 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -1943,7 +1943,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) - 執行:照線從觸發器往下(一張接到好幾張時上面的先、再左邊)。Bot 步驟是那個 Bot 聊天裡的一則「[工作流「名稱」第 k/n 步]…」加上一步的結果,它這一輪最後的回答交給下一步(它正忙就排在後面)。檢查回「通過」才往下。等你確認在畫布上方一條「繼續/停止」,並發通知。一次只跑一個,排程到了自動跑;卡片標完成、進行中、等你確認、失敗;執行紀錄留 20 次。 - 版本歷史:停手 20 秒、關畫布、每次執行前、Bot 每次改之前各留一版,最多 30 版,點一版就還原。 - 在聊天裡做:跟 Bot 用一般的話說要什麼流程(「每天早上 9 點去 Threads 找 5 則熱門育兒討論,整理好先問我,我說可以再傳給我」),Bot 用 `workflow` 工具建立、看、改(加步驟、改內容、刪步驟、連線、貼便利貼、整理排版)、執行、停止、刪除(刪除先問)。你看著那個 Bot 的聊天時,畫布自動開在聊天旁邊(視窗夠寬時),它改到的卡片和便利貼亮一下,跑出畫面就縮到看得到全部;Bot 做的排成一欄,插在中間時後面的往下移,你自己排過的不重排。旁邊的聊天跟著側邊欄選的 Bot 換。 -- 手機沒有畫布:在聊天裡請 Bot 做、改、跑,它用文字說工作流的樣子;「等你確認」的通知會到手機,按「繼續」要在電腦上。螢幕電腦(看著帳號裡大腦的那台)一樣能新增、修改、刪除、畫和註解,執行、停止、繼續交給大腦做;執行紀錄以大腦的為準。 +- 手機沒有畫布:在聊天裡請 Bot 做、改、跑,它用文字說工作流的樣子;「等你確認」的通知會到手機,按「繼續」要在電腦上。螢幕電腦(看著帳號裡大腦的那台)一樣能新增、修改、刪除、畫和註解,執行、停止、繼續交給大腦做;執行紀錄以大腦的為準。螢幕上的修改一張卡片、一個欄位地傳給大腦(舊版大腦收到的是整個工作流,跟以前一樣):你在螢幕上搬卡片、改名,Bot 同時在大腦上加的步驟和線都留著;兩邊同時新建、編號一樣的工作流各留一個,螢幕那個換新編號。同一張卡片兩邊都改了,後到的為準;線整組一起傳,兩邊同時拉線時後到的為準。 - 側邊欄最左邊一條直欄(照 OpenBot 的排版):上面「聊天、工作流、日程、任務、所有檔案」,下面「傳送、市集、設定」,現在在哪一個就亮哪一個,滑過寫名字;從 Mac 視窗左上角的紅黃綠按鈕下面開始。Bot 那一欄最上面只剩「搜尋」「+」,最下面是你的名字和 Bot 在哪一台(整個寬度)。以前這些全擠在最上面一排,側邊欄一窄就被切掉、或被紅黃綠按鈕壓到。 ### 日程(側邊欄最左邊那一欄的日曆圖示) diff --git a/docs/feature-map/workflow.md b/docs/feature-map/workflow.md index 153dd0c..b246cb7 100644 --- a/docs/feature-map/workflow.md +++ b/docs/feature-map/workflow.md @@ -42,6 +42,6 @@ - 改正在跑的工作流:那次照開始時的步驟走,刪掉的步驟輪到時會停下來(新加的步驟不會用到刪掉的編號)。 - AnyBot 關掉時正在跑的那次:再開時標成失敗「AnyBot 在執行中被關掉,這一步可能做到一半」;停在等你確認的那次留著,等著交付的那次重開後照樣交付。跑著的時候又有事件或排程到了,排隊等這次跑完接著跑(「執行紀錄」最上面寫還有幾次);按「停止」連排隊的一起清掉。 - 步驟的 Bot 在做的時候被刪掉:那次停下來,寫「這一步的 Bot 已經不在了」。 -- 螢幕電腦(看著帳號裡大腦的那台)一樣可以新增、修改、刪除、畫和註解,改的內容傳到大腦;執行、停止、繼續交給大腦做(大腦要是一樣新的版本)。執行紀錄永遠是大腦的:螢幕上慢一步的那份不會蓋掉正在跑的。Bot 在大腦上改了,螢幕上也會亮、會開在那個 Bot 的聊天旁邊。 +- 螢幕電腦(看著帳號裡大腦的那台)一樣可以新增、修改、刪除、畫和註解,改的內容一張卡片一張卡片傳到大腦(你搬一張卡、Bot 同時在大腦上加一步,兩個都留;兩邊同時新建的工作流各留一個,螢幕的換新編號);執行、停止、繼續交給大腦做(大腦要是一樣新的版本)。執行紀錄永遠是大腦的:螢幕上慢一步的那份不會蓋掉正在跑的。Bot 在大腦上改了,螢幕上也會亮、會開在那個 Bot 的聊天旁邊。 - (驗證)有真資料的桌面截圖前 500 格左右是啟動畫面:`ANYBOT_SHOT_FRAMES` 至少 520。 - (驗證)假模型的回答不是「通過」開頭:跑到檢查之前先 `$CTL model-reply '步・檢查' '通過'`。 diff --git a/src/app/mirror_ui.rs b/src/app/mirror_ui.rs index b17720b..8d4b13b 100644 --- a/src/app/mirror_ui.rs +++ b/src/app/mirror_ui.rs @@ -254,7 +254,75 @@ pub(super) fn patch_of(old: &Value, new: &Value) -> Value { } } -/// Apply a change made by `patch_of` to whatever the value is now. +/// `patch_of` all the way down (工作流): a changed item of a keyed list, or a changed field +/// that is itself a map or a keyed list, also goes as its own smaller change ("edit", by key) — +/// so a screen moving one card doesn't put the whole workflow back over a step a bot added at +/// the brain meanwhile. ("put" still carries the whole item: a brain from before this applies +/// that, as it always did.) The items the screen made are named in "new": one whose key the +/// brain has given to another meanwhile is added under a fresh id, not over it. +pub(super) fn deep_patch_of(old: &Value, new: &Value) -> Value { + let mut p = patch_of(old, new); + match (old, new) { + (Value::Object(o), Value::Object(n)) => { + let edit: serde_json::Map = n + .iter() + .filter_map(|(k, v)| o.get(k).filter(|was| *was != v).map(|was| (k, deep_patch_of(was, v)))) + .filter(|(_, d)| d.get("value").is_none()) + .map(|(k, d)| (k.clone(), d)) + .collect(); + if !edit.is_empty() { + p["map"]["edit"] = Value::Object(edit); + } + } + (Value::Array(o), Value::Array(n)) if p.get("list").is_some() => { + let key = |x: &Value| item_key(x).unwrap_or(Value::Null); + let mut edit = serde_json::Map::new(); + let mut fresh = vec![]; + for x in n { + match o.iter().find(|y| key(y) == key(x)) { + Some(y) if y != x => { + let d = deep_patch_of(y, x); + if d.get("value").is_none() { + edit.insert(key(x).to_string(), d); + } + } + Some(_) => {} + None => fresh.push(key(x)), + } + } + if !edit.is_empty() { + p["list"]["edit"] = Value::Object(edit); + } + if !fresh.is_empty() { + p["list"]["new"] = Value::Array(fresh); + } + } + _ => {} + } + p +} + +/// At the brain, before a screen's change to a list is applied (once: edits come numbered): an +/// item the screen made whose number the brain has given to another meanwhile gets a fresh +/// one, so it is added beside that one instead of over it. +pub(super) fn renumber_new(cur: &Value, p: &mut Value) { + let Some(l) = p.get_mut("list") else { return }; + let taken: Vec = cur.as_array().into_iter().flatten().filter_map(|x| x["id"].as_u64()).collect(); + let mut next = taken.iter().copied().max().unwrap_or(0) + 1; + let made: Vec = l["new"].as_array().cloned().unwrap_or_default(); + for k in made.iter().filter(|k| k.as_u64().is_some_and(|id| taken.contains(&id))) { + let fresh = json!(next); + next += 1; + for item in l["put"].as_array_mut().into_iter().flatten().filter(|x| x["id"] == *k) { + item["id"] = fresh.clone(); + } + for o in l["order"].as_array_mut().into_iter().flatten().filter(|x| *x == k) { + *o = fresh.clone(); + } + } +} + +/// Apply a change made by `patch_of` (or `deep_patch_of`) to whatever the value is now. pub(super) fn apply_patch(cur: &mut Value, p: &Value) { if let Some(v) = p.get("value") { *cur = v.clone(); @@ -264,7 +332,13 @@ pub(super) fn apply_patch(cur: &mut Value, p: &Value) { } let o = cur.as_object_mut().expect("object"); for (k, v) in m["put"].as_object().into_iter().flatten() { - o.insert(k.clone(), v.clone()); + // (Only what changed in it, onto what is there now.) + match (m["edit"].get(k), o.get_mut(k)) { + (Some(e), Some(x)) => apply_patch(x, e), + _ => { + o.insert(k.clone(), v.clone()); + } + } } for k in m["del"].as_array().into_iter().flatten().filter_map(Value::as_str) { o.remove(k); @@ -276,9 +350,11 @@ pub(super) fn apply_patch(cur: &mut Value, p: &Value) { let a = cur.as_array_mut().expect("array"); let key = |x: &Value| item_key(x).unwrap_or(Value::Null); for item in l["put"].as_array().into_iter().flatten() { - match a.iter_mut().find(|x| key(x) == key(item)) { - Some(x) => *x = item.clone(), - None => a.push(item.clone()), + let k = key(item); + match (a.iter_mut().find(|x| key(x) == k), l["edit"].get(k.to_string())) { + (Some(x), Some(edit)) => apply_patch(x, edit), + (Some(x), None) => *x = item.clone(), + (None, _) => a.push(item.clone()), } } let del: Vec = l["del"].as_array().cloned().unwrap_or_default(); @@ -298,7 +374,8 @@ pub(super) fn diff_ops(base: &Value, cur: &Value) -> Vec { continue; } if b.get(k) != Some(v) { - let p = patch_of(b.get(k).unwrap_or(&Value::Null), v); + // (工作流 card by card: see deep_patch_of.) + let p = if k == "workflows" { deep_patch_of(b.get(k).unwrap_or(&Value::Null), v) } else { patch_of(b.get(k).unwrap_or(&Value::Null), v) }; ops.push(if p.get("value").is_some() { json!({"op": "set", "key": k, "value": v}) } else { json!({"op": "patch", "key": k, "patch": p}) }); } } @@ -1132,7 +1209,11 @@ impl App { "patch" => { let key = op["key"].as_str().unwrap_or(""); let mut v = self.top_value(key); - apply_patch(&mut v, &op["patch"]); + let mut patch = op["patch"].clone(); + if key == "workflows" { + renumber_new(&v, &mut patch); + } + apply_patch(&mut v, &patch); self.brain_set_top(key, v) } "bot_patch" => { @@ -2714,6 +2795,54 @@ mod tests { assert_eq!(screen.wf.flash.as_ref().map(|f| f.1.clone()), Some(vec![3]), "what the bot added lights up"); } + /// A screen's edit to a workflow is card by card: moving a card and renaming it on the screen + /// keeps the step and line a bot added at the brain meanwhile; a workflow made on both sides + /// under the same number is kept twice, not one over the other. (Before: the screen's whole + /// workflow went back over the brain's, and the bot's step was gone.) + #[test] + fn a_screen_edit_to_a_workflow_keeps_what_a_bot_added_meanwhile() { + use crate::workflow::{Node, NodeKind, Workflow}; + let ctx = egui::Context::default(); + let dir = fresh_dir("screen-workflow-merge"); + let (mut brain, _screen) = brain_and_screen(&ctx, &dir); + let bot = brain.state.bots[0].id; + let mut w = Workflow::new(1, "早報", 0); + w.nodes.push(Node { id: 2, kind: NodeKind::Step { bot, prompt: "查".into() }, x: 0.0, y: 190.0 }); + assert!(w.connect(1, 2)); + brain.state.workflows = vec![w]; + let base = serde_json::to_value(&brain.state).unwrap(); + // At the brain, a bot adds a step and its line, and makes a workflow of its own (#2). + brain.state.workflows[0].nodes.push(Node { id: 3, kind: NodeKind::Notify { text: "好了".into() }, x: 0.0, y: 380.0 }); + assert!(brain.state.workflows[0].connect(2, 3)); + brain.state.workflows.push(Workflow::new(2, "Bot 做的", 0)); + // The screen, behind, moves card 2, renames the workflow, and makes one of its own (#2). + let mut cur = base.clone(); + cur["workflows"][0]["nodes"][1]["x"] = json!(120.0); + cur["workflows"][0]["name"] = json!("晚報"); + cur["workflows"].as_array_mut().unwrap().push(serde_json::to_value(Workflow::new(2, "螢幕做的", 0)).unwrap()); + let ops = diff_ops(&base, &cur); + // (A brain from before this reads only "put": the whole workflow, as it always did.) + let mut old_brain = base["workflows"].clone(); + let mut plain = ops[0]["patch"].clone(); + plain["list"].as_object_mut().unwrap().retain(|k, _| k == "put" || k == "del" || k == "order"); + apply_patch(&mut old_brain, &plain); + assert_eq!(old_brain[0]["name"], json!("晚報")); + // The screen doing its own edit again (as it does on a newer copy) adds nothing twice. + let mut again = cur.clone(); + apply_ops(&mut again, &ops); + assert_eq!(again["workflows"].as_array().unwrap().len(), 2, "{}", again["workflows"]); + let r = brain.brain_edit(&json!({"ops": ops, "client": "screen-1", "seq": 1})); + assert_eq!(r["applied"], json!(ops.len()), "{r}"); + let ws = &brain.state.workflows; + let first = &ws[0]; + assert_eq!(first.name, "晚報", "the screen's rename"); + assert_eq!(first.nodes.iter().find(|n| n.id == 2).map(|n| n.x), Some(120.0), "the screen's move"); + assert!(first.nodes.iter().any(|n| n.id == 3), "the bot's step stays: {:?}", first.nodes.iter().map(|n| n.id).collect::>()); + assert!(first.edges.iter().any(|e| e.from == 2 && e.to == 3), "and its line"); + let names: Vec<(u64, &str)> = ws.iter().map(|w| (w.id, w.name.as_str())).collect(); + assert_eq!(names, vec![(1, "晚報"), (2, "Bot 做的"), (3, "螢幕做的")], "both new ones, under their own numbers"); + } + /// 沒網路照樣能改設定: an edit made while the brain is out of reach waits in the queue; the /// brain's newer settings, when they come, keep it on top (nothing the brain changed /// meanwhile is undone), and once it is sent the brain has both.