Skip to main content

ProtocolHandler

Trait ProtocolHandler 

Source
pub trait ProtocolHandler: Send + Sync {
    // Required methods
    fn endpoint_path(&self) -> &str;
    fn auth_headers(&self, api_key: &str) -> Vec<(String, String)>;
    fn build_request_body(&self, req: &ApiRequest) -> Value;
    fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError>;
    fn parse_stream_event(
        &self,
        event_type: &str,
        data: &str,
    ) -> Vec<StreamEvent>;
    fn build_messages(
        &self,
        messages: &[Message],
        prompt: &str,
        context: Option<&str>,
        images: Option<&[ContentBlock]>,
    ) -> (Vec<Value>, Option<String>);
    fn build_tools(&self, tools: &[Value]) -> Vec<Value>;

    // Provided methods
    fn supports_streaming(&self) -> bool { ... }
    fn supports_thinking(&self) -> bool { ... }
    fn supports_video(&self) -> bool { ... }
    fn supports_audio(&self) -> bool { ... }
    fn protocol_name(&self) -> &'static str { ... }
}
Expand description

Protocol handler trait — each provider implements this.

Required Methods§

Source

fn endpoint_path(&self) -> &str

URL path for the API endpoint (appended to the base URL).

Source

fn auth_headers(&self, api_key: &str) -> Vec<(String, String)>

Build HTTP headers for authentication.

Source

fn build_request_body(&self, req: &ApiRequest) -> Value

Build the JSON request body from unified ApiRequest.

Source

fn parse_response(&self, body: &str) -> Result<ApiResponse, InferenceError>

Parse a complete (non-streaming) response body into ApiResponse.

Source

fn parse_stream_event(&self, event_type: &str, data: &str) -> Vec<StreamEvent>

Parse a single SSE line into StreamEvents (for streaming responses). Returns multiple events when a single SSE chunk contains multiple tool calls.

Source

fn build_messages( &self, messages: &[Message], prompt: &str, context: Option<&str>, images: Option<&[ContentBlock]>, ) -> (Vec<Value>, Option<String>)

Convert conversation Messages to this protocol’s message format.

Source

fn build_tools(&self, tools: &[Value]) -> Vec<Value>

Convert tool definitions to this protocol’s format.

Provided Methods§

Source

fn supports_streaming(&self) -> bool

Whether this protocol supports streaming.

Source

fn supports_thinking(&self) -> bool

Whether this protocol supports extended thinking.

Source

fn supports_video(&self) -> bool

Whether this protocol accepts video content blocks natively on the generation endpoint. Defaults to false. When a request carries a video block and the selected protocol returns false, RemoteBackend::execute_request rejects the call with InferenceError::UnsupportedMode rather than silently downgrading the video to a text placeholder.

Source

fn supports_audio(&self) -> bool

Whether this protocol accepts audio content blocks natively. Same contract as [supports_video] — false means the remote backend pre-check returns UnsupportedMode instead of silently stringifying the audio reference.

Source

fn protocol_name(&self) -> &'static str

Backend identifier used in UnsupportedMode error messages. Handlers override to something recognizable (e.g. “openai”, “anthropic-messages-v1”).

Implementors§