avocado_core/storage/
traits.rs1use async_trait::async_trait;
4use std::sync::Arc;
5
6use crate::storage::vector::VectorSearchProvider;
7use crate::types::{
8 Agent, AgentRelation, AgentRelationSummary, Artifact, CompilerConfig, IngestAction, Message,
9 MessageRole, Result, Session, SessionWithMessages, SessionWorkingSet, Span, Stance, WorkingSet,
10};
11
12#[derive(Debug, Clone)]
14pub enum StorageConfig {
15 Sqlite {
17 path: String,
19 },
20 Postgres {
22 connection_string: String,
24 },
25}
26
27impl StorageConfig {
28 pub fn from_env(default_sqlite_path: &str) -> Self {
36 match std::env::var("AVOCADO_BACKEND").ok() {
37 None => StorageConfig::Sqlite {
38 path: default_sqlite_path.to_string(),
39 },
40 Some(ref s) if s == "sqlite" || s.is_empty() => StorageConfig::Sqlite {
41 path: default_sqlite_path.to_string(),
42 },
43 Some(ref s) if s.starts_with("sqlite:") => StorageConfig::Sqlite {
44 path: s.strip_prefix("sqlite:").unwrap().to_string(),
45 },
46 Some(ref s) if s.starts_with("postgres://") || s.starts_with("postgresql://") => {
47 StorageConfig::Postgres {
48 connection_string: s.clone(),
49 }
50 }
51 Some(s) => {
52 eprintln!(
53 "[AvocadoDB] Unknown AVOCADO_BACKEND '{}', defaulting to SQLite",
54 s
55 );
56 StorageConfig::Sqlite {
57 path: default_sqlite_path.to_string(),
58 }
59 }
60 }
61 }
62}
63
64#[async_trait]
69pub trait StorageBackend: Send + Sync {
70 async fn get_stats(&self) -> Result<(usize, usize, usize)>;
77
78 async fn clear(&self) -> Result<()>;
80
81 async fn insert_artifact(&self, artifact: &Artifact) -> Result<()>;
85
86 async fn get_artifact(&self, artifact_id: &str) -> Result<Option<Artifact>>;
88
89 async fn get_artifact_by_path(&self, path: &str) -> Result<Option<Artifact>>;
91
92 async fn delete_artifact(&self, artifact_id: &str) -> Result<usize>;
97
98 async fn determine_ingest_action(
100 &self,
101 path: &str,
102 content_hash: &str,
103 ) -> Result<IngestAction>;
104
105 async fn insert_spans(&self, spans: &[Span]) -> Result<()>;
109
110 async fn get_all_spans(&self) -> Result<Vec<Span>>;
112
113 async fn search_spans(&self, query: &str, limit: usize) -> Result<Vec<Span>>;
115
116 async fn get_vector_search(&self) -> Result<Arc<dyn VectorSearchProvider>>;
123
124 async fn invalidate_vector_index(&self);
126
127 async fn create_session(
131 &self,
132 user_id: Option<&str>,
133 title: Option<&str>,
134 ) -> Result<Session>;
135
136 async fn get_session(&self, session_id: &str) -> Result<Option<Session>>;
138
139 async fn list_sessions(
141 &self,
142 user_id: Option<&str>,
143 limit: Option<usize>,
144 ) -> Result<Vec<Session>>;
145
146 async fn update_session(
148 &self,
149 session_id: &str,
150 title: Option<&str>,
151 metadata: Option<&serde_json::Value>,
152 ) -> Result<()>;
153
154 async fn delete_session(&self, session_id: &str) -> Result<()>;
156
157 async fn add_message(
161 &self,
162 session_id: &str,
163 role: MessageRole,
164 content: &str,
165 metadata: Option<&serde_json::Value>,
166 ) -> Result<Message>;
167
168 async fn get_messages(
170 &self,
171 session_id: &str,
172 limit: Option<usize>,
173 ) -> Result<Vec<Message>>;
174
175 async fn associate_working_set(
179 &self,
180 session_id: &str,
181 message_id: Option<&str>,
182 working_set: &WorkingSet,
183 query: &str,
184 config: &CompilerConfig,
185 ) -> Result<SessionWorkingSet>;
186
187 async fn get_session_full(&self, session_id: &str) -> Result<Option<SessionWithMessages>>;
189
190 async fn register_agent(&self, agent: &Agent) -> Result<Agent>;
194
195 async fn get_agent(&self, agent_id: &str) -> Result<Option<Agent>>;
197
198 async fn get_agent_by_name(&self, name: &str) -> Result<Option<Agent>>;
200
201 async fn list_agents(&self) -> Result<Vec<Agent>>;
203
204 async fn add_agent_relation(
208 &self,
209 session_id: &str,
210 message_id: &str,
211 from_agent_id: &str,
212 target_message_id: &str,
213 stance: Stance,
214 ) -> Result<AgentRelation>;
215
216 async fn get_agent_relations(&self, session_id: &str) -> Result<AgentRelationSummary>;
218
219 async fn get_session_agents(&self, session_id: &str) -> Result<Vec<Agent>>;
221}