agent-status 2.0.0

Tmux-integrated indicator showing which AI coding agent sessions are waiting on user input.
Documentation
use crate::agents::Agent;

/// pi ([pi.dev](https://pi.dev)).
///
/// Reads `session_id` from the JSON piped in by the bundled pi extension at
/// `extensions/pi.ts`, which subscribes to pi's session, tool, and agent
/// lifecycle events. See the bridge file for the full subscription list.
pub struct PiAgent;

impl Agent for PiAgent {
    fn name(&self) -> &'static str {
        "pi"
    }

    fn extract_session_id(&self, stdin_json: &str) -> Option<String> {
        let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
        let id = v.get("session_id")?.as_str()?;
        if id.is_empty() {
            None
        } else {
            Some(id.to_string())
        }
    }

    fn extract_message(&self, stdin_json: &str) -> Option<String> {
        let v: serde_json::Value = serde_json::from_str(stdin_json).ok()?;
        let m = v.get("message")?.as_str()?;
        if m.is_empty() {
            None
        } else {
            Some(m.to_string())
        }
    }
}

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

    #[test]
    fn name_is_pi() {
        assert_eq!(PiAgent.name(), "pi");
    }

    #[test]
    fn extract_session_id_returns_id() {
        let json = r#"{"session_id":"abc-123","other":"stuff"}"#;
        assert_eq!(
            PiAgent.extract_session_id(json).as_deref(),
            Some("abc-123")
        );
    }

    #[test]
    fn extract_session_id_returns_none_for_missing_field() {
        assert_eq!(
            PiAgent.extract_session_id(r#"{"other":1}"#),
            None
        );
    }

    #[test]
    fn extract_session_id_returns_none_for_empty_string() {
        assert_eq!(
            PiAgent.extract_session_id(r#"{"session_id":""}"#),
            None
        );
    }

    #[test]
    fn extract_session_id_returns_none_for_invalid_json() {
        assert_eq!(PiAgent.extract_session_id("not json"), None);
    }

    #[test]
    fn extract_message_returns_string_when_present() {
        let json = r#"{"session_id":"x","message":"Done with refactor"}"#;
        assert_eq!(
            PiAgent.extract_message(json).as_deref(),
            Some("Done with refactor")
        );
    }

    #[test]
    fn extract_message_returns_none_when_field_missing() {
        assert!(PiAgent.extract_message(r#"{"session_id":"x"}"#).is_none());
    }

    #[test]
    fn extract_message_returns_none_when_empty() {
        assert!(PiAgent.extract_message(r#"{"session_id":"x","message":""}"#).is_none());
    }

    #[test]
    fn extract_message_returns_none_for_non_string_value() {
        assert!(PiAgent.extract_message(r#"{"session_id":"x","message":null}"#).is_none());
    }

    #[test]
    fn extract_message_returns_none_for_invalid_json() {
        assert!(PiAgent.extract_message("not json").is_none());
    }
}