Skip to main content

oxios_kernel/kernel_handle/
state_api.rs

1//! State API — data persistence, session management.
2
3use crate::state_store::{Session, SessionId, SessionSummary, StateStore};
4use serde::{de::DeserializeOwned, Serialize};
5use std::sync::Arc;
6
7/// State management system calls.
8///
9/// All data persistence: file (JSON/Markdown) storage, session management.
10pub struct StateApi {
11    pub(crate) state_store: Arc<StateStore>,
12}
13
14impl StateApi {
15    /// Create a new StateApi.
16    pub fn new(state_store: Arc<StateStore>) -> Self {
17        Self { state_store }
18    }
19    /// Save JSON data.
20    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    /// Save markdown content.
30    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    /// Load JSON data.
42    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    /// Load markdown content.
51    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    /// Delete a file.
60    pub async fn delete(&self, category: &str, name: &str) -> anyhow::Result<bool> {
61        self.state_store.delete_file(category, name).await
62    }
63
64    /// List files in a category.
65    pub async fn list_category(&self, category: &str) -> anyhow::Result<Vec<String>> {
66        self.state_store.list_category(category).await
67    }
68
69    /// Commit all changes to git via the provided GitLayer.
70    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    /// Save session.
84    pub async fn save_session(&self, session: &Session) -> anyhow::Result<()> {
85        self.state_store.save_session(session).await
86    }
87
88    /// Load session.
89    pub async fn load_session(&self, id: &SessionId) -> anyhow::Result<Option<Session>> {
90        self.state_store.load_session(id).await
91    }
92
93    /// List sessions.
94    pub async fn list_sessions(&self) -> anyhow::Result<Vec<SessionSummary>> {
95        self.state_store.list_sessions().await
96    }
97
98    /// Delete session.
99    pub async fn delete_session(&self, id: &SessionId) -> anyhow::Result<bool> {
100        self.state_store.delete_session(id).await
101    }
102
103    /// Get workspace base path.
104    pub fn workspace_path(&self) -> &std::path::Path {
105        &self.state_store.base_path
106    }
107
108    /// Access the underlying StateStore (for backup/restore).
109    pub fn store(&self) -> &Arc<StateStore> {
110        &self.state_store
111    }
112}