oxios_kernel/kernel_handle/
state_api.rs1use crate::state_store::{Session, SessionId, SessionSummary, StateStore};
4use serde::{de::DeserializeOwned, Serialize};
5use std::sync::Arc;
6
7pub struct StateApi {
11 pub(crate) state_store: Arc<StateStore>,
12}
13
14impl StateApi {
15 pub fn new(state_store: Arc<StateStore>) -> Self {
17 Self { state_store }
18 }
19 pub async fn save<T: Serialize>(
21 &self,
22 category: &str,
23 name: &str,
24 data: &T,
25 ) -> anyhow::Result<()> {
26 self.state_store.save_json(category, name, data).await
27 }
28
29 pub async fn save_markdown(
31 &self,
32 category: &str,
33 name: &str,
34 content: &str,
35 ) -> anyhow::Result<()> {
36 self.state_store
37 .save_markdown(category, name, content)
38 .await
39 }
40
41 pub async fn load<T: DeserializeOwned>(
43 &self,
44 category: &str,
45 name: &str,
46 ) -> anyhow::Result<Option<T>> {
47 self.state_store.load_json(category, name).await
48 }
49
50 pub async fn load_markdown(
52 &self,
53 category: &str,
54 name: &str,
55 ) -> anyhow::Result<Option<String>> {
56 self.state_store.load_markdown(category, name).await
57 }
58
59 pub async fn delete(&self, category: &str, name: &str) -> anyhow::Result<bool> {
61 self.state_store.delete_file(category, name).await
62 }
63
64 pub async fn list_category(&self, category: &str) -> anyhow::Result<Vec<String>> {
66 self.state_store.list_category(category).await
67 }
68
69 pub fn commit_all(
71 &self,
72 git: &crate::git_layer::GitLayer,
73 message: &str,
74 ) -> anyhow::Result<Option<crate::git_layer::CommitInfo>> {
75 if !git.is_enabled() {
76 return Ok(None);
77 }
78 git.commit_file(".", message)
79 .ok()
80 .map_or(Ok(None), |info| Ok(Some(info)))
81 }
82
83 pub async fn save_session(&self, session: &Session) -> anyhow::Result<()> {
85 self.state_store.save_session(session).await
86 }
87
88 pub async fn load_session(&self, id: &SessionId) -> anyhow::Result<Option<Session>> {
90 self.state_store.load_session(id).await
91 }
92
93 pub async fn list_sessions(&self) -> anyhow::Result<Vec<SessionSummary>> {
95 self.state_store.list_sessions().await
96 }
97
98 pub async fn delete_session(&self, id: &SessionId) -> anyhow::Result<bool> {
100 self.state_store.delete_session(id).await
101 }
102
103 pub fn workspace_path(&self) -> &std::path::Path {
105 &self.state_store.base_path
106 }
107
108 pub fn store(&self) -> &Arc<StateStore> {
110 &self.state_store
111 }
112}