Skip to main content

agent_status/agents/
oh_my_pi.rs

1use crate::agents::Agent;
2
3/// omp / oh-my-pi ([omp.sh](https://omp.sh)), a coding-first fork of pi.
4///
5/// Reads `session_id` from the JSON piped in by the bundled omp extension at
6/// `extensions/oh-my-pi.ts`, which fires on omp's `session_start`,
7/// `session_switch`, `session_branch`, `before_agent_start`,
8/// `tool_execution_start`, `tool_execution_end`, `agent_end`, and
9/// `session_shutdown` events.
10pub struct OhMyPiAgent;
11
12impl Agent for OhMyPiAgent {
13    fn name(&self) -> &'static str {
14        "oh-my-pi"
15    }
16
17    fn extract_session_id(&self, stdin_json: &str) -> Option<String> {
18        let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
19        let id = v.get("session_id")?.as_str()?;
20        if id.is_empty() {
21            None
22        } else {
23            Some(id.to_string())
24        }
25    }
26
27    fn extract_message(&self, stdin_json: &str) -> Option<String> {
28        let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
29        let m = v.get("message")?.as_str()?;
30        if m.is_empty() {
31            None
32        } else {
33            Some(m.to_string())
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn name_is_oh_my_pi() {
44        assert_eq!(OhMyPiAgent.name(), "oh-my-pi");
45    }
46
47    #[test]
48    fn extract_session_id_returns_id() {
49        let json = r#"{"session_id":"0196fdb1-1111-7000-8000-aaaaaaaaaaaa","other":"stuff"}"#;
50        assert_eq!(
51            OhMyPiAgent.extract_session_id(json).as_deref(),
52            Some("0196fdb1-1111-7000-8000-aaaaaaaaaaaa")
53        );
54    }
55
56    #[test]
57    fn extract_session_id_returns_none_for_missing_field() {
58        assert_eq!(OhMyPiAgent.extract_session_id(r#"{"other":1}"#), None);
59    }
60
61    #[test]
62    fn extract_session_id_returns_none_for_empty_string() {
63        assert_eq!(OhMyPiAgent.extract_session_id(r#"{"session_id":""}"#), None);
64    }
65
66    #[test]
67    fn extract_session_id_returns_none_for_invalid_json() {
68        assert_eq!(OhMyPiAgent.extract_session_id("not json"), None);
69    }
70
71    #[test]
72    fn extract_message_returns_string_when_present() {
73        let json = r#"{"session_id":"x","message":"Running: cargo test"}"#;
74        assert_eq!(
75            OhMyPiAgent.extract_message(json).as_deref(),
76            Some("Running: cargo test")
77        );
78    }
79
80    #[test]
81    fn extract_message_returns_none_when_field_missing() {
82        assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x"}"#).is_none());
83    }
84
85    #[test]
86    fn extract_message_returns_none_when_empty() {
87        assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x","message":""}"#).is_none());
88    }
89
90    #[test]
91    fn extract_message_returns_none_for_non_string_value() {
92        assert!(OhMyPiAgent.extract_message(r#"{"session_id":"x","message":null}"#).is_none());
93    }
94
95    #[test]
96    fn extract_message_returns_none_for_invalid_json() {
97        assert!(OhMyPiAgent.extract_message("not json").is_none());
98    }
99}