mod decode;
mod encode;
use crate::canonical::{CanonicalError, CanonicalRequest, Event};
use crate::protocol::{
CountRequest, DecodeState, Frame, Framing, ModelKeys, ModelsShape, Protocol, ProviderCtx,
WireRequest,
};
pub struct AnthropicMessages;
impl Protocol for AnthropicMessages {
fn encode(
&self,
req: &CanonicalRequest,
ctx: &ProviderCtx,
) -> Result<WireRequest, CanonicalError> {
encode::encode(req, ctx)
}
fn path(&self, _ctx: &ProviderCtx) -> String {
encode::REQUEST_PATH.to_string()
}
fn content_type(&self) -> &str {
"application/json"
}
fn decode(&self, frame: Frame, state: &mut DecodeState) -> Result<Vec<Event>, CanonicalError> {
decode::decode(frame, state)
}
fn decode_full(
&self,
body: &[u8],
state: &mut DecodeState,
) -> Result<Vec<Event>, CanonicalError> {
decode::decode_full(body, state)
}
fn framing(&self) -> Framing {
Framing::Sse
}
fn models_shape(&self) -> Option<ModelsShape> {
Some(ModelsShape {
path: "/v1/models",
keys: ModelKeys {
array_key: "data",
id_key: "id",
strip: "",
context_key: "",
max_output_key: "",
display_name_key: "display_name",
},
})
}
fn count_tokens(
&self,
req: &CanonicalRequest,
ctx: &ProviderCtx,
) -> Option<Result<CountRequest, CanonicalError>> {
Some(encode::count_body(req, ctx).map(|wire| CountRequest {
wire,
token_key: "input_tokens",
}))
}
}