Skip to main content

ProtocolAdapter

Trait ProtocolAdapter 

Source
pub trait ProtocolAdapter:
    Debug
    + Send
    + Sync {
    // Required methods
    fn endpoint_path(&self) -> &str;
    fn build_request_body(
        &self,
        request: &CompletionRequest,
        stream: bool,
    ) -> Result<Value, ProviderError>;
    fn build_auth_headers(&self, auth: &AuthMethod) -> Vec<(String, String)>;
    fn parse_response(
        &self,
        body: &Value,
    ) -> Result<CompletionResponse, ProviderError>;
    fn parse_sse_event(
        &self,
        data: &str,
    ) -> Result<Option<StreamEvent>, ProviderError>;
    fn protocol_name(&self) -> &str;
}
Expand description

A protocol adapter translates between the unified request/response types and a specific provider’s API protocol.

This trait is object-safe so it can be used as &dyn ProtocolAdapter or Box<dyn ProtocolAdapter>. That means:

  • No async methods
  • No generic type parameters
  • No impl Trait return types

Each variant represents a different API protocol:

  • OpenAI Chat Completions (/v1/chat/completions)
  • OpenAI Responses (/v1/responses)
  • Anthropic Messages (/v1/messages)
  • Ollama (/api/chat)

§Examples

use xz_provider::protocol::{ProtocolAdapter, AuthMethod};
use xz_provider::ProviderError;

let adapter: &dyn ProtocolAdapter = &DummyAdapter;
assert_eq!(adapter.protocol_name(), "test");

Required Methods§

Source

fn endpoint_path(&self) -> &str

Returns the API endpoint path for this protocol (e.g., /v1/chat/completions).

The caller appends this to the provider’s base URL.

Source

fn build_request_body( &self, request: &CompletionRequest, stream: bool, ) -> Result<Value, ProviderError>

Builds the JSON request body for a completion request.

When stream is true, the body should include the protocol-appropriate streaming flag (e.g., "stream": true for OpenAI, no change for Anthropic which uses SSE headers instead).

Source

fn build_auth_headers(&self, auth: &AuthMethod) -> Vec<(String, String)>

Converts an AuthMethod into the appropriate HTTP auth headers.

For AuthMethod::None, returns an empty vector. For AuthMethod::Bearer, returns a single Authorization: Bearer ... header. For AuthMethod::ApiKey, returns a single header with the specified name and value.

Source

fn parse_response( &self, body: &Value, ) -> Result<CompletionResponse, ProviderError>

Parses a complete (non-streaming) JSON response body into a CompletionResponse.

Source

fn parse_sse_event( &self, data: &str, ) -> Result<Option<StreamEvent>, ProviderError>

Parses the data portion of a single SSE event into a StreamEvent.

Returns Ok(None) when the event is ignorable (e.g., a heartbeat). The input data string is the content after the data: prefix, already trimmed.

Source

fn protocol_name(&self) -> &str

Returns the human-readable name of this protocol for logging and metrics.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§