AnyBot/src/stats.rs

308 lines
15 KiB
Rust
Raw Normal View History

個人資料與用量(計畫 C4、C5,照 OpenBot 的排版):設定多一頁「個人資料」(左下角選單也有「個人資料與用量…」)。名字和「過去一年有 N 個活躍日」;五張卡——Token(過去一年,不算快取命中)、最活躍的一天、訊息、目前連續天數(今天還沒用不算斷)、最長連續天數;「活動」一年的熱度格子(53 週,越忙越深,滑過看日期、token、訊息數);「用量」7/30/90 天/一年:token(新讀、寫、快取讀到)、呼叫次數、花費(只算模型服務回報的,沒回報寫「未定價」,不是 0 元)、每天一條、依模型、依 Bot。 全部從已經有的紀錄算(stats.rs:usage/YYYY-MM.jsonl 和每則訊息的時間,usage::since_any 讀一年),用大腦的時區切日子,打開時在背景算、一分鐘內不重算。螢幕電腦向大腦要(api/sync/stats),手機的路是 api/stats(擁有者才有,手機頁面還沒做)。卡片做成 kit.rs 的 stat_card(設計檢查不准散落的手工卡片)。功能地圖 settings.md(settings-me、驗證步驟)、FEATURES.md、計畫 C4/C5 狀態、版面檢查加「設定 › 個人資料」。 已驗證: cargo test 全綠(831),新測試 stats::a_year_in_figures(不算快取命中、最忙的一天、訊息、今天還沒用不斷、最長、台北時區 23:30 還算那天、熱度四級、7 天的依模型依 Bot、檢查算在它的 Bot、刪掉的 Bot、花費和未定價)、counts_and_costs_are_said_plainly、me_ui 的日期;layout_audit 過(第一版卡片的邊框讓頁面寬了 4 px,被它抓到);clippy 錯誤數跟之前一樣(12);桌面截圖(scratch 寫進一年假的用量紀錄):五張卡、熱度格子、用量的 30 天、每日條、依模型、依 Bot;verify-anybot:api/stats 回 371 天、四個期間,沒登入 401。 未驗證: 螢幕電腦向大腦要(要兩台);真的一年資料的大腦算多久(Jenny 那台 3.5 萬則舊訊息)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-21 05:54:31 +00:00
//! 個人資料與用量 (plan C4, C5): what the person did with AnyBot over the past year, worked out
//! when asked from what is already kept — every model call's tokens (`usage/YYYY-MM.jsonl`) and
//! every message's time — nothing stored apart. Days are cut in the brain's time zone.
//!
//! The figures: tokens over the past year (not counting cache hits: what the model read anew and
//! wrote), the busiest day, messages, the current and the longest run of days used (today not
//! used yet doesn't end a run), a year of days for the heat map (53 weeks), and for 7 / 30 / 90 /
//! 365 days the tokens by kind, what the providers said it cost (a call none priced is counted as
//! 「未定價」, never $0), by model and by bot, and per day.
use crate::usage::Entry;
use chrono::{Datelike, NaiveDate, TimeZone};
use serde_json::{json, Value};
use std::collections::{BTreeMap, HashMap};
/// What the figures are made from (gathered on the app's thread: the chats live there).
pub struct Inputs {
/// When each message the person or a bot wrote was written (ms), any bot, any conversation.
pub messages: Vec<i64>,
/// Bots by id, for the names in 「依 Bot」.
pub names: HashMap<u64, String>,
/// The brain's time zone (設定 › 一般 › Bot › 時區).
pub timezone: String,
}
/// The days of the heat map: 53 weeks, ending today.
pub const DAYS: i64 = 53 * 7;
const DAY_MS: i64 = 86_400_000;
/// From the app's state: every message's time (the person's and the bots' words, not steps or
/// AnyBot's own lines), the bots' names, the time zone.
pub fn inputs(st: &crate::model::State) -> Inputs {
use crate::model::Role;
let from = crate::model::now_ms() - (DAYS + 2) * DAY_MS;
let counts = |m: &crate::model::Message| matches!(m.role, Role::User | Role::Assistant) && !m.note && m.at >= from && !(m.role == Role::Assistant && m.text.trim().is_empty());
let mut messages = vec![];
for b in &st.bots {
對話不再無限長大:長對話較早的部分收進封存檔,只留最近約 2000 則 jenny-Host 的對話 36 000 則、95 MB:整份放在記憶體,每次存檔整份重寫,每格畫面整份排過一次。 現在這段對話超過 2500 則、Bot 閒著(沒在做事、沒卡片、沒在通話)時,切在使用者說的一句之前、 不越過最新的摘要(Bot 讀到的不變),較早的寫進 chats/archive/bot-<id>/<起點>-<則數>.json (500 則一個、寫好就不再改、寫完同步到磁碟),記憶體和聊天檔只留最近約 2000 則。 - 整段對話的編號 = 封存數 + 記憶體裡的位置:手機的 index、載入更早的、表情回應、搜尋的前往都照整段算, 手機頁面和伺服器不用改。 - 桌面:最上面「更早的 N 則已封存/載入更早的」,載入的只看不存(畫在另一個 id 下); 搜尋到封存裡的那則就載入前後各 100 則並捲過去,下面有「載入後面的」。 - 搜尋、熱度圖、所有檔案:封存的話第一次在背景讀成精簡清單(chatarchive::said)。 - 開新對話、/rewind、/fork、匯出、備份、搬到大腦用整段;清除連封存一起;附件清理算進封存的附件 (讀不齊或有保留的封存時整晚不清)。分享匯出入、支線、團隊成員不帶封存欄位。 - 封存數、第一句、指紋(最後一則的時間)跟訊息寫在同一個聊天檔:任何時候停掉都對得上。 整理只在 App 自己啟動時做,只對聊天檔有寫封存數的 Bot;退役的封存只在對得上的那一個沒存好時找回, 對得上之後才刪其他的;舊版 AnyBot 寫的聊天檔照資料夾接回(緊接著才接); 讀不齊的、不屬於現在對話的封存移到 .kept- 資料夾,永遠不刪。設定讀不到時什麼都不刪;刪 Bot 時才刪它的封存。 - 寫不進去 10 分鐘後再試,不每格重試。 獨立審查(乾淨脈絡)四輪:第一輪 3 個 P0(停在聊天檔和設定之間、別的指令讀設定時清理、換對話後沒存好) 和 4 個 P1;第二輪 1 個 P0(兩個退役資料夾搬錯)、1 個 P1(舊版改寫聊天檔);第三輪 1 個 P0(設定讀不到時全刪); 第四輪剩 1 個 P1(沒接回的舊封存在下次封存被蓋掉),已修。每個都有測試。 已驗證: cargo test 全綠(938 通過,含 featuremap::、layout_audit::);新測試 chatarchive::、 a_long_chat_keeps_its_newest_and_comes_back_whole、a_stop_at_any_moment_loses_no_archived_message、 the_right_archive_comes_back_and_an_old_writer_loses_nothing、an_archived_head_is_still_the_same_conversation_everywhere; verify-anybot 實跑兩次(36 000 則:聊天檔 28 MB → 1.5 MB、68 個封存檔、手機 API 跨封存載入編號連續、 搜尋找得到封存、重開後還在;證據 target/verify-anybot/20261127-172809、20261127-175512)。 未驗證: 手機頁面點搜尋結果跳到很舊的一則(手機本來就只留最近的,提示「那則訊息太早了」,未改); 顯示大腦的另一台電腦上載入封存(設計上不提供);第二階段「過去的對話」(threads,88 MB)還沒拆。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 09:55:41 +00:00
// 封存: its words once read (chatarchive::said; until then the heat map lacks them).
if let Some(said) = crate::chatarchive::said(b.id, b.archived) {
messages.extend(said.iter().filter(|s| !s.note && s.at >= from && (s.mine || !s.text.trim().is_empty())).map(|s| s.at));
}
個人資料與用量(計畫 C4、C5,照 OpenBot 的排版):設定多一頁「個人資料」(左下角選單也有「個人資料與用量…」)。名字和「過去一年有 N 個活躍日」;五張卡——Token(過去一年,不算快取命中)、最活躍的一天、訊息、目前連續天數(今天還沒用不算斷)、最長連續天數;「活動」一年的熱度格子(53 週,越忙越深,滑過看日期、token、訊息數);「用量」7/30/90 天/一年:token(新讀、寫、快取讀到)、呼叫次數、花費(只算模型服務回報的,沒回報寫「未定價」,不是 0 元)、每天一條、依模型、依 Bot。 全部從已經有的紀錄算(stats.rs:usage/YYYY-MM.jsonl 和每則訊息的時間,usage::since_any 讀一年),用大腦的時區切日子,打開時在背景算、一分鐘內不重算。螢幕電腦向大腦要(api/sync/stats),手機的路是 api/stats(擁有者才有,手機頁面還沒做)。卡片做成 kit.rs 的 stat_card(設計檢查不准散落的手工卡片)。功能地圖 settings.md(settings-me、驗證步驟)、FEATURES.md、計畫 C4/C5 狀態、版面檢查加「設定 › 個人資料」。 已驗證: cargo test 全綠(831),新測試 stats::a_year_in_figures(不算快取命中、最忙的一天、訊息、今天還沒用不斷、最長、台北時區 23:30 還算那天、熱度四級、7 天的依模型依 Bot、檢查算在它的 Bot、刪掉的 Bot、花費和未定價)、counts_and_costs_are_said_plainly、me_ui 的日期;layout_audit 過(第一版卡片的邊框讓頁面寬了 4 px,被它抓到);clippy 錯誤數跟之前一樣(12);桌面截圖(scratch 寫進一年假的用量紀錄):五張卡、熱度格子、用量的 30 天、每日條、依模型、依 Bot;verify-anybot:api/stats 回 371 天、四個期間,沒登入 401。 未驗證: 螢幕電腦向大腦要(要兩台);真的一年資料的大腦算多久(Jenny 那台 3.5 萬則舊訊息)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-21 05:54:31 +00:00
messages.extend(b.messages.iter().filter(|m| counts(m)).map(|m| m.at));
for (_, t) in b.threads.iter() {
messages.extend(t.iter().filter(|m| counts(m)).map(|m| m.at));
}
}
Inputs { messages, names: st.bots.iter().map(|b| (b.id, b.name.clone())).collect(), timezone: st.timezone.clone() }
}
/// Everything, from the inputs and the year's usage on disk.
pub fn build(inp: &Inputs, now_ms: i64) -> Value {
compute(inp, &crate::usage::since_any(now_ms - (DAYS + 2) * DAY_MS), now_ms)
}
/// Tokens that count: what the model read anew (and wrote to its cache) and what it wrote — a
/// cache hit is the same words read again, cheaply.
fn counted(u: &crate::providers::Usage) -> u64 {
u.input + u.cache_write + u.output
}
pub fn compute(inp: &Inputs, entries: &[Entry], now_ms: i64) -> Value {
let tz = crate::routines::tz_of(&inp.timezone);
let day_of = |ms: i64| tz.timestamp_millis_opt(ms).single().map(|t| t.date_naive());
let Some(today) = day_of(now_ms) else { return json!({}) };
let first = today - chrono::Duration::days(DAYS - 1);
// Per day of the past year: tokens, messages.
let mut days: BTreeMap<NaiveDate, (u64, u64)> = BTreeMap::new();
for e in entries {
if let Some(d) = day_of(e.at).filter(|d| *d >= first && *d <= today) {
days.entry(d).or_default().0 += counted(&e.usage);
}
}
for &at in &inp.messages {
if let Some(d) = day_of(at).filter(|d| *d >= first && *d <= today) {
days.entry(d).or_default().1 += 1;
}
}
let year_from = today - chrono::Duration::days(364);
let in_year = |d: &NaiveDate| *d >= year_from;
let tokens_year: u64 = days.iter().filter(|(d, _)| in_year(d)).map(|(_, v)| v.0).sum();
let messages_year: u64 = days.iter().filter(|(d, _)| in_year(d)).map(|(_, v)| v.1).sum();
let busiest = days.iter().filter(|(d, v)| in_year(d) && v.0 > 0).max_by_key(|(d, v)| (v.0, std::cmp::Reverse(**d))).map(|(d, v)| json!({"date": d.to_string(), "tokens": v.0}));
let active = |d: &NaiveDate| days.get(d).is_some_and(|v| v.0 > 0 || v.1 > 0);
// The run up to today (today not used yet: up to yesterday).
let mut end = today;
if !active(&end) {
end = today - chrono::Duration::days(1);
}
let mut start = end;
while active(&start) && start > first {
start -= chrono::Duration::days(1);
}
let start = if active(&start) { start } else { start + chrono::Duration::days(1) };
let now_run = if active(&end) { (end - start).num_days() + 1 } else { 0 };
// The longest run in the year.
let (mut best, mut best_from, mut best_to, mut run, mut run_from) = (0i64, first, first, 0i64, first);
let mut d = first;
while d <= today {
if active(&d) {
if run == 0 {
run_from = d;
}
run += 1;
if run > best {
(best, best_from, best_to) = (run, run_from, d);
}
} else {
run = 0;
}
d += chrono::Duration::days(1);
}
let active_days = days.iter().filter(|(d, v)| in_year(d) && (v.0 > 0 || v.1 > 0)).count();
// The heat map: every day from `first` (a Sunday-started week grid is the page's to draw).
let max = days.values().map(|v| v.0).max().unwrap_or(0).max(1);
let max_msgs = days.values().map(|v| v.1).max().unwrap_or(0).max(1);
let level = |t: u64, m: u64| -> u8 {
// Tokens when there are any; a day with only messages (a provider that reports none) by them.
let share = if t > 0 { t as f64 / max as f64 } else { m as f64 / max_msgs as f64 };
if t == 0 && m == 0 {
0
} else if share < 0.25 {
1
} else if share < 0.5 {
2
} else if share < 0.75 {
3
} else {
4
}
};
let mut grid = vec![];
let mut d = first;
while d <= today {
let (t, m) = days.get(&d).copied().unwrap_or_default();
grid.push(json!([d.to_string(), t, m, level(t, m)]));
d += chrono::Duration::days(1);
}
// 用量: the last 7 / 30 / 90 / 365 days.
let name = |id: u64, what: &str| -> String {
if id == 0 {
return format!("({what})");
}
let n = inp.names.get(&id).cloned().unwrap_or_else(|| format!("Bot {id}(已刪除)"));
if what == "回合" { n } else { format!("{n}({what})") }
};
let mut periods = serde_json::Map::new();
for span in [7i64, 30, 90, 365] {
let from = today - chrono::Duration::days(span - 1);
let mut total = crate::providers::Usage::default();
let (mut calls, mut unpriced) = (0u64, 0u64);
let mut by_model: BTreeMap<String, (u64, u64, u64)> = BTreeMap::new();
let mut by_bot: BTreeMap<String, (u64, u64, u64)> = BTreeMap::new();
let mut per_day: BTreeMap<NaiveDate, u64> = BTreeMap::new();
for e in entries {
let Some(d) = day_of(e.at).filter(|d| *d >= from && *d <= today) else { continue };
total.add(&e.usage);
calls += 1;
if e.usage.cost_micros == 0 {
unpriced += 1;
}
let t = counted(&e.usage);
let m = by_model.entry(format!("{} · {}", e.provider, e.model)).or_default();
(m.0, m.1, m.2) = (m.0 + t, m.1 + e.usage.cost_micros, m.2 + 1);
let (id, what) = e.whose();
let b = by_bot.entry(name(id, what)).or_default();
(b.0, b.1, b.2) = (b.0 + t, b.1 + e.usage.cost_micros, b.2 + 1);
*per_day.entry(d).or_default() += t;
}
let rows = |m: BTreeMap<String, (u64, u64, u64)>| -> Vec<Value> {
let mut v: Vec<(String, (u64, u64, u64))> = m.into_iter().collect();
v.sort_by_key(|(k, x)| (std::cmp::Reverse(x.0), k.clone()));
v.into_iter().map(|(k, x)| json!({"name": k, "tokens": x.0, "cost_micros": x.1, "calls": x.2})).collect()
};
let mut daily = vec![];
let mut d = from;
while d <= today {
daily.push(json!([d.to_string(), per_day.get(&d).copied().unwrap_or(0)]));
d += chrono::Duration::days(1);
}
periods.insert(
span.to_string(),
json!({
"tokens": counted(&total), "input": total.input, "output": total.output, "cache_read": total.cache_read, "cache_write": total.cache_write,
"cost_micros": total.cost_micros, "calls": calls, "unpriced": unpriced,
"by_model": rows(by_model), "by_bot": rows(by_bot), "daily": daily,
}),
);
}
json!({
"today": today.to_string(),
"first": first.to_string(),
"tokens_year": tokens_year,
"messages_year": messages_year,
"busiest": busiest,
"streak_now": now_run,
"streak_now_since": (now_run > 0).then(|| start.to_string()),
"streak_best": best,
"streak_best_from": (best > 0).then(|| best_from.to_string()),
"streak_best_to": (best > 0).then(|| best_to.to_string()),
"active_days": active_days,
"days": grid,
"periods": periods,
"weekday_first": first.weekday().num_days_from_sunday(),
})
}
/// A count the way the page says it: 9,999; 5993.4萬; 1.20億.
pub fn wan(n: u64) -> String {
match n {
0..=9_999 => {
let s = n.to_string();
if n >= 1_000 { format!("{},{}", &s[..s.len() - 3], &s[s.len() - 3..]) } else { s }
}
字體改成 Apple 的(SF Pro+蘋方,粗體用半粗),回答不再用宋體;設定左邊每頁加圖示;「模型」改成供應商圖塊(不再是設定旁邊再一排清單);「個人資料與用量」改成數字列+大數字+長條+彩色比例條,不再一整片字。 - 字體(src/main.rs):Mac 上介面和讀的文字都用系統的 SF Pro(可變字體,400/600)加蘋方 PingFang TC(Regular/Semibold),跟 macOS 自己一樣;沒有蘋方的電腦用包在 App 裡的思源黑體。蘋方 78MB 只讀一次共用。思源宋體拿掉(App 小 16MB)。手機的 --font-read 改成跟介面同一種,標題 600。 - 設定左邊分頁:原本就配好的圖示畫出來(齒輪、人、星光、格子、螢幕、盾牌、保險庫、麥克風、雲);拿掉分頁和內容之間的線(底色已經分開)。 - 模型(新 src/app/models_ui.rs):每家一塊圖塊——服務的圖案(src/icons.rs provider_mark,自己畫的簡單圖形,不是任何人的商標圖檔)、名字、「● 可以用/還沒設定」、雲端/本機;點進去才是金鑰和網址,上面「‹ 模型」回來。從聊天、搜尋設定跳來直接進那一家。 - 個人資料與用量(src/app/me_ui.rs):五張有框的卡(兩三欄)→ 一條淡底沒有框的數字列(細節在滑過);最活躍的一天放到熱度格子旁邊一行;用量:大數字 token(新讀、寫、快取在滑過)、呼叫和花費一行、每天的長條、「依模型/依 Bot」切換(依模型一條彩色比例條,每列有服務圖案;依 Bot 每列有頭像)。示範截圖有一年的假資料(ANYBOT_SHOT_TAB=me)。 - 數字:過千萬不帶小數(3,699萬,不是 3699.0萬),整數不寫「.0」。 - 修:任務頁兩個任務同一毫秒開頭時 id 一樣,兩張卡搶同一個按鍵(版面檢查在變慢後抓到);卡片 id 加上它在清單裡的位置。 已驗證: cargo test 876 個全過(含 layout_audit::、featuremap::、stats::);clippy 沒有新警告;desktop-shot 看過聊天(中英混排基準線對齊)、設定一般、模型圖塊、個人資料上半和用量(捲下去)。 未驗證: 手機頁面的字體只改了 CSS,沒有在真的 iPhone 上看;Windows/Linux 的思源黑體備案沒有實機跑;版面檢查變慢(約 40 秒 → 100 秒,每個畫面都載入蘋方)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 00:35:28 +00:00
// (No「.0」, and no decimal at all past a thousand 萬: 3,699萬, not 3699.0萬.)
修獨立審查(乾淨脈絡審 main...design-fresh-look)找到的 5 個 P1 和 10 個 P2;手機整合頁的技能列補圖示。 P1 1. 手機接管的鎖會永遠卡住:改成每次只算 30 秒(cloudbox::hold_for,跟電腦的接管分開存),畫面頁每 10 秒續一次,斷線時放開、重連再鎖;沒人續就自己過期,電腦也不會因此不休眠(held_by_person)。 2. Mac 的觸控板點兩下不是雙擊:第二下送 count 2(VNC 那邊照舊多按一下,螢幕自己數)。 3. 聊天的流程圖每次重畫都重抓、閃:畫好的記住(DIAGRAMS,最多 60 張),畫不出來的也記住;還在寫的回答等寫完才畫。 4. 同一個網路直接連的手機能改服務網址(存著的金鑰會被送到那個網址):provider 和 voice 帶 base_url 算放寬,擋下。 5. 智慧模式秒數:大腦收在 30–3600(以前手機能設 0 或 1 秒,電腦一開又被改回去);手機拿掉「不自己回答」;直接連線一律擋。 P2 6. 手機開關一律放行,那行說明不再寫進主要 Bot 的聊天(從聊天上方關的寫在那個聊天,設定頁的不寫)。 7. 從通知進聊天草稿不還原、之後被刪:輸入框是誰的另外記(S.draftOf)。 8. 觸控板按慢一點(0.26–0.56 秒)沒反應:沒移動、長按還沒到就算點一下;系統打斷觸控不算點。 9. 拖曳中斷線左鍵卡住:停用觸控板時先放開;斷線時按鍵狀態歸零。 10. 工作流和搜尋設定的延後重畫會蓋掉別頁:重畫前先看還在不在那一頁。 11. 所有檔案每打一個字重抓縮圖:縮圖記住。 12. 日程每次進來都捲到今天。 13. 數字邊界:9,995,000 是「1,000萬」、99,995,000 是「1億」(Rust 的 wan;手機的同名函式本來就照四捨五入後的值)。 14. 圖表的 SVG 回傳前檢查(svg_is_plain):有 script、foreignObject、on…= 或 javascript: 的不給。 15. circle-dot 圖示補上;團隊成員不再看到按了會錯的「匯出文字記錄」;電腦搜尋設定點「模型」回到圖塊。 已驗證: cargo test 883 個、relay 27 個全過,clippy 沒有新警告;node mobile/trackpad.test.mjs 15 項 OK(新增:第二下是 count 2、慢點也算點、被打斷不算);scratch 手機:流程圖畫好後再重畫聊天,圖還是同一張(沒有重抓);先設 S.open 再進聊天草稿還在;13 個清單頁和 Bot 設定逐一打開,沒有任何執行錯誤;直接連線設智慧模式秒數、改服務網址都回 403;api/box/hold 回 ok;桌面日程、市集、所有檔案截圖正常。 未驗證: 接管的鎖 30 秒過期在真的 Bot 電腦上(要 Docker;有 cloudbox 的單元測試);Mac 上真的雙擊(會動到使用者的螢幕)。觸控板、接管這些電腦操作的路徑照規定還要實機測試和人簽名才算發佈——這次照使用者的要求合併回 main,沒有部署到任何伺服器。 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 03:07:39 +00:00
// (The edges by what they round to: 9,995,000 is 1,000萬, 99,995,000 is 1億.)
10_000..=9_994_999 => format!("{:.1}萬", n as f64 / 10_000.0).replace(".0萬", "萬"),
9_995_000..=99_994_999 => {
字體改成 Apple 的(SF Pro+蘋方,粗體用半粗),回答不再用宋體;設定左邊每頁加圖示;「模型」改成供應商圖塊(不再是設定旁邊再一排清單);「個人資料與用量」改成數字列+大數字+長條+彩色比例條,不再一整片字。 - 字體(src/main.rs):Mac 上介面和讀的文字都用系統的 SF Pro(可變字體,400/600)加蘋方 PingFang TC(Regular/Semibold),跟 macOS 自己一樣;沒有蘋方的電腦用包在 App 裡的思源黑體。蘋方 78MB 只讀一次共用。思源宋體拿掉(App 小 16MB)。手機的 --font-read 改成跟介面同一種,標題 600。 - 設定左邊分頁:原本就配好的圖示畫出來(齒輪、人、星光、格子、螢幕、盾牌、保險庫、麥克風、雲);拿掉分頁和內容之間的線(底色已經分開)。 - 模型(新 src/app/models_ui.rs):每家一塊圖塊——服務的圖案(src/icons.rs provider_mark,自己畫的簡單圖形,不是任何人的商標圖檔)、名字、「● 可以用/還沒設定」、雲端/本機;點進去才是金鑰和網址,上面「‹ 模型」回來。從聊天、搜尋設定跳來直接進那一家。 - 個人資料與用量(src/app/me_ui.rs):五張有框的卡(兩三欄)→ 一條淡底沒有框的數字列(細節在滑過);最活躍的一天放到熱度格子旁邊一行;用量:大數字 token(新讀、寫、快取在滑過)、呼叫和花費一行、每天的長條、「依模型/依 Bot」切換(依模型一條彩色比例條,每列有服務圖案;依 Bot 每列有頭像)。示範截圖有一年的假資料(ANYBOT_SHOT_TAB=me)。 - 數字:過千萬不帶小數(3,699萬,不是 3699.0萬),整數不寫「.0」。 - 修:任務頁兩個任務同一毫秒開頭時 id 一樣,兩張卡搶同一個按鍵(版面檢查在變慢後抓到);卡片 id 加上它在清單裡的位置。 已驗證: cargo test 876 個全過(含 layout_audit::、featuremap::、stats::);clippy 沒有新警告;desktop-shot 看過聊天(中英混排基準線對齊)、設定一般、模型圖塊、個人資料上半和用量(捲下去)。 未驗證: 手機頁面的字體只改了 CSS,沒有在真的 iPhone 上看;Windows/Linux 的思源黑體備案沒有實機跑;版面檢查變慢(約 40 秒 → 100 秒,每個畫面都載入蘋方)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 00:35:28 +00:00
let s = ((n as f64 / 10_000.0).round() as u64).to_string();
format!("{},{}萬", &s[..s.len() - 3], &s[s.len() - 3..])
}
_ => format!("{:.2}億", n as f64 / 100_000_000.0).replace(".00億", "億"),
個人資料與用量(計畫 C4、C5,照 OpenBot 的排版):設定多一頁「個人資料」(左下角選單也有「個人資料與用量…」)。名字和「過去一年有 N 個活躍日」;五張卡——Token(過去一年,不算快取命中)、最活躍的一天、訊息、目前連續天數(今天還沒用不算斷)、最長連續天數;「活動」一年的熱度格子(53 週,越忙越深,滑過看日期、token、訊息數);「用量」7/30/90 天/一年:token(新讀、寫、快取讀到)、呼叫次數、花費(只算模型服務回報的,沒回報寫「未定價」,不是 0 元)、每天一條、依模型、依 Bot。 全部從已經有的紀錄算(stats.rs:usage/YYYY-MM.jsonl 和每則訊息的時間,usage::since_any 讀一年),用大腦的時區切日子,打開時在背景算、一分鐘內不重算。螢幕電腦向大腦要(api/sync/stats),手機的路是 api/stats(擁有者才有,手機頁面還沒做)。卡片做成 kit.rs 的 stat_card(設計檢查不准散落的手工卡片)。功能地圖 settings.md(settings-me、驗證步驟)、FEATURES.md、計畫 C4/C5 狀態、版面檢查加「設定 › 個人資料」。 已驗證: cargo test 全綠(831),新測試 stats::a_year_in_figures(不算快取命中、最忙的一天、訊息、今天還沒用不斷、最長、台北時區 23:30 還算那天、熱度四級、7 天的依模型依 Bot、檢查算在它的 Bot、刪掉的 Bot、花費和未定價)、counts_and_costs_are_said_plainly、me_ui 的日期;layout_audit 過(第一版卡片的邊框讓頁面寬了 4 px,被它抓到);clippy 錯誤數跟之前一樣(12);桌面截圖(scratch 寫進一年假的用量紀錄):五張卡、熱度格子、用量的 30 天、每日條、依模型、依 Bot;verify-anybot:api/stats 回 371 天、四個期間,沒登入 401。 未驗證: 螢幕電腦向大腦要(要兩台);真的一年資料的大腦算多久(Jenny 那台 3.5 萬則舊訊息)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-21 05:54:31 +00:00
}
}
/// What the providers said it cost, or 「未定價」 when none did.
pub fn cost_words(micros: u64, unpriced: u64, calls: u64) -> String {
if micros == 0 {
return "未定價".into();
}
let s = format!("US${:.2}", micros as f64 / 1_000_000.0);
if unpriced > 0 && unpriced < calls {
format!("{s}(另有 {unpriced} 次呼叫未定價)")
} else {
s
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::providers::Usage;
fn at(tz: &str, y: i32, m: u32, d: u32, h: u32) -> i64 {
crate::routines::tz_of(tz).with_ymd_and_hms(y, m, d, h, 0, 0).unwrap().timestamp_millis()
}
fn entry(ms: i64, model: &str, session: &str, input: u64, output: u64, cache_read: u64, cost: u64) -> Entry {
Entry { at: ms, provider: "xAI Grok".into(), model: model.into(), session: session.into(), usage: Usage { input, output, cache_read, cache_write: 0, cost_micros: cost } }
}
/// The year in figures: tokens without cache hits, the busiest day, messages, the run up to
/// today (today not used yet doesn't end it), the longest run, days cut in the brain's zone.
#[test]
fn a_year_in_figures() {
let tz = "Asia/Taipei";
let now = at(tz, 2026, 11, 21, 15);
let inp = Inputs {
// 11/18, 11/19, 11/20 (one at 23:30 Taipei — 15:30 UTC, still the 20th there), nothing today.
messages: vec![at(tz, 2026, 11, 18, 10), at(tz, 2026, 11, 19, 10), at(tz, 2026, 11, 20, 23) + 30 * 60_000],
names: [(2u64, "賴桑".to_string())].into_iter().collect(),
timezone: tz.into(),
};
let entries = vec![
entry(at(tz, 2026, 11, 19, 9), "grok-4", "anybot-bot-2", 1_000, 200, 50_000, 0),
entry(at(tz, 2026, 11, 20, 9), "grok-4", "anybot-bot-2", 3_000, 500, 0, 0),
entry(at(tz, 2026, 11, 20, 10), "kimi-k3", "anybot-check-2", 100, 10, 0, 12_000),
// A year and more ago: not in the year.
entry(at(tz, 2025, 11, 1, 9), "grok-4", "anybot-bot-9", 9_999_999, 0, 0, 0),
// Two weeks ago, one day alone.
entry(at(tz, 2026, 11, 7, 9), "grok-4", "anybot-bot-9", 10, 0, 0, 0),
];
let s = compute(&inp, &entries, now);
assert_eq!(s["tokens_year"], 1_200 + 3_500 + 110 + 10, "cache hits aren't counted");
assert_eq!(s["busiest"], json!({"date": "2026-11-20", "tokens": 3_610}));
assert_eq!(s["messages_year"], 3);
assert_eq!((s["streak_now"].as_i64(), s["streak_now_since"].as_str()), (Some(3), Some("2026-11-18")), "today not used yet: the run still stands");
assert_eq!((s["streak_best"].as_i64(), s["streak_best_from"].as_str(), s["streak_best_to"].as_str()), (Some(3), Some("2026-11-18"), Some("2026-11-20")));
assert_eq!(s["active_days"], 4);
let days = s["days"].as_array().unwrap();
assert_eq!(days.len() as i64, DAYS);
assert_eq!(days.last().unwrap()[0], "2026-11-21");
let lvl = |date: &str| days.iter().find(|d| d[0] == date).map(|d| d[3].as_u64().unwrap()).unwrap();
assert_eq!((lvl("2026-11-20"), lvl("2026-11-19"), lvl("2026-11-21")), (4, 2, 0));
// 用量 for 7 days: by model, by bot (a check by its bot), what it cost, what wasn't priced.
let w = &s["periods"]["7"];
assert_eq!((w["calls"].as_u64(), w["unpriced"].as_u64(), w["cost_micros"].as_u64()), (Some(3), Some(2), Some(12_000)));
assert_eq!(w["by_model"][0]["name"], "xAI Grok · grok-4");
let bots: Vec<&str> = w["by_bot"].as_array().unwrap().iter().map(|r| r["name"].as_str().unwrap()).collect();
assert_eq!(bots, ["賴桑", "賴桑(發布前檢查)"]);
assert_eq!(w["daily"].as_array().unwrap().len(), 7);
assert_eq!(s["periods"]["30"]["by_bot"].as_array().unwrap().iter().find(|r| r["name"] == "Bot 9(已刪除)").map(|r| r["tokens"].as_u64()), Some(Some(10)));
// A day used today: the run goes through today.
let mut inp2 = inp;
inp2.messages.push(at(tz, 2026, 11, 21, 9));
assert_eq!(compute(&inp2, &entries, now)["streak_now"], 4);
}
#[test]
fn counts_and_costs_are_said_plainly() {
字體改成 Apple 的(SF Pro+蘋方,粗體用半粗),回答不再用宋體;設定左邊每頁加圖示;「模型」改成供應商圖塊(不再是設定旁邊再一排清單);「個人資料與用量」改成數字列+大數字+長條+彩色比例條,不再一整片字。 - 字體(src/main.rs):Mac 上介面和讀的文字都用系統的 SF Pro(可變字體,400/600)加蘋方 PingFang TC(Regular/Semibold),跟 macOS 自己一樣;沒有蘋方的電腦用包在 App 裡的思源黑體。蘋方 78MB 只讀一次共用。思源宋體拿掉(App 小 16MB)。手機的 --font-read 改成跟介面同一種,標題 600。 - 設定左邊分頁:原本就配好的圖示畫出來(齒輪、人、星光、格子、螢幕、盾牌、保險庫、麥克風、雲);拿掉分頁和內容之間的線(底色已經分開)。 - 模型(新 src/app/models_ui.rs):每家一塊圖塊——服務的圖案(src/icons.rs provider_mark,自己畫的簡單圖形,不是任何人的商標圖檔)、名字、「● 可以用/還沒設定」、雲端/本機;點進去才是金鑰和網址,上面「‹ 模型」回來。從聊天、搜尋設定跳來直接進那一家。 - 個人資料與用量(src/app/me_ui.rs):五張有框的卡(兩三欄)→ 一條淡底沒有框的數字列(細節在滑過);最活躍的一天放到熱度格子旁邊一行;用量:大數字 token(新讀、寫、快取在滑過)、呼叫和花費一行、每天的長條、「依模型/依 Bot」切換(依模型一條彩色比例條,每列有服務圖案;依 Bot 每列有頭像)。示範截圖有一年的假資料(ANYBOT_SHOT_TAB=me)。 - 數字:過千萬不帶小數(3,699萬,不是 3699.0萬),整數不寫「.0」。 - 修:任務頁兩個任務同一毫秒開頭時 id 一樣,兩張卡搶同一個按鍵(版面檢查在變慢後抓到);卡片 id 加上它在清單裡的位置。 已驗證: cargo test 876 個全過(含 layout_audit::、featuremap::、stats::);clippy 沒有新警告;desktop-shot 看過聊天(中英混排基準線對齊)、設定一般、模型圖塊、個人資料上半和用量(捲下去)。 未驗證: 手機頁面的字體只改了 CSS,沒有在真的 iPhone 上看;Windows/Linux 的思源黑體備案沒有實機跑;版面檢查變慢(約 40 秒 → 100 秒,每個畫面都載入蘋方)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 00:35:28 +00:00
assert_eq!((wan(999), wan(9_999), wan(59_934_000), wan(294_000), wan(120_000_000)), ("999".into(), "9,999".into(), "5,993萬".into(), "29.4萬".into(), "1.20億".into()));
assert_eq!((wan(20_000), wan(100_000_000)), ("2萬".into(), "1億".into()));
修獨立審查(乾淨脈絡審 main...design-fresh-look)找到的 5 個 P1 和 10 個 P2;手機整合頁的技能列補圖示。 P1 1. 手機接管的鎖會永遠卡住:改成每次只算 30 秒(cloudbox::hold_for,跟電腦的接管分開存),畫面頁每 10 秒續一次,斷線時放開、重連再鎖;沒人續就自己過期,電腦也不會因此不休眠(held_by_person)。 2. Mac 的觸控板點兩下不是雙擊:第二下送 count 2(VNC 那邊照舊多按一下,螢幕自己數)。 3. 聊天的流程圖每次重畫都重抓、閃:畫好的記住(DIAGRAMS,最多 60 張),畫不出來的也記住;還在寫的回答等寫完才畫。 4. 同一個網路直接連的手機能改服務網址(存著的金鑰會被送到那個網址):provider 和 voice 帶 base_url 算放寬,擋下。 5. 智慧模式秒數:大腦收在 30–3600(以前手機能設 0 或 1 秒,電腦一開又被改回去);手機拿掉「不自己回答」;直接連線一律擋。 P2 6. 手機開關一律放行,那行說明不再寫進主要 Bot 的聊天(從聊天上方關的寫在那個聊天,設定頁的不寫)。 7. 從通知進聊天草稿不還原、之後被刪:輸入框是誰的另外記(S.draftOf)。 8. 觸控板按慢一點(0.26–0.56 秒)沒反應:沒移動、長按還沒到就算點一下;系統打斷觸控不算點。 9. 拖曳中斷線左鍵卡住:停用觸控板時先放開;斷線時按鍵狀態歸零。 10. 工作流和搜尋設定的延後重畫會蓋掉別頁:重畫前先看還在不在那一頁。 11. 所有檔案每打一個字重抓縮圖:縮圖記住。 12. 日程每次進來都捲到今天。 13. 數字邊界:9,995,000 是「1,000萬」、99,995,000 是「1億」(Rust 的 wan;手機的同名函式本來就照四捨五入後的值)。 14. 圖表的 SVG 回傳前檢查(svg_is_plain):有 script、foreignObject、on…= 或 javascript: 的不給。 15. circle-dot 圖示補上;團隊成員不再看到按了會錯的「匯出文字記錄」;電腦搜尋設定點「模型」回到圖塊。 已驗證: cargo test 883 個、relay 27 個全過,clippy 沒有新警告;node mobile/trackpad.test.mjs 15 項 OK(新增:第二下是 count 2、慢點也算點、被打斷不算);scratch 手機:流程圖畫好後再重畫聊天,圖還是同一張(沒有重抓);先設 S.open 再進聊天草稿還在;13 個清單頁和 Bot 設定逐一打開,沒有任何執行錯誤;直接連線設智慧模式秒數、改服務網址都回 403;api/box/hold 回 ok;桌面日程、市集、所有檔案截圖正常。 未驗證: 接管的鎖 30 秒過期在真的 Bot 電腦上(要 Docker;有 cloudbox 的單元測試);Mac 上真的雙擊(會動到使用者的螢幕)。觸控板、接管這些電腦操作的路徑照規定還要實機測試和人簽名才算發佈——這次照使用者的要求合併回 main,沒有部署到任何伺服器。 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-22 03:07:39 +00:00
assert_eq!((wan(9_994_999), wan(9_995_000), wan(99_994_999), wan(99_995_000)), ("999.5萬".into(), "1,000萬".into(), "9,999萬".into(), "1億".into()));
個人資料與用量(計畫 C4、C5,照 OpenBot 的排版):設定多一頁「個人資料」(左下角選單也有「個人資料與用量…」)。名字和「過去一年有 N 個活躍日」;五張卡——Token(過去一年,不算快取命中)、最活躍的一天、訊息、目前連續天數(今天還沒用不算斷)、最長連續天數;「活動」一年的熱度格子(53 週,越忙越深,滑過看日期、token、訊息數);「用量」7/30/90 天/一年:token(新讀、寫、快取讀到)、呼叫次數、花費(只算模型服務回報的,沒回報寫「未定價」,不是 0 元)、每天一條、依模型、依 Bot。 全部從已經有的紀錄算(stats.rs:usage/YYYY-MM.jsonl 和每則訊息的時間,usage::since_any 讀一年),用大腦的時區切日子,打開時在背景算、一分鐘內不重算。螢幕電腦向大腦要(api/sync/stats),手機的路是 api/stats(擁有者才有,手機頁面還沒做)。卡片做成 kit.rs 的 stat_card(設計檢查不准散落的手工卡片)。功能地圖 settings.md(settings-me、驗證步驟)、FEATURES.md、計畫 C4/C5 狀態、版面檢查加「設定 › 個人資料」。 已驗證: cargo test 全綠(831),新測試 stats::a_year_in_figures(不算快取命中、最忙的一天、訊息、今天還沒用不斷、最長、台北時區 23:30 還算那天、熱度四級、7 天的依模型依 Bot、檢查算在它的 Bot、刪掉的 Bot、花費和未定價)、counts_and_costs_are_said_plainly、me_ui 的日期;layout_audit 過(第一版卡片的邊框讓頁面寬了 4 px,被它抓到);clippy 錯誤數跟之前一樣(12);桌面截圖(scratch 寫進一年假的用量紀錄):五張卡、熱度格子、用量的 30 天、每日條、依模型、依 Bot;verify-anybot:api/stats 回 371 天、四個期間,沒登入 401。 未驗證: 螢幕電腦向大腦要(要兩台);真的一年資料的大腦算多久(Jenny 那台 3.5 萬則舊訊息)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
2026-11-21 05:54:31 +00:00
assert_eq!(cost_words(0, 5, 5), "未定價");
assert_eq!(cost_words(1_230_000, 0, 4), "US$1.23");
assert_eq!(cost_words(1_230_000, 2, 4), "US$1.23(另有 2 次呼叫未定價)");
}
}