codex/codex.rs
1use crate::codex_options::CodexOptions;
2use crate::errors::Result;
3use crate::exec::CodexExec;
4use crate::thread::Thread;
5use crate::thread_options::ThreadOptions;
6
7/// Entry point for interacting with the Codex agent.
8#[derive(Debug, Clone)]
9pub struct Codex {
10 exec: CodexExec,
11 options: CodexOptions,
12}
13
14impl Codex {
15 /// Creates a new Codex client.
16 ///
17 /// When `options` is `None`, default options are used and the SDK attempts
18 /// to discover the `codex` executable automatically.
19 ///
20 /// # Example
21 ///
22 /// ```rust,no_run
23 /// use codex::Codex;
24 ///
25 /// let _codex = Codex::new(None)?;
26 /// # Ok::<(), codex::Error>(())
27 /// ```
28 pub fn new(options: Option<CodexOptions>) -> Result<Self> {
29 let options = options.unwrap_or_default();
30 let exec = CodexExec::new(
31 options.codex_path_override.clone(),
32 options.env.clone(),
33 options.config.clone(),
34 )?;
35 Ok(Self { exec, options })
36 }
37
38 /// Starts a new thread.
39 ///
40 /// # Example
41 ///
42 /// ```rust,no_run
43 /// use codex::Codex;
44 ///
45 /// let codex = Codex::new(None)?;
46 /// let _thread = codex.start_thread(None);
47 /// # Ok::<(), codex::Error>(())
48 /// ```
49 pub fn start_thread(&self, options: Option<ThreadOptions>) -> Thread {
50 Thread::new(
51 self.exec.clone(),
52 self.options.clone(),
53 options.unwrap_or_default(),
54 None,
55 )
56 }
57
58 /// Resumes an existing thread by id.
59 ///
60 /// # Example
61 ///
62 /// ```rust,no_run
63 /// use codex::Codex;
64 ///
65 /// let codex = Codex::new(None)?;
66 /// let _thread = codex.resume_thread("thread_123", None);
67 /// # Ok::<(), codex::Error>(())
68 /// ```
69 pub fn resume_thread(&self, id: impl Into<String>, options: Option<ThreadOptions>) -> Thread {
70 Thread::new(
71 self.exec.clone(),
72 self.options.clone(),
73 options.unwrap_or_default(),
74 Some(id.into()),
75 )
76 }
77}