mod decode;
mod encode;
use crate::canonical::{CanonicalError, CanonicalRequest, Event};
use crate::protocol::{
CountRequest, DecodeState, Frame, Framing, ModelKeys, ModelsShape, Protocol, ProviderCtx,
WireRequest,
};
pub struct GoogleGenAi;
impl Protocol for GoogleGenAi {
fn encode(
&self,
req: &CanonicalRequest,
ctx: &ProviderCtx,
) -> Result<WireRequest, CanonicalError> {
encode::encode(req, ctx)
}
fn path(&self, ctx: &ProviderCtx) -> String {
encode::request_path(ctx, true)
}
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: "/v1beta/models",
keys: ModelKeys {
array_key: "models",
id_key: "name",
strip: "models/",
context_key: "inputTokenLimit",
max_output_key: "outputTokenLimit",
display_name_key: "displayName",
},
})
}
fn count_tokens(
&self,
req: &CanonicalRequest,
ctx: &ProviderCtx,
) -> Option<Result<CountRequest, CanonicalError>> {
Some(encode::count_body(req, ctx).map(|wire| CountRequest {
wire,
token_key: "totalTokens",
}))
}
}