claudette 0.8.1

Local-first AI personal secretary for Ollama. Telegram bot, voice, persistent scheduler, Gmail and Calendar. Single-binary Rust.
Documentation
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! `claudette --doctor` — diagnostic probe of every external dependency.
//!
//! Resolves every `CLAUDETTE_*` env var the runtime cares about, then prints
//! green/red status lines for: Ollama / LM Studio reachable, the configured
//! brain model pulled, the embed endpoint + recall model loaded, Google
//! OAuth tokens valid for each configured scope, `ffmpeg` / `whisper-cli`
//! on PATH, and the `~/.claudette/secrets/*` token files.
//!
//! This is intentionally a flat probe — each check is independent, no probe
//! short-circuits the others. The user wants to see *everything* at once;
//! one broken row shouldn't hide a different broken row underneath.

use std::path::PathBuf;
use std::process::Command;
use std::time::Duration;

use serde_json::Value;

use crate::theme;

/// Outcome of one diagnostic probe.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status {
    Ok,
    Warn,
    Err,
}

impl Status {
    fn glyph(self) -> &'static str {
        match self {
            Self::Ok => theme::OK_GLYPH,
            Self::Warn => theme::WARN_GLYPH,
            Self::Err => theme::ERR_GLYPH,
        }
    }
}

fn print_row(label: &str, status: Status, detail: &str) {
    let glyph = status.glyph();
    let painted = match status {
        Status::Ok => theme::ok(label).to_string(),
        Status::Warn => theme::warn(label).to_string(),
        Status::Err => theme::error(label).to_string(),
    };
    if detail.is_empty() {
        eprintln!("  {glyph} {painted}");
    } else {
        eprintln!("  {glyph} {painted}  {}", theme::dim(detail));
    }
}

fn print_section(title: &str) {
    eprintln!();
    eprintln!("{}", theme::accent(title));
}

fn home_dir() -> PathBuf {
    let raw = std::env::var("USERPROFILE")
        .or_else(|_| std::env::var("HOME"))
        .unwrap_or_else(|_| ".".to_string());
    PathBuf::from(raw)
}

fn claudette_home() -> PathBuf {
    home_dir().join(".claudette")
}

/// Entry point — runs every probe and returns the exit code for the CLI.
/// Returns `0` when nothing is `Err` (warnings are allowed), `1` otherwise.
pub fn run() -> i32 {
    theme::init();
    eprintln!(
        "{} {}",
        theme::GEAR,
        theme::brand(&format!(
            "claudette --doctor (v{})",
            env!("CARGO_PKG_VERSION")
        ))
    );

    let mut any_err = false;
    let mut bump = |s: Status| {
        if s == Status::Err {
            any_err = true;
        }
    };

    print_section("environment");
    bump(probe_env());

    print_section("local brain");
    bump(probe_brain());

    print_section("recall / embeddings");
    bump(probe_recall());

    print_section("google oauth");
    bump(probe_google_oauth());

    print_section("voice (optional)");
    bump(probe_voice());

    print_section("secrets directory");
    bump(probe_secrets());

    eprintln!();
    if any_err {
        eprintln!(
            "{} {}",
            theme::ERR_GLYPH,
            theme::error("one or more probes failed — see red rows above")
        );
        1
    } else {
        eprintln!("{} {}", theme::OK_GLYPH, theme::ok("all probes passed"));
        0
    }
}

// ─── Env vars ────────────────────────────────────────────────────────────

/// Every `CLAUDETTE_*` env var the runtime reads. Kept as a flat list so
/// the doctor view stays scannable. Names mirror their source-file
/// definitions; values are printed verbatim (no redaction) — these are
/// configuration knobs, not secrets.
const TRACKED_VARS: &[&str] = &[
    // Top-level switches
    "CLAUDETTE_MODEL",
    "CLAUDETTE_FALLBACK_BRAIN_MODEL",
    "CLAUDETTE_CODER_MODEL",
    "CLAUDETTE_NUM_CTX",
    "CLAUDETTE_NUM_PREDICT",
    "CLAUDETTE_MAX_ITERATIONS",
    "CLAUDETTE_MAX_FIX_ROUNDS",
    "CLAUDETTE_SESSION",
    "CLAUDETTE_WORKSPACE",
    // Compaction & resilience
    "CLAUDETTE_COMPACT_THRESHOLD",
    "CLAUDETTE_SOFT_COMPACT_THRESHOLD",
    "CLAUDETTE_MODEL_RELOAD_RETRY_MS",
    "CLAUDETTE_DISABLE_MODEL_RELOAD_RETRY",
    // Backends
    "OLLAMA_HOST",
    "CLAUDETTE_OPENAI_COMPAT",
    "CLAUDETTE_ALLOW_REMOTE_OLLAMA",
    "CLAUDETTE_SKIP_OLLAMA_PROBE",
    "CLAUDETTE_SKIP_LM_STUDIO_PROBE",
    "CLAUDETTE_MAX_TOOLS",
    // Recall
    "CLAUDETTE_RECALL_MODEL",
    "CLAUDETTE_RECALL_DB",
    "CLAUDETTE_RECALL_DISABLE",
    // Voice
    "CLAUDETTE_FFMPEG_BIN",
    "CLAUDETTE_WHISPER_BIN",
    "CLAUDETTE_WHISPER_MODEL",
    // Integrations
    "TELEGRAM_BOT_TOKEN",
    "CLAUDETTE_TELEGRAM_CHAT",
    "GITHUB_TOKEN",
    "BRAVE_API_KEY",
    "CLAUDETTE_GOOGLE_CLIENT_ID",
    "GOOGLE_CLIENT_ID",
];

