use std::future::Future;
use std::pin::Pin;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Block {
tag: String,
content: String,
}
impl Block {
#[must_use]
pub fn new(tag: impl Into<String>, content: impl Into<String>) -> Self {
Self {
tag: tag.into(),
content: content.into(),
}
}
#[must_use]
pub fn tag(&self) -> &str {
&self.tag
}
#[must_use]
pub fn content(&self) -> &str {
&self.content
}
#[must_use]
pub const fn len(&self) -> usize {
self.content.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.content.is_empty()
}
}
pub trait ContextSource: Send + Sync + 'static {
fn render(&self) -> impl Future<Output = Option<Block>> + Send;
fn commit(&mut self, block: &Block) -> impl Future<Output = ()> + Send {
let _ = block;
async {}
}
fn on_tool_turn_end(&mut self) -> impl Future<Output = Option<Block>> + Send {
async { None }
}
fn on_final_turn(&mut self) -> impl Future<Output = Option<Block>> + Send {
async { None }
}
}
trait ContextSourceImpl: Send + Sync {
fn render<'a>(&'a self) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>>;
fn commit<'a>(&'a mut self, block: &'a Block) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
fn on_tool_turn_end<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>>;
fn on_final_turn<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>>;
}
impl<T: ContextSource> ContextSourceImpl for T {
fn render<'a>(&'a self) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>> {
Box::pin(ContextSource::render(self))
}
fn commit<'a>(&'a mut self, block: &'a Block) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
Box::pin(ContextSource::commit(self, block))
}
fn on_tool_turn_end<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>> {
Box::pin(ContextSource::on_tool_turn_end(self))
}
fn on_final_turn<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Option<Block>> + Send + 'a>> {
Box::pin(ContextSource::on_final_turn(self))
}
}
pub struct AnyContextSource {
inner: Box<dyn ContextSourceImpl>,
}
impl std::fmt::Debug for AnyContextSource {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.debug_struct("AnyContextSource").finish()
}
}
impl AnyContextSource {
#[must_use]
pub fn new<T: ContextSource>(source: T) -> Self {
Self {
inner: Box::new(source),
}
}
pub fn render(&self) -> impl Future<Output = Option<Block>> + Send + '_ {
self.inner.render()
}
pub fn commit<'a>(&'a mut self, block: &'a Block) -> impl Future<Output = ()> + Send + 'a {
self.inner.commit(block)
}
pub fn on_tool_turn_end(&mut self) -> impl Future<Output = Option<Block>> + Send + '_ {
self.inner.on_tool_turn_end()
}
pub fn on_final_turn(&mut self) -> impl Future<Output = Option<Block>> + Send + '_ {
self.inner.on_final_turn()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Default)]
struct Counter {
committed: Vec<String>,
}
impl ContextSource for Counter {
fn render(&self) -> impl Future<Output = Option<Block>> + Send {
std::future::ready(Some(Block::new(
"counter",
self.committed.len().to_string(),
)))
}
async fn commit(&mut self, block: &Block) {
self.committed.push(block.content().to_string());
}
fn on_tool_turn_end(&mut self) -> impl Future<Output = Option<Block>> + Send {
std::future::ready(Some(Block::new("counter_alert", "tool turn observed")))
}
}
#[tokio::test]
async fn erased_source_forwards_every_method() {
let mut source = AnyContextSource::new(Counter::default());
let block = source.render().await.expect("counter renders");
assert_eq!(block.content(), "0");
source.commit(&block).await;
let next = source.render().await.expect("counter renders");
assert_eq!(next.content(), "1");
assert_eq!(
source.on_tool_turn_end().await.map(|b| b.tag().to_string()),
Some("counter_alert".to_string())
);
assert!(source.on_final_turn().await.is_none());
}
}