codex-cli-sdk 0.0.1

Rust SDK for the OpenAI Codex CLI
Documentation
use super::builders::*;
use serde_json::Value;

/// A simple session: prompt → agent responds with text.
pub fn simple_text_response(thread_id: &str, response: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        agent_message_started("msg-1"),
        agent_message_completed("msg-1", response),
        turn_completed(100, 0, 50),
    ]
}

/// A session with tool calls: prompt → shell command → response.
pub fn tool_call_session(thread_id: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        command_started("cmd-1", "ls -la"),
        command_completed("cmd-1", "ls -la", "total 42\n...", 0),
        agent_message_started("msg-1"),
        agent_message_completed("msg-1", "Here are the files in the directory..."),
        turn_completed(200, 0, 80),
    ]
}

/// A session with an approval request.
pub fn approval_session(thread_id: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        approval_request("approval-1", "rm -rf /tmp/test"),
    ]
}

/// A session that fails.
pub fn error_session(thread_id: &str, error_msg: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        turn_failed(error_msg),
    ]
}

/// A session with streaming deltas (item.updated events).
pub fn streaming_session(thread_id: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        agent_message_started("msg-1"),
        agent_message_updated("msg-1", "Hello"),
        agent_message_updated("msg-1", "Hello, "),
        agent_message_updated("msg-1", "Hello, world!"),
        agent_message_completed("msg-1", "Hello, world!"),
        turn_completed(50, 0, 10),
    ]
}

/// A session with reasoning + agent message.
pub fn reasoning_session(thread_id: &str) -> Vec<Value> {
    vec![
        thread_started(thread_id),
        turn_started(),
        reasoning_started("r-1"),
        reasoning_completed("r-1", "Let me think about this..."),
        agent_message_started("msg-1"),
        agent_message_completed("msg-1", "Here is my answer."),
        turn_completed(150, 0, 60),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::events::ThreadEvent;

    #[test]
    fn all_fixtures_produce_valid_events() {
        let fixtures: Vec<Vec<Value>> = vec![
            simple_text_response("t1", "Hello!"),
            tool_call_session("t2"),
            approval_session("t3"),
            error_session("t4", "oops"),
            streaming_session("t5"),
            reasoning_session("t6"),
        ];

        for fixture in fixtures {
            for event_json in fixture {
                let result = serde_json::from_value::<ThreadEvent>(event_json.clone());
                assert!(
                    result.is_ok(),
                    "Failed to parse fixture event: {}",
                    serde_json::to_string(&event_json).unwrap()
                );
            }
        }
    }
}