openagent 0.1.10

OpenAI Agent Kit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! HTTP client abstraction and implementations.

// std
use std::{
	env,
	fmt::Debug,
	io::{Error as IoError, Result as IoResult},
	mem,
	pin::Pin,
	task::{Context, Poll},
	time::Duration,
};
// crates.io
use futures::{Stream, TryStreamExt};
use reqwest::{
	Body, Client,
	multipart::{Form, Part},
};
use tokio_util::{
	bytes::Bytes,
	codec::{FramedRead, LinesCodec},
	io::StreamReader,
};
// self
use crate::_prelude::*;

pub(crate) type EventStream<T> = _Stream<Result<T>>;

type _Stream<T> = Pin<Box<dyn Send + Stream<Item = T>>>;
type ByteStream = _Stream<IoResult<Bytes>>;

/// Top-level HTTP abstraction trait defining all client capabilities.
pub trait ApiBase
where
	Self: Send + Sync,
{
	/// Returns the root URL that is prepended to every request path.
	fn base_uri(&self) -> &str;

	/// Issues a GET request and returns the full response body as `String`.
	fn get(&self, endpoint: &str) -> impl Send + Future<Output = Result<String>>;

	/// Issues a multipart POST request and returns the full response body as `String`.
	fn post_multipart(
		&self,
		endpoint: &str,
		multipart: Multipart,
	) -> impl Send + Future<Output = Result<String>>;

	/// Issues a JSON POST request and returns the full response body as `String`.
	fn post_json<S>(&self, endpoint: &str, body: S) -> impl Send + Future<Output = Result<String>>
	where
		S: Send + Serialize;

	/// Performs a streaming POST request and yields server-sent events.
	fn sse<S, H>(
		&self,
		endpoint: &str,
		body: S,
		options: SseOptions<H>,
	) -> impl Send + Future<Output = Result<EventStream<H::Event>>>
	where
		S: Send + Serialize,
		H: 'static + EventHandler;

	/// Same as `sse` but supports resuming from a given event ID.
	fn sse_with_resume<S, H>(
		&self,
		endpoint: &str,
		body: S,
		options: SseOptions<H>,
		last_event_id: Option<&str>,
	) -> impl Send + Future<Output = Result<EventStream<H::Event>>>
	where
		S: Send + Serialize,
		H: 'static + EventHandler;
}

/// Trait implemented by user code to transform raw SSE frames into domain events.
pub trait EventHandler
where
	Self: Send,
{
	/// Output event type generated by this handler.
	type Event;

	/// Called when an `event:` line is seen.
	fn handle_event(&self, #[allow(unused)] event: &str) -> Result<()> {
		Ok(())
	}

	/// Called when a full `data:` block representing one logical event is ready.
	fn handle_data(&self, data: String) -> Result<Self::Event>;

	/// Called when unexpected non-SSE content is encountered.
	fn handle_unexpected(&self, #[allow(unused)] unexpected: String) -> Result<()> {
		Ok(())
	}
}
impl EventHandler for () {
	type Event = String;

	/// Pass-through handler that returns the raw data string.
	fn handle_data(&self, data: String) -> Result<Self::Event> {
		Ok(data)
	}
}

/// Configuration struct that bundles options for an SSE connection.
#[derive(Debug)]
pub struct SseOptions<H> {
	/// If true, `event:` lines are ignored and only `data:` is processed.
	pub drop_event: bool,
	/// User-supplied handler that converts raw SSE frames into events.
	pub event_handler: H,
	/// Policy that governs automatic reconnection behaviour.
	pub reconnect: Reconnect,
}
impl<H> SseOptions<H> {
	/// Creates a new `SseOptions` instance with default settings.
	pub fn new(event_handler: H) -> Self {
		Self { drop_event: false, event_handler, reconnect: Reconnect::default() }
	}

	/// Enables or disables dropping of `event:` frames.
	pub fn drop_event(mut self, drop: bool) -> Self {
		self.drop_event = drop;

		self
	}

	/// Replaces the current event handler with `event_handler`.
	pub fn event_handler(mut self, event_handler: H) -> Self {
		self.event_handler = event_handler;

		self
	}

	/// Replaces the reconnection policy with `reconnect`.
	pub fn reconnect(mut self, reconnect: Reconnect) -> Self {
		self.reconnect = reconnect;

		self
	}
}

/// Policy defining how the client should attempt to reconnect to an SSE stream.
#[derive(Debug)]
pub struct Reconnect {
	/// Whether reconnection attempts are performed.
	pub support: bool,
	/// Maximum number of reconnection attempts before giving up.
	pub max_retries: usize,
	/// Delay between reconnection attempts.
	pub retry_interval: Duration,
}
impl Default for Reconnect {
	fn default() -> Self {
		Self { support: false, max_retries: 3, retry_interval: Duration::from_millis(200) }
	}
}

