Skip to main content

ModelAdapter

Trait ModelAdapter 

Source
pub trait ModelAdapter: Send + Sync {
    type Session: ModelSession;

    // Required method
    fn start_session<'life0, 'async_trait>(
        &'life0 self,
        config: SessionConfig,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Session, LoopError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;

    // Provided method
    fn provider_name(&self) -> Option<&str> { ... }
}
Expand description

Factory for creating model sessions.

Implement this trait to integrate a model provider (e.g. OpenRouter, Anthropic, a local LLM server) with the agent loop. Agent holds a single adapter and calls start_session once when Agent::start is invoked.

§Example

use agentkit_loop::{ModelAdapter, ModelSession, SessionConfig, LoopError};
use async_trait::async_trait;

struct MyAdapter;

#[async_trait]
impl ModelAdapter for MyAdapter {
    type Session = MySession;

    async fn start_session(&self, config: SessionConfig) -> Result<MySession, LoopError> {
        // Initialize provider-specific session state here.
        Ok(MySession { /* ... */ })
    }
}

Required Associated Types§

Source

type Session: ModelSession

The session type produced by this adapter.

Required Methods§

Source

fn start_session<'life0, 'async_trait>( &'life0 self, config: SessionConfig, ) -> Pin<Box<dyn Future<Output = Result<Self::Session, LoopError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Create a new model session from the given configuration.

§Errors

Returns LoopError if the provider connection or initialisation fails.

Provided Methods§

Source

fn provider_name(&self) -> Option<&str>

Name of the underlying model provider, when known.

Stamped onto agent telemetry spans as the gen_ai.provider.name attribute from the OpenTelemetry GenAI semantic conventions. Use a lowercase identifier (e.g. openrouter, ollama). The default returns None for adapters without a meaningful provider identity.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§