Skip to main content

codex_runtime/ergonomic/
workflow.rs

1use crate::runtime::{
2    Client, ClientError, PromptRunError, PromptRunResult, RunProfile, RuntimeError, Session,
3    SessionConfig,
4};
5
6use crate::ergonomic::WorkflowConfig;
7
8/// One reusable workflow handle:
9/// - simple path: `run(prompt)`
10/// - expert path: profile/config mutation via `WorkflowConfig`
11#[derive(Clone)]
12pub struct Workflow {
13    client: Client,
14    config: WorkflowConfig,
15}
16
17impl Workflow {
18    /// Connect once with one explicit workflow config.
19    pub async fn connect(config: WorkflowConfig) -> Result<Self, ClientError> {
20        let client = Client::connect(config.client_config.clone()).await?;
21        Ok(Self { client, config })
22    }
23
24    /// Connect with defaults for one cwd.
25    pub async fn connect_default(cwd: impl Into<String>) -> Result<Self, ClientError> {
26        Self::connect(WorkflowConfig::new(cwd)).await
27    }
28
29    /// Run one prompt using workflow defaults.
30    pub async fn run(&self, prompt: impl Into<String>) -> Result<PromptRunResult, PromptRunError> {
31        self.run_with_profile(prompt, self.config.run_profile.clone())
32            .await
33    }
34
35    /// Run one prompt with explicit profile override.
36    pub async fn run_with_profile(
37        &self,
38        prompt: impl Into<String>,
39        profile: RunProfile,
40    ) -> Result<PromptRunResult, PromptRunError> {
41        self.client
42            .run_with_profile(self.config.cwd.clone(), prompt.into(), profile)
43            .await
44    }
45
46    /// Start one session using workflow defaults.
47    pub async fn setup_session(&self) -> Result<Session, PromptRunError> {
48        self.setup_session_with_profile(self.config.run_profile.clone())
49            .await
50    }
51
52    /// Start one session with explicit profile override.
53    pub async fn setup_session_with_profile(
54        &self,
55        profile: RunProfile,
56    ) -> Result<Session, PromptRunError> {
57        self.client
58            .start_session(SessionConfig::from_profile(
59                self.config.cwd.clone(),
60                profile,
61            ))
62            .await
63    }
64
65    pub fn config(&self) -> &WorkflowConfig {
66        &self.config
67    }
68
69    pub fn client(&self) -> &Client {
70        &self.client
71    }
72
73    /// Explicit shutdown to keep lifecycle obvious.
74    pub async fn shutdown(self) -> Result<(), RuntimeError> {
75        self.client.shutdown().await
76    }
77}