Skip to main content

SessionBackend

Trait SessionBackend 

Source
pub trait SessionBackend: Send + 'static {
    // Required methods
    fn id(&self) -> &str;
    fn model_id(&self) -> &ModelId;
    fn send(
        &mut self,
        input: SessionInput,
    ) -> impl Future<Output = Result<(), KitError>> + Send + '_;
    fn recv(
        &mut self,
    ) -> impl Future<Output = Result<SessionEvent, KitError>> + Send + '_;
    fn close(self) -> impl Future<Output = Result<(), KitError>> + Send;
}
Expand description

Public implementation contract for one unique provider-native session.

Caller models return their concrete session implementation directly through crate::ModelBackend::Session.

This example requires valid credentials and a live model with native session support. Sessions are unique, mutable, and non-Clone. Consuming close settles the session transport; the Provider retains separate shared shutdown authority. Ordinary multi-turn generation instead uses messages and continuation.

§Examples


use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    assert!(!session.id().is_empty());
    assert_eq!(session.model_id(), model.model_id());
    let closed = session.close().await;
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;

Required Methods§

Source

fn id(&self) -> &str

Stable session identifier.

This example requires a live native session on a supporting exact model.

§Examples

use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    assert!(!session.id().is_empty());
    assert_eq!(session.model_id(), model.model_id());
    let closed = session.close().await;
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;
Source

fn model_id(&self) -> &ModelId

Exact selected model.

This example requires a live native session on a supporting exact model.

§Examples

use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    assert!(!session.id().is_empty());
    assert_eq!(session.model_id(), model.model_id());
    let closed = session.close().await;
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;
Source

fn send( &mut self, input: SessionInput, ) -> impl Future<Output = Result<(), KitError>> + Send + '_

Sends interactive input.

This example requires a live native session on a supporting exact model.

§Errors

Returns the owning input, unsupported, closed, transport, deadline, service-rejection, or protocol error. Consuming close does not make the session reusable.

§Examples

use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    let sent = session.send(SessionInput::Text { text: "Hello".into() }).await;
    let closed = session.close().await;
    sent?;
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;
Source

fn recv( &mut self, ) -> impl Future<Output = Result<SessionEvent, KitError>> + Send + '_

Receives the next interactive event.

This example requires a live native session on a supporting exact model.

§Errors

Returns the owning input, unsupported, closed, transport, deadline, service-rejection, or protocol error. Consuming close does not make the session reusable.

§Examples

use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    let sent = session.send(SessionInput::Text { text: "Hello".into() }).await;
    let event = if sent.is_ok() { Some(session.recv().await) } else { None };
    let closed = session.close().await;
    sent?;
    if let Some(event) = event { let _event = event?; }
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;
Source

fn close(self) -> impl Future<Output = Result<(), KitError>> + Send

Closes the unique session and its transport.

This example requires a live native session on a supporting exact model.

§Errors

Returns the owning input, unsupported, closed, transport, deadline, service-rejection, or protocol error. Consuming close does not make the session reusable.

§Examples

use providers_kit::{Provider, ProviderBackend, ProviderConfig, config::{ApiKey, OpenAiConfig, ProviderKind, TransportConfig}};
let provider = Provider::from_config(ProviderConfig::new(ProviderKind::OpenAi(OpenAiConfig::new(ApiKey::new("replace-with-your-key")?)), TransportConfig::default()))?;
use providers_kit::{ModelBackend, ModelId, SessionBackend, session::{SessionConfig, SessionInput}};
let model = provider.model(ModelId::new("your-exact-session-model-id")?)?;
let observed = async {
    let mut session = model.open_session(SessionConfig::default()).await?;
    let closed = session.close().await;
    closed
}.await;
let cleanup = provider.shutdown().await;
observed?;
cleanup?;

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementations on Foreign Types§

Source§

impl SessionBackend for Infallible

Source§

fn id(&self) -> &str

Source§

fn model_id(&self) -> &ModelId

Source§

async fn send(&mut self, _input: SessionInput) -> Result<(), KitError>

Source§

async fn recv(&mut self) -> Result<SessionEvent, KitError>

Source§

async fn close(self) -> Result<(), KitError>

Implementors§