Skip to main content

codex_cli_sdk/types/
input.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4/// Input to a Codex thread turn.
5#[derive(Debug, Clone, Serialize, Deserialize)]
6#[serde(tag = "type", rename_all = "snake_case")]
7pub enum UserInput {
8    /// Plain text prompt.
9    Text { text: String },
10    /// Local image file path.
11    LocalImage { path: PathBuf },
12}
13
14impl UserInput {
15    /// Create a text input.
16    pub fn text(s: impl Into<String>) -> Self {
17        Self::Text { text: s.into() }
18    }
19
20    /// Create a local image input.
21    pub fn image(path: impl Into<PathBuf>) -> Self {
22        Self::LocalImage { path: path.into() }
23    }
24}
25
26impl From<String> for UserInput {
27    fn from(s: String) -> Self {
28        Self::text(s)
29    }
30}
31
32impl From<&str> for UserInput {
33    fn from(s: &str) -> Self {
34        Self::text(s)
35    }
36}
37
38/// The input accepted by `Thread::run()` and `Thread::run_streamed()`.
39pub enum Input {
40    Text(String),
41    Items(Vec<UserInput>),
42}
43
44impl From<String> for Input {
45    fn from(s: String) -> Self {
46        Self::Text(s)
47    }
48}
49
50impl From<&str> for Input {
51    fn from(s: &str) -> Self {
52        Self::Text(s.to_owned())
53    }
54}
55
56impl From<Vec<UserInput>> for Input {
57    fn from(items: Vec<UserInput>) -> Self {
58        Self::Items(items)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn user_input_from_str() {
68        let input: UserInput = "hello".into();
69        let UserInput::Text { text } = input else {
70            panic!("wrong variant");
71        };
72        assert_eq!(text, "hello");
73    }
74
75    #[test]
76    fn input_from_string() {
77        let input: Input = "test".into();
78        let Input::Text(s) = input else {
79            panic!("wrong variant");
80        };
81        assert_eq!(s, "test");
82    }
83
84    #[test]
85    fn user_input_image() {
86        let input = UserInput::image("/tmp/test.png");
87        let UserInput::LocalImage { path } = input else {
88            panic!("wrong variant");
89        };
90        assert_eq!(path, PathBuf::from("/tmp/test.png"));
91    }
92}