Skip to main content

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

1//! Inbound protocol adapters: caller wire format to canonical model.
2//!
3//! The [`InboundAdapter`] trait parses a request body into a
4//! [`CanonicalRequest`] and renders canonical responses, streaming events, and
5//! errors back in the caller's protocol. Implementations cover the Anthropic
6//! Messages and `OpenAI` Responses surfaces; [`InboundParseError`] reports
7//! malformed or unsupported inputs.
8
9pub mod anthropic_messages;
10pub mod openai_responses;
11
12use bytes::Bytes;
13use http::StatusCode;
14
15use super::canonical::CanonicalRequest;
16use super::canonical_response::{CanonicalEvent, CanonicalResponse};
17
18#[derive(Debug, thiserror::Error)]
19pub enum InboundParseError {
20    #[error("invalid request body: {0}")]
21    InvalidJson(String),
22    #[error("missing required field: {0}")]
23    MissingField(&'static str),
24    #[error("unsupported value for {field}: {detail}")]
25    Unsupported { field: &'static str, detail: String },
26}
27
28pub trait InboundAdapter: Send + Sync + std::fmt::Debug {
29    fn wire_name(&self) -> &'static str;
30    fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError>;
31    fn render_response(&self, response: &CanonicalResponse) -> Bytes;
32    fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes>;
33    fn render_error(&self, status: StatusCode, message: &str) -> Bytes;
34    fn streaming_content_type(&self) -> &'static str {
35        "text/event-stream"
36    }
37}