use async_trait::async_trait;
use futures_util::Stream;
use std::pin::Pin;
use super::RunnableConfig;
#[async_trait]
pub trait Runnable<Input: Send + Sync + 'static, Output: Send + Sync + 'static>: Send + Sync {
type Error: std::error::Error + Send + Sync + 'static;
async fn invoke(&self, input: Input, config: Option<RunnableConfig>) -> Result<Output, Self::Error>;
async fn batch(
&self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Result<Vec<Output>, Self::Error> {
let mut results = Vec::with_capacity(inputs.len());
for input in inputs {
let result = self.invoke(input, config.clone()).await?;
results.push(result);
}
Ok(results)
}
async fn stream(
&self,
_input: Input,
_config: Option<RunnableConfig>,
) -> Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error> {
unimplemented!("此 Runnable 不支持流式处理")
}
}