use crate::state_store::{
PruneConfig, PruneThrottle, Session, SessionId, SessionSummary, StateStore,
};
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
pub struct StateApi {
pub(crate) state_store: Arc<StateStore>,
pub prune_throttle: PruneThrottle,
}
impl StateApi {
pub fn new(state_store: Arc<StateStore>) -> Self {
Self {
state_store,
prune_throttle: PruneThrottle::new(3600), }
}
pub async fn save<T: Serialize>(
&self,
category: &str,
name: &str,
data: &T,
) -> anyhow::Result<()> {
self.state_store.save_json(category, name, data).await
}
pub async fn save_markdown(
&self,
category: &str,
name: &str,
content: &str,
) -> anyhow::Result<()> {
self.state_store
.save_markdown(category, name, content)
.await
}
pub async fn load<T: DeserializeOwned>(
&self,
category: &str,
name: &str,
) -> anyhow::Result<Option<T>> {
self.state_store.load_json(category, name).await
}
pub async fn load_markdown(
&self,
category: &str,
name: &str,
) -> anyhow::Result<Option<String>> {
self.state_store.load_markdown(category, name).await
}
pub async fn delete(&self, category: &str, name: &str) -> anyhow::Result<bool> {
self.state_store.delete_file(category, name).await
}
pub async fn list_category(&self, category: &str) -> anyhow::Result<Vec<String>> {
self.state_store.list_category(category).await
}
pub fn commit_all(
&self,
git: &crate::git_layer::GitLayer,
message: &str,
) -> anyhow::Result<Option<crate::git_layer::CommitInfo>> {
if !git.is_enabled() {
return Ok(None);
}
git.commit_file(".", message)
.ok()
.map_or(Ok(None), |info| Ok(Some(info)))
}
pub async fn save_session(&self, session: &Session) -> anyhow::Result<()> {
self.state_store.save_session(session).await
}
pub async fn load_session(&self, id: &SessionId) -> anyhow::Result<Option<Session>> {
self.state_store.load_session(id).await
}
pub async fn list_sessions(&self) -> anyhow::Result<Vec<SessionSummary>> {
self.state_store.list_sessions().await
}
pub async fn delete_session(&self, id: &SessionId) -> anyhow::Result<bool> {
self.state_store.delete_session(id).await
}
pub fn workspace_path(&self) -> &std::path::Path {
&self.state_store.base_path
}
pub fn store(&self) -> &Arc<StateStore> {
&self.state_store
}
pub async fn prune_sessions(&self, config: &PruneConfig) -> anyhow::Result<usize> {
self.state_store.prune_sessions(config).await
}
pub fn should_auto_prune(&self) -> bool {
self.prune_throttle.should_prune()
}
}