use std::sync::Arc;
use entelix_core::{ExecutionContext, Result};
use crate::namespace::Namespace;
use crate::store::Store;
const DEFAULT_KEY: &str = "summary";
pub struct SummaryMemory {
store: Arc<dyn Store<String>>,
namespace: Namespace,
}
impl SummaryMemory {
pub fn new(store: Arc<dyn Store<String>>, namespace: Namespace) -> Self {
Self { store, namespace }
}
pub const fn namespace(&self) -> &Namespace {
&self.namespace
}
pub async fn set(&self, ctx: &ExecutionContext, summary: impl Into<String>) -> Result<()> {
self.store
.put(ctx, &self.namespace, DEFAULT_KEY, summary.into())
.await
}
pub async fn append(&self, ctx: &ExecutionContext, addition: &str) -> Result<()> {
let merged = match self.store.get(ctx, &self.namespace, DEFAULT_KEY).await? {
Some(existing) if !existing.is_empty() => format!("{existing}\n\n{addition}"),
_ => addition.to_owned(),
};
self.store
.put(ctx, &self.namespace, DEFAULT_KEY, merged)
.await
}
pub async fn get(&self, ctx: &ExecutionContext) -> Result<Option<String>> {
self.store.get(ctx, &self.namespace, DEFAULT_KEY).await
}
pub async fn clear(&self, ctx: &ExecutionContext) -> Result<()> {
self.store.delete(ctx, &self.namespace, DEFAULT_KEY).await
}
}