#[derive(Clone, Debug)]
/// Concrete API client that talks to the remote service using `reqwest`.
pub struct Api {
	http: Client,
	auth: Auth,
}
impl Api {
	/// Constructs a new [`Api`] client with the supplied `auth` settings.
	pub fn new(auth: Auth) -> Self {
		let http =
			Client::builder().user_agent("openagent").build().expect("build must succeed; qed");

		Self { http, auth }
	}
}
impl ApiBase for Api {
	fn base_uri(&self) -> &str {
		&self.auth.uri
	}

	async fn get(&self, endpoint: &str) -> Result<String> {
		Ok(self
			.http
			.get(format!("{}{endpoint}", self.base_uri()))
			.bearer_auth(&self.auth.key)
			.send()
			.await?
			.text()
			.await?)
	}

	async fn post_multipart(&self, endpoint: &str, multipart: Multipart) -> Result<String> {
		Ok(self
			.http
			.post(format!("{}{endpoint}", self.base_uri()))
			.bearer_auth(&self.auth.key)
			.multipart(multipart.into())
			.send()
			.await?
			.text()
			.await?)
	}

	async fn post_json<S>(&self, endpoint: &str, body: S) -> Result<String>
	where
		S: Send + Serialize,
	{
		Ok(self
			.http
			.post(format!("{}{endpoint}", self.base_uri()))
			.bearer_auth(&self.auth.key)
			.json(&body)
			.send()
			.await?
			.text()
			.await?)
	}

	async fn sse<S, H>(
		&self,
		endpoint: &str,
		body: S,
		options: SseOptions<H>,
	) -> Result<Pin<Box<dyn Send + Stream<Item = Result<H::Event>>>>>
	where
		S: Send + Serialize,
		H: 'static + EventHandler,
	{
		let stream = self
			.http
			.post(format!("{}{endpoint}", self.base_uri()))
			.bearer_auth(&self.auth.key)
			.header("Accept", "text/event-stream")
			.header("Cache-Control", "no-cache")
			.json(&body)
			.send()
			.await?
			.bytes_stream()
			.map_err(IoError::other);
		let reader = StreamReader::new(Box::pin(stream) as _);
		let stream = FramedRead::new(reader, LinesCodec::new());

		Ok(Box::pin(Sse {
			stream,
			options,
			last_event: Default::default(),
			data: Default::default(),
			unexpected: Default::default(),
		}))
	}

	async fn sse_with_resume<S, H>(
		&self,
		endpoint: &str,
		body: S,
		options: SseOptions<H>,
		last_event_id: Option<&str>,
	) -> Result<Pin<Box<dyn Send + Stream<Item = Result<H::Event>>>>>
	where
		S: Send + Serialize,
		H: 'static + EventHandler,
	{
		let mut req = self
			.http
			.post(format!("{}{endpoint}", self.base_uri()))
			.bearer_auth(&self.auth.key)
			.header("Accept", "text/event-stream")
			.header("Cache-Control", "no-cache")
			.json(&body);

		// Add Last-Event-ID header for resumption.
		if let Some(event_id) = last_event_id {
			req = req.header("Last-Event-ID", event_id);
		}

		let stream = req.send().await?.bytes_stream().map_err(IoError::other);
		let reader = StreamReader::new(Box::pin(stream) as _);
		let stream = FramedRead::new(reader, LinesCodec::new());

		Ok(Box::pin(Sse {
			stream,
			options,
			last_event: (None, last_event_id.map(Into::into)),
			data: Default::default(),
			unexpected: Default::default(),
		}))
	}
}

/// Authentication tuple holding the API base URL and bearer token.
#[derive(Clone, Debug)]
pub struct Auth {
	/// Remote service root endpoint URL.
	pub uri: String,
	/// Secret authentication key used as bearer token.
	pub key: String,
}
impl Auth {
	/// Builds an `Auth` from the `OPENAI_BASE_URL` and `OPENAI_API_KEY` env variables.
	pub fn from_env() -> Self {
		Auth {
			uri: env::var("OPENAI_BASE_URL").expect("OPENAI_BASE_URL must be set; qed"),
			key: env::var("OPENAI_API_KEY").expect("OPENAI_API_KEY must be set; qed"),
		}
	}
}

