1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::Result;
use crate::episode::Episode;
use crate::pattern::Pattern;
use crate::storage::StorageBackend;
use std::sync::Arc;
use uuid::Uuid;
use super::SelfLearningMemory;
use super::queries;
impl SelfLearningMemory {
/// Check if Turso storage is configured
#[must_use]
pub fn has_turso_storage(&self) -> bool {
queries::has_turso_storage(&self.turso_storage)
}
/// Check if cache storage is configured
#[must_use]
pub fn has_cache_storage(&self) -> bool {
queries::has_cache_storage(&self.cache_storage)
}
/// Get a reference to Turso storage backend (if configured)
#[must_use]
pub fn turso_storage(&self) -> Option<&Arc<dyn StorageBackend>> {
queries::turso_storage(&self.turso_storage)
}
/// Get a reference to cache storage backend (if configured)
#[must_use]
pub fn cache_storage(&self) -> Option<&Arc<dyn StorageBackend>> {
queries::cache_storage(&self.cache_storage)
}
/// Get all episodes with proper lazy loading from storage backends.
pub async fn get_all_episodes(&self) -> Result<Vec<Episode>> {
queries::get_all_episodes(
&self.episodes_fallback,
self.cache_storage.as_ref(),
self.turso_storage.as_ref(),
)
.await
}
/// Get all patterns with proper lazy loading from storage backends.
pub async fn get_all_patterns(&self) -> Result<Vec<Pattern>> {
queries::get_all_patterns(&self.patterns_fallback).await
}
/// List episodes with optional filtering, using proper lazy loading.
pub async fn list_episodes(
&self,
limit: Option<usize>,
offset: Option<usize>,
completed_only: Option<bool>,
) -> Result<Vec<Episode>> {
queries::list_episodes(
&self.episodes_fallback,
self.cache_storage.as_ref(),
self.turso_storage.as_ref(),
limit,
offset,
completed_only,
)
.await
}
/// List episodes with advanced filtering support.
///
/// Provides rich filtering capabilities including tags, date ranges,
/// task types, outcomes, and more. Use `EpisodeFilter::builder()` for a fluent API.
///
/// # Arguments
///
/// * `filter` - Episode filter criteria
/// * `limit` - Maximum number of episodes to return
/// * `offset` - Number of episodes to skip (for pagination)
///
/// # Examples
///
/// ```
/// use do_memory_core::{SelfLearningMemory, EpisodeFilter, TaskType};
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
///
/// // Get successful episodes with specific tags
/// let filter = EpisodeFilter::builder()
/// .with_any_tags(vec!["async".to_string()])
/// .success_only(true)
/// .build();
///
/// let episodes = memory.list_episodes_filtered(filter, Some(10), None).await?;
/// # Ok(())
/// # }
/// ```
pub async fn list_episodes_filtered(
&self,
filter: super::filters::EpisodeFilter,
limit: Option<usize>,
offset: Option<usize>,
) -> Result<Vec<Episode>> {
queries::list_episodes_filtered(
&self.episodes_fallback,
self.cache_storage.as_ref(),
self.turso_storage.as_ref(),
filter,
limit,
offset,
)
.await
}
/// Get patterns extracted from a specific episode
#[allow(clippy::unused_async)]
pub async fn get_episode_patterns(&self, episode_id: Uuid) -> Result<Vec<Pattern>> {
queries::get_episode_patterns(episode_id, &self.patterns_fallback).await
}
/// Get multiple episodes by their IDs in a single operation.
///
/// This is more efficient than calling `get_episode()` multiple times,
/// as it can batch storage queries and reduce round trips.
///
/// Uses the standard lazy-loading pattern for each episode and attempts
/// to fetch missing episodes from storage backends in batches where possible.
///
/// # Arguments
///
/// * `episode_ids` - Collection of episode UUIDs to retrieve
///
/// # Returns
///
/// Vector of episodes that were found. Episodes that don't exist are silently
/// omitted (no error is raised for missing episodes).
///
/// # Examples
///
/// ```
/// use do_memory_core::SelfLearningMemory;
/// use uuid::Uuid;
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
///
/// let ids = vec![Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4()];
/// let episodes = memory.get_episodes_by_ids(&ids).await?;
///
/// println!("Found {} out of {} episodes", episodes.len(), ids.len());
/// # Ok(())
/// # }
/// ```
pub async fn get_episodes_by_ids(&self, episode_ids: &[Uuid]) -> Result<Vec<Episode>> {
queries::get_episodes_by_ids(
episode_ids,
&self.episodes_fallback,
self.cache_storage.as_ref(),
self.turso_storage.as_ref(),
)
.await
}
/// List episodes by tags
///
/// Returns episodes that match the specified tags.
///
/// # Arguments
///
/// * `tags` - Tags to search for
/// * `match_all` - If true, episodes must have ALL tags; if false, ANY tag
/// * `limit` - Maximum number of episodes to return
///
/// # Examples
///
/// ```
/// use do_memory_core::SelfLearningMemory;
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
///
/// // Find episodes with "bug-fix" OR "critical" tag
/// let episodes = memory.list_episodes_by_tags(
/// vec!["bug-fix".to_string(), "critical".to_string()],
/// false, // match ANY
/// Some(10),
/// ).await?;
///
/// // Find episodes with BOTH "feature" AND "async" tags
/// let episodes = memory.list_episodes_by_tags(
/// vec!["feature".to_string(), "async".to_string()],
/// true, // match ALL
/// None,
/// ).await?;
/// # Ok(())
/// # }
/// ```
pub async fn list_episodes_by_tags(
&self,
tags: Vec<String>,
match_all: bool,
limit: Option<usize>,
) -> Result<Vec<Episode>> {
queries::list_episodes_by_tags(self, tags, match_all, limit).await
}
/// Get all unique tags used across all episodes
///
/// Returns a sorted list of all tags.
///
/// # Examples
///
/// ```
/// use do_memory_core::SelfLearningMemory;
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
/// let tags = memory.get_all_tags().await?;
/// println!("Available tags: {:?}", tags);
/// # Ok(())
/// # }
/// ```
pub async fn get_all_tags(&self) -> Result<Vec<String>> {
queries::get_all_tags(self).await
}
/// Get tag statistics
///
/// Returns usage counts and timestamps for all tags.
///
/// # Examples
///
/// ```
/// use do_memory_core::SelfLearningMemory;
///
/// # async fn example() -> anyhow::Result<()> {
/// let memory = SelfLearningMemory::new();
/// let stats = memory.get_tag_statistics().await?;
/// for (tag, stat) in stats {
/// println!("{}: used {} times", tag, stat.usage_count);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_tag_statistics(
&self,
) -> Result<std::collections::HashMap<String, queries::TagStats>> {
queries::get_tag_statistics(self).await
}
}