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, `OpenAI` Responses, and `OpenAI` Chat Completions surfaces;
7//! [`InboundParseError`] reports malformed or unsupported inputs.
8//!
9//! Copyright (c) systemprompt.io — Business Source License 1.1.
10//! See <https://systemprompt.io> for licensing details.
11
12pub mod anthropic_messages;
13pub mod openai_chat;
14pub mod openai_responses;
15
16use bytes::Bytes;
17use http::StatusCode;
18use systemprompt_models::profile::WireProtocol;
19
20use super::canonical::CanonicalRequest;
21use super::canonical_response::{CanonicalEvent, CanonicalResponse};
22
23#[derive(Debug, thiserror::Error)]
24pub enum InboundParseError {
25    #[error("invalid request body: {0}")]
26    InvalidJson(String),
27    #[error("missing required field: {0}")]
28    MissingField(&'static str),
29    #[error("unsupported value for {field}: {detail}")]
30    Unsupported { field: &'static str, detail: String },
31}
32
33pub trait InboundAdapter: Send + Sync + std::fmt::Debug {
34    fn wire_name(&self) -> &'static str;
35
36    fn passthrough_wire(&self) -> Option<WireProtocol> {
37        None
38    }
39
40    fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError>;
41    fn render_response(&self, response: &CanonicalResponse) -> Bytes;
42    fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes>;
43
44    fn render_terminal_event(
45        &self,
46        event: &CanonicalEvent,
47        snapshot: &CanonicalResponse,
48        model: &str,
49    ) -> Option<Bytes> {
50        // Why: unused-arg suppression in a default trait method body.
51        let _ = (event, snapshot, model);
52        None
53    }
54
55    fn render_error(&self, status: StatusCode, message: &str) -> Bytes;
56    fn streaming_content_type(&self) -> &'static str {
57        "text/event-stream"
58    }
59}