use std::{pin::Pin, time::Instant};
use async_trait::async_trait;
use futures_core::Stream;
use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;
use crate::{
BridgeError, ImageRequest, ImageResponse, ProviderCapabilities, ProviderDescriptor,
ProviderEvent, SessionMetadata,
};
pub type ProviderEventStream =
Pin<Box<dyn Stream<Item = Result<ProviderEvent, BridgeError>> + Send + 'static>>;
#[derive(Debug, Clone)]
pub struct ProviderContext {
pub request_id: String,
pub deadline: Instant,
pub cancellation: CancellationToken,
pub events: Option<mpsc::Sender<ProviderEvent>>,
}
#[async_trait]
pub trait ImageProvider: Send + Sync {
fn descriptor(&self) -> ProviderDescriptor;
async fn capabilities(&self, model: Option<&str>) -> Result<ProviderCapabilities, BridgeError>;
async fn execute(
&self,
request: ImageRequest,
context: ProviderContext,
) -> Result<ImageResponse, BridgeError>;
async fn execute_stream(
&self,
_request: ImageRequest,
_context: ProviderContext,
) -> Result<ProviderEventStream, BridgeError> {
Err(BridgeError::new(
crate::ErrorCode::UnsupportedCapability,
"provider does not support streaming image events",
))
}
async fn check_ready(&self) -> Result<(), BridgeError>;
async fn get_session(&self, _key: &str) -> Result<SessionMetadata, BridgeError> {
Err(BridgeError::new(
crate::ErrorCode::UnsupportedCapability,
"provider does not expose persistent sessions",
))
}
async fn delete_session(&self, _key: &str) -> Result<(), BridgeError> {
Err(BridgeError::new(
crate::ErrorCode::UnsupportedCapability,
"provider does not expose persistent sessions",
))
}
fn restart_count(&self) -> Option<u64> {
None
}
async fn shutdown(&self) -> Result<(), BridgeError> {
Ok(())
}
}