fn probe_env() -> Status {
    let mut set_count = 0;
    for var in TRACKED_VARS {
        if let Ok(val) = std::env::var(var) {
            if !val.is_empty() {
                set_count += 1;
                let preview = redact_for_display(var, &val);
                print_row(var, Status::Ok, &preview);
            }
        }
    }
    if set_count == 0 {
        print_row(
            "no CLAUDETTE_* env vars set",
            Status::Warn,
            "running with defaults; consult README.md for tunables",
        );
        return Status::Warn;
    }
    Status::Ok
}

/// Mask the value of vars whose name implies a secret. Configuration knobs
/// stay readable. The match is conservative — anything containing `TOKEN`,
/// `KEY`, `SECRET`, or `CLIENT_ID` is reduced to a length + last-4 preview.
fn redact_for_display(var: &str, val: &str) -> String {
    let upper = var.to_ascii_uppercase();
    let looks_secret = upper.contains("TOKEN")
        || upper.contains("KEY")
        || upper.contains("SECRET")
        || upper.contains("CLIENT_ID");
    if !looks_secret {
        return val.to_string();
    }
    if val.len() <= 6 {
        return "***".to_string();
    }
    let tail = &val[val.len().saturating_sub(4)..];
    format!("*** ({} chars, …{tail})", val.len())
}

// ─── Brain ───────────────────────────────────────────────────────────────

fn probe_brain() -> Status {
    let base = crate::api::resolve_ollama_url();
    let compat = is_openai_compat();
    let configured_model = crate::run::current_model();

    print_row(
        if compat {
            "backend: openai-compat"
        } else {
            "backend: ollama"
        },
        Status::Ok,
        &base,
    );

    // Reachability
    let client = match reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(4))
        .build()
    {
        Ok(c) => c,
        Err(e) => {
            print_row("http client", Status::Err, &format!("build failed: {e}"));
            return Status::Err;
        }
    };

    let mut overall = Status::Ok;

    let tags_url = if compat {
        format!("{base}/v1/models")
    } else {
        format!("{base}/api/tags")
    };
    let resp = client.get(&tags_url).send();
    match resp {
        Ok(r) if r.status().is_success() => {
            print_row(
                if compat {
                    "reachable: /v1/models"
                } else {
                    "reachable: /api/tags"
                },
                Status::Ok,
                &format!("HTTP {}", r.status().as_u16()),
            );
            // Parse the model list and look for the configured brain.
            let body: Value = match r.json() {
                Ok(v) => v,
                Err(e) => {
                    print_row(
                        "parse model list",
                        Status::Warn,
                        &format!("non-JSON response: {e}"),
                    );
                    return Status::Warn;
                }
            };
            let names = extract_model_names(&body, compat);
            if names.is_empty() {
                print_row(
                    "model list",
                    Status::Err,
                    "server returned an empty model list — load one first",
                );
                overall = Status::Err;
            } else if model_present(&names, &configured_model) {
                print_row(
                    &format!("brain '{configured_model}' loaded"),
                    Status::Ok,
                    &format!("{} model(s) available", names.len()),
                );
            } else {
                let hint = if compat {
                    format!(
                        "load it in LM Studio's Local Server tab (looking for: {configured_model})"
                    )
                } else {
                    format!("`ollama pull {configured_model}` to fetch it")
                };
                print_row(
                    &format!("brain '{configured_model}' NOT in model list"),
                    Status::Err,
                    &hint,
                );
                overall = Status::Err;
            }
        }
        Ok(r) => {
            print_row(
                "reachable",
                Status::Err,
                &format!("HTTP {} at {tags_url}", r.status().as_u16()),
            );
            overall = Status::Err;
        }
        Err(e) => {
            print_row(
                "reachable",
                Status::Err,
                &format!("{e} — start the server or set OLLAMA_HOST"),
            );
            overall = Status::Err;
        }
    }
    overall
}

