diff --git a/FEATURES.md b/FEATURES.md index 5c5729c..8ed9a87 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -1566,7 +1566,7 @@ computer_use_screenshot、click、mouse_move、mouse_button(按住/放開) ### 頁面(Bot 做、Bot 持續更新的報告) -- 你請 Bot 追蹤、整理、比較一件之後還會回來看的事(例如「幫我追蹤一下台灣 FSD 狀況」),它用 `write_page` 做成一頁:先寫現況,再寫細節和來源(附日期)。回答裡用「[標題](page:ID)」連到它,回答下面出現頁面卡片(一張精簡卡:頁面圖示、標題、一行它說什麼或這次改了什麼、一行灰字的種類和時間),點一下打開。 +- 你請 Bot 追蹤、整理、比較一件之後還會回來看的事(例如「幫我追蹤一下台灣 FSD 狀況」),它用 `write_page` 做成一頁:先寫現況,再寫細節和來源(附日期)。回答裡用「[標題](page:ID)」連到它,回答下面出現頁面卡片(一張精簡卡:頁面圖示、標題、一行它說什麼或這次改了什麼、一行灰字的種類和時間),點一下打開。一個回覆裡每一頁只出一張卡(在最後存的那一步下面);沒存成的寫入(改別的 Bot 的頁面被擋、被寫法檢查退回的那一次)不出卡;手機一樣。 - 頁面可以放表格、Mermaid 圖表(圓餅、長條、折線、時間軸…白底卡節點、細灰連線),以及頁面專用的區塊:```stats 數字卡片(數字大字,超過 10 個字的值縮成小一號;值和備註開頭 ↑/+ 綠色、↓/−/- 紅色,「+」「-」後面要緊接著東西才算,單獨一個「-」當作沒資料;電腦和手機同一套規則)、```steps 進度(✓ 完成、◉ 進行中、○ 還沒到)、```chart 原生圖表(bar 長條、line 折線、pie 圓餅;只畫淡水平參考線,長條自動由大到小排序——月份/年份等自有順序的標籤不排;單系列把數字直接標在圖上、不畫 y 軸;多條線在線尾標系列名;懸停看數值、拖曳平移、右鍵框選放大、雙擊還原;長條的 y 軸從 0 開始,折線離 0 很遠時(最小值比資料的高低差還大)照資料範圍畫,起伏才看得出來;圖下自動附數據表,和頁面的表格同一種樣子(灰字表頭、每列一條細線、名稱靠左、數字靠右,每欄只有內容那麼寬,太寬就在卡片裡左右捲動,不會把頁面視窗撐出畫面),超過 6 列收成「▸ 數據 · N 筆」,點了展開;多系列用 `| 1月 | 2月` 當表頭;第一行可以是「圖型 標題」、只寫圖型(bar/line/pie 或 長條/折線/圓餅)或只寫標題,`#` 開頭的行是註解,`|---|` 分隔列略過,少填的格子當 0——電腦和手機同一套讀法;手機看同一頁也畫同樣樣式的原生 SVG 圖:照手機寬度畫、字不會縮小,折線和多系列長條有 y 軸數字(y 軸從不從 0 開始的規則和電腦一樣),圖下同樣附數據表(超過 6 列同樣收起),只有一項的圓餅畫成整圓,圖例項目再多也都畫得到,負數的數字不會壓到下面的標籤)、`> [!NOTE]`/`[!TIP]`/`[!WARNING]`/`[!IMPORTANT]` 提示框。這些區塊在聊天裡也能用。所有圖表(含 Mermaid 圓餅)共用同一套低飽和六色色板,深淺兩主題各一組。 - **細節區塊** ```details 標題:預設收起,只顯示一行灰色「▸ 標題」(沒寫標題就是「細節」),點了展開裡面的 Markdown。讓第一屏只有結論、數字和下一步;證據、長清單、指令收在下面。 - **寫法檢查**:Bot 存頁面時程式先看一遍(Anthropic 的報告寫法:結論先講、句子短、少粗體、數字放卡片或表格、不用裝飾)。段落超過 120 字、一節超過一處粗體、括號裡一串數字、標題編號、分隔線、用「→」帶結論、表格裡放指令、正文很長卻沒有收起的細節——有就退回一次、列出每一處要怎麼改;同一頁第二次照存,不會卡住。 diff --git a/docs/feature-map/pages.md b/docs/feature-map/pages.md index edae369..99b3edd 100644 --- a/docs/feature-map/pages.md +++ b/docs/feature-map/pages.md @@ -14,7 +14,7 @@ Bot 把要追蹤、整理、比較的事寫成一頁,之後一直更新同一 ## 使用者怎麼到 -- 回覆裡的 [標題](page:ID) 連結,和回覆下面的頁面卡片(一張精簡卡:頁面圖示、標題一次、一行它說什麼——更新過的是「已更新:改了什麼」,否則是現況那句——和一行灰字「種類 · 頁面 · 時間 · 第 N 版」):點一下打開。 +- 回覆裡的 [標題](page:ID) 連結,和回覆下面的頁面卡片(一張精簡卡:頁面圖示、標題一次、一行它說什麼——更新過的是「已更新:改了什麼」,否則是現況那句——和一行灰字「種類 · 頁面 · 時間 · 第 N 版」):點一下打開。一個回覆裡每一頁只出一張卡,在最後存那一頁的步驟下面(被寫法檢查退回再存、或存了又更新,都不會重複);沒存成的(別的 Bot 的頁面被擋、被退回那一次)不出卡。手機一樣。 - 右側面板的「資料庫」分頁,最上面的「頁面」:這個 Bot 的頁面,最近改過的在前面。 - 打開的頁面像一份文件(和手機同一套):標題宋體粗體 28,下面一行小字作者、「更新於」、第幾版和「這一版更新了:…」;一行現況(summary)是開頭段落、不加框;結論(verdict)是淡底的「結論」提示框;kpis 是只有細框的數字卡。有 3 個以上的標題時有一行「▸ 目錄 · N 節」,點開才是標題清單和小搜尋框(⌘F 直接展開)。內文宋體 16、行距 1.7,閱讀欄最寬 680。下方左邊「版本紀錄」(看舊版時有「回到最新版」)和「刪除」(按兩次),右邊「複製」(Markdown)、「存成檔案」(存成 .md 到「下載」)、「完成」。 - 手機:首頁上面一排的「頁面」列出所有頁面;聊天裡的頁面卡片;打開的頁面的「⋯」有「複製 Markdown」「其他版本」,一條細線下面才是「刪除頁面」。 @@ -23,7 +23,7 @@ Bot 把要追蹤、整理、比較的事寫成一頁,之後一直更新同一 ## 怎麼操作 - Bot:使用者要你追蹤、整理、比較、研究一件之後還會回來看的事,用 `write_page`(title、content;summary 一行現況;kind;kpis、verdict 只放真的資料;更新時帶 id 和 note),回覆裡用 [標題](page:ID) 連過去。更新前先 `read_page`(id 或 title)看現在的內容。要查的事同事可能已經在追,先 `list_pages`、`read_page` 讀它的,不要重查一遍。要定期追蹤就配一條例行任務([routines](routines.md)):每次讀頁面、查新消息,有實質進展才更新,沒有就說「沒有新消息」。這三個工具屬於「頁面」能力。 -- 驗證:請 Bot「幫我追蹤某件事,整理成一頁」,回覆下面要出現頁面卡片,右側面板 資料庫 › 頁面 要列出它;再請它更新,卡片寫「已更新」,打開頁面的「版本紀錄」有上一版。請另一個 Bot 改這一頁,要被擋下並說是誰的頁面。頁裡的 ```chart:長條要按大小排序(月份圖不排)、單系列數字在條上;手機同頁看同一張圖要是同一套樣式——字跟內文差不多大、折線有 y 軸數字、圖下有數據表(24 筆的折線收成「▸ 數據 · 24 筆」)、一條在 1,700 上下的折線看得出起伏(y 軸不從 0 開始)、深色模式下打開頁面視窗不會超出畫面、只寫 `pie` 當第一行也畫得出來、只有一項的圓餅是整圓。手機打開有 3 個以上標題的頁面,標題下面要有收起來的「目錄 · N 節」一行,點開點一個標題會捲過去;內文是宋體、表格沒有直線。自動測試:`cargo test pages::`、`cargo test appctl::`。 +- 驗證:請 Bot「幫我追蹤某件事,整理成一頁」,回覆下面要出現頁面卡片,右側面板 資料庫 › 頁面 要列出它;再請它更新,卡片寫「已更新」,打開頁面的「版本紀錄」有上一版;同一個回覆裡就算寫了好幾次(被退回再存),下面也只有一張卡。請另一個 Bot 改這一頁,要被擋下並說是誰的頁面。頁裡的 ```chart:長條要按大小排序(月份圖不排)、單系列數字在條上;手機同頁看同一張圖要是同一套樣式——字跟內文差不多大、折線有 y 軸數字、圖下有數據表(24 筆的折線收成「▸ 數據 · 24 筆」)、一條在 1,700 上下的折線看得出起伏(y 軸不從 0 開始)、深色模式下打開頁面視窗不會超出畫面、只寫 `pie` 當第一行也畫得出來、只有一項的圓餅是整圓。手機打開有 3 個以上標題的頁面,標題下面要有收起來的「目錄 · N 節」一行,點開點一個標題會捲過去;內文是宋體、表格沒有直線。自動測試:`cargo test pages::`、`cargo test appctl::`。 ## 陷阱 diff --git a/src/app/chat_ui.rs b/src/app/chat_ui.rs index a0aabf4..fa7be0d 100644 --- a/src/app/chat_ui.rs +++ b/src/app/chat_ui.rs @@ -788,10 +788,13 @@ impl App { for c in m.tool_calls.iter().filter(|c| is_talk(&c.name)) { self.talk_card(ui, ctx, bot, m, c); } - // 頁面 written in this reply: the card (the page as it is now). - for id in crate::pages::ids_in(&m.tool_calls) { - ui.add_space(8.0); - self.page_card(ui, id); + // 頁面 saved in this reply: the card (the page as it is now) — once a reply, under the step + // that saved it last. (Worked out only for a message that saved one: few do.) + if !crate::pages::ids_in(&m.tool_calls).is_empty() { + for id in crate::pages::cards_by_message(&bot.messages).get(idx).cloned().unwrap_or_default() { + ui.add_space(8.0); + self.page_card(ui, id); + } } for a in m.attachments.clone() { ui.add_space(6.0); diff --git a/src/app/remote_ui.rs b/src/app/remote_ui.rs index c537c2f..7e7b216 100644 --- a/src/app/remote_ui.rs +++ b/src/app/remote_ui.rs @@ -410,11 +410,14 @@ impl App { // Each message with its place in the chat (表情回應), its thinking and, on each step, // what came back (the desktop's 顯示步驟 / 思考過程), in short. let results: HashMap<&str, &str> = b.messages[start..].iter().filter(|m| m.role == Role::Tool).map(|m| (m.tool_call_id.as_str(), m.text.as_str())).collect(); + // 頁面 cards: once a reply, under the step that saved the page last (over the whole chat, + // so a reply cut by the tail still knows its later steps). + let cards = crate::pages::cards_by_message(&b.messages); let msgs: Vec = b.messages[start..] .iter() .enumerate() .filter_map(|(k, m)| { - let mut v = remote::message_json(m, &names, &self.state.pages)?; + let mut v = remote::message_json(m, &names, &self.state.pages, cards.get(start + k).map(Vec::as_slice).unwrap_or(&[]))?; v["index"] = json!(start + k); // 支線: its record, for the card (and the report's label). if m.branch != 0 { diff --git a/src/pages.rs b/src/pages.rs index 21b3aa8..73e2d2b 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -381,8 +381,8 @@ pub fn mermaid_blocks(body: &str) -> Vec { /// The pages written in one reply (its write_page calls), each once, in order. pub fn ids_in(calls: &[crate::model::ToolCall]) -> Vec { let mut out = vec![]; - for c in calls.iter().filter(|c| c.name == "write_page") { - if let Some(id) = serde_json::from_str::(&c.args).ok().and_then(|v| v["id"].as_u64()) { + for c in calls.iter().filter(|c| saved(c)) { + if let Some(id) = written_id(c) { if !out.contains(&id) { out.push(id); } @@ -391,6 +391,39 @@ pub fn ids_in(calls: &[crate::model::ToolCall]) -> Vec { out } +/// A write_page call that saved its page. A refused try (another bot's page) or one sent back by +/// the 寫法檢查 changed nothing — its card showed someone else's page, or the page as a later +/// try left it. +fn saved(c: &crate::model::ToolCall) -> bool { + c.name == "write_page" && c.status == crate::model::ToolStatus::Done +} + +/// The page a write_page call names (the app writes the id in once it has saved a new page). +fn written_id(c: &crate::model::ToolCall) -> Option { + serde_json::from_str::(&c.args).ok().and_then(|v| v["id"].as_u64()) +} + +/// For each message, the pages whose card shows under it: each page a reply saved, once, under the +/// step that saved it last (a reply that wrote its page three times — sent back, fixed, updated — +/// showed three cards of the same 第 3 版). A reply runs from one message of yours to the next. +pub fn cards_by_message(messages: &[crate::model::Message]) -> Vec> { + let mut out = vec![vec![]; messages.len()]; + let mut shown: Vec = vec![]; + for (i, m) in messages.iter().enumerate().rev() { + if m.role == crate::model::Role::User { + shown.clear(); + continue; + } + for id in ids_in(&m.tool_calls).into_iter().rev() { + if !shown.contains(&id) { + shown.push(id); + out[i].insert(0, id); + } + } + } + out +} + /// ```stats lines: 標籤 | 數值 | 說明 pub fn stats(src: &str) -> Vec<(String, String, String)> { src.lines() @@ -766,6 +799,31 @@ mod tests { assert!(chart_from_zero(&bars), "a bar's length is its value"); } + /// 一個回覆裡:想改別的 Bot 的頁面被擋、寫法檢查退回一次、再存兩次——只出一張卡,在最後 + /// 存的那一步下面;被擋的那頁不出卡。下一個回覆又寫同一頁,就再出一張。 + #[test] + fn a_reply_shows_each_saved_page_once_under_its_last_save() { + use crate::model::{Message, ToolCall, ToolStatus}; + let call = |id: &str, page: u64, st: ToolStatus| ToolCall { id: id.into(), name: "write_page".into(), args: json!({"id": page, "title": "SCC 機房狀態報告"}).to_string(), status: st }; + let step = |calls: Vec| { + let mut m = Message::assistant("", ""); + m.tool_calls = calls; + m + }; + let msgs = vec![ + Message::user("做成報告"), + step(vec![call("a", 3, ToolStatus::Failed)]), + step(vec![call("b", 7, ToolStatus::Failed)]), + step(vec![call("c", 7, ToolStatus::Done)]), + step(vec![call("d", 7, ToolStatus::Done)]), + Message::assistant("資料齊了,我做成一份報告頁面。", ""), + Message::user("再補一點"), + step(vec![call("e", 7, ToolStatus::Done)]), + ]; + let cards = cards_by_message(&msgs); + assert_eq!(cards, vec![vec![], vec![], vec![], vec![], vec![7], vec![], vec![], vec![7]]); + } + #[test] fn chart_ignores_a_closing_pipe_and_names_a_lone_pie_slice() { let c = chart("bar\n| 1月 | 2月 |\n下載 | 120 | 320 |\n上傳 | 90 | 140 |").unwrap(); diff --git a/src/remote.rs b/src/remote.rs index 753ea06..c370384 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -385,11 +385,14 @@ pub(crate) fn tool_detail(name: &str, args: &str) -> String { } /// One message as the phone shows it (tool results are folded away like in the window). -pub fn message_json(m: &crate::model::Message, names: &HashMap, pages: &[crate::pages::Page]) -> Option { +/// `cards`: the pages whose card shows under this message (pages::cards_by_message); a write_page +/// call for any other is an ordinary step. +pub fn message_json(m: &crate::model::Message, names: &HashMap, pages: &[crate::pages::Page], cards: &[u64]) -> Option { use crate::model::{Role, ToolStatus}; if m.role == Role::Tool { return None; } + let mut carded: Vec = vec![]; let calls: Vec = m .tool_calls .iter() @@ -401,7 +404,16 @@ pub fn message_json(m: &crate::model::Message, names: &HashMap, pag ToolStatus::Denied => "denied", ToolStatus::Failed => "failed", }; - let page = (c.name == "write_page").then(|| serde_json::from_str::(&c.args).ok().and_then(|a| a["id"].as_u64())).flatten().and_then(|id| pages.iter().find(|p| p.id == id)); + // A page's card once, on the first call that saved it here (and only if it is this + // message's to show). + let page = (c.name == "write_page" && c.status == ToolStatus::Done) + .then(|| serde_json::from_str::(&c.args).ok().and_then(|a| a["id"].as_u64())) + .flatten() + .filter(|id| cards.contains(id) && !carded.contains(id)) + .and_then(|id| pages.iter().find(|p| p.id == id)); + if let Some(p) = page { + carded.push(p.id); + } if c.name == "show_card" { json!({"card": serde_json::from_str::(&c.args).unwrap_or(Value::Null), "status": st}) } else if matches!(c.name.as_str(), "ask_bot" | "message_bot") {