use async_trait::async_trait;
use serde::{Serialize, de::DeserializeOwned};
use crate::error::LlmConnectorError;
use crate::types::{ChatRequest as Request, ChatResponse as Response};
#[cfg(feature = "streaming")]
use crate::types::ChatStream;
#[async_trait]
pub trait Protocol: Send + Sync + 'static {
type Request: Serialize + Send + Sync;
type Response: DeserializeOwned + Send + Sync;
#[cfg(feature = "streaming")]
type StreamResponse: DeserializeOwned + Send + Sync;
#[cfg(not(feature = "streaming"))]
type StreamResponse: Send + Sync;
type Error: ProtocolError;
fn name(&self) -> &str;
fn endpoint(&self, base_url: &str) -> String;
fn models_endpoint(&self, base_url: &str) -> Option<String>;
fn build_request(&self, request: &Request, stream: bool) -> Self::Request;
fn parse_response(&self, response: Self::Response) -> Response;
#[cfg(feature = "streaming")]
fn parse_stream_response(&self, response: Self::StreamResponse) -> ChatStream;
#[cfg(feature = "streaming")]
fn uses_sse_stream(&self) -> bool { true }
fn validate_success_body(&self, status: u16, raw: &serde_json::Value) -> Result<(), LlmConnectorError> {
let _ = (status, raw);
Ok(())
}
}
pub trait ProtocolError: Send + Sync {
fn map_http_error(status: u16, body: serde_json::Value) -> LlmConnectorError;
fn map_network_error(error: reqwest::Error) -> LlmConnectorError;
fn is_retriable_error(error: &LlmConnectorError) -> bool;
}