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 pub fn into_input(self) -> RunAgentInput {
130 self.input
131 }
132}
133
134impl From<RunParams> for RunAgentInput {
135 fn from(params: RunParams) -> Self {
136 params.input
137 }
138}
139
140impl From<RunAgentInput> for RunParams {
141 fn from(input: RunAgentInput) -> Self {
142 Self { input }
143 }
144}
145
146#[derive(Clone, Debug, Default)]
161pub struct RemoteAgent<T> {
162 transport: T,
163}
164
165impl<T> RemoteAgent<T> {
166 pub fn new(transport: T) -> Self {
168 Self { transport }
169 }
170
171 pub fn transport(&self) -> &T {
173 &self.transport
174 }
175
176 pub fn into_transport(self) -> T {
178 self.transport
179 }
180}
181
182impl<T: Transport> RemoteAgent<T> {
183 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
192 let connecting = self.transport.run(params.into());
193 boxed_stream(futures_util::stream::once(connecting).try_flatten())
194 }
195}
196
197#[cfg(feature = "http")]
200#[derive(Clone, Debug)]
201pub struct HttpAgent {
202 agent: RemoteAgent<std::sync::Arc<HttpTransport>>,
203}
204
205#[cfg(feature = "http")]
206impl HttpAgent {
207 pub fn new(url: impl AsRef<str>) -> Result<Self> {
209 Ok(Self::from_transport(HttpTransport::new(url)?))
210 }
211 pub fn from_transport(transport: HttpTransport) -> Self {
213 Self {
214 agent: RemoteAgent::new(std::sync::Arc::new(transport)),
215 }
216 }
217 pub fn builder(url: impl AsRef<str>) -> HttpAgentBuilder {
219 HttpAgentBuilder {
220 transport: HttpTransport::builder(url),
221 }
222 }
223 pub fn thread(&self, id: impl Into<ThreadId>) -> crate::client::Thread<HttpTransport> {
225 crate::client::Thread::from_shared(self.agent.transport().clone(), id)
226 }
227 pub fn thread_with_state<S: serde::Serialize + serde::de::DeserializeOwned>(
229 &self,
230 id: impl Into<ThreadId>,
231 state: S,
232 ) -> Result<crate::client::Thread<HttpTransport, S>> {
233 self.thread_builder(id)
234 .state(serde_json::to_value(state)?)
235 .build()
236 }
237 pub fn thread_builder<S>(
239 &self,
240 id: impl Into<ThreadId>,
241 ) -> crate::client::ThreadBuilder<HttpTransport, S> {
242 crate::client::ThreadBuilder::from_shared(self.agent.transport().clone(), id)
243 }
244 pub fn restore_thread(
246 &self,
247 snapshot: crate::client::ThreadSnapshot,
248 ) -> Result<crate::client::Thread<HttpTransport>> {
249 self.restore_thread_with_state(snapshot)
250 }
251 pub fn restore_thread_with_state<S: serde::de::DeserializeOwned>(
253 &self,
254 snapshot: crate::client::ThreadSnapshot,
255 ) -> Result<crate::client::Thread<HttpTransport, S>> {
256 crate::client::Thread::restore_shared(self.agent.transport().clone(), snapshot)
257 }
258 pub fn run_events(&self, params: impl Into<RunAgentInput>) -> EventStream {
260 self.agent.run_events(params)
261 }
262 pub fn transport(&self) -> &HttpTransport {
264 self.agent.transport()
265 }
266}
267
268#[cfg(feature = "http")]
270#[derive(Clone, Debug)]
271pub struct HttpAgentBuilder {
272 transport: HttpTransportBuilder,
273}
274
275#[cfg(feature = "http")]
276impl HttpAgentBuilder {
277 #[must_use]
279 pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
280 self.transport = self.transport.header(name, value);
281 self
282 }
283
284 #[must_use]
286 pub fn headers<K, V>(mut self, headers: impl IntoIterator<Item = (K, V)>) -> Self
287 where
288 K: Into<String>,
289 V: Into<String>,
290 {
291 self.transport = self.transport.headers(headers);
292 self
293 }
294
295 #[must_use]
297 pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
298 self.transport = self.transport.timeout(timeout);
299 self
300 }
301
302 #[must_use]
304 pub fn connect_timeout(mut self, timeout: std::time::Duration) -> Self {
305 self.transport = self.transport.connect_timeout(timeout);
306 self
307 }
308
309 #[must_use]
311 pub fn client(mut self, client: reqwest::Client) -> Self {
312 self.transport = self.transport.client(client);
313 self
314 }
315
316 pub fn build(self) -> Result<HttpAgent> {
323 Ok(HttpAgent::from_transport(self.transport.build()?))
324 }
325}