codex-cli-sdk 0.0.1

Rust SDK for the OpenAI Codex CLI
Documentation
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Input to a Codex thread turn.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum UserInput {
    /// Plain text prompt.
    Text { text: String },
    /// Local image file path.
    LocalImage { path: PathBuf },
}

impl UserInput {
    /// Create a text input.
    pub fn text(s: impl Into<String>) -> Self {
        Self::Text { text: s.into() }
    }

    /// Create a local image input.
    pub fn image(path: impl Into<PathBuf>) -> Self {
        Self::LocalImage { path: path.into() }
    }
}

impl From<String> for UserInput {
    fn from(s: String) -> Self {
        Self::text(s)
    }
}

impl From<&str> for UserInput {
    fn from(s: &str) -> Self {
        Self::text(s)
    }
}

/// The input accepted by `Thread::run()` and `Thread::run_streamed()`.
pub enum Input {
    Text(String),
    Items(Vec<UserInput>),
}

impl From<String> for Input {
    fn from(s: String) -> Self {
        Self::Text(s)
    }
}

impl From<&str> for Input {
    fn from(s: &str) -> Self {
        Self::Text(s.to_owned())
    }
}

impl From<Vec<UserInput>> for Input {
    fn from(items: Vec<UserInput>) -> Self {
        Self::Items(items)
    }
}

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

    #[test]
    fn user_input_from_str() {
        let input: UserInput = "hello".into();
        let UserInput::Text { text } = input else {
            panic!("wrong variant");
        };
        assert_eq!(text, "hello");
    }

    #[test]
    fn input_from_string() {
        let input: Input = "test".into();
        let Input::Text(s) = input else {
            panic!("wrong variant");
        };
        assert_eq!(s, "test");
    }

    #[test]
    fn user_input_image() {
        let input = UserInput::image("/tmp/test.png");
        let UserInput::LocalImage { path } = input else {
            panic!("wrong variant");
        };
        assert_eq!(path, PathBuf::from("/tmp/test.png"));
    }
}