systemprompt_api/services/gateway/protocol/inbound/
mod.rs1pub mod anthropic_messages;
13pub mod openai_chat;
14pub mod openai_responses;
15
16use bytes::Bytes;
17use http::StatusCode;
18use systemprompt_models::services::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(crate) fn error_type_for_status(status: StatusCode) -> &'static str {
34 match status {
35 StatusCode::UNAUTHORIZED => "authentication_error",
36 StatusCode::FORBIDDEN => "permission_error",
37 StatusCode::NOT_FOUND => "not_found_error",
38 StatusCode::TOO_MANY_REQUESTS => "rate_limit_error",
39 s if s.is_client_error() => "invalid_request_error",
40 _ => "api_error",
41 }
42}
43
44pub trait InboundAdapter: Send + Sync + std::fmt::Debug {
45 fn wire_name(&self) -> &'static str;
46
47 fn passthrough_wire(&self) -> Option<WireProtocol> {
48 None
49 }
50
51 fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError>;
52 fn render_response(&self, response: &CanonicalResponse) -> Bytes;
53 fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes>;
54
55 fn render_terminal_event(
56 &self,
57 _event: &CanonicalEvent,
58 _snapshot: &CanonicalResponse,
59 _model: &str,
60 ) -> Option<Bytes> {
61 None
62 }
63
64 fn wants_stream_usage(&self, _raw: &Bytes) -> bool {
65 false
66 }
67
68 fn render_stream_tail(
69 &self,
70 _snapshot: &CanonicalResponse,
71 _include_usage: bool,
72 ) -> Option<Bytes> {
73 None
74 }
75
76 fn render_error(&self, status: StatusCode, message: &str) -> Bytes;
77 fn streaming_content_type(&self) -> &'static str {
78 "text/event-stream"
79 }
80}