Skip to main content

openagent/api/
response.rs

1//! OpenAI Responses API
2//!
3//! <https://platform.openai.com/docs/api-reference/responses>
4
5// self
6use crate::_prelude::*;
7
8mod create;
9pub use create::*;
10
11mod event;
12pub use event::*;
13
14mod object;
15pub use object::*;
16
17mod r#type;
18pub use r#type::*;
19
20/// OpenAI responses API.
21pub trait ApiResponse
22where
23	Self: ApiBase,
24{
25	/// Create a response (non-streaming).
26	fn create_response(
27		&self,
28		mut request: ResponseRequest,
29	) -> impl Send + Future<Output = Result<ResponseObject>> {
30		async {
31			// Ensure stream is disabled for non-streaming.
32			request.stream = None;
33
34			let resp = self.post_json("/responses", request).await?;
35
36			tracing::debug!("{resp}");
37
38			Ok(serde_json::from_str::<ApiResult<ResponseObject>>(&resp)?.as_result()?)
39		}
40	}
41
42	/// Create a response with streaming.
43	fn create_response_stream<H>(
44		&self,
45		mut request: ResponseRequest,
46		options: SseOptions<H>,
47	) -> impl Send + Future<Output = Result<EventStream<H::Event>>>
48	where
49		H: 'static + EventHandler,
50	{
51		async move {
52			// Ensure stream is enabled for streaming.
53			request.stream = Some(true);
54
55			self.sse("/responses", request, options).await
56		}
57	}
58}
59impl<T> ApiResponse for T where T: ApiBase {}