Skip to main content

codex_app_server_client/
daemon.rs

1use std::path::{Path, PathBuf};
2
3use crate::{CodexSession, Result, SessionOptions};
4
5#[derive(Clone, Debug)]
6pub struct CodexDaemon {
7    socket_path: PathBuf,
8    extra_args: Vec<String>,
9}
10
11impl CodexDaemon {
12    pub fn new(socket_path: impl Into<PathBuf>) -> Self {
13        Self {
14            socket_path: socket_path.into(),
15            extra_args: Vec::new(),
16        }
17    }
18
19    pub fn with_extra_arg(mut self, arg: impl Into<String>) -> Self {
20        self.extra_args.push(arg.into());
21        self
22    }
23
24    pub fn with_config(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
25        self.extra_args.push("-c".to_owned());
26        self.extra_args
27            .push(format!("{}={}", key.into(), value.into()));
28        self
29    }
30
31    pub fn socket_path(&self) -> &Path {
32        &self.socket_path
33    }
34
35    pub fn listen_url(&self) -> String {
36        format!("unix://{}", self.socket_path.display())
37    }
38
39    pub fn app_server_args(&self) -> Vec<String> {
40        let mut args = vec![
41            "app-server".to_owned(),
42            "--listen".to_owned(),
43            self.listen_url(),
44        ];
45        args.extend(self.extra_args.clone());
46        args
47    }
48
49    pub fn start_args(&self) -> Vec<String> {
50        let mut args = vec!["app-server".to_owned(), "daemon".to_owned()];
51        args.extend(self.extra_args.clone());
52        args.push("start".to_owned());
53        args
54    }
55
56    pub fn stop_args(&self) -> Vec<String> {
57        vec![
58            "app-server".to_owned(),
59            "daemon".to_owned(),
60            "stop".to_owned(),
61        ]
62    }
63
64    #[cfg(unix)]
65    pub async fn connect(&self, options: SessionOptions) -> Result<CodexSession> {
66        CodexSession::connect_unix(&self.socket_path, options).await
67    }
68}