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//!
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_responses;
14
15use bytes::Bytes;
16use http::StatusCode;
17
18use super::canonical::CanonicalRequest;
19use super::canonical_response::{CanonicalEvent, CanonicalResponse};
20
21#[derive(Debug, thiserror::Error)]
22pub enum InboundParseError {
23    #[error("invalid request body: {0}")]
24    InvalidJson(String),
25    #[error("missing required field: {0}")]
26    MissingField(&'static str),
27    #[error("unsupported value for {field}: {detail}")]
28    Unsupported { field: &'static str, detail: String },
29}
30
31pub trait InboundAdapter: Send + Sync + std::fmt::Debug {
32    fn wire_name(&self) -> &'static str;
33    fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError>;
34    fn render_response(&self, response: &CanonicalResponse) -> Bytes;
35    fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes>;
36
37    /// Render a terminal streaming event whose wire form must embed
38    /// fully-accumulated item content — the complete tool-call arguments and
39    /// the output list — which the per-event [`CanonicalEvent`] alone does
40    /// not carry. Returns `None` for wires that finalize correctly from
41    /// per-event deltas (the caller then falls back to
42    /// [`InboundAdapter::render_event`]).
43    fn render_terminal_event(
44        &self,
45        event: &CanonicalEvent,
46        snapshot: &CanonicalResponse,
47        model: &str,
48    ) -> Option<Bytes> {
49        // Why: default impl ignores args; wires that need terminal finalization
50        // override this.
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}