AnyBot/src/app/doctor_ui.rs

126 lines
6.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 診斷 (/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))),
}
}
}
// (Commands for 「你的電腦」 run here, a screen of a brain too.)
lines.push(crate::doctor::sandbox_line());
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;
}
}
}