mod decode;
mod encode;
use crate::canonical::{CanonicalError, CanonicalRequest, Event};
use crate::protocol::{
Collection, CountRequest, DecodeState, Frame, Framing, Method, ModelKeys, ModelsShape,
Protocol, ProviderCtx, Shapes, Tuning, 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 tuning(&self) -> Tuning {
Tuning {
effort: true,
budget: true,
priority: true,
image: false,
}
}
fn shapes(&self) -> Shapes {
Shapes {
tools: true,
multi_turn: true,
}
}
fn models_shape(&self) -> Option<ModelsShape> {
Some(ModelsShape {
method: Method::Get,
path: "/v1/models",
keys: ModelKeys {
collection: Collection::Array {
key: "data",
id_key: "id",
},
strip: "",
context_key: "",
max_output_key: "",
display_name_key: "display_name",
default_key: "",
},
})
}
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",
}))
}
}