systemprompt_api/services/gateway/protocol/inbound/anthropic_messages/
mod.rs1use 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
18pub use systemprompt_models::wire::anthropic::content_to_anthropic_block;
19
20#[cfg(feature = "test-api")]
21pub mod test_api {
22 pub use super::parse::parse as parse_request;
23 pub use super::render::{render_event_frame, render_response_value};
24}
25
26#[derive(Debug, Clone, Copy, Default)]
27pub struct AnthropicMessagesInbound;
28
29impl InboundAdapter for AnthropicMessagesInbound {
30 fn wire_name(&self) -> &'static str {
31 "anthropic.messages"
32 }
33
34 fn parse_request(&self, raw: &Bytes) -> Result<CanonicalRequest, InboundParseError> {
35 let value: Value = serde_json::from_slice(raw)
36 .map_err(|e| InboundParseError::InvalidJson(e.to_string()))?;
37 parse::parse(&value)
38 }
39
40 fn render_response(&self, response: &CanonicalResponse) -> Bytes {
41 let value = render::render_response_value(response);
42 Bytes::from(serde_json::to_vec(&value).unwrap_or_else(|_| b"{}".to_vec()))
43 }
44
45 fn render_event(&self, event: &CanonicalEvent, model: &str) -> Option<Bytes> {
46 render::render_event_frame(event, model)
47 }
48
49 fn render_error(&self, _status: StatusCode, message: &str) -> Bytes {
50 let escaped = message.replace('\\', "\\\\").replace('"', "\\\"");
51 let body = format!(
52 "{{\"type\":\"error\",\"error\":{{\"type\":\"api_error\",\"message\":\"{escaped}\"}}}}"
53 );
54 Bytes::from(body)
55 }
56}