fn is_openai_compat() -> bool {
    matches!(
        std::env::var("CLAUDETTE_OPENAI_COMPAT").ok().as_deref(),
        Some("1" | "true" | "yes" | "on")
    )
}

/// Pull model ids out of an Ollama `/api/tags` or OpenAI-compat `/v1/models`
/// response body.
fn extract_model_names(body: &Value, openai_compat: bool) -> Vec<String> {
    let arr = if openai_compat {
        body.get("data").and_then(Value::as_array)
    } else {
        body.get("models").and_then(Value::as_array)
    };
    let Some(arr) = arr else {
        return Vec::new();
    };
    let mut out = Vec::with_capacity(arr.len());
    for entry in arr {
        // Ollama: `{"name": "qwen3.5:9b", …}`. OpenAI-compat: `{"id": "…", …}`.
        let key = if openai_compat { "id" } else { "name" };
        if let Some(name) = entry.get(key).and_then(Value::as_str) {
            out.push(name.to_string());
        }
    }
    out
}

/// Loose match — accepts `qwen3:8b` ↔ `qwen3:8b-latest` etc. Both sides are
/// lowercased; the configured name matches if it's equal to OR a prefix of
/// the listed name when delimited by `:` (so the listed `qwen3:8b-q4_0`
/// satisfies a configured `qwen3:8b` only if the user spelled it that way).
fn model_present(names: &[String], wanted: &str) -> bool {
    let w = wanted.to_ascii_lowercase();
    names.iter().any(|n| {
        let n = n.to_ascii_lowercase();
        n == w || n == format!("{w}:latest") || w == format!("{n}:latest")
    })
}

// ─── Recall / embeddings ─────────────────────────────────────────────────

fn probe_recall() -> Status {
    if matches!(
        std::env::var("CLAUDETTE_RECALL_DISABLE").as_deref(),
        Ok("1")
    ) {
        print_row(
            "recall disabled by env",
            Status::Warn,
            "CLAUDETTE_RECALL_DISABLE=1 — skipping embed probe",
        );
        return Status::Warn;
    }
    match crate::recall::probe() {
        Ok(()) => {
            print_row(
                "embed probe",
                Status::Ok,
                "1-token /embeddings round-trip OK",
            );
            Status::Ok
        }
        Err(e) => {
            print_row("embed probe", Status::Err, &e);
            Status::Err
        }
    }
}

// ─── Google OAuth ────────────────────────────────────────────────────────

fn probe_google_oauth() -> Status {
    let mut worst = Status::Ok;
    for ctx in [
        crate::google_auth::AuthContext::Calendar,
        crate::google_auth::AuthContext::GmailRead,
    ] {
        let label = ctx.label();
        match crate::google_auth::access_token(ctx) {
            Err(e) => {
                let s = if e.contains("not authenticated") {
                    print_row(
                        &format!("{label}: not configured"),
                        Status::Warn,
                        &format!("run `claudette --auth-google {label}` to enable"),
                    );
                    Status::Warn
                } else {
                    print_row(&format!("{label} token"), Status::Err, &e);
                    Status::Err
                };
                if s == Status::Err {
                    worst = Status::Err;
                } else if worst == Status::Ok {
                    worst = Status::Warn;
                }
            }
            Ok(token) => {
                // Live verify with one tiny read call.
                match verify_scope(ctx, &token) {
                    Ok(detail) => print_row(&format!("{label} access"), Status::Ok, &detail),
                    Err(e) => {
                        print_row(&format!("{label} access"), Status::Err, &e);
                        worst = Status::Err;
                    }
                }
            }
        }
    }
    worst
}

/// Shared live-verify with the `--auth-google` post-grant check —
/// `crate::google_auth::verify_scope_live` is the single source of truth
/// for "does this token actually work against the API". Keeps the doctor
/// and the OAuth flow in lockstep so a passing `--doctor` row implies
/// the same thing as the "OK: ... verified" line the auth flow prints.
fn verify_scope(ctx: crate::google_auth::AuthContext, token: &str) -> Result<String, String> {
    crate::google_auth::verify_scope_live(ctx, token)
}

// ─── Voice deps ──────────────────────────────────────────────────────────

