1use crate::{
33 Context, Message, MessageId, ResumeEntry, RunAgentInput, RunId, ThreadId, Tool, UserContent,
34};
35use futures_util::TryStreamExt;
36use serde_json::Value;
37
38use crate::client::transport::{EventStream, Transport, boxed_stream};
39
40#[cfg(feature = "http")]
41use crate::client::error::Result;
42#[cfg(feature = "http")]
43use crate::client::transport::{HttpTransport, HttpTransportBuilder};
44
45#[derive(Clone, Debug, Default, PartialEq)]
53pub struct RunParams {
54 input: RunAgentInput,
55}
56
57impl RunParams {
58 pub fn new(thread_id: impl Into<ThreadId>, run_id: impl Into<RunId>) -> Self {
60 Self {
61 input: RunAgentInput::new(thread_id, run_id),
62 }
63 }
64
65 #[must_use]
67 pub fn messages(mut self, messages: impl Into<Vec<Message>>) -> Self {
68 self.input.messages = messages.into();
69 self
70 }
71
72 #[must_use]
74 pub fn message(mut self, message: impl Into<Message>) -> Self {
75 self.input.messages.push(message.into());
76 self
77 }
78
79 #[must_use]
81 pub fn user(self, id: impl Into<MessageId>, content: impl Into<UserContent>) -> Self {
82 self.message(Message::user(id, content))
83 }
84
85 #[must_use]
87 pub fn state(mut self, state: impl Into<Value>) -> Self {
88 self.input.state = state.into();
89 self
90 }
91
92 #[must_use]
94 pub fn tools(mut self, tools: impl Into<Vec<Tool>>) -> Self {
95 self.input.tools = tools.into();
96 self
97 }
98
99 #[must_use]
101 pub fn context(mut self, context: impl Into<Vec<Context>>) -> Self {
102 self.input.context = context.into();
103 self
104 }
105
106 #[must_use]
108 pub fn forwarded_props(mut self, props: impl Into<Value>) -> Self {
109 self.input.forwarded_props = props.into();
110 self
111 }
112
113 #[must_use]
116 pub fn resume(mut self, entries: impl Into<Vec<ResumeEntry>>) -> Self {
117 self.input.resume = Some(entries.into());
118 self
119 }
120
121 #[must_use]
123 pub fn parent_run_id(mut self, run_id: impl Into<RunId>) -> Self {
124 self.input.parent_run_id = Some(run_id.into());
125 self
126 }
127
128 #[must_use]
131 pub fn protocol_version(mut self, version: Option<String>) -> Self {
132 self.input.protocol_version = version;
133 self
134 }
135
136 pub fn into_input(self) -> RunAgentInput {
138 self.input
139 }
140}
141
142impl From<RunParams> for RunAgentInput {
143 fn from(params: RunParams) -> Self {
144 params.input
145 }
146}
147
148impl From<RunAgentInput> for RunParams {
149 fn from(input: RunAgentInput) -> Self {
150 Self { input }
151 }
152}
153
154#[derive(Clone, Debug, Default)]
168pub struct RemoteAgent<T> {
169 transport: T,
170}
171
172impl<T> RemoteAgent<T> {
173 pub fn new(transport: T) -> Self {
175 Self { transport }
176 }
177
178 pub fn transport(&self) -> &T {
180 &self.transport
181 }
182
183 pub fn into_transport(self) -> T {
185 self.transport
186 }
187}
188
189impl<T: Transport> RemoteAgent<T> {
190 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
199 let connecting = self.transport.run_raw(params.into());
200 let raw = futures_util::stream::once(connecting).try_flatten();
201 boxed_stream(crate::client::enforce::stream(raw))
202 }
203}
204
205#[cfg(feature = "http")]
209#[derive(Clone, Debug)]
210pub struct HttpAgent {
211 agent: RemoteAgent<std::sync::Arc<HttpTransport>>,
212}
213
214#[cfg(feature = "http")]
215impl HttpAgent {
216 pub fn new(url: impl AsRef<str>) -> Result<Self> {
218 Ok(Self::from_transport(HttpTransport::new(url)?))
219 }
220 pub fn from_transport(transport: HttpTransport) -> Self {
222 Self {
223 agent: RemoteAgent::new(std::sync::Arc::new(transport)),
224 }
225 }
226 pub fn builder(url: impl AsRef<str>) -> HttpAgentBuilder {
228 HttpAgentBuilder {
229 transport: HttpTransport::builder(url),
230 }
231 }
232 pub fn thread(&self, id: impl Into<ThreadId>) -> crate::client::Thread<HttpTransport> {
234 crate::client::Thread::from_shared(self.agent.transport().clone(), id)
235 }
236 pub fn thread_with_state<S: serde::Serialize + serde::de::DeserializeOwned>(
238 &self,
239 id: impl Into<ThreadId>,
240 state: S,
241 ) -> Result<crate::client::Thread<HttpTransport, S>> {
242 self.thread_builder(id)
243 .state(serde_json::to_value(state)?)
244 .build()
245 }
246 pub fn thread_builder<S>(
248 &self,
249 id: impl Into<ThreadId>,
250 ) -> crate::client::ThreadBuilder<HttpTransport, S> {
251 crate::client::ThreadBuilder::from_shared(self.agent.transport().clone(), id)
252 }
253 pub fn restore_thread(
255 &self,
256 snapshot: crate::client::ThreadSnapshot,
257 ) -> Result<crate::client::Thread<HttpTransport>> {
258 self.restore_thread_with_state(snapshot)
259 }
260 pub fn restore_thread_with_state<S: serde::de::DeserializeOwned>(
262 &self,
263 snapshot: crate::client::ThreadSnapshot,
264 ) -> Result<crate::client::Thread<HttpTransport, S>> {
265 crate::client::Thread::restore_shared(self.agent.transport().clone(), snapshot)
266 }
267 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
271 self.agent.run_events(params)
272 }
273 pub fn transport(&self) -> &HttpTransport {
275 self.agent.transport()
276 }
277}
278
279#[cfg(feature = "http")]
281#[derive(Clone, Debug)]
282pub struct HttpAgentBuilder {
283 transport: HttpTransportBuilder,
284}
285
286#[cfg(feature = "http")]
287impl HttpAgentBuilder {
288 #[must_use]
290 pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
291 self.transport = self.transport.header(name, value);
292 self
293 }
294
295 #[must_use]
297 pub fn headers<K, V>(mut self, headers: impl IntoIterator<Item = (K, V)>) -> Self
298 where
299 K: Into<String>,
300 V: Into<String>,
301 {
302 self.transport = self.transport.headers(headers);
303 self
304 }
305
306 #[must_use]
308 pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
309 self.transport = self.transport.timeout(timeout);
310 self
311 }
312
313 #[must_use]
315 pub fn connect_timeout(mut self, timeout: std::time::Duration) -> Self {
316 self.transport = self.transport.connect_timeout(timeout);
317 self
318 }
319
320 #[must_use]
322 pub fn client(mut self, client: reqwest::Client) -> Self {
323 self.transport = self.transport.client(client);
324 self
325 }
326
327 pub fn build(self) -> Result<HttpAgent> {
334 Ok(HttpAgent::from_transport(self.transport.build()?))
335 }
336}