oxios_kernel/kernel_handle/
state_api.rs1use crate::state_store::{
4 PruneConfig, PruneThrottle, Session, SessionId, SessionSummary, StateStore,
5};
6use serde::{de::DeserializeOwned, Serialize};
7use std::sync::Arc;
8
9pub struct StateApi {
13 pub(crate) state_store: Arc<StateStore>,
14 pub prune_throttle: PruneThrottle,
16}
17
18impl StateApi {
19 pub fn new(state_store: Arc<StateStore>) -> Self {
21 Self {
22 state_store,
23 prune_throttle: PruneThrottle::new(3600), }
25 }
26 pub async fn save<T: Serialize>(
28 &self,
29 category: &str,
30 name: &str,
31 data: &T,
32 ) -> anyhow::Result<()> {
33 self.state_store.save_json(category, name, data).await
34 }
35
36 pub async fn save_markdown(
38 &self,
39 category: &str,
40 name: &str,
41 content: &str,
42 ) -> anyhow::Result<()> {
43 self.state_store
44 .save_markdown(category, name, content)
45 .await
46 }
47
48 pub async fn load<T: DeserializeOwned>(
50 &self,
51 category: &str,
52 name: &str,
53 ) -> anyhow::Result<Option<T>> {
54 self.state_store.load_json(category, name).await
55 }
56
57 pub async fn load_markdown(
59 &self,
60 category: &str,
61 name: &str,
62 ) -> anyhow::Result<Option<String>> {
63 self.state_store.load_markdown(category, name).await
64 }
65
66 pub async fn delete(&self, category: &str, name: &str) -> anyhow::Result<bool> {
68 self.state_store.delete_file(category, name).await
69 }
70
71 pub async fn list_category(&self, category: &str) -> anyhow::Result<Vec<String>> {
73 self.state_store.list_category(category).await
74 }
75
76 pub fn commit_all(
78 &self,
79 git: &crate::git_layer::GitLayer,
80 message: &str,
81 ) -> anyhow::Result<Option<crate::git_layer::CommitInfo>> {
82 if !git.is_enabled() {
83 return Ok(None);
84 }
85 git.commit_file(".", message)
86 .ok()
87 .map_or(Ok(None), |info| Ok(Some(info)))
88 }
89
90 pub async fn save_session(&self, session: &Session) -> anyhow::Result<()> {
92 self.state_store.save_session(session).await
93 }
94
95 pub async fn load_session(&self, id: &SessionId) -> anyhow::Result<Option<Session>> {
97 self.state_store.load_session(id).await
98 }
99
100 pub async fn list_sessions(&self) -> anyhow::Result<Vec<SessionSummary>> {
102 self.state_store.list_sessions().await
103 }
104
105 pub async fn delete_session(&self, id: &SessionId) -> anyhow::Result<bool> {
107 self.state_store.delete_session(id).await
108 }
109
110 pub fn workspace_path(&self) -> &std::path::Path {
112 &self.state_store.base_path
113 }
114
115 pub fn store(&self) -> &Arc<StateStore> {
117 &self.state_store
118 }
119
120 pub async fn prune_sessions(&self, config: &PruneConfig) -> anyhow::Result<usize> {
124 self.state_store.prune_sessions(config).await
125 }
126
127 pub fn should_auto_prune(&self) -> bool {
129 self.prune_throttle.should_prune()
130 }
131}