AnyBot/src/app/doctor_ui.rs

124 lines
6.7 KiB
Rust
Raw Normal View History

feat: /doctor 診斷這台電腦:連線、Tailscale 直連還是中繼、大腦、模型服務、Bot 的電腦、連接器、磁碟、上次出錯 工程基礎計畫 E11(grok-build 的 /doctor)。早上「連不到大腦」花了半小時用指令查出是 Tailscale 走香港中繼,現在打一句就看得到。 - doctor.rs:報告的樣子(一項一行 ✓/✗,最後數有幾項要看);帳號伺服器網址是 *.ts.net 時跑 tailscale ping,讀出是直連還是走哪個中繼(走中繼寫延遲和「多半是中間擋了 UDP 41641」);df 看磁碟;找最新的當掉報告。 - app/doctor_ui.rs:當下就知道的(版本、資料夾、紀錄檔、這台 ↔ 帳號伺服器、帳號伺服器 ↔ 大腦、螢幕連不連得到大腦和原因、上次同步的大小和速度、Bot 的電腦、每個開著的連接器、磁碟少於 2 GB、上次出錯),要上網的在背景跑(Tailscale、每個 Bot 用到的模型服務列模型清單),好了在聊天留一行。 - 在連到大腦的螢幕電腦上:/doctor 在連不到大腦的檢查之前就處理,檢查的是這台本身,結果跳成提示(那個聊天是大腦的)。手機打的是大腦檢查自己。 - / 清單、手機的指令清單多一個 doctor;功能地圖 garden.md、chat.md,FEATURES.md。 已驗證:cargo test 全過、clippy -D warnings 過。新測試:報告的格式和計數;tailscale ping 的直連和中繼兩種輸出(用今天早上這台真的輸出「via DERP(hkg) in 63ms」);最新的當掉報告、磁碟;從聊天打 /doctor 先說「診斷中」、再一份報告(模型服務指向測試用的假模型)。 未驗證:螢幕電腦上真的跑一次(要更新這台 App)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 03:48:01 +00:00
// 診斷 (/doctor) in the window: what this computer can tell about itself, gathered now (the
// settings and states the window has) and on a thread (the checks that go over the network: each
// model service the bots use, how Tailscale reaches the account server), then said in one line of
// AnyBot's in the chat — on a screen of a brain, whose chats are the brain's, as a notice. What
// each line says and how: doctor.rs.
use super::*;
use crate::doctor::{bad, ok, Line};
impl App {
/// 「/doctor」: gather, check on a thread, tell (doctor_tick).
pub(super) fn doctor_start(&mut self, bot_id: u64) {
let mut lines: Vec<Line> = vec![];
let data = State::path().parent().map(|p| p.to_path_buf()).unwrap_or_default();
lines.push(ok(format!("AnyBot {};資料夾 {}", crate::version::full(crate::version::exe_modified()), data.display())));
lines.push(ok(format!("紀錄檔 {}", crate::logfile::dir().join(format!("anybot-{}.log", chrono::Local::now().format("%Y-%m-%d"))).display())));
// The account server and the brain.
let s = self.state.remote.clone();
if s.relay_url.is_empty() {
lines.push(ok("沒有登入帳號(只在這台用)"));
} else {
let status = crate::remote::hub().relay_status();
if status == "已連線" {
lines.push(ok(format!("這台 ↔ 帳號伺服器({}):已連線", s.relay_url)));
} else {
lines.push(bad(format!("這台 ↔ 帳號伺服器({}):{}", s.relay_url, if status.is_empty() { "沒有連線" } else { status.as_str() })));
}
}
if !s.brain_id.is_empty() {
match self.rm.peers.lock().unwrap().iter().find(|(id, _, _)| *id == s.brain_id).map(|x| x.2) {
Some(true) => lines.push(ok(format!("帳號伺服器 ↔ 大腦「{}」:在線上", s.brain_name))),
Some(false) => lines.push(bad(format!("帳號伺服器 ↔ 大腦「{}」:不在線上(大腦那台的 AnyBot 沒開、沒網路,或正在重新啟動)", s.brain_name))),
None => {}
}
if self.mir.on {
if self.mir.online {
lines.push(ok(format!("大腦「{}」:連得到", s.brain_name)));
} else {
lines.push(bad(format!("大腦「{}」:{}", s.brain_name, self.mir.reason)));
}
}
if let Some((at, bytes, ms)) = *self.mir.last_sync.lock().unwrap() {
let slow = bytes >= 1_000_000 && ms > 0 && bytes * 1000 / ms < 300_000;
let words = super::mirror_ui::sync_words(at, bytes, ms);
lines.push(if slow { bad(format!("{words}:線很慢")) } else { ok(words) });
}
}
// The bots' computer and the connectors (a screen's are the brain's).
if self.mir.on {
lines.push(ok("Bot 的電腦、連接器、模型服務在大腦上(到大腦那台打 /doctor 看)"));
} else {
let run = crate::cloudbox::run_state();
if crate::cloudbox::available() {
lines.push(ok(format!("Bot 的電腦:{}", run.label())));
} else {
lines.push(bad(format!("Bot 的電腦:{}(指令改在封閉的資料夾裡跑,沒有桌面)", run.label())));
}
let status = crate::mcp::status();
let errors = self.ch.mcp_errors.lock().unwrap().clone();
for m in self.state.mcp_servers.iter().filter(|m| m.enabled) {
match (status.get(&m.name), errors.iter().find(|e| e.0 == m.name)) {
(Some(n), _) => lines.push(ok(format!("連接器「{}」:已連上,{n} 個工具", m.name))),
(None, Some((_, e))) => lines.push(bad(format!("連接器「{}」:{e}", m.name))),
(None, None) => lines.push(bad(format!("連接器「{}」:還沒連上", m.name))),
}
}
}
match crate::doctor::free_mb(&data) {
Some(mb) if mb < 2048 => lines.push(bad(format!("磁碟只剩 {mb} MB(快照、模型和 Bot 的電腦都要空間)"))),
Some(mb) => lines.push(ok(format!("磁碟還有 {:.1} GB", mb as f64 / 1024.0))),
None => {}
}
if let Some((when, at)) = crate::doctor::last_crash(&crate::logfile::dir()) {
lines.push(bad(format!("上次內部出錯:{when},在 {at}(報告在紀錄檔資料夾)")));
}
// Over the network, on a thread: the model services the bots use (here, not on a screen),
// and how Tailscale reaches the account server.
let mut services: Vec<(String, crate::providers::ProviderKind, String, String)> = vec![];
if !self.mir.on {
for kind in ALL_PROVIDERS.into_iter().filter(|k| self.state.bots.iter().any(|b| b.provider == *k)) {
let ps = self.state.provider(kind);
services.push((kind.label().to_string(), kind, providers::resolve_base(kind, &ps.base_url), providers::resolve_key_for(kind, &ps.api_key, &ps.base_url)));
}
}
let ts_host = crate::doctor::tailscale_host(&s.relay_url);
let out = self.doctor_out.clone();
let repaint = self.repaint_fn();
if self.mir.on {
self.notice = Some("診斷中:正在檢查連線…".into());
} else {
self.goal_note(bot_id, "診斷中:正在檢查連線和模型服務…");
}
std::thread::spawn(move || {
if let Some(host) = ts_host {
if let Some(l) = crate::doctor::ping_tailscale(&host) {
lines.push(l);
}
}
for (name, kind, base, key) in services {
match providers::list_models(kind, &base, &key) {
Ok(m) => lines.push(ok(format!("模型服務 {name}:連得到({} 個模型)", m.len()))),
Err(e) => lines.push(bad(format!("模型服務 {name}:{}", e.chars().take(160).collect::<String>()))),
}
}
out.lock().unwrap().push((bot_id, crate::doctor::report(&lines)));
repaint();
});
self.dirty = true;
}
/// A finished 「/doctor」: its report in the chat it was asked in (a notice on a screen).
pub(super) fn doctor_tick(&mut self) {
let done: Vec<(u64, String)> = std::mem::take(&mut *self.doctor_out.lock().unwrap());
for (bot_id, text) in done {
if self.mir.on {
self.notice = Some(text);
} else {
self.goal_note(bot_id, &text);
}
self.dirty = true;
}
}
}