/// Helper struct for building multipart/form-data request bodies.
#[derive(Clone, Debug, Default)]
pub struct Multipart {
	/// List of binary parts each containing a name, data buffer, and optional filename.
	#[allow(clippy::type_complexity)]
	pub binary: Vec<(Cow<'static, str>, Cow<'static, [u8]>, Option<String>)>,
	/// List of text parts each containing a name and UTF-8 string value.
	pub text: Vec<(Cow<'static, str>, Cow<'static, str>)>,
}
impl From<Multipart> for Form {
	fn from(val: Multipart) -> Form {
		val.binary.into_iter().fold(
			val.text.into_iter().fold(Form::new(), |form, (k, v)| form.text(k, v)),
			|form, (k, v, filename)| {
				let len = v.len() as _;

				form.part(
					k,
					match v {
						Cow::Borrowed(v) => build_stream_part(v, len, filename),
						Cow::Owned(v) => build_stream_part(v, len, filename),
					},
				)
			},
		)
	}
}

/// Stream wrapper that parses raw bytes from the HTTP response into SSE frames.
#[pin_project::pin_project]
pub struct Sse<T> {
	/// Line-based parser around the raw HTTP byte stream.
	#[pin]
	pub stream: FramedRead<StreamReader<ByteStream, Bytes>, LinesCodec>,
	/// Configuration options controlling behaviour of the SSE consumer.
	pub options: SseOptions<T>,
	/// Tuple storing the most recently observed `(event_type, event_id)`.
	pub last_event: (Option<String>, Option<String>),
	/// Buffer holding concatenated `data:` lines until an empty line finalises the event.
	pub data: String,
	/// Buffer holding non-SSE content encountered in the stream.
	pub unexpected: String,
}
impl<T> Stream for Sse<T>
where
	T: EventHandler,
{
	type Item = Result<T::Event>;

	/// Polls the underlying byte stream and emits parsed events.
	fn poll_next(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
		let mut this = self.project();

		loop {
			match Pin::new(&mut this.stream).poll_next(ctx) {
				Poll::Ready(Some(Ok(line))) => {
					let line = line.trim();

					// Handle SSE protocol.
					if line.is_empty() {
						// Empty line indicates end of an event.
						if !this.data.is_empty() {
							let data = mem::take(this.data);

							// Shrink capacity to free unused memory if the string was large.
							this.data.shrink_to_fit();

							let res = this.options.event_handler.handle_data(data);

							// Clear current event type.
							this.last_event.0 = None;

							return Poll::Ready(Some(res));
						}

						continue;
					}

					tracing::debug!("{line}");

					// Parse SSE line.
					if let Some(data_chunk) = line.strip_prefix("data: ") {
						if data_chunk == "[DONE]" {
							return Poll::Ready(None);
						}

						// Accumulate data.
						if !this.data.is_empty() {
							this.data.push('\n');
						}

						this.data.push_str(data_chunk);
					} else if let Some(event) = line.strip_prefix("event: ") {
						// Handle event.
						if !this.options.drop_event {
							this.last_event.0 = Some(event.into());

							if let Err(e) = this.options.event_handler.handle_event(event) {
								return Poll::Ready(Some(Err(e)));
							}
						}
					} else if let Some(event_id) = line.strip_prefix("id: ") {
						// Store event ID for reconnection.
						this.last_event.1 = Some(event_id.into());
					} else if let Some(retry_ms) = line.strip_prefix("retry: ") {
						// Handle retry instruction (optional implementation).
						if let Ok(_ms) = retry_ms.parse::<u64>() {
							// Update retry interval if needed (currently ignored).
						}
					} else if line.starts_with(':') {
						// Comment line, ignore.
						continue;
					} else {
						// Non-SSE formatted line - accumulate as unexpected content.
						if !this.unexpected.is_empty() {
							this.unexpected.push('\n');
						}

						this.unexpected.push_str(line);
					}
				},
				Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e.into()))),
				Poll::Ready(None) => {
					// Stream ended - check if we have accumulated unexpected content to process.
					if !this.unexpected.is_empty() {
						let unexpected = mem::take(this.unexpected);

						this.unexpected.shrink_to_fit();

						if let Err(e) = this.options.event_handler.handle_unexpected(unexpected) {
							return Poll::Ready(Some(Err(e)));
						}
					}

					return Poll::Ready(None);
				},
				Poll::Pending => return Poll::Pending,
			}
		}
	}
}

/// Builds a `Part` from raw bytes and an optional filename for multipart uploads.
fn build_stream_part<T>(data: T, data_len: u64, filename: Option<String>) -> Part
where
	T: Into<Body>,
{
	let part = Part::stream_with_length(data, data_len);

	if let Some(filename) = filename { part.file_name(filename) } else { part }
}