codex_runtime/ergonomic/
workflow.rs1use crate::runtime::{
2 Client, ClientError, PromptRunError, PromptRunResult, RunProfile, RuntimeError, Session,
3 SessionConfig,
4};
5
6use crate::ergonomic::WorkflowConfig;
7
8#[derive(Clone)]
12pub struct Workflow {
13 client: Client,
14 config: WorkflowConfig,
15}
16
17impl Workflow {
18 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 pub async fn connect_default(cwd: impl Into<String>) -> Result<Self, ClientError> {
26 Self::connect(WorkflowConfig::new(cwd)).await
27 }
28
29 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 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 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 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 pub async fn shutdown(self) -> Result<(), RuntimeError> {
75 self.client.shutdown().await
76 }
77}