// 證據: 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). 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, } } /// Count the steps of `msgs`. pub fn tally(msgs: &[Message]) -> Tally { 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; match c.status { ToolStatus::Failed | ToolStatus::Denied => t.failed += 1, ToolStatus::Done if sends(c) => { t.sent += 1; t.sends.push(format!("{} {}", c.name, crate::tools::describe(&c.name, &c.args)).trim().to_string()); } _ => {} } } 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("browser_press", r#"{"key":"ctrl+Return"}"#, ToolStatus::Done), call("browser_press", r#"{"key":"ctrl+Return"}"#, ToolStatus::Failed), call("browser_click", r#"{"text":"發佈"}"#, ToolStatus::Done), call("browser_fill_contact", r#"{"contact_id":"x"}"#, ToolStatus::Denied), call("browser_type", r#"{"ref":"3","value":"貓抓板","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("browser_press")); } }