1use std::path::PathBuf;
2
3use super::super::session::AgentSessionRunRequest;
4use super::super::types::{AgentDefinition, AgentRun, AgentSDKOptions};
5use super::AgentSDKClient;
6
7impl AgentSDKClient {
8 pub fn run(&self, prompt: impl Into<String>) -> Result<AgentRun, String> {
9 let (name, definition) = self.default_or_only_agent(
10 "No agent configured. Call run_with_agent(...) or register named agents first.",
11 "Multiple agents configured. Call run_agent(name, ...) with one of:",
12 )?;
13 self.run_named_agent(&name, definition, prompt)
14 }
15
16 pub fn run_in_workspace(
17 &self,
18 prompt: impl Into<String>,
19 workspace: impl Into<PathBuf>,
20 ) -> Result<AgentRun, String> {
21 let workspace = workspace.into();
22 let (name, definition) = self.default_or_only_agent(
23 "No agent configured. Call run_with_agent_in_workspace(...) or register named agents first.",
24 "Multiple agents configured. Call run_agent_in_workspace(name, ...) with one of:",
25 )?;
26 self.run_named_agent_with_workspace(&name, definition, prompt, Some(workspace))
27 }
28
29 pub fn run_with_agent(
30 &self,
31 definition: AgentDefinition,
32 prompt: impl Into<String>,
33 ) -> Result<AgentRun, String> {
34 self.run_named_agent("inline", definition, prompt)
35 }
36
37 pub fn run_with_agent_in_workspace(
38 &self,
39 definition: AgentDefinition,
40 prompt: impl Into<String>,
41 workspace: impl Into<PathBuf>,
42 ) -> Result<AgentRun, String> {
43 self.run_named_agent_with_workspace("inline", definition, prompt, Some(workspace.into()))
44 }
45
46 pub fn run_with_agent_request(
47 &self,
48 definition: AgentDefinition,
49 request: AgentSessionRunRequest,
50 ) -> Result<AgentRun, String> {
51 self.run_named_agent_with_request("inline", definition, request)
52 }
53
54 pub fn run_with_request(&self, request: AgentSessionRunRequest) -> Result<AgentRun, String> {
55 let (name, definition) = self.default_or_only_agent(
56 "No agent configured. Call run_with_agent_request(...) or register named agents first.",
57 "Multiple agents configured. Call run_agent_with_request(name, ...) with one of:",
58 )?;
59 self.run_named_agent_with_request(&name, definition, request)
60 }
61
62 pub fn run_agent(
63 &self,
64 agent_name: impl AsRef<str>,
65 prompt: impl Into<String>,
66 ) -> Result<AgentRun, String> {
67 let agent_name = agent_name.as_ref().trim();
68 let definition = self.get_agent(agent_name)?.clone();
69 self.run_named_agent(agent_name, definition, prompt)
70 }
71
72 pub fn run_agent_in_workspace(
73 &self,
74 agent_name: impl AsRef<str>,
75 prompt: impl Into<String>,
76 workspace: impl Into<PathBuf>,
77 ) -> Result<AgentRun, String> {
78 let agent_name = agent_name.as_ref().trim();
79 let definition = self.get_agent(agent_name)?.clone();
80 self.run_named_agent_with_workspace(agent_name, definition, prompt, Some(workspace.into()))
81 }
82
83 pub fn run_agent_with_request(
84 &self,
85 agent_name: impl AsRef<str>,
86 request: AgentSessionRunRequest,
87 ) -> Result<AgentRun, String> {
88 let agent_name = agent_name.as_ref().trim();
89 let definition = self.get_agent(agent_name)?.clone();
90 self.run_named_agent_with_request(agent_name, definition, request)
91 }
92
93 fn run_named_agent(
94 &self,
95 agent_name: &str,
96 definition: AgentDefinition,
97 prompt: impl Into<String>,
98 ) -> Result<AgentRun, String> {
99 self.run_named_agent_with_workspace(agent_name, definition, prompt, None)
100 }
101
102 fn run_named_agent_with_workspace(
103 &self,
104 agent_name: &str,
105 definition: AgentDefinition,
106 prompt: impl Into<String>,
107 workspace: Option<PathBuf>,
108 ) -> Result<AgentRun, String> {
109 let mut request = AgentSessionRunRequest::new(prompt);
110 request.workspace = Some(workspace.unwrap_or_else(|| self.options.workspace.clone()));
111 self.run_named_agent_with_request(agent_name, definition, request)
112 }
113
114 fn run_named_agent_with_request(
115 &self,
116 agent_name: &str,
117 definition: AgentDefinition,
118 mut request: AgentSessionRunRequest,
119 ) -> Result<AgentRun, String> {
120 let definition = self.effective_definition(definition);
121 if request.workspace.is_none() {
122 request.workspace = Some(self.options.workspace.clone());
123 }
124 if request.task_name.is_none() {
125 request.task_name = Some(agent_name.to_string());
126 }
127 if request.stream_callback.is_none() {
128 request.stream_callback = self.options.stream_callback.clone();
129 }
130 let mut run = self.runtime.run_with_session(&definition, request)?;
131 run.agent_name = agent_name.to_string();
132 Ok(run)
133 }
134}
135
136pub fn run(client: &AgentSDKClient, prompt: impl Into<String>) -> Result<AgentRun, String> {
137 client.run(prompt)
138}
139
140pub fn run_with_options_and_agent(
141 options: AgentSDKOptions,
142 definition: AgentDefinition,
143 prompt: impl Into<String>,
144) -> Result<AgentRun, String> {
145 AgentSDKClient::new(options).run_with_agent(definition, prompt)
146}
147
148pub fn run_with_options_and_agent_in_workspace(
149 options: AgentSDKOptions,
150 definition: AgentDefinition,
151 prompt: impl Into<String>,
152 workspace: impl Into<PathBuf>,
153) -> Result<AgentRun, String> {
154 AgentSDKClient::new(options).run_with_agent_in_workspace(definition, prompt, workspace)
155}
156
157pub fn run_with_options_and_agent_request(
158 options: AgentSDKOptions,
159 definition: AgentDefinition,
160 request: AgentSessionRunRequest,
161) -> Result<AgentRun, String> {
162 AgentSDKClient::new(options).run_with_agent_request(definition, request)
163}