1use crate::{
34 Context, Message, MessageId, ResumeEntry, RunAgentInput, RunId, ThreadId, Tool, UserContent,
35};
36use futures_util::TryStreamExt;
37use serde_json::Value;
38
39use crate::client::transport::{EventStream, Transport, TransportStream};
40
41use crate::client::error::{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
184impl<T: Transport> RemoteAgent<T> {
185 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
202 let input = match outgoing(params.into(), &mut Vec::new()) {
203 Ok(input) => input,
204 Err(error) => return Box::pin(futures_util::stream::once(async { Err(error) })),
205 };
206 Box::pin(crate::client::enforce::unchecked(
207 self.run_checked(input),
208 |_| {},
209 ))
210 }
211
212 pub(crate) fn run_checked(
214 &self,
215 input: RunAgentInput,
216 ) -> crate::client::enforce::CheckedStream {
217 let connecting = self.transport.run(input);
218 Box::pin(
219 futures_util::stream::once(connecting)
220 .map_ok(TransportStream::into_checked)
221 .try_flatten(),
222 )
223 }
224}
225
226pub(crate) fn outgoing(input: RunAgentInput, warnings: &mut Vec<String>) -> Result<RunAgentInput> {
229 Ok(serde_json::from_value(outgoing_json(input, warnings)?)?)
230}
231
232pub(crate) fn outgoing_json(mut input: RunAgentInput, warnings: &mut Vec<String>) -> Result<Value> {
234 input
235 .messages
236 .retain(|message| !matches!(message, Message::Activity(_)));
237 crate::message::upgrade_legacy_content(&mut input.messages);
238 crate::protocol::input(serde_json::to_value(input)?, warnings).map_err(Error::Request)
239}
240
241#[cfg(feature = "http")]
245#[derive(Clone, Debug)]
246pub struct HttpAgent {
247 agent: RemoteAgent<std::sync::Arc<HttpTransport>>,
248}
249
250#[cfg(feature = "http")]
251impl HttpAgent {
252 pub fn new(url: impl AsRef<str>) -> Result<Self> {
254 Ok(Self::from_transport(HttpTransport::new(url)?))
255 }
256 pub fn from_transport(transport: HttpTransport) -> Self {
258 Self {
259 agent: RemoteAgent::new(std::sync::Arc::new(transport)),
260 }
261 }
262 pub fn builder(url: impl AsRef<str>) -> HttpAgentBuilder {
264 HttpAgentBuilder {
265 transport: HttpTransport::builder(url),
266 }
267 }
268 pub fn thread(&self, id: impl Into<ThreadId>) -> crate::client::Thread<HttpTransport> {
270 crate::client::Thread::from_shared(self.agent.transport().clone(), id)
271 }
272 pub fn thread_with_state<S: serde::Serialize + serde::de::DeserializeOwned>(
274 &self,
275 id: impl Into<ThreadId>,
276 state: S,
277 ) -> Result<crate::client::Thread<HttpTransport, S>> {
278 self.thread_builder(id)
279 .state(serde_json::to_value(state)?)
280 .build()
281 }
282 pub fn thread_builder<S>(
284 &self,
285 id: impl Into<ThreadId>,
286 ) -> crate::client::ThreadBuilder<HttpTransport, S> {
287 crate::client::ThreadBuilder::from_shared(self.agent.transport().clone(), id)
288 }
289 pub fn restore_thread(
291 &self,
292 snapshot: crate::client::ThreadSnapshot,
293 ) -> Result<crate::client::Thread<HttpTransport>> {
294 self.restore_thread_with_state(snapshot)
295 }
296 pub fn restore_thread_with_state<S: serde::de::DeserializeOwned>(
298 &self,
299 snapshot: crate::client::ThreadSnapshot,
300 ) -> Result<crate::client::Thread<HttpTransport, S>> {
301 crate::client::Thread::restore_shared(self.agent.transport().clone(), snapshot)
302 }
303 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
308 self.agent.run_events(params)
309 }
310 pub fn transport(&self) -> &HttpTransport {
312 self.agent.transport()
313 }
314}
315
316#[cfg(feature = "http")]
318#[derive(Clone, Debug)]
319pub struct HttpAgentBuilder {
320 transport: HttpTransportBuilder,
321}
322
323#[cfg(feature = "http")]
324impl HttpAgentBuilder {
325 #[must_use]
327 pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
328 self.transport = self.transport.header(name, value);
329 self
330 }
331
332 #[must_use]
334 pub fn headers<K, V>(mut self, headers: impl IntoIterator<Item = (K, V)>) -> Self
335 where
336 K: Into<String>,
337 V: Into<String>,
338 {
339 self.transport = self.transport.headers(headers);
340 self
341 }
342
343 #[must_use]
345 pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
346 self.transport = self.transport.timeout(timeout);
347 self
348 }
349
350 #[must_use]
352 pub fn connect_timeout(mut self, timeout: std::time::Duration) -> Self {
353 self.transport = self.transport.connect_timeout(timeout);
354 self
355 }
356
357 #[must_use]
360 pub fn allow_insecure_http(mut self, allow: bool) -> Self {
361 self.transport = self.transport.allow_insecure_http(allow);
362 self
363 }
364
365 #[must_use]
367 pub fn client(mut self, client: reqwest::Client) -> Self {
368 self.transport = self.transport.client(client);
369 self
370 }
371
372 pub fn build(self) -> Result<HttpAgent> {
379 Ok(HttpAgent::from_transport(self.transport.build()?))
380 }
381}