Skip to main content

tj_core/classifier/
mod.rs

1//! Event classifier: takes a chat chunk + recent task context,
2//! returns suggested event_type + task_id + confidence.
3
4use crate::event::{EventType, EvidenceStrength};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize)]
8pub struct ClassifyInput {
9    pub text: String,
10    pub author_hint: String,
11    pub recent_tasks: Vec<TaskContext>,
12}
13
14#[derive(Debug, Clone, Serialize)]
15pub struct TaskContext {
16    pub task_id: String,
17    pub title: String,
18    pub last_events: Vec<String>,
19}
20
21#[derive(Debug, Clone, Deserialize, Serialize)]
22pub struct ClassifyOutput {
23    pub event_type: EventType,
24    pub task_id_guess: Option<String>,
25    pub confidence: f64,
26    pub evidence_strength: Option<EvidenceStrength>,
27    pub suggested_text: String,
28    /// v0.6.0: optional structured artifacts the classifier extracted
29    /// directly. When absent (old protocol or model didn't bother),
30    /// the journal falls back to regex extraction in
31    /// `db::ingest_new_events`. When present, the two sets are merged
32    /// at ingest time so the model can surface artifacts the regex
33    /// would miss (e.g. ticket ids in non-ASCII brackets).
34    #[serde(default)]
35    pub artifacts: Option<crate::artifacts::Artifacts>,
36}
37
38pub trait Classifier: Send + Sync {
39    fn classify(&self, input: &ClassifyInput) -> anyhow::Result<ClassifyOutput>;
40}
41
42use crate::event::EventStatus;
43
44pub const CONFIDENCE_THRESHOLD: f64 = 0.85;
45
46pub fn decide_status(confidence: f64) -> EventStatus {
47    if confidence >= CONFIDENCE_THRESHOLD {
48        EventStatus::Confirmed
49    } else {
50        EventStatus::Suggested
51    }
52}
53
54pub mod cli;
55pub mod http;
56pub mod mock;
57pub mod prompt;
58pub mod telemetry;
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    /// Both classifiers must honour `TJ_CLASSIFIER_MODEL`. Combined into a
65    /// single test to avoid env-var races with other tests in this crate;
66    /// inside the test we serialize the read-set-restore steps.
67    #[test]
68    fn tj_classifier_model_env_var_overrides_defaults_for_both_backends() {
69        let prev_model = std::env::var("TJ_CLASSIFIER_MODEL").ok();
70        let prev_key = std::env::var("ANTHROPIC_API_KEY").ok();
71
72        // Unset → defaults.
73        // SAFETY: tests in this crate do not concurrently read these env vars.
74        unsafe {
75            std::env::remove_var("TJ_CLASSIFIER_MODEL");
76        }
77
78        let cli_default = cli::ClaudeCliClassifier::default();
79        assert_eq!(cli_default.model, cli::DEFAULT_MODEL);
80
81        unsafe {
82            std::env::set_var("ANTHROPIC_API_KEY", "test-key-do-not-use");
83        }
84        let http_default = http::AnthropicClassifier::from_env().unwrap();
85        assert_eq!(http_default.model, http::DEFAULT_MODEL);
86
87        // Set → override applied to both.
88        unsafe {
89            std::env::set_var("TJ_CLASSIFIER_MODEL", "sonnet-override");
90        }
91        let cli_override = cli::ClaudeCliClassifier::default();
92        assert_eq!(cli_override.model, "sonnet-override");
93
94        let http_override = http::AnthropicClassifier::from_env().unwrap();
95        assert_eq!(http_override.model, "sonnet-override");
96
97        // Restore.
98        unsafe {
99            match prev_model {
100                Some(v) => std::env::set_var("TJ_CLASSIFIER_MODEL", v),
101                None => std::env::remove_var("TJ_CLASSIFIER_MODEL"),
102            }
103            match prev_key {
104                Some(v) => std::env::set_var("ANTHROPIC_API_KEY", v),
105                None => std::env::remove_var("ANTHROPIC_API_KEY"),
106            }
107        }
108    }
109
110    #[test]
111    fn classify_input_serializes() {
112        let i = ClassifyInput {
113            text: "Adopted Rust for the journal".into(),
114            author_hint: "assistant".into(),
115            recent_tasks: vec![],
116        };
117        let s = serde_json::to_string(&i).unwrap();
118        assert!(s.contains("Adopted Rust"));
119    }
120
121    #[test]
122    fn decide_status_high_confidence_is_confirmed() {
123        assert_eq!(decide_status(0.95), EventStatus::Confirmed);
124        assert_eq!(decide_status(0.85), EventStatus::Confirmed);
125    }
126
127    #[test]
128    fn decide_status_low_confidence_is_suggested() {
129        assert_eq!(decide_status(0.84), EventStatus::Suggested);
130        assert_eq!(decide_status(0.0), EventStatus::Suggested);
131    }
132}