fn probe_voice() -> Status {
    let ffmpeg = std::env::var("CLAUDETTE_FFMPEG_BIN").unwrap_or_else(|_| "ffmpeg".to_string());
    let whisper =
        std::env::var("CLAUDETTE_WHISPER_BIN").unwrap_or_else(|_| "whisper-cli".to_string());

    let ffmpeg_ok = Command::new(&ffmpeg)
        .arg("-version")
        .output()
        .is_ok_and(|o| o.status.success());
    if ffmpeg_ok {
        print_row(&ffmpeg, Status::Ok, "on PATH");
    } else {
        print_row(
            &ffmpeg,
            Status::Warn,
            "not found — voice transcription disabled",
        );
    }

    let whisper_ok = Command::new(&whisper).arg("--help").output().is_ok();
    if whisper_ok {
        print_row(&whisper, Status::Ok, "on PATH");
    } else {
        print_row(
            &whisper,
            Status::Warn,
            "not found — voice transcription disabled",
        );
    }
    if ffmpeg_ok && whisper_ok {
        Status::Ok
    } else {
        Status::Warn
    }
}

// ─── Secrets dir ─────────────────────────────────────────────────────────

fn probe_secrets() -> Status {
    let dir = claudette_home().join("secrets");
    if !dir.exists() {
        print_row(
            "secrets dir",
            Status::Warn,
            &format!("{} does not exist (no tokens stored yet)", dir.display()),
        );
        return Status::Warn;
    }
    let mut count = 0;
    if let Ok(entries) = std::fs::read_dir(&dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_file() {
                continue;
            }
            let Some(name) = path.file_name().and_then(|s| s.to_str()) else {
                continue;
            };
            let Ok(meta) = entry.metadata() else {
                continue;
            };
            count += 1;
            print_row(name, Status::Ok, &format!("{} bytes", meta.len()));
        }
    }
    if count == 0 {
        print_row(
            "secrets dir",
            Status::Warn,
            &format!("{} is empty", dir.display()),
        );
        return Status::Warn;
    }
    Status::Ok
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn extract_model_names_ollama_shape() {
        let body = json!({
            "models": [
                { "name": "qwen3:8b" },
                { "name": "nomic-embed-text:latest" }
            ]
        });
        let names = extract_model_names(&body, false);
        assert_eq!(names, vec!["qwen3:8b", "nomic-embed-text:latest"]);
    }

    #[test]
    fn extract_model_names_openai_compat_shape() {
        let body = json!({
            "data": [
                { "id": "gemma-4-26b-a4b-it" },
                { "id": "text-embedding-nomic-embed-text-v1.5" }
            ]
        });
        let names = extract_model_names(&body, true);
        assert_eq!(
            names,
            vec!["gemma-4-26b-a4b-it", "text-embedding-nomic-embed-text-v1.5"]
        );
    }

    #[test]
    fn extract_model_names_returns_empty_on_unknown_shape() {
        let body = json!({ "unexpected": [] });
        assert!(extract_model_names(&body, false).is_empty());
        assert!(extract_model_names(&body, true).is_empty());
    }

    #[test]
    fn model_present_matches_latest_alias_either_direction() {
        let names = vec!["qwen3:8b".to_string()];
        assert!(model_present(&names, "qwen3:8b"));
        assert!(model_present(&names, "qwen3:8b:latest"));
        let names2 = vec!["qwen3:8b:latest".to_string()];
        assert!(model_present(&names2, "qwen3:8b"));
    }

    #[test]
    fn model_present_is_case_insensitive() {
        let names = vec!["Qwen3:8B".to_string()];
        assert!(model_present(&names, "qwen3:8b"));
    }

    #[test]
    fn model_present_rejects_mismatch() {
        let names = vec!["qwen3:8b".to_string()];
        assert!(!model_present(&names, "llama3:70b"));
    }

    #[test]
    fn redact_masks_anything_with_token_or_key_or_secret() {
        let r = redact_for_display("GITHUB_TOKEN", "ghp_abcdef123456");
        assert!(r.contains("***"), "GITHUB_TOKEN should be masked: {r}");
        assert!(r.contains("3456"));
        let r2 = redact_for_display("BRAVE_API_KEY", "bsk_supersecretvalue");
        assert!(r2.contains("***"));
    }

    #[test]
    fn redact_preserves_config_values() {
        assert_eq!(
            redact_for_display("OLLAMA_HOST", "localhost:11434"),
            "localhost:11434"
        );
        assert_eq!(redact_for_display("CLAUDETTE_NUM_CTX", "32768"), "32768");
        assert_eq!(
            redact_for_display("CLAUDETTE_MODEL", "qwen3:8b"),
            "qwen3:8b"
        );
    }

    #[test]
    fn redact_short_secret_is_fully_starred() {
        assert_eq!(redact_for_display("SOME_TOKEN", "abc"), "***");
        assert_eq!(redact_for_display("SOME_TOKEN", ""), "***");
    }
}