summaryrefslogtreecommitdiffstats
path: root/src/doctor.rs
blob: 42d24a8096efb956b9550d7a9c9ed440c05be600 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::path::Path;

use crate::config::{self, Config, KeySource};
use crate::provider;
use crate::session;

/// Prints the state of everything the harness needs, and checks the things that
/// are cheap to check but expensive to discover mid-turn.
pub fn run(cfg: &Config) -> i32 {
    let mut failed = false;

    println!(
        "agent686 {} · {} · {}",
        env!("CARGO_PKG_VERSION"),
        std::env::consts::ARCH,
        std::env::consts::OS
    );
    println!();

    match config::key_source() {
        KeySource::Env => row(true, "key", "present in OPENCODE_API_KEY"),
        KeySource::File(path) => row(true, "key", &format!("present in {}", path.display())),
        KeySource::Missing => {
            row(
                false,
                "key",
                "missing — set OPENCODE_API_KEY or write ~/.config/agent686/key",
            );
            failed = true;
        }
    }

    let sessions = session::sessions_dir();
    match probe_dir(&sessions) {
        Ok(()) => row(true, "sessions", &sessions.display().to_string()),
        Err(error) => {
            row(false, "sessions", &format!("{}: {error}", sessions.display()));
            failed = true;
        }
    }

    let history = session::data_root().join("history");
    match std::fs::read_to_string(&history) {
        Ok(text) => row(
            true,
            "history",
            &format!("{} ({} entries)", history.display(), text.lines().count()),
        ),
        Err(_) => row(true, "history", &format!("{} (new)", history.display())),
    }

    row(true, "provider", &cfg.base_url);
    row(true, "model", &cfg.model);

    if cfg.api_key.is_empty() {
        row(false, "auth", "skipped: no key");
        failed = true;
    } else {
        match provider::models(cfg) {
            Ok(models) => {
                row(true, "auth", &format!("ok — {} models visible", models.len()));
                if models.iter().any(|id| id == &cfg.model) {
                    row(true, "model known", "yes");
                } else {
                    row(
                        false,
                        "model known",
                        "no — the provider does not list this id; pick another with --model",
                    );
                    failed = true;
                }
            }
            Err(error) => {
                row(false, "auth", &error.to_string());
                failed = true;
            }
        }
    }

    println!();
    if failed {
        println!("some checks failed");
        1
    } else {
        println!("all checks passed");
        0
    }
}

fn row(ok: bool, label: &str, detail: &str) {
    println!("{:<13} {:<4} {detail}", label, if ok { "ok" } else { "FAIL" });
}

fn probe_dir(dir: &Path) -> std::io::Result<()> {
    std::fs::create_dir_all(dir)?;
    let probe = dir.join(".doctor-probe");
    std::fs::write(&probe, b"ok")?;
    std::fs::remove_file(probe)
}