Skip to main content

systemprompt_api/services/gateway/protocol/inbound/openai_responses/
mod.rs

1//! Inbound adapter for the `OpenAI` Responses wire protocol.
2//!
3//! [`OpenAiResponsesInbound`] parses Responses-format request bodies into the
4//! canonical request model and renders canonical responses, streaming events,
5//! and errors back in Responses format.
6
7use bytes::Bytes;
8use http::StatusCode;
9use serde_json::Value;
10
11use super::super::canonical::CanonicalRequest;
12use super::super::canonical_response::{CanonicalEvent, CanonicalResponse};
13use super::{InboundAdapter, InboundParseError};
14
15mod parse;
16mod render;
17
18#[cfg(feature = "test-api")]
19pub mod test_api {
20    pub use super::parse::parse as parse_request;
21    pub use super::render::{render_event_frame, render_response_object};
22}
23
24#[derive(Debug, Clone, Copy, Default)]
25pub struct OpenAiResponsesInbound;
26
27impl InboundAdapter for OpenAiResponsesInbound {
28    fn wire_name(&self) -> &'static str {
29        "openai.responses"
30    }
31
32    fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError> {
33        let value: Value = serde_json::from_slice(raw)
34            .map_err(|e| InboundParseError::InvalidJson(e.to_string()))?;
35        parse::parse(&value)
36    }
37
38    fn render_response(&self, response: &CanonicalResponse) -> Bytes {
39        let value = render::render_response_object(response);
40        Bytes::from(serde_json::to_vec(&value).unwrap_or_else(|_| b"{}".to_vec()))
41    }
42
43    fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes> {
44        render::render_event_frame(event, model)
45    }
46
47    fn render_error(&self, _status: StatusCode, message: &str) -> Bytes {
48        let escaped = message.replace('\\', "\\\\").replace('"', "\\\"");
49        let body = format!("{{\"error\":{{\"type\":\"api_error\",\"message\":\"{escaped}\"}}}}");
50        Bytes::from(body)
51    }
52}