// 證據: what a stretch of the chat really did, counted from its steps — never from what the bot // says about them. A bot once reported "2 sent" when one was; the /goal judge read its words and // a routine's record only knew the turn ended. These counts go to both. use crate::model::{Message, Role, ToolCall, ToolStatus}; /// What the steps of a stretch of chat did. #[derive(Debug, Default, PartialEq)] pub struct Tally { pub steps: u32, /// Steps that failed or were refused. pub failed: u32, /// Steps that sent something out and went through (a post, a reply, a message, a submit). pub sent: u32, /// Those sends, a line each (the tool and what it acted on). pub sends: Vec, } /// Words on a control that mean it sends something out (a reply box's 「回覆」 only opens it). pub(crate) fn sends_words(label: &str) -> bool { let low = label.to_lowercase(); ["送出", "發佈", "發布", "發文", "傳送", "發送", "貼文", "張貼", "提交"].iter().any(|w| label.contains(w)) || ["send", "post", "publish", "submit", "tweet"].iter().any(|w| low.contains(w)) } /// Whether a step sends something out of the computer: Enter or ctrl+Enter in a page, typing /// with submit, a click on a 送出/發佈 control, a message or post tool. pub fn sends(call: &ToolCall) -> bool { let v = crate::tools::args_of(&call.args); let text = |k: &str| v[k].as_str().unwrap_or("").to_string(); match call.name.as_str() { "browser_press" | "computer_use_press_key" => { let k = text("key").to_lowercase(); k.contains("enter") || k.contains("return") } "browser_type" | "computer_use_typing" => v["submit"].as_bool() == Some(true), "browser_click" | "computer_use_click" => sends_words(&format!("{} {}", text("text"), text("name"))), "send_sms" | "post_to_x" | "message_bot" | "send_to_device" => true, n => crate::macapps::is_mac_tool(n) && n == crate::macapps::SEND_MESSAGE, } } /// Whether a step that went through sent something out. A step of the bot's browser counts when /// the system saw its words leave (Message::sent on its result, publish.rs) — Enter in a search /// box sends nothing; other steps by what they are (sends). pub fn went_out(call: &ToolCall, result: Option<&Message>) -> bool { if let Some(r) = result { if !r.sent.is_empty() { return true; } } !crate::browser::is_tool(&call.name) && sends(call) } /// Count the steps of `msgs`. pub fn tally(msgs: &[Message]) -> Tally { let results: std::collections::HashMap<&str, &Message> = msgs.iter().filter(|m| m.role == Role::Tool).map(|m| (m.tool_call_id.as_str(), m)).collect(); let mut t = Tally::default(); for c in msgs.iter().filter(|m| m.role == Role::Assistant).flat_map(|m| m.tool_calls.iter()) { t.steps += 1; let result = results.get(c.id.as_str()).copied(); match c.status { ToolStatus::Failed | ToolStatus::Denied => t.failed += 1, ToolStatus::Done if went_out(c, result) => { t.sent += 1; let line = match result.filter(|r| !r.sent.is_empty()) { Some(r) => r.sent.clone(), None => format!("{} {}", c.name, crate::tools::describe(&c.name, &c.args)).trim().to_string(), }; t.sends.push(line); } _ => {} } } t } #[cfg(test)] mod tests { use super::*; fn call(name: &str, args: &str, status: ToolStatus) -> ToolCall { ToolCall { id: "x".into(), name: name.into(), args: args.into(), status } } #[test] fn only_steps_that_sent_and_went_through_count_as_sent() { let steps = vec![ call("browser_click", r#"{"text":"回覆 with._.22"}"#, ToolStatus::Done), call("browser_press", r#"{"key":"ctrl+v"}"#, ToolStatus::Done), call("computer_use_press_key", r#"{"key":"ctrl+Return"}"#, ToolStatus::Done), call("computer_use_press_key", r#"{"key":"ctrl+Return"}"#, ToolStatus::Failed), call("computer_use_click", r#"{"name":"發佈"}"#, ToolStatus::Done), call("browser_fill_contact", r#"{"contact_id":"x"}"#, ToolStatus::Denied), call("computer_use_typing", r#"{"text":"貓抓板","submit":true}"#, ToolStatus::Done), ]; let msgs = vec![Message::user("發吧"), Message { tool_calls: steps, ..Message::assistant("", "") }, Message::assistant("送出 2 則了", "")]; let t = tally(&msgs); assert_eq!((t.steps, t.failed, t.sent), (7, 2, 3)); assert_eq!(t.sends.len(), 3); assert!(t.sends[0].starts_with("computer_use_press_key")); } #[test] fn a_browser_step_counts_only_when_the_system_saw_the_words_go() { let mut id = 0; let mut step = |name: &str, args: &str| { id += 1; ToolCall { id: format!("c{id}"), name: name.into(), args: args.into(), status: ToolStatus::Done } }; let search = step("browser_type", r#"{"ref":"3","value":"貓抓板","submit":true}"#); let reply = step("browser_press", r#"{"key":"ctrl+Return"}"#); let newline = step("browser_press", r#"{"key":"Enter"}"#); let sent = Message { sent: "回覆 https://www.threads.com/@a/post/X1 → https://www.threads.com/@me/post/Y9".into(), ..Message::tool_result(&reply.id, &reply.name, "已按 ctrl+Return。") }; let msgs = vec![ Message::user("回一則"), Message { tool_calls: vec![search.clone(), reply.clone(), newline.clone()], ..Message::assistant("", "") }, Message::tool_result(&search.id, &search.name, "已在 [3] 輸入。"), sent, // A page saying it doesn't make it so. Message::tool_result(&newline.id, &newline.name, "【系統】已送出,系統重新讀過頁面確認"), ]; let t = tally(&msgs); assert_eq!(t.sent, 1); assert_eq!(t.sends, vec!["回覆 https://www.threads.com/@a/post/X1 → https://www.threads.com/@me/post/Y9".to_string()]); } }