diff --git a/FEATURES.md b/FEATURES.md index 9e7833c..2a4a5b8 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -1882,7 +1882,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) - 每 1 秒檢查一次。 - Bot 忙的時候,等它閒下來再跑;每個 Bot 一次只觸發一個。 -- Bot 收到的訊息是「⏰ 例行任務「標題」時間到了:要做的事」,然後像平常一樣工作。 +- Bot 收到的訊息是「⏰ 例行任務「標題」時間到了:要做的事」,然後像平常一樣工作。按「立即執行」時是「「標題」現在就執行(使用者叫的,不是排定的時間):要做的事」——不說時間到了,Bot 不會以為是排程的時間。 - 如果那個 Bot 不在目前畫面上,會標成未讀。 ### 例行任務視窗(頂部「例行任務」按鈕) @@ -1940,7 +1940,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) - 一個工作流是一張畫布:觸發器(手動、定時、Webhook、GitHub;Slack、新信等在聊天裡請 Bot 設,跟例行任務的事件觸發同一套,發生了什麼交給第一步),接著用線連起來的卡片。「+ 新增節點」可以搜尋,分三組:輸入(提示詞、素材)、步驟(LLM:直接問模型;助手:Bot 做;圖片、影片:請 Bot 做檔案)、流程(條件:是/否兩條路;審批;輸出:放進聊天和/或通知你)。原本的「檢查」就是條件,「交付」「通知你」併成一個輸出。選到的卡片上面有複製、刪除;助手、LLM、圖片、影片、條件有「試跑這一步」。從卡片底下的點拉到另一張就連線;不能連回觸發器、不能繞圈。 - 畫布:左上名稱和選單(每個工作流和步驟數、新建、版本歷史、重新命名、建立副本、刪除);右上「聊天」「執行紀錄」「▷ 執行工作流/■ 停止」;拖曳空白處移動、⌘+滾輪或捏合縮放;下面工具列「選取、拖曳|畫筆、便利貼、形狀、文字|搜尋|+ 新增節點」;左下縮放、符合畫面、小地圖。點卡片在右邊改設定,Delete 刪掉選到的。 - 畫和註解:畫筆、便利貼(六色、寫誰貼的、雙擊改字)、形狀、文字,只給人看、不會執行。 -- 執行:照線從觸發器往下(一張接到好幾張時上面的先、再左邊)。Bot 步驟是那個 Bot 聊天裡的一則「[工作流「名稱」第 k/n 步]…」加上一步的結果,它這一輪最後的回答交給下一步(它正忙就排在後面)。檢查回「通過」才往下。等你確認在畫布上方一條「繼續/停止」,並發通知。一次只跑一個,排程到了自動跑;卡片標完成、進行中、等你確認、失敗;執行紀錄留 20 次。 +- 執行:照線從觸發器往下(一張接到好幾張時上面的先、再左邊);觸發器後面還沒有步驟時不跑,說「這個工作流還沒有步驟,沒有執行:…」(以前馬上記一次「完成」,日程、執行紀錄、Bot 看到的「最近一次」都是假的)。Bot 步驟是那個 Bot 聊天裡的一則「[工作流「名稱」第 k/n 步]…」加上一步的結果(聊天裡泡泡上面一行小標、淡色泡泡,結果只顯示前三行,滑過看全文;以前看起來像你自己打的),它這一輪最後的回答交給下一步(它正忙就排在後面)。檢查回「通過」才往下。等你確認在畫布上方一條「繼續/停止」,並發通知。一次只跑一個,排程到了自動跑;卡片標完成、進行中、等你確認、失敗;執行紀錄留 20 次。 - 版本歷史:停手 20 秒、關畫布、每次執行前、Bot 每次改之前各留一版,最多 30 版,點一版就還原。 - 在聊天裡做:跟 Bot 用一般的話說要什麼流程(「每天早上 9 點去 Threads 找 5 則熱門育兒討論,整理好先問我,我說可以再傳給我」),Bot 用 `workflow` 工具建立、看、改(加步驟、改內容、刪步驟、連線、貼便利貼、整理排版)、執行、停止、刪除(刪除先問)。你看著那個 Bot 的聊天時,畫布自動開在聊天旁邊(視窗夠寬時),它改到的卡片和便利貼亮一下,跑出畫面就縮到看得到全部;Bot 做的排成一欄,插在中間時後面的往下移,你自己排過的不重排。旁邊的聊天跟著側邊欄選的 Bot 換。 - 手機沒有畫布:在聊天裡請 Bot 做、改、跑,它用文字說工作流的樣子;「等你確認」的通知會到手機,按「繼續」要在電腦上。螢幕電腦(看著帳號裡大腦的那台)一樣能新增、修改、刪除、畫和註解,執行、停止、繼續交給大腦做;執行紀錄以大腦的為準。螢幕上的修改一張卡片、一個欄位地傳給大腦(舊版大腦收到的是整個工作流,跟以前一樣):你在螢幕上搬卡片、改名,Bot 同時在大腦上加的步驟和線都留著;兩邊同時新建、編號一樣的工作流各留一個,螢幕那個換新編號。同一張卡片兩邊都改了,後到的為準;線整組一起傳,兩邊同時拉線時後到的為準。 @@ -1950,9 +1950,9 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) 照 OpenBot 日程的排版做的,風格是 AnyBot 自己的;功能地圖 schedule.md。不用一個個問 Bot「你今天要幹嘛」:每個 Bot 的例行任務(照排程的)和有排程的工作流放在同一個日曆上。 -- 「日/週/月」三種看法,「‹ 今天 ›」往前往後;右上角寫時區(「時間以 Asia/Taipei 為準」)。週和月從週一開始,今天那格是強調色的框。 -- 跑過的照執行紀錄:✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中;還沒到的照排程算出來(跟真的排程同一套算法),暫停的變灰帶 ‖。事件觸發的例行任務只看得到跑過的。週每筆是時間、Bot 的頭像和名稱;月每天最多三筆、多的「還有 N 個」,點那天換成日看法;日照時間一列列。 -- 下面「即將執行」:所有 Bot 接下來最先要跑的六筆,帶頭像。 +- 「日/週/月」三種看法,「‹ 今天 ›」往前往後;時區跟這台電腦不同時(看的是別處的大腦),右上角寫「時間以 … 為準」。週和月從週一開始,今天那格是強調色的框。 +- 排程或事件自己跑的那幾次照執行紀錄:✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中。你按的「立即執行」「執行工作流」、請 Bot 跑的、試跑一步都不放(以前一按就出現在日曆上,像是排好的事);它們在執行紀錄裡。還沒到的照排程算出來(跟真的排程同一套算法),暫停的變灰帶 ‖。事件觸發的例行任務只看得到跑過的。週每筆是時間、Bot 的頭像和名稱;月每天最多三筆、多的「還有 N 個」,點那天換成日看法;日照時間一列列。 +- 下面「即將執行」:每個例行任務、工作流各一行,寫它下一次什麼時候跑,照時間排、最多六行,帶頭像(以前一個每天跑的就把六行占滿)。 - 點任何一筆打開卡片:哪個 Bot、那次的時間和狀態、怎麼重複、最近一次怎麼樣、要它做什麼;「打開聊天」「立即執行」「暫停/繼續」(工作流:「打開工作流」「立即執行」)。 - 「+ 新增例行任務」回到 Bot 的聊天,輸入框先放好「每天早上 9 點幫我」,接著用一般的話說要做什麼。手機還沒有日程。 diff --git a/docs/feature-map/routines.md b/docs/feature-map/routines.md index ebea711..68d3da4 100644 --- a/docs/feature-map/routines.md +++ b/docs/feature-map/routines.md @@ -6,7 +6,7 @@ Bot 照排程(每天、每週、每月、只一次)或某件事發生時(P - `routines-schedule` 排程:每天/每週幾/每月幾號/只一次,時區照使用者的設定。 - `routines-trigger` 事件觸發:GitHub、Slack、Sentry、PagerDuty、Linear、新信、webhook、行事曆等,到了才跑,沒有排程時間;跟排程的一樣留執行紀錄、照完成條件、失敗會停。發生了什麼(貼文、信、PR 標題)當成外面來的資料交給 Bot,不當指示。 -- `routines-fresh` 每次執行是新的開始:Bot 從這則任務訊息(「[例行任務]「名稱」時間到了:…」)讀起,不重讀前幾次的對話;要延續的東西放頁面或記憶。 +- `routines-fresh` 每次執行是新的開始:Bot 從這則任務訊息(「[例行任務]「名稱」時間到了:…」;立即執行是「…現在就執行(使用者叫的,不是排定的時間):…」)讀起,不重讀前幾次的對話;要延續的東西放頁面或記憶。 - `routines-runs` 執行紀錄:每條保留最近 20 次的開始時間、花多久、結果(完成/失敗/被停止/中斷)、走幾步、幾步沒成功、原因,以及系統看到送出的每一則(回哪篇 → 留言網址)。 - `routines-carry` 上次的問題帶進下次:上次失敗,下次的指令會附原因;上次被 AnyBot 關掉打斷,會提醒先確認做過哪些。 - `routines-pause` 自動暫停:連續失敗 2 次就暫停,聊天裡留一行原因,不在電腦前時跳通知。按「繼續」清掉原因。 @@ -30,7 +30,7 @@ Bot 照排程(每天、每週、每月、只一次)或某件事發生時(P - **建三條**(covers `routines-schedule`、`routines-target`):首頁「例行任務」›「新增例行任務」建一條每天的;建一個 Bot「同事」,在 Bot 1 的聊天請它建兩條要送 1 則的。指令:`$CTL phone click '[data-q="routines"]' && $CTL phone click '#rt-new' && $CTL phone type '#rt-title' 早報 && $CTL phone type '#rt-prompt' 整理重點 && $CTL phone click '#rt-ok' && $CTL phone wait-text '每天 09:00' --in '#list' && $CTL phone open --listed && $CTL phone click '#b-newbot' && $CTL phone click '#sheet-in .opt' --text '建立 Bot' && $CTL phone type '#ask-in' 同事 && $CTL phone click '#ask-ok' && $CTL phone wait-text 同事 --in '#isl-name' && $CTL phone open --listed && $CTL phone click '.row[data-bot="1"]' && $CTL phone send '[[call create_routine {"title":"回覆","prompt":"回一則","schedule":{"frequency":"weekly","daysOfWeek":["MO","WE"],"time":"10:00"},"target_sends":1}]]' && $CTL phone wait-text '已建立例行任務 #2' --in '#msgs' && $CTL phone send '[[call create_routine {"title":"打招呼","prompt":"跟同事說早安","schedule":{"frequency":"monthly","monthDay":1,"time":"08:00"},"target_sends":1}]]' && $CTL phone wait-text '已建立例行任務 #3' --in '#msgs' && $CTL api GET /api/bots/1/detail --jq '[.routines[] | "\(.title) \(.repeat) \(.when) \(.target)"]'`。看到:「每天 09:00 · 下次 …」;「已建立例行任務 #2 「回覆」:每週…」;三條的排程,後兩條「完成條件:送出 1 則」。 - **立即執行、紀錄**(covers `routines-now`、`routines-runs`、`routines-fresh`):點「早報」›「立即執行」,重新整理再點開。指令:`$CTL phone open --listed && $CTL phone click '[data-q="routines"]' && $CTL phone click '[data-rt="1:1"]' && $CTL phone click '#sheet-in .opt' --text 立即執行 && $CTL wait-api /api/bots/1/detail '.routines[0].runs[0].line | test("完成")' && $CTL phone open --listed && $CTL phone click '[data-q="routines"]' && $CTL phone click '[data-rt="1:1"]' && $CTL phone wait-text 最近執行 --in '#sheet-in' && $CTL phone shot routines-run && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.wake) | .text]'`。看到:Bot 1 收到「[例行任務]「早報」時間到了:整理重點」;「最近執行」一行「…完成:FAKE-MODEL 回答:…」、「上次 今天 …」。 - **不到數算失敗、兩次就暫停、繼續**(covers `routines-target`、`routines-pause`、`routines-carry`、`routines-runs`):「回覆」立即執行兩次,再按「繼續」。指令:`$CTL phone click '[data-back]' && $CTL api POST /api/act/app '{"name":"run_routine_now","args":{"id":2},"bot":1}' && $CTL wait-api /api/bots/1/detail '.routines[1].runs[0].bad' && $CTL api POST /api/act/app '{"name":"run_routine_now","args":{"id":2},"bot":1}' && $CTL wait-api /api/bots/1/detail '.routines[1].paused' && $CTL phone open --listed && $CTL phone click '.row[data-bot="1"]' && $CTL phone wait-text '連續 2 次失敗,AnyBot 先暫停了' --in '#msgs' && $CTL phone click '#island' && $CTL phone click '[data-tab="routines"]' && $CTL phone wait-text 打招呼 --in '#details' && $CTL wait-api /api/bots/1/detail '.routines | length == 3' && $CTL api GET /api/bots/1 --jq '[.messages[] | select(.wake) | .text][-1]' && $CTL phone open --listed && $CTL phone click '[data-q="routines"]' && $CTL phone click '[data-rt="1:2"]' && $CTL phone wait-text '只送出 0/1 則(系統計數)' --in '#sheet-in' && $CTL phone click '#sheet-in .opt' --text 繼續 && $CTL wait-api /api/bots/1/detail '.routines[1] | (.paused | not) and .paused_why == ""'`。看到:兩次都是「失敗:只送出 0/1 則(系統計數):…」;第二次的任務訊息多「(上一次執行沒有成功:只送出 0/1 則…)」;聊天「例行任務「回覆」連續 2 次失敗,AnyBot 先暫停了。…按繼續。」;詳細資訊的「例行任務」有三條;按繼續後不再暫停、原因清掉。 -- **送到了、送出證據**(covers `routines-target`、`routines-runs`):「打招呼」立即執行,它傳訊息給同事。指令:`$CTL model-reply '「打招呼」時間到了' '[[call message_bot {"bot":"同事","text":"早安"}]] 送了' && $CTL api POST /api/act/app '{"name":"run_routine_now","args":{"id":3},"bot":1}' && $CTL wait-api /api/bots/1/detail '.routines[2].runs[0].line | test("完成")' && $CTL api GET /api/bots/1/detail --jq '.routines[2].runs[0]' && $CTL api GET /api/bots/2 --jq '[.messages[].text]'`。看到:「完成(1 步,系統記到送出 1 次):送了」,送出證據一行「傳訊息給其他 Bot 給 同事:早安」(照步驟的名字寫,不是 message_bot);同事收到「[來自 Bot]「驗證小幫手」:早安」。 +- **送到了、送出證據**(covers `routines-target`、`routines-runs`):「打招呼」立即執行,它傳訊息給同事。指令:`$CTL model-reply '「打招呼」現在就執行' '[[call message_bot {"bot":"同事","text":"早安"}]] 送了' && $CTL api POST /api/act/app '{"name":"run_routine_now","args":{"id":3},"bot":1}' && $CTL wait-api /api/bots/1/detail '.routines[2].runs[0].line | test("完成")' && $CTL api GET /api/bots/1/detail --jq '.routines[2].runs[0]' && $CTL api GET /api/bots/2 --jq '[.messages[].text]'`。看到:「完成(1 步,系統記到送出 1 次):送了」,送出證據一行「傳訊息給其他 Bot 給 同事:早安」(照步驟的名字寫,不是 message_bot);同事收到「[來自 Bot]「驗證小幫手」:早安」。 - **桌面的例行任務視窗**(covers `routines-runs`、`routines-now`):指令:要開視窗——`$CTL stop && ABV_ALLOW_WINDOW=1 $CTL desktop-shot routines ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=panel && ABV_ALLOW_WINDOW=1 $CTL desktop-shot routines-demo ANYBOT_SHOT_VIEW=routines`(第二張是示範資料:真資料的截圖開不了例行任務視窗)。看到:右側面板「例行任務」列出這次建的(標題、時間或「已暫停」);示範視窗只有一張「AI 新聞早報」卡——示範資料沒有暫停原因、完成條件、執行紀錄,這三樣靠上面的步驟和 `cargo test routines::`。 - **事件觸發**(covers `routines-trigger`):指令:走不到——GitHub、Slack…要真的帳號;webhook 聽在 127.0.0.1:8792 起,ctl 沒有打它的指令(使用者真的 AnyBot 也可能在 8792)。看到:最接近的真路是 `create_routine` 帶 trigger 建得起來、沒有「下次執行時間」,和自動測試。 - 自動測試:`cargo test routines::`、`cargo test appctl::`、`cargo test a_routines_runs_are_kept_and_two_failures_pause_it`、`cargo test a_routine_run_starts_fresh_for_the_model`、`cargo test triggers::`、`cargo test garden::`。 diff --git a/docs/feature-map/schedule.md b/docs/feature-map/schedule.md index 795c022..15456ba 100644 --- a/docs/feature-map/schedule.md +++ b/docs/feature-map/schedule.md @@ -4,9 +4,9 @@ ## 子功能 -- `schedule-views` 看法:右上「日/週/月」,左上「‹ 今天 ›」往前往後、回到今天,旁邊寫「2026年11月」「11月16日 – 22日」「11月21日 星期六」;右上角寫「時間以 Asia/Taipei 為準」(你的時區)。週和月從週一開始,今天那格是強調色的框。 -- `schedule-entries` 每一筆:跑過的照它的執行紀錄(✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中),還沒到的照排程算出來(圓點;暫停的變灰、帶 ‖)。週:每天一欄,每筆是時間、Bot 的頭像,下面是名稱。月:每天最多三筆「時間 名稱」,多的寫「還有 N 個」;點那一天換成那天的日看法。日:照時間一列列,寫「名稱 · 時間 · 怎麼重複 · 啟用中/已暫停/完成」。事件觸發的例行任務沒有預定的時間,只看得到跑過的。時間到了但 Bot 正忙的,照原來的時間留著(圓點),等它空下來跑。每小時跑的排滿月看法的六週;每 5 分鐘跑的大約只列得下四天。 -- `schedule-coming` 即將執行:所有 Bot 接下來兩週內最先要跑的六筆(暫停的不算),每筆「名稱 · 明天 08:30 · 每天 08:30」和 Bot 的頭像;沒有時說怎麼設一個。 +- `schedule-views` 看法:右上「日/週/月」,左上「‹ 今天 ›」往前往後、回到今天,旁邊寫「2026年11月」「11月16日 – 22日」「11月21日 星期六」;時區跟這台電腦不同時(看的是別處的大腦),右上角寫「時間以 … 為準」。週和月從週一開始,今天那格是強調色的框。 +- `schedule-entries` 每一筆:排程或事件自己跑的照執行紀錄(✓ 完成、✗ 失敗或中斷、■ 停止、圓點執行中;你按的「立即執行」「執行工作流」、請 Bot 跑的、試跑一步都不放,看執行紀錄),還沒到的照排程算出來(圓點;暫停的變灰、帶 ‖)。週:每天一欄,每筆是時間、Bot 的頭像,下面是名稱。月:每天最多三筆「時間 名稱」,多的寫「還有 N 個」;點那一天換成那天的日看法。日:照時間一列列,寫「名稱 · 時間 · 怎麼重複 · 啟用中/已暫停/完成」。事件觸發的例行任務沒有預定的時間,只看得到跑過的。時間到了但 Bot 正忙的,照原來的時間留著(圓點),等它空下來跑。每小時跑的排滿月看法的六週;每 5 分鐘跑的大約只列得下四天。 +- `schedule-coming` 即將執行:每個例行任務、工作流各一行,寫它下一次(兩週內,暫停的不算),照時間排、最多六行:「名稱 · 明天 08:30 · 每天 08:30」和 Bot 的頭像;沒有時說怎麼設一個。 - `schedule-card` 點任何一筆打開卡片:哪個 Bot、那一次的時間和狀態、怎麼重複、最近一次怎麼樣、要它做什麼;按鈕「打開聊天」「立即執行」「暫停/繼續」。工作流的是「打開工作流」「立即執行」(在螢幕電腦上按,交給大腦執行)。 - `schedule-new` 「+ 新增例行任務」:回到那個 Bot 的聊天,輸入框先放好「每天早上 9 點幫我」,接著說要做什麼;Bot 用 `create_routine` 設好,日程上就出現。 @@ -23,9 +23,9 @@ - 基本前提和「手機已登入」(README),開著 Bot 1 的聊天:`$CTL phone click '.row[data-bot="1"]'`。桌面的步驟要開視窗(`ABV_ALLOW_WINDOW=1`,先 `ctl stop`),座標照 1400×900(打開日程那一步是 1200×820)。 -- **在聊天裡設兩個例行任務、先跑一次**(covers `schedule-entries`、`schedule-new`):指令:`$CTL phone send '每天早上 8:30 掃競品 [[call create_routine {"title":"競品掃描","prompt":"找三則競品新聞","schedule":{"frequency":"daily","time":"08:30"}}]]' && $CTL phone wait-text '已建立例行任務' --in '#msgs' 60 && $CTL phone send '工作日 9 點晨報 [[call create_routine {"title":"晨間簡報","prompt":"整理今天要做的事","schedule":{"frequency":"weekly","daysOfWeek":["MO","TU","WE","TH","FR"],"time":"09:00"}}]]' && $CTL phone wait-text '晨間簡報' --in '#msgs' 60 && $CTL phone send '先跑一次競品 [[call run_routine_now {"id":1}]]' && $CTL wait-api /api/bots/1 '.messages[-1].role == "assistant" and ((.messages[-1].text // "") | contains("找三則競品新聞"))' 60 && $CTL phone stop && $CTL stop && $CTL state '[.bots[0].routines[] | {id, title, runs: [.runs[]? | .outcome]}]'`。看到:兩個都建立了;競品掃描的 runs 是 ["succeeded"],晨間簡報還沒有。 -- **從側邊欄打開日程**(covers `schedule-views`、`schedule-entries`、`schedule-coming`):按側邊欄的日曆。指令:要開視窗——`ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-open ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_SIZE=1200x820 ANYBOT_SHOT_FRAMES=520 'ANYBOT_SHOT_CLICK=26,152@440'`。看到:「日程」,週看法這一週,今天那格強調色框、裡面 ✓ 剛才的時間「競品掃描」,之後每天 08:30 圓點「競品掃描」;工作日 09:00「晨間簡報」從週一起;右上「時間以 Asia/Taipei 為準」;下面「即將執行」六筆照時間排、每筆有 Bot 的頭像。 -- **月和日**(covers `schedule-views`、`schedule-entries`):指令:要開視窗——`ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-month ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_TAB=month ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=520 && ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-day ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_TAB=day ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=520`。看到:月:週一到週日的格子,這個月以外的日期灰字,工作日的格子兩筆(08:30、09:00),今天之前沒跑過的日子是空的;日:今天的「競品掃描 · 時間 · 每天 08:30 · 完成」,左邊寫幾時。 +- **在聊天裡設兩個例行任務、先跑一次**(covers `schedule-entries`、`schedule-new`):指令:`$CTL phone send '每天早上 8:30 掃競品 [[call create_routine {"title":"競品掃描","prompt":"找三則競品新聞","schedule":{"frequency":"daily","time":"08:30"}}]]' && $CTL phone wait-text '已建立例行任務' --in '#msgs' 60 && $CTL phone send '工作日 9 點晨報 [[call create_routine {"title":"晨間簡報","prompt":"整理今天要做的事","schedule":{"frequency":"weekly","daysOfWeek":["MO","TU","WE","TH","FR"],"time":"09:00"}}]]' && $CTL phone wait-text '晨間簡報' --in '#msgs' 60 && $CTL phone send '先跑一次競品 [[call run_routine_now {"id":1}]]' && $CTL wait-api /api/bots/1 '.messages[-1].role == "assistant" and ((.messages[-1].text // "") | contains("找三則競品新聞"))' 60 && $CTL phone stop && $CTL stop && $CTL state '[.bots[0].routines[] | {id, title, runs: [.runs[]? | .outcome]}]'`。看到:兩個都建立了;競品掃描的 runs 是 ["succeeded"](state 裡 by_hand 是 true),晨間簡報還沒有。 +- **從側邊欄打開日程**(covers `schedule-views`、`schedule-entries`、`schedule-coming`):按側邊欄的日曆。指令:要開視窗——`ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-open ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_SIZE=1200x820 ANYBOT_SHOT_FRAMES=520 'ANYBOT_SHOT_CLICK=26,152@440'`。看到:「日程」,週看法這一週,今天那格強調色框、裡面沒有剛才立即執行的那次,之後每天 08:30 圓點「競品掃描」;工作日 09:00「晨間簡報」從週一起;右上沒有時區(跟這台一樣);下面「即將執行」競品掃描、晨間簡報各一行,照時間排、有 Bot 的頭像。 +- **月和日**(covers `schedule-views`、`schedule-entries`):指令:要開視窗——`ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-month ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_TAB=month ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=520 && ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-day ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_TAB=day ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=520`。看到:月:週一到週日的格子,這個月以外的日期灰字,工作日的格子兩筆(08:30、09:00),今天之前沒跑過的日子是空的;日:今天寫「這一天沒有排定的事。」(立即執行的不放),下面照樣是即將執行。 - **卡片和暫停**(covers `schedule-card`、`schedule-coming`):按即將執行第一筆,再按卡片上的「暫停」。指令:要開視窗——`ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-card ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=560 'ANYBOT_SHOT_CLICK=700,439@530' && ABV_ALLOW_WINDOW=1 $CTL desktop-shot schedule-paused ANYBOT_SHOT_REAL=1 ANYBOT_SHOT_VIEW=schedule ANYBOT_SHOT_SIZE=1400x900 ANYBOT_SHOT_FRAMES=580 'ANYBOT_SHOT_CLICK=700,439@530;705,558@545' && $CTL state '[.bots[0].routines[] | {title, paused}]'`。看到:卡片「競品掃描」:驗證小幫手的頭像和名字、「明天 08:30(啟用中)」「每天 08:30」、最近一次完成、「找三則競品新聞」,按鈕「打開聊天、立即執行、暫停」;暫停後之後的競品掃描變灰帶 ‖,即將執行只剩晨間簡報;state 裡競品掃描 paused 是 true。 - 自動測試:`cargo test schedule`、`cargo test layout_audit::`(「日程」週、月、日三景)。 diff --git a/docs/feature-map/workflow.md b/docs/feature-map/workflow.md index b246cb7..1101316 100644 --- a/docs/feature-map/workflow.md +++ b/docs/feature-map/workflow.md @@ -7,7 +7,7 @@ - `workflow-canvas` 畫布:左上是名稱,按下去是選單(每個工作流和它的步驟數、新建工作流、版本歷史、重新命名、建立副本、刪除工作流);右上「聊天」(聊天放在旁邊或收起來)、「執行紀錄」、「▷ 執行工作流」(跑的時候變「■ 停止」)。點點底的畫布拖曳空白處移動、⌘+滾輪或捏合縮放;下面工具列「選取、拖曳|畫筆、便利貼、形狀、文字|搜尋|+ 新增節點」,左下「- 百分比 +|符合畫面|小地圖」。搜尋卡片和註解的字,Enter 跳到下一個。視窗窄時縮放列移到工具列上面。 - `workflow-nodes` 卡片:觸發器(手動、定時(每天、每週…,寫「下次執行」)、Webhook(給一個呼叫網址和密碼)、GitHub(存放庫和事件:PR 合併、CI 失敗…);Slack、新信這些在聊天裡請 Bot 設,跟例行任務的事件觸發是同一套;事情發生時,發生了什麼交給第一步;都有「立即執行」)。「+ 新增節點」可以搜尋,分三組——輸入:提示詞(一段話交給下一步)、素材(檔案,Bot 收到附件);步驟:LLM(直接問模型,選模型、系統提示詞,不用 Bot)、助手(Bot 用它的工具做)、圖片、影片(請 Bot 做出檔案);流程:條件(成立走「是」、不成立走「否」,兩個點各拉一條線;「否」沒接就停下來說原因)、審批(停下來等你按繼續)、輸出(放進哪個 Bot 的聊天,和/或通知你,「{結果}」換成上一步結果的第一行)。舊的「檢查」就是條件,「交付」「通知你」併成輸出。從卡片底下的點拉到另一張卡就連線;不能連回觸發器、不能繞成一圈。點卡片在右邊改設定,選到的卡片上面有「複製」「刪除」;助手、LLM、圖片、影片、條件有「試跑這一步」(只跑這一步,用前一步上次的結果)。Delete 刪掉選到的卡片或線(觸發器刪不掉)。 - `workflow-draw` 畫和註解:畫筆、便利貼(六種顏色,下面寫誰貼的,雙擊改字)、形狀、文字;點選後右邊換顏色、改文字、刪除。只是給人看,不會執行。 -- `workflow-run` 執行:照線從觸發器往下走(一張接到好幾張時,上面的先,再左邊)。Bot 步驟是那個 Bot 聊天裡的一則訊息「[工作流「名稱」第 k/n 步]…」加上上一步的結果,它這一輪最後的回答就是這一步的結果;它正忙就排在後面,等它做完手上那一輪。檢查要回「通過」才往下,否則停下並寫原因。等你確認:畫布上方一條「等你確認:…」,「繼續」/「停止」,並發通知(電腦和手機)。交付把結果放進那個 Bot 的聊天(它正在回答時,等這一輪結束才放);通知你發通知。每一步不另外通知「完成」,只有審批和輸出會通知;Bot 的步驟算沒人在看的一輪(卡在等允許 15 分鐘就結束)。一次只跑一個;排程到了自動跑。停止時,還排在 Bot 後面的那一步問題直接拿掉,不會停掉 Bot 手上別的事。卡片上標完成、進行中、等你確認、失敗;「執行紀錄」留最近 20 次(誰開始的、每一步的結果、為什麼停)。 +- `workflow-run` 執行:照線從觸發器往下走(一張接到好幾張時,上面的先,再左邊)。Bot 步驟是那個 Bot 聊天裡的一則訊息「[工作流「名稱」第 k/n 步]…」加上上一步的結果(聊天裡泡泡上面一行小標「工作流「名稱」第 k/n 步」、淡色泡泡,結果只顯示前三行,滑過看全文),它這一輪最後的回答就是這一步的結果;它正忙就排在後面,等它做完手上那一輪。檢查要回「通過」才往下,否則停下並寫原因。等你確認:畫布上方一條「等你確認:…」,「繼續」/「停止」,並發通知(電腦和手機)。交付把結果放進那個 Bot 的聊天(它正在回答時,等這一輪結束才放);通知你發通知。每一步不另外通知「完成」,只有審批和輸出會通知;Bot 的步驟算沒人在看的一輪(卡在等允許 15 分鐘就結束)。一次只跑一個;排程到了自動跑。觸發器後面還沒有步驟時,按「執行工作流」只說「這個工作流還沒有步驟,沒有執行:…」,不記一次執行。停止時,還排在 Bot 後面的那一步問題直接拿掉,不會停掉 Bot 手上別的事。卡片上標完成、進行中、等你確認、失敗;「執行紀錄」留最近 20 次(誰開始的、每一步的結果、為什麼停);日程只放定時和觸發跑的那幾次。 - `workflow-chat` 聊出來:在聊天裡請 Bot 做工作流,它用 `workflow` 工具建立、看、改(加步驟、改內容、刪步驟、連線、貼便利貼、整理排版)、執行、停止、刪除(刪除先問你)。你正看著那個 Bot 的聊天時(只有它),畫布自動開在聊天旁邊,跳到那個工作流,它改到的卡片和便利貼亮一下;跑出畫面就自動縮放到看得到全部。Bot 做的排成一欄,插在中間時後面的往下移;你自己排過的不重排。要刪除時,允許卡片寫的就是會刪的那一個(沒指定時是畫布上開著的)。旁邊的聊天跟著側邊欄選的 Bot 換。Bot 知道有哪些工作流(說明裡列著),你在畫布旁邊打的每一則都帶著「開著工作流「X」(#1)的畫布」,所以「這個」「畫布上」它知道是哪一個。 - `workflow-versions` 版本歷史:改完停手 20 秒、關掉畫布、每次執行前、Bot 每次改之前,各留一版(一樣的不重複留),最多 30 版;選單 › 版本歷史點一版就還原,還原前先留目前的樣子。 diff --git a/mobile/index.html b/mobile/index.html index 395cab5..5bcd02b 100644 --- a/mobile/index.html +++ b/mobile/index.html @@ -142,6 +142,7 @@ a{color:var(--accent-ink)} /* The chat takes its bot's colour (--bot, set when the chat opens; a black bot keeps the plain bubble). */ .m.user .bub{background-color:var(--bot,var(--bubble-user));color:var(--bot-ink,var(--bubble-user-ink));white-space:pre-wrap;border-radius:var(--r-l) var(--r-l) var(--r-s) var(--r-l)} .quiet .m.user .bub{background-color:var(--hover);color:var(--text)} +.m.user.woke .bub{background-color:var(--hover);color:var(--text)} details.dt{margin:var(--sp-3) 0}details.dt>summary{font-size:var(--fs-small);color:var(--text2);cursor:pointer;list-style:none} details.dt>summary::-webkit-details-marker{display:none}details.dt>summary::before{content:"▸ "}details.dt[open]>summary::before{content:"▾ "} details.dt>.dt-in{padding-left:var(--sp-4);margin-top:var(--sp-2)} @@ -768,6 +769,7 @@ button,.row,.ib,.island,.opt,.chip,.tab,label.sw,[role="button"]{-webkit-user-se "use strict"; // Lucide icons (ISC licence), the set the desktop app uses. const LUCIDE = { + "workflow": '', "send": '', "image": '', "file": '', @@ -2041,7 +2043,7 @@ function renderMsgs() { const mems = m.memories || []; const open = S.carried.has(String(m.at || i)); const carried = mems.length ? `${open ? `
${mems.map((x) => "• " + esc(x)).join("
")}
` : ""}` : ""; - h += `
${tag}${m.text ? `
${quote}${esc(text)}
` : ""}${(m.attachments || []).map(attHtml).join("")}${reacts}${carried}
`; + h += `
${tag}${m.text ? `
${quote}${esc(text)}
` : ""}${(m.attachments || []).map(attHtml).join("")}${reacts}${carried}
`; return; } const t = turnOf[i]; @@ -2362,6 +2364,21 @@ function wakeTag(text) { const t = String(text || ""); for (const [tag, icon, label] of WAKE_TAGS) if (t.startsWith(tag)) return {icon, label, body: t.slice(tag.length)}; if (t.startsWith("[監看 ")) return {icon: "eye", label: "監看", body: t}; + // A workflow's question: its tag, and the previous step's result cut short (whole on tap-and-hold → 複製). + const wf = t.match(/^[(工作流「[^」]*」第 \d+\/\d+ 步[^]]*)]/); + if (wf) { + let body = t.slice(wf[0].length).trim(); + // (A step hands on 「上一步的結果:」; a 檢查 shows what it checks as 「結果:」.) + for (const mark of ["\n\n上一步的結果:", "\n\n結果:"]) { + const cut = body.indexOf(mark); + if (cut < 0) continue; + const last = body.slice(cut + mark.length).trim(); + const short = last.split("\n").slice(0, 3).join("\n").slice(0, 160); + body = body.slice(0, cut) + mark + short + (short.length < last.length ? "…" : ""); + break; + } + return {icon: "workflow", label: wf[1], body}; + } return null; } /** The skills a draft like 「/pp」 asks for, as the brain ranks them: name starts with it, name diff --git a/src/app.rs b/src/app.rs index 978fb73..c57eda6 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2541,6 +2541,7 @@ impl App { let selected = self.state.selected; if let Some(b) = self.state.bot_mut(bot_id) { let routine = msg.routine; + let by_hand = appctl::is_run_now(&msg.text); b.messages.push(msg); if bot_id != selected { b.unread = true; @@ -2548,6 +2549,9 @@ impl App { // 例行任務: this run is on its record from now (its end is noted in settle). if let Some(r) = b.routines.iter_mut().find(|r| routine != 0 && r.id == routine) { r.run_started(crate::routines::now()); + if let Some(run) = r.runs.last_mut() { + run.by_hand = by_hand; + } } } self.start_reply(ctx, bot_id); @@ -5729,7 +5733,7 @@ impl App { } if let Some(r) = b.routines.iter_mut().find(|r| r.id == id) { r.last_run = Some(now); - wake = Some(appctl::wake_message(r)); + wake = Some(appctl::run_now_message(r)); } } _ => self.confirm_delete_routine = Some((bot_id, id)), diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs index 83abc56..b3a310b 100644 --- a/src/app/chat_ui.rs +++ b/src/app/chat_ui.rs @@ -593,33 +593,49 @@ impl App { }); ui.add_space(6.0); } - // Messages that woke the bot (a routine, a trigger, a channel, another bot) carry a tag: - // shown as a small line with an icon above the bubble, not inside it. + // Messages that woke the bot (a routine, a trigger, a channel, another bot, a workflow's + // step) carry a tag: shown as a small line with an icon above the bubble, not inside it, + // and the bubble is a quiet one — the person didn't type it just now. let (tag, body) = wake_tag(&m.text); + let flow = workflow_tag(&m.text); + let woke = tag.is_some() || flow.is_some(); if let Some((icon, label)) = tag { ui.with_layout(Layout::right_to_left(Align::Min), |ui| { crate::icons::label(ui, icon, &t(label), 12.0, p.text3); }); ui.add_space(2.0); + } else if let Some((label, _)) = flow { + ui.with_layout(Layout::right_to_left(Align::Min), |ui| { + crate::icons::label(ui, crate::icons::Icon::Workflow, label, 12.0, p.text3); + }); + ui.add_space(2.0); } - let shown_text = body.to_string(); + let shown_text = match flow { + Some((_, words)) => workflow_words(words), + None => body.to_string(), + }; + let full_text = flow.map(|(_, words)| words.trim().to_string()); if !m.text.trim().is_empty() { let hover_id = egui::Id::unique(("hover-user", bot.id, idx)); let was_hovered = ctx.data(|d| d.get_temp::(hover_id)).unwrap_or(false); let row = ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - let (fill, text_col) = self.user_bubble(bot); + let (fill, text_col) = if woke { (p.hover, p.text) } else { self.user_bubble(bot) }; let max_w = message_max_width(ui.available_width()); // As wide as the words (up to the column's limit), lines reading left to right. let rich = RichText::new(&shown_text).size(TRANSCRIPT_FONT).line_height(Some(TRANSCRIPT_LINE_HEIGHT)).color(text_col); let text_w = egui::WidgetText::from(rich.clone()).into_galley(ui, Some(egui::TextWrapMode::Extend), f32::INFINITY, egui::FontSelection::Default).size().x; let inner_w = text_w.min(max_w - 24.0).max(8.0); // (Its corner toward the person is the small one.) - Frame::new().fill(fill).corner_radius(CornerRadius { nw: 20, ne: 20, sw: 20, se: 6 }).inner_margin(bubble_margin()).show(ui, |ui| { + let bubble = Frame::new().fill(fill).corner_radius(CornerRadius { nw: 20, ne: 20, sw: 20, se: 6 }).inner_margin(bubble_margin()).show(ui, |ui| { ui.set_width(inner_w); ui.with_layout(Layout::top_down(Align::Min), |ui| { ui.add(egui::Label::new(rich).wrap()); }); }); + // (The previous step's result, cut short in the bubble: whole on hover.) + if let Some(full) = full_text.as_ref().filter(|f| **f != shown_text) { + bubble.response.on_hover_text(full.clone()); + } // Hover: ↩ and ⋯ to the left of the bubble (kept while the menu is open). let menu_open = self.menu_for == Some((bot.id, idx)) && egui::Popup::is_any_open(ctx); if was_hovered || menu_open { @@ -1226,6 +1242,32 @@ pub fn wake_tag(text: &str) -> (Option<(crate::icons::Icon, &'static str)>, &str (None, text) } +/// A workflow's question to a bot: "[工作流「X」第 k/n 步…]…" → the tag's words, and the +/// words after it. +pub fn workflow_tag(text: &str) -> Option<(&str, &str)> { + let inner = text.strip_prefix("[")?; + if !inner.starts_with("工作流「") { + return None; + } + let close = inner.find(']')?; + Some((&inner[..close], &inner[close + ']'.len_utf8()..])) +} + +/// What a workflow's question shows in its bubble: what it asks, and the previous step's result +/// in its first lines (the whole is on hover; the bot got all of it). +pub fn workflow_words(words: &str) -> String { + // (A step hands on 「上一步的結果:」; a 檢查 shows what it checks as 「結果:」.) + for mark in ["\n\n上一步的結果:", "\n\n結果:"] { + if let Some((ask, last)) = words.split_once(mark) { + let last = last.trim(); + let short: String = last.lines().take(3).collect::>().join("\n").chars().take(160).collect(); + let more = if short.chars().count() < last.chars().count() { "…" } else { "" }; + return format!("{}{mark}{short}{more}", ask.trim()); + } + } + words.trim().to_string() +} + /// A 26 px quiet icon button (the hover actions). fn hover_icon(ui: &mut egui::Ui, icon: crate::icons::Icon, p: Palette) -> egui::Response { crate::icons::button(ui, icon, 26.0, p.text3, p.hover) @@ -1246,6 +1288,23 @@ mod tests { assert!(!page.contains("m.via || \"例行任務\"")); } + /// A workflow's question shows under its own tag, in a quiet bubble, with the previous + /// step's result cut short (it read as something the person typed, all of it). + #[test] + fn a_workflows_question_is_tagged_and_short() { + let asked = crate::workflow::step_message("Threads 早報", 2, 5, "看有沒有連結", &"第一行\n".repeat(20)); + let (tag, words) = workflow_tag(&asked).expect("a workflow's"); + assert_eq!(tag, "工作流「Threads 早報」第 2/5 步"); + let shown = workflow_words(words); + assert!(shown.starts_with("看有沒有連結\n\n上一步的結果:第一行") && shown.ends_with('…') && shown.lines().count() <= 6, "{shown}"); + assert!(workflow_tag("[例行任務]早報").is_none() && workflow_tag("工作流「X」").is_none()); + let check = crate::workflow::check_message("早報", 3, 5, "每則都有連結", &"一則新聞\n".repeat(9)); + let shown = workflow_words(workflow_tag(&check).unwrap().1); + assert!(shown.contains("\n\n結果:一則新聞") && shown.ends_with('…'), "{shown}"); + let page = include_str!("../../mobile/index.html"); + assert!(page.contains("工作流「") && page.contains("上一步的結果:"), "the phone does the same"); + } + #[test] fn phrases_follow_what_it_does() { let say = |name: &str, args: &str| status_phrase(Some((name, args)), None, false, false, false, 0); diff --git a/src/app/remote_ui.rs b/src/app/remote_ui.rs index 55d834e..023a2e8 100644 --- a/src/app/remote_ui.rs +++ b/src/app/remote_ui.rs @@ -716,7 +716,7 @@ impl App { if let Some(b) = self.state.bot_mut(bot) { if let Some(r) = b.routines.iter_mut().find(|r| r.id == routine) { r.last_run = Some(now); - wake = Some(appctl::wake_message(r)); + wake = Some(appctl::run_now_message(r)); } } if let Some(m) = wake { diff --git a/src/app/schedule_ui.rs b/src/app/schedule_ui.rs index 3279031..283c5e0 100644 --- a/src/app/schedule_ui.rs +++ b/src/app/schedule_ui.rs @@ -200,7 +200,10 @@ impl App { ui.scope_builder(egui::UiBuilder::new().max_rect(top.shrink2(vec2(16.0, 0.0))), |ui| { ui.horizontal_centered(|ui| { ui.with_layout(Layout::right_to_left(Align::Center), |ui| { - ui.add(egui::Label::new(RichText::new(format!("{} {tz} {}", t("時間以"), t("為準"))).size(FONT_SM).color(p.text3)).truncate()); + // (Only when the times aren't this computer's: a brain somewhere else.) + if tz != crate::routines::local_timezone() { + ui.add(egui::Label::new(RichText::new(format!("{} {tz} {}", t("時間以"), t("為準"))).size(FONT_SM).color(p.text3)).truncate()); + } ui.with_layout(Layout::left_to_right(Align::Center), |ui| { if ui.add(egui::Button::new(RichText::new(format!("‹ {}", t("聊天"))).size(FONT_SM).color(p.text2)).frame(false)).on_hover_text(t("回到聊天")).clicked() { self.sched.open = false; @@ -441,7 +444,7 @@ impl App { } if let Some(w) = &workflow { ui.label(RichText::new(format!("{} {}", w.step_count(), t("個步驟"))).size(FONT_SM).color(p.text2)); - if let Some(r) = w.runs.last() { + if let Some(r) = w.last_run() { ui.label(RichText::new(format!("{}{}", t("最近:"), crate::workflow::describe_run(r))).size(FONT_XS).color(p.text3)); } } @@ -486,7 +489,9 @@ impl App { self.wf.fitted = 0; self.wf_open(); } - (What::Workflow { id }, "run") => self.wf_do(ctx, id, "run"), + (What::Workflow { id }, "run") => { + self.wf_run_now(ctx, id); + } _ => {} } self.dirty = true; @@ -522,7 +527,7 @@ impl App { } if t > now - 6 * 86_400 { let outcome = if i % 5 == 3 { crate::routines::RunOutcome::Failed } else { crate::routines::RunOutcome::Succeeded }; - runs.push(crate::routines::RoutineRun { started: t, ended: t + 300, outcome, note: String::new(), steps: 4, failed_steps: 0, sent: 0, sends: vec![] }); + runs.push(crate::routines::RoutineRun { started: t, ended: t + 300, outcome, note: String::new(), steps: 4, failed_steps: 0, sent: 0, sends: vec![], by_hand: false }); i += 1; } at = t; diff --git a/src/app/workflow_run.rs b/src/app/workflow_run.rs index d9b6476..b8a4126 100644 --- a/src/app/workflow_run.rs +++ b/src/app/workflow_run.rs @@ -23,6 +23,10 @@ impl App { pub(super) fn wf_start_with(&mut self, ctx: &egui::Context, wf: u64, by: &str, input: &str) -> bool { let now = crate::routines::now(); let Some(w) = self.state.workflows.iter_mut().find(|w| w.id == wf) else { return false }; + // Nothing after the trigger: nothing to run (and no 完成 for it — see NO_STEPS). + if !w.has_steps() { + return false; + } if w.live_run().is_some() { // (Started by hand: 「已經在跑了」 is said there. An event or the schedule: it waits // for this one to end — the schedule once, events each.) @@ -47,6 +51,16 @@ impl App { true } + /// 執行工作流 / 立即執行 by hand: one with nothing after its trigger says so instead. + pub(super) fn wf_run_now(&mut self, ctx: &egui::Context, wf: u64) -> bool { + if self.state.workflows.iter().find(|w| w.id == wf).is_some_and(|w| !w.has_steps()) { + self.notice = Some(t(crate::workflow::NO_STEPS)); + return false; + } + self.wf_do(ctx, wf, "run"); + true + } + /// 執行 (run), 停止 (stop), 等你確認的繼續 (go) or 停止 (halt), from this window: done here on /// the brain; a screen asks the brain, which runs them (its answer comes back with the state). pub(super) fn wf_do(&mut self, ctx: &egui::Context, wf: u64, what: &str) { @@ -695,6 +709,32 @@ mod tests { app.state.workflows[0].runs.last().cloned().unwrap() } + /// A workflow with nothing after its trigger doesn't "run": pressing 執行 says to add a step, + /// the bot's run says the same, and nothing is recorded (it was marked 完成 at once — on + /// 日程, in 執行紀錄, and as 「最近一次:完成」 to the bots — with nothing done). + #[test] + fn a_workflow_with_no_steps_does_not_run() { + let (ctx, mut app, bot) = app_with(vec![]); + assert!(!app.state.workflows[0].has_steps()); + assert!(!app.wf_run_now(&ctx, 1)); + assert_eq!(app.notice.as_deref(), Some(crate::workflow::NO_STEPS)); + assert!(!app.wf_start(&ctx, 1, "定時")); + assert!(app.state.workflows[0].runs.is_empty(), "no run recorded"); + let (tx, rx) = std::sync::mpsc::channel(); + app.workflow_tool(&ctx, bot, AppCall { call_id: "c".into(), name: crate::workflow::TOOL.into(), args: serde_json::json!({"action": "run", "id": 1}).to_string(), reply: tx }); + let (said, _) = rx.try_recv().unwrap(); + assert!(said.contains("還沒有連任何步驟"), "{said}"); + assert!(app.state.workflows[0].runs.is_empty()); + // One step tried on its own, and an empty "run" from before: not runs of the workflow. + let w = &mut app.state.workflows[0]; + w.nodes.push(crate::workflow::Node { id: 2, kind: NodeKind::Notify { text: "好".into() }, x: 0.0, y: 190.0 }); + assert!(w.connect(1, 2)); + w.start_single(2, "", 100); + w.runs.push(crate::workflow::Run { id: 9, started: 50, ended: Some(50), state: crate::workflow::RunState::Done, by: "手動".into(), order: vec![1], at: 1, steps: vec![], why: String::new(), input: String::new() }); + assert!(w.last_run().is_none()); + assert!(crate::schedule::items(&app.state, 0, 1_000, 500).is_empty(), "nothing on the calendar"); + } + /// A run goes step by step: the bot is asked in its own chat, its last words are handed /// on, a check that passes hands on what it checked, the result goes into the chat, and it /// ends done. One run at a time. diff --git a/src/app/workflow_ui.rs b/src/app/workflow_ui.rs index 9c71a8c..afdde76 100644 --- a/src/app/workflow_ui.rs +++ b/src/app/workflow_ui.rs @@ -381,8 +381,9 @@ impl App { if ui.add(primary_button("▷ 執行工作流", p)).clicked() { let (ctx, id) = (ui.ctx().clone(), self.wf.current); self.wf_keep_version(); - self.wf_do(&ctx, id, "run"); - self.wf.runs_open = true; + if self.wf_run_now(&ctx, id) { + self.wf.runs_open = true; + } } } } @@ -401,18 +402,8 @@ impl App { self.wf.runs_open = !on; self.wf.selected = None; } - // (Only when there is room: the name comes first.) - if ui.available_width() > 360.0 { - match live { - Some(RunState::Waiting) => { - ui.label(RichText::new(t("等你確認")).size(FONT_SM).color(p.attention)); - } - Some(_) => { - ui.label(RichText::new(t("執行中…")).size(FONT_SM).color(p.up)); - } - None => {} - } - } + // (No 「等你確認」/「執行中…」 here: the bar over the canvas says the one with its + // buttons, ■ 停止 the other.) left(self, ui); }); }); @@ -866,8 +857,9 @@ impl App { self.wf.card_tools = card_tools; if let Some(id) = run_now { self.wf_keep_version(); - self.wf_do(&ctx, id, "run"); - self.wf.runs_open = true; + if self.wf_run_now(&ctx, id) { + self.wf.runs_open = true; + } } // ── The pointer ── @@ -1780,8 +1772,9 @@ impl App { let ctx = ui.ctx().clone(); let wf = self.wf.current; self.wf_keep_version(); - self.wf_do(&ctx, wf, "run"); - self.wf.runs_open = true; + if self.wf_run_now(&ctx, wf) { + self.wf.runs_open = true; + } } if try_now { let ctx = ui.ctx().clone(); diff --git a/src/appctl.rs b/src/appctl.rs index 198b65d..e51474a 100644 --- a/src/appctl.rs +++ b/src/appctl.rs @@ -464,6 +464,21 @@ pub fn give_skill(state: &mut State, bot_id: u64, name: &str) { } } +/// What 立即執行 tells the bot in place of 「時間到了」 (it wasn't its time: someone asked now). +pub const RUN_NOW: &str = "現在就執行(使用者叫的,不是排定的時間):"; + +/// A routine run by hand (立即執行), as the message that wakes the bot. +pub fn run_now_message(r: &Routine) -> Message { + let mut m = wake_message(r); + m.text = m.text.replacen(&format!("「{}」時間到了:", r.title), &format!("「{}」{RUN_NOW}", r.title), 1); + m +} + +/// Whether a routine's wake message is one run by hand. +pub fn is_run_now(text: &str) -> bool { + text.starts_with("[例行任務]") && text.lines().next().is_some_and(|l| l.contains(RUN_NOW)) +} + /// A routine firing, as the message that wakes the bot. pub fn wake_message(r: &Routine) -> Message { // A new run starts fresh for the model (Message::fresh): what earlier runs found is in its @@ -640,7 +655,7 @@ pub fn apply(state: &mut State, bot_id: u64, name: &str, args: &str) -> Outcome "run_routine_now" => { let r = &mut b.routines[idx]; r.last_run = Some(now); - let msg = wake_message(r); + let msg = run_now_message(r); Outcome { text: format!("已開始執行「{}」", r.title), ok: true, wake: vec![(owner, msg)], theme_changed: false, page: None } } _ => { diff --git a/src/garden.rs b/src/garden.rs index 9b6c801..e4ef193 100644 --- a/src/garden.rs +++ b/src/garden.rs @@ -161,7 +161,7 @@ mod tests { } fn run(started: i64, outcome: crate::routines::RunOutcome, steps: u32, failed: u32) -> crate::routines::RoutineRun { - crate::routines::RoutineRun { started, ended: started + 600, outcome, note: "原因".into(), steps, failed_steps: failed, sent: 0, sends: vec![] } + crate::routines::RoutineRun { started, ended: started + 600, outcome, note: "原因".into(), steps, failed_steps: failed, sent: 0, sends: vec![], by_hand: false } } #[test] diff --git a/src/routines.rs b/src/routines.rs index 61ea146..9b4b6c8 100644 --- a/src/routines.rs +++ b/src/routines.rs @@ -146,6 +146,9 @@ pub struct RoutineRun { /// evidence, at most SENDS_KEPT. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub sends: Vec, + /// Run by hand (立即執行), not by its time or its event: not on 日程. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub by_hand: bool, } /// Evidence lines kept per run. @@ -162,7 +165,7 @@ const INTERRUPTED_NOTE: &str = "AnyBot 在執行中被關掉,可能做到一 impl Routine { /// A run begins. pub fn run_started(&mut self, at: i64) { - self.runs.push(RoutineRun { started: at, ended: 0, outcome: RunOutcome::Running, note: String::new(), steps: 0, failed_steps: 0, sent: 0, sends: vec![] }); + self.runs.push(RoutineRun { started: at, ended: 0, outcome: RunOutcome::Running, note: String::new(), steps: 0, failed_steps: 0, sent: 0, sends: vec![], by_hand: false }); let extra = self.runs.len().saturating_sub(RUNS_KEPT); self.runs.drain(..extra); } @@ -192,7 +195,7 @@ impl Routine { } // (Started before runs were kept, or by a path that didn't note the start.) None => { - self.runs.push(RoutineRun { started: at, ended: at, outcome, note: note.clone(), steps, failed_steps, sent, sends }); + self.runs.push(RoutineRun { started: at, ended: at, outcome, note: note.clone(), steps, failed_steps, sent, sends, by_hand: false }); let extra = self.runs.len().saturating_sub(RUNS_KEPT); self.runs.drain(..extra); } @@ -603,7 +606,7 @@ mod tests { r.run_started(100); assert_eq!(r.runs[0].outcome, RunOutcome::Running); assert!(!r.run_ended(160, RunOutcome::Failed, "對話太長了", &crate::evidence::Tally { steps: 12, failed: 3, sent: 0, sends: vec![] })); - assert_eq!(r.runs[0], RoutineRun { started: 100, ended: 160, outcome: RunOutcome::Failed, note: "對話太長了".into(), steps: 12, failed_steps: 3, sent: 0, sends: vec![] }); + assert_eq!(r.runs[0], RoutineRun { started: 100, ended: 160, outcome: RunOutcome::Failed, note: "對話太長了".into(), steps: 12, failed_steps: 3, sent: 0, sends: vec![], by_hand: false }); // A success in between: the count starts again. r.run_started(200); r.run_ended(260, RunOutcome::Succeeded, "送出 15 則", &crate::evidence::Tally { steps: 80, failed: 2, sent: 0, sends: vec![] }); @@ -687,12 +690,12 @@ mod tests { #[test] fn a_run_reads_in_one_line() { - let r = RoutineRun { started: ts(2026, 11, 14, 17, 0), ended: ts(2026, 11, 14, 17, 35), outcome: RunOutcome::Failed, note: "找不到回覆框".into(), steps: 87, failed_steps: 12, sent: 0, sends: vec![] }; + let r = RoutineRun { started: ts(2026, 11, 14, 17, 0), ended: ts(2026, 11, 14, 17, 35), outcome: RunOutcome::Failed, note: "找不到回覆框".into(), steps: 87, failed_steps: 12, sent: 0, sends: vec![], by_hand: false }; assert_eq!(describe_run(&r, "Asia/Taipei"), "11/14 17:00 失敗(35 分鐘,87 步,12 步沒成功):找不到回覆框"); // The system's own count of sends, beside the report's words. let posted = RoutineRun { outcome: RunOutcome::Succeeded, note: "送出 2 則".into(), sent: 1, ..r.clone() }; assert_eq!(describe_run(&posted, "Asia/Taipei"), "11/14 17:00 完成(35 分鐘,87 步,12 步沒成功,系統記到送出 1 次):送出 2 則"); - let going = RoutineRun { started: ts(2026, 11, 14, 9, 0), ended: 0, outcome: RunOutcome::Running, note: String::new(), steps: 0, failed_steps: 0, sent: 0, sends: vec![] }; + let going = RoutineRun { started: ts(2026, 11, 14, 9, 0), ended: 0, outcome: RunOutcome::Running, note: String::new(), steps: 0, failed_steps: 0, sent: 0, sends: vec![], by_hand: false }; assert_eq!(describe_run(&going, "Asia/Taipei"), "11/14 09:00 執行中"); } diff --git a/src/schedule.rs b/src/schedule.rs index c41d339..b24597e 100644 --- a/src/schedule.rs +++ b/src/schedule.rs @@ -56,7 +56,8 @@ pub fn items(state: &State, from: i64, to: i64, now: i64) -> Vec { Some(t) => t.describe(), None => routines::describe(&r.schedule), }; - for run in r.runs.iter().filter(|x| x.started >= from && x.started < to) { + // (What it did on its own: not a run by hand — 日程 is what is set to happen.) + for run in r.runs.iter().filter(|x| !x.by_hand && x.started >= from && x.started < to) { let status = match run.outcome { RunOutcome::Succeeded => Status::Done, RunOutcome::Failed | RunOutcome::Interrupted => Status::Failed, @@ -87,7 +88,9 @@ pub fn items(state: &State, from: i64, to: i64, now: i64) -> Vec { }); let bot = bot.unwrap_or(0); let when = w.trigger().map(|n| n.kind.words()).unwrap_or_else(|| "手動".into()); - for run in w.runs.iter().filter(|x| x.started >= from && x.started < to) { + // (Only what its schedule or its event started: a run pressed by hand, or asked of a bot, + // is in 執行紀錄 — on the calendar it read as something planned.) + for run in w.runs.iter().filter(|x| x.counts() && matches!(x.by.as_str(), "定時" | "觸發") && x.started >= from && x.started < to) { let status = match run.state { RunState::Done => Status::Done, RunState::Failed => Status::Failed, @@ -106,10 +109,16 @@ pub fn items(state: &State, from: i64, to: i64, now: i64) -> Vec { out } -/// The next few times coming, across everything (即將執行): paused ones left out. +/// What runs next (即將執行): each routine or workflow once, at its next time, soonest first; +/// paused ones left out. (Every time of each, a daily one filled the list with itself.) pub fn coming(state: &State, now: i64, n: usize) -> Vec { // (From a day back: one whose time came while its bot was busy is still to run.) - let mut out: Vec = items(state, now - 86_400, now + 14 * 86_400, now).into_iter().filter(|i| i.status == Status::Coming).collect(); + let mut out: Vec = vec![]; + for i in items(state, now - 86_400, now + 14 * 86_400, now).into_iter().filter(|i| i.status == Status::Coming) { + if !out.iter().any(|o| o.what == i.what) { + out.push(i); + } + } out.truncate(n); out } @@ -185,8 +194,8 @@ mod tests { // Now: Saturday 21 at 12:00. let now = day_start(mon + chrono::Duration::days(5), tz) + 12 * 3600; let mut daily = routine(1, "競品掃描", sched(serde_json::json!({"frequency": "daily", "time": "08:30", "timezone": tz})), created); - daily.runs.push(RoutineRun { started: day_start(mon + chrono::Duration::days(4), tz) + 8 * 3600 + 1800, ended: 0, outcome: RunOutcome::Succeeded, note: String::new(), steps: 3, failed_steps: 0, sent: 0, sends: vec![] }); - daily.runs.push(RoutineRun { started: day_start(mon + chrono::Duration::days(5), tz) + 8 * 3600 + 1800, ended: 0, outcome: RunOutcome::Failed, note: "沒網路".into(), steps: 1, failed_steps: 1, sent: 0, sends: vec![] }); + daily.runs.push(RoutineRun { started: day_start(mon + chrono::Duration::days(4), tz) + 8 * 3600 + 1800, ended: 0, outcome: RunOutcome::Succeeded, note: String::new(), steps: 3, failed_steps: 0, sent: 0, sends: vec![], by_hand: false }); + daily.runs.push(RoutineRun { started: day_start(mon + chrono::Duration::days(5), tz) + 8 * 3600 + 1800, ended: 0, outcome: RunOutcome::Failed, note: "沒網路".into(), steps: 1, failed_steps: 1, sent: 0, sends: vec![], by_hand: false }); let workdays = routine(2, "晨報", sched(serde_json::json!({"frequency": "weekly", "daysOfWeek": ["MO", "TU", "WE", "TH", "FR"], "time": "09:00", "timezone": tz})), created); let mut paused = routine(3, "週報", sched(serde_json::json!({"frequency": "daily", "time": "18:00", "timezone": tz})), created); paused.paused = true; @@ -213,12 +222,14 @@ mod tests { assert!(all.iter().filter(|i| i.title == "Threads 早報").all(|i| i.bot == bot && i.what == What::Workflow { id: 1 })); assert!(all.windows(2).all(|w| w[0].at <= w[1].at), "oldest first"); // 即將執行: across everything, paused left out. + // Each one once, at its next time. let next = coming(&st, now, 4); let day = |i: &Item| day_of(i.at, tz).format("%d").to_string(); assert_eq!( - next.iter().map(|i| (day(i), clock(i.at, tz), i.title.as_str())).collect::>(), - vec![("22".into(), "07:00".into(), "Threads 早報"), ("22".into(), "08:30".into(), "競品掃描"), ("23".into(), "07:00".into(), "Threads 早報"), ("23".into(), "08:30".into(), "競品掃描")] + next.iter().take(2).map(|i| (day(i), clock(i.at, tz), i.title.as_str())).collect::>(), + vec![("22".into(), "07:00".into(), "Threads 早報"), ("22".into(), "08:30".into(), "競品掃描")] ); + assert!(next.iter().all(|i| next.iter().filter(|o| o.what == i.what).count() == 1), "each once"); assert!(next.iter().all(|i| i.status == Status::Coming && i.title != "週報")); assert!(next.windows(2).all(|w| w[0].at <= w[1].at)); // A time that came while its bot was busy (next_run still before now): shown at its time @@ -239,4 +250,34 @@ mod tests { st.bots[0].routines.push(routine(5, "盯盤", sched(serde_json::json!({"frequency": "minutely", "interval": 5, "time": "00:00", "timezone": tz})), created)); assert_eq!(items(&st, from, from + 42 * 86_400, now).iter().filter(|i| i.title == "盯盤").count(), MAX_PER_THING); } + + /// 日程 is what is set to happen: a workflow pressed by hand or asked of a bot, and a routine + /// run by hand (立即執行), are not on it — what its schedule or its event started is. (Pressing + /// 執行 put it on the calendar at once, which read as something planned.) 立即執行 doesn't tell + /// the bot 「時間到了」 either. + #[test] + fn what_was_run_by_hand_is_not_on_the_calendar() { + let mut st = State::demo(); + st.workflows.clear(); + for b in st.bots.iter_mut() { + b.routines.clear(); + } + let mut w = crate::workflow::Workflow::new(1, "早報", 0); + w.nodes.push(crate::workflow::Node { id: 2, kind: NodeKind::Notify { text: "好".into() }, x: 0.0, y: 190.0 }); + assert!(w.connect(1, 2)); + for (at, by) in [(100, "手動"), (200, "驗證小幫手"), (300, "定時"), (400, "觸發")] { + w.start_run(by, at); + } + st.workflows.push(w); + let mut r = routine(1, "競品掃描", sched(serde_json::json!({"frequency": "daily", "time": "08:30", "timezone": "Asia/Taipei"})), 0); + let now_msg = crate::appctl::run_now_message(&r); + assert!(crate::appctl::is_run_now(&now_msg.text) && !now_msg.text.contains("時間到了"), "{}", now_msg.text); + assert!(!crate::appctl::is_run_now(&crate::appctl::wake_message(&r).text)); + r.run_started(500); + r.runs.last_mut().unwrap().by_hand = true; + r.run_started(600); + st.bots[0].routines.push(r); + let ran: Vec = items(&st, 0, 1_000, 10_000).iter().map(|i| i.at).collect(); + assert_eq!(ran, vec![300, 400, 600]); + } } diff --git a/src/workflow.rs b/src/workflow.rs index 421926b..30ae459 100644 --- a/src/workflow.rs +++ b/src/workflow.rs @@ -265,6 +265,13 @@ pub enum StepState { Skipped, } +/// The `by` of a run that is one step tried on its own (試跑這一步). +pub const TRIED: &str = "試跑這一步"; + +/// Said when a workflow with nothing after its trigger is asked to run (it used to be marked +/// 完成 at once — on the calendar, in 執行紀錄 and to the bots — with nothing done). +pub const NO_STEPS: &str = "這個工作流還沒有步驟,沒有執行:先在觸發器下面加一步(+ 新增節點),或在聊天裡請 Bot 加。"; + #[derive(Clone, Serialize, Deserialize, Debug, PartialEq)] pub struct Run { pub id: u64, @@ -289,6 +296,13 @@ pub struct Run { pub input: String, } +impl Run { + /// A run of the workflow itself: what the calendar and 「最近一次」 count. + pub fn counts(&self) -> bool { + self.by != TRIED && self.order.len() > 1 + } +} + #[derive(Clone, Serialize, Deserialize, Debug, PartialEq)] pub struct RunStep { pub node: u64, @@ -399,6 +413,15 @@ impl Workflow { pub fn live_run(&self) -> Option<&Run> { self.runs.iter().rev().find(|r| r.live()) } + /// Anything after the trigger to run. + pub fn has_steps(&self) -> bool { + self.step_count() > 0 + } + /// The latest time the workflow itself ran (not one step tried on its own, nor a "run" of + /// nothing from before NO_STEPS). + pub fn last_run(&self) -> Option<&Run> { + self.runs.iter().rev().find(|r| r.counts()) + } /// A line from `from` to `to`, unless it is there already, goes into the trigger, or would /// close a circle. pub fn connect(&mut self, from: u64, to: u64) -> bool { @@ -444,7 +467,7 @@ impl Workflow { pub fn start_single(&mut self, node: u64, input: &str, now: i64) -> u64 { let id = self.runs.iter().map(|r| r.id).max().unwrap_or(0) + 1; let steps = vec![RunStep { node, state: StepState::Waiting, output: String::new(), bot: 0, asked_at: 0, files: vec![] }]; - self.runs.push(Run { id, started: now, ended: None, state: RunState::Running, by: "試跑這一步".into(), order: vec![node], at: 0, steps, why: String::new(), input: input.to_string() }); + self.runs.push(Run { id, started: now, ended: None, state: RunState::Running, by: TRIED.into(), order: vec![node], at: 0, steps, why: String::new(), input: input.to_string() }); if self.runs.len() > RUNS_KEPT { self.runs.remove(0); } @@ -1111,7 +1134,7 @@ pub fn describe(w: &Workflow, who: &Who) -> String { if drawn > 0 { out.push(format!("另外有 {drawn} 個手畫的線條或形狀")); } - if let Some(r) = w.runs.last() { + if let Some(r) = w.last_run() { out.push(format!("最近一次執行:{}", run_words(r))); } if !w.queued.is_empty() { @@ -1123,7 +1146,7 @@ pub fn describe(w: &Workflow, who: &Who) -> String { fn list_line(w: &Workflow, tz: &str) -> String { let when = w.schedule().map(crate::routines::describe).unwrap_or_else(|| "手動".into()); let next = w.next_run.map(|t| format!("(下次 {})", crate::routines::describe_when(t, tz))).unwrap_or_default(); - let last = w.runs.last().map(|r| format!(" · 最近一次:{}", run_words(r))).unwrap_or_default(); + let last = w.last_run().map(|r| format!(" · 最近一次:{}", run_words(r))).unwrap_or_default(); format!("#{}「{}」 {} 個步驟 · {when}{next}{last}", w.id, w.name, w.step_count()) }