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 heuristic;
55pub mod http;
56pub mod hybrid;
57pub mod mock;
58pub mod prompt;
59pub mod telemetry;
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    /// The HTTP backend must honour `TJ_CLASSIFIER_MODEL`. Wraps the
66    /// read-set-restore steps in one test to avoid env-var races with
67    /// other tests in this crate.
68    #[test]
69    fn tj_classifier_model_env_var_overrides_http_default() {
70        let prev_model = std::env::var("TJ_CLASSIFIER_MODEL").ok();
71        let prev_key = std::env::var("ANTHROPIC_API_KEY").ok();
72
73        // SAFETY: tests in this crate do not concurrently read these env vars.
74        unsafe {
75            std::env::remove_var("TJ_CLASSIFIER_MODEL");
76            std::env::set_var("ANTHROPIC_API_KEY", "test-key-do-not-use");
77        }
78        let http_default = http::AnthropicClassifier::from_env().unwrap();
79        assert_eq!(http_default.model, http::DEFAULT_MODEL);
80
81        unsafe {
82            std::env::set_var("TJ_CLASSIFIER_MODEL", "sonnet-override");
83        }
84        let http_override = http::AnthropicClassifier::from_env().unwrap();
85        assert_eq!(http_override.model, "sonnet-override");
86
87        // Restore.
88        unsafe {
89            match prev_model {
90                Some(v) => std::env::set_var("TJ_CLASSIFIER_MODEL", v),
91                None => std::env::remove_var("TJ_CLASSIFIER_MODEL"),
92            }
93            match prev_key {
94                Some(v) => std::env::set_var("ANTHROPIC_API_KEY", v),
95                None => std::env::remove_var("ANTHROPIC_API_KEY"),
96            }
97        }
98    }
99
100    #[test]
101    fn classify_input_serializes() {
102        let i = ClassifyInput {
103            text: "Adopted Rust for the journal".into(),
104            author_hint: "assistant".into(),
105            recent_tasks: vec![],
106        };
107        let s = serde_json::to_string(&i).unwrap();
108        assert!(s.contains("Adopted Rust"));
109    }
110
111    #[test]
112    fn decide_status_high_confidence_is_confirmed() {
113        assert_eq!(decide_status(0.95), EventStatus::Confirmed);
114        assert_eq!(decide_status(0.85), EventStatus::Confirmed);
115    }
116
117    #[test]
118    fn decide_status_low_confidence_is_suggested() {
119        assert_eq!(decide_status(0.84), EventStatus::Suggested);
120        assert_eq!(decide_status(0.0), EventStatus::Suggested);
121    }
122}