1use std::collections::{HashMap, HashSet};
11use std::sync::{Arc, Mutex};
12
13use async_trait::async_trait;
14use rap_protocol::ThreadId;
15use serde::{Deserialize, Serialize};
16
17use crate::message::InfinityMessage;
18use crate::system::UserChoice;
19use crate::traits::{ConversationStore, StateStore};
20
21#[derive(Debug)]
24pub struct InMemoryStoreError(pub String);
25
26impl std::fmt::Display for InMemoryStoreError {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 write!(f, "{}", self.0)
29 }
30}
31
32impl std::error::Error for InMemoryStoreError {}
33
34#[derive(Clone, Debug, Serialize, Deserialize)]
42pub struct ThreadInfo {
43 pub parent_thread_id: Option<ThreadId>,
44 pub root_thread_id: ThreadId,
45 pub spawn_message_order: Option<i64>,
47 pub spawn_tool_call_id: Option<String>,
48 pub closed: bool,
49 pub is_subscription_event: bool,
50 #[serde(default)]
51 pub is_compaction: bool,
52}
53
54#[derive(Clone, Debug, Serialize, Deserialize)]
57pub struct CompactionSummary {
58 pub summary: String,
59 pub up_to_order: i64,
60}
61
62#[derive(Clone, Default)]
65pub struct InMemoryConversationStore {
66 #[expect(clippy::type_complexity, reason = "shared state")]
67 messages: Arc<Mutex<HashMap<ThreadId, Vec<(InfinityMessage, String)>>>>,
68 threads: Arc<Mutex<HashMap<ThreadId, ThreadInfo>>>,
69 compaction_summaries: Arc<Mutex<HashMap<ThreadId, Vec<CompactionSummary>>>>,
70}
71
72impl InMemoryConversationStore {
73 pub fn new() -> Self {
74 Self::default()
75 }
76
77 pub fn thread_info(&self, thread_id: &ThreadId<str>) -> Option<ThreadInfo> {
87 self.threads
88 .lock()
89 .expect("bug: mutex poisoned")
90 .get(thread_id)
91 .cloned()
92 }
93
94 pub fn set_thread_info(&self, thread_id: &ThreadId<str>, info: ThreadInfo) {
96 self.threads
97 .lock()
98 .expect("bug: mutex poisoned")
99 .insert(thread_id.to_owned(), info);
100 }
101
102 pub fn thread_messages(
105 &self,
106 thread_id: &ThreadId<str>,
107 ) -> Option<Vec<(InfinityMessage, String)>> {
108 self.messages
109 .lock()
110 .expect("bug: mutex poisoned")
111 .get(thread_id)
112 .cloned()
113 }
114
115 pub fn set_thread_messages(
118 &self,
119 thread_id: &ThreadId<str>,
120 messages: Vec<(InfinityMessage, String)>,
121 ) -> bool {
122 self.messages
123 .lock()
124 .expect("bug: mutex poisoned")
125 .insert(thread_id.to_owned(), messages)
126 .is_some()
127 }
128
129 pub fn thread_compaction_summaries(&self, thread_id: &ThreadId<str>) -> Vec<CompactionSummary> {
131 self.compaction_summaries
132 .lock()
133 .expect("bug: mutex poisoned")
134 .get(thread_id)
135 .cloned()
136 .unwrap_or_default()
137 }
138
139 pub fn set_thread_compaction_summaries(
142 &self,
143 thread_id: &ThreadId<str>,
144 summaries: Vec<CompactionSummary>,
145 ) -> bool {
146 self.compaction_summaries
147 .lock()
148 .expect("bug: mutex poisoned")
149 .insert(thread_id.to_owned(), summaries)
150 .is_some()
151 }
152
153 pub fn spawn_thread_with_id(
159 &self,
160 new_thread_id: &ThreadId<str>,
161 parent_thread_id: &ThreadId<str>,
162 spawn_tool_call_id: &str,
163 is_for_subscription_event: bool,
164 spawn_order_override: Option<usize>,
165 ) {
166 let msgs = self.messages.lock().expect("bug: mutex poisoned");
167 let spawn_message_order = spawn_order_override
168 .unwrap_or_else(|| msgs.get(parent_thread_id).map(|v| v.len()).unwrap_or(0))
169 as i64;
170 drop(msgs);
171
172 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
173 let root = threads
174 .get(parent_thread_id)
175 .map(|t| t.root_thread_id.clone())
176 .unwrap_or_else(|| parent_thread_id.to_owned());
177 threads.insert(
178 new_thread_id.to_owned(),
179 ThreadInfo {
180 parent_thread_id: Some(parent_thread_id.to_owned()),
181 root_thread_id: root,
182 spawn_message_order: Some(spawn_message_order),
183 spawn_tool_call_id: Some(spawn_tool_call_id.to_owned()),
184 closed: false,
185 is_subscription_event: is_for_subscription_event,
186 is_compaction: false,
187 },
188 );
189 }
190}
191
192#[async_trait]
193impl ConversationStore for InMemoryConversationStore {
194 type Error = InMemoryStoreError;
195
196 async fn ensure_root_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error> {
197 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
198 threads
199 .entry(thread_id.to_owned())
200 .or_insert_with(|| ThreadInfo {
201 parent_thread_id: None,
202 root_thread_id: thread_id.to_owned(),
203 spawn_message_order: None,
204 spawn_tool_call_id: None,
205 closed: false,
206 is_subscription_event: false,
207 is_compaction: false,
208 });
209 Ok(())
210 }
211
212 async fn thread_exists(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
213 Ok(self
214 .threads
215 .lock()
216 .expect("bug: mutex poisoned")
217 .contains_key(thread_id))
218 }
219
220 async fn load_history_up_to(
221 &self,
222 session_id: &ThreadId<str>,
223 start_from: Option<i64>,
224 up_to: Option<i64>,
225 ) -> Result<Vec<InfinityMessage>, Self::Error> {
226 let msgs = self.messages.lock().expect("bug: mutex poisoned");
227 Ok(msgs
228 .get(session_id)
229 .map(|v| {
230 let start = start_from.unwrap_or(0) as usize;
231 let end = up_to.map(|u| u as usize).unwrap_or(v.len()).min(v.len());
232 let start = start.min(end);
233 v[start..end].iter().map(|(m, _)| m.clone()).collect()
234 })
235 .unwrap_or_default())
236 }
237
238 async fn append_messages(
239 &self,
240 session_id: &ThreadId<str>,
241 messages: Vec<(InfinityMessage, String)>,
242 ) -> Result<(), Self::Error> {
243 let mut store = self.messages.lock().expect("bug: mutex poisoned");
244 store
245 .entry(session_id.to_owned())
246 .or_default()
247 .extend(messages);
248 Ok(())
249 }
250
251 async fn spawn_thread(
252 &self,
253 parent_thread_id: &ThreadId<str>,
254 spawn_tool_call_id: &str,
255 is_for_subscription_event: bool,
256 spawn_order_override: Option<usize>,
257 ) -> Result<ThreadId, Self::Error> {
258 let new_id = ThreadId::from(uuid::Uuid::new_v4().to_string());
259 self.spawn_thread_with_id(
260 &new_id,
261 parent_thread_id,
262 spawn_tool_call_id,
263 is_for_subscription_event,
264 spawn_order_override,
265 );
266 Ok(new_id)
267 }
268
269 async fn is_thread_closed(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
270 let threads = self.threads.lock().expect("bug: mutex poisoned");
271 Ok(threads.get(thread_id).map(|t| t.closed).unwrap_or(false))
272 }
273
274 async fn close_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error> {
275 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
276 if let Some(t) = threads.get_mut(thread_id) {
277 t.closed = true;
278 }
279 Ok(())
280 }
281
282 async fn is_subscription_event_thread(
283 &self,
284 thread_id: &ThreadId<str>,
285 ) -> Result<bool, Self::Error> {
286 let threads = self.threads.lock().expect("bug: mutex poisoned");
287 Ok(threads
288 .get(thread_id)
289 .map(|t| t.is_subscription_event)
290 .unwrap_or(false))
291 }
292
293 async fn get_thread_parent_info(
294 &self,
295 thread_id: &ThreadId<str>,
296 ) -> Result<Option<(ThreadId, String)>, Self::Error> {
297 let threads = self.threads.lock().expect("bug: mutex poisoned");
298 Ok(threads.get(thread_id).and_then(|t| {
299 match (&t.parent_thread_id, &t.spawn_tool_call_id) {
300 (Some(p), Some(tc)) => Some((p.clone(), tc.clone())),
301 _ => None,
302 }
303 }))
304 }
305
306 async fn get_ancestor_chain(
307 &self,
308 thread_id: &ThreadId<str>,
309 ) -> Result<Vec<(ThreadId, i64)>, Self::Error> {
310 let threads = self.threads.lock().expect("bug: mutex poisoned");
311 let mut result = Vec::new();
312 let mut current = thread_id.to_owned();
313 while let Some(info) = threads.get(¤t) {
314 let Some(parent) = info.parent_thread_id.clone() else {
315 break;
316 };
317 let order = info.spawn_message_order.unwrap_or(0);
318 result.push((parent.clone(), order));
319 current = parent;
320 }
321 result.reverse();
322 Ok(result)
323 }
324
325 async fn mark_thread_as_compaction(
326 &self,
327 thread_id: &ThreadId<str>,
328 ) -> Result<(), Self::Error> {
329 let mut threads = self.threads.lock().expect("bug: mutex poisoned");
330 if let Some(t) = threads.get_mut(thread_id) {
331 t.is_compaction = true;
332 }
333 Ok(())
334 }
335
336 async fn is_compaction_thread(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
337 let threads = self.threads.lock().expect("bug: mutex poisoned");
338 Ok(threads
339 .get(thread_id)
340 .map(|t| t.is_compaction)
341 .unwrap_or(false))
342 }
343
344 async fn get_thread_spawn_order(
345 &self,
346 thread_id: &ThreadId<str>,
347 ) -> Result<Option<i64>, Self::Error> {
348 let threads = self.threads.lock().expect("bug: mutex poisoned");
349 Ok(threads.get(thread_id).and_then(|t| t.spawn_message_order))
350 }
351
352 async fn save_compaction_summary(
353 &self,
354 thread_id: &ThreadId<str>,
355 summary: &str,
356 up_to_order: i64,
357 ) -> Result<(), Self::Error> {
358 let mut cs = self
359 .compaction_summaries
360 .lock()
361 .expect("bug: mutex poisoned");
362 cs.entry(thread_id.to_owned())
363 .or_default()
364 .push(CompactionSummary {
365 summary: summary.to_owned(),
366 up_to_order,
367 });
368 Ok(())
369 }
370
371 async fn load_latest_compaction_summary_up_to(
372 &self,
373 thread_id: &ThreadId<str>,
374 up_to_order: Option<i64>,
375 ) -> Result<Option<(String, i64)>, Self::Error> {
376 let cs = self
377 .compaction_summaries
378 .lock()
379 .expect("bug: mutex poisoned");
380 Ok(cs.get(thread_id).and_then(|v| {
381 v.iter()
382 .rev()
383 .find(|s| up_to_order.is_none_or(|n| s.up_to_order <= n))
384 .map(|s| (s.summary.clone(), s.up_to_order))
385 }))
386 }
387}
388
389#[derive(Debug, Default, Clone, Serialize, Deserialize)]
392pub struct ThreadState {
393 #[serde(default)]
394 pub processed_message_ids: HashSet<String>,
395 #[serde(default)]
396 pub metadata: Option<serde_json::Value>,
397 #[serde(default)]
398 pub subscriptions: HashSet<String>,
399 #[serde(default)]
400 pub pending_user_choices: Vec<UserChoice>,
401}
402
403#[derive(Clone, Default)]
406pub struct InMemoryStateStore {
407 processed_ids: Arc<Mutex<HashMap<ThreadId, HashSet<String>>>>,
408 metadata: Arc<Mutex<HashMap<ThreadId, serde_json::Value>>>,
409 subscriptions: Arc<Mutex<HashMap<ThreadId, HashSet<String>>>>,
410 pending_user_choices: Arc<Mutex<HashMap<ThreadId, Vec<UserChoice>>>>,
411}
412
413impl InMemoryStateStore {
414 pub fn new() -> Self {
415 Self::default()
416 }
417
418 pub fn thread_state(&self, thread_id: &ThreadId<str>) -> ThreadState {
420 let processed = self.processed_ids.lock().expect("bug: mutex poisoned");
421 let metadata = self.metadata.lock().expect("bug: mutex poisoned");
422 let subscriptions = self.subscriptions.lock().expect("bug: mutex poisoned");
423 let pending_user_choices = self
424 .pending_user_choices
425 .lock()
426 .expect("bug: mutex poisoned");
427 ThreadState {
428 processed_message_ids: processed.get(thread_id).cloned().unwrap_or_default(),
429 metadata: metadata.get(thread_id).cloned(),
430 subscriptions: subscriptions.get(thread_id).cloned().unwrap_or_default(),
431 pending_user_choices: pending_user_choices
432 .get(thread_id)
433 .cloned()
434 .unwrap_or_default(),
435 }
436 }
437
438 pub fn set_thread_state(&self, thread_id: &ThreadId<str>, state: ThreadState) {
440 self.processed_ids
441 .lock()
442 .expect("bug: mutex poisoned")
443 .insert(thread_id.to_owned(), state.processed_message_ids);
444 if let Some(meta) = state.metadata {
445 self.metadata
446 .lock()
447 .expect("bug: mutex poisoned")
448 .insert(thread_id.to_owned(), meta);
449 }
450 if !state.subscriptions.is_empty() {
451 self.subscriptions
452 .lock()
453 .expect("bug: mutex poisoned")
454 .insert(thread_id.to_owned(), state.subscriptions);
455 }
456 if !state.pending_user_choices.is_empty() {
457 self.pending_user_choices
458 .lock()
459 .expect("bug: mutex poisoned")
460 .insert(thread_id.to_owned(), state.pending_user_choices);
461 }
462 }
463}
464
465#[async_trait]
466impl StateStore for InMemoryStateStore {
467 type Error = InMemoryStoreError;
468
469 async fn get_processed_ids(
470 &self,
471 thread_id: &ThreadId<str>,
472 ) -> Result<HashSet<String>, Self::Error> {
473 let store = self.processed_ids.lock().expect("bug: mutex poisoned");
474 Ok(store.get(thread_id).cloned().unwrap_or_default())
475 }
476
477 async fn add_processed_message_ids(
478 &self,
479 thread_id: &ThreadId<str>,
480 message_ids: Vec<String>,
481 ) -> Result<(), Self::Error> {
482 let mut store = self.processed_ids.lock().expect("bug: mutex poisoned");
483 store
484 .entry(thread_id.to_owned())
485 .or_default()
486 .extend(message_ids);
487 Ok(())
488 }
489
490 async fn get_metadata(
491 &self,
492 root_thread_id: &ThreadId<str>,
493 ) -> Result<Option<serde_json::Value>, Self::Error> {
494 let store = self.metadata.lock().expect("bug: mutex poisoned");
495 Ok(store.get(root_thread_id).cloned())
496 }
497
498 async fn set_metadata(
499 &self,
500 root_thread_id: &ThreadId<str>,
501 metadata: serde_json::Value,
502 ) -> Result<(), Self::Error> {
503 let mut store = self.metadata.lock().expect("bug: mutex poisoned");
504 store.insert(root_thread_id.to_owned(), metadata);
505 Ok(())
506 }
507
508 async fn get_active_subscriptions(
509 &self,
510 thread_id: &ThreadId<str>,
511 ) -> Result<Vec<String>, Self::Error> {
512 let store = self.subscriptions.lock().expect("bug: mutex poisoned");
513 Ok(store
514 .get(thread_id)
515 .map(|s| s.iter().cloned().collect())
516 .unwrap_or_default())
517 }
518
519 async fn add_active_subscription(
520 &self,
521 thread_id: &ThreadId<str>,
522 tool_call_id: &str,
523 ) -> Result<(), Self::Error> {
524 let mut store = self.subscriptions.lock().expect("bug: mutex poisoned");
525 store
526 .entry(thread_id.to_owned())
527 .or_default()
528 .insert(tool_call_id.to_owned());
529 Ok(())
530 }
531
532 async fn remove_active_subscription(
533 &self,
534 thread_id: &ThreadId<str>,
535 tool_call_id: &str,
536 ) -> Result<(), Self::Error> {
537 let mut store = self.subscriptions.lock().expect("bug: mutex poisoned");
538 if let Some(s) = store.get_mut(thread_id) {
539 s.remove(tool_call_id);
540 }
541 Ok(())
542 }
543
544 async fn add_pending_user_choice(
545 &self,
546 thread_id: &ThreadId<str>,
547 choice: UserChoice,
548 ) -> Result<(), Self::Error> {
549 let mut store = self
550 .pending_user_choices
551 .lock()
552 .expect("bug: mutex poisoned");
553 let choices = store.entry(thread_id.to_owned()).or_default();
554 if let Some(existing) = choices.iter_mut().find(|existing| existing.id == choice.id) {
555 *existing = choice;
556 } else {
557 choices.push(choice);
558 }
559 Ok(())
560 }
561
562 async fn remove_pending_user_choice(
563 &self,
564 thread_id: &ThreadId<str>,
565 choice_id: &str,
566 ) -> Result<(), Self::Error> {
567 if let Some(choices) = self
568 .pending_user_choices
569 .lock()
570 .expect("bug: mutex poisoned")
571 .get_mut(thread_id)
572 {
573 choices.retain(|choice| choice.id != choice_id);
574 }
575 Ok(())
576 }
577
578 async fn get_pending_user_choices(
579 &self,
580 thread_id: &ThreadId<str>,
581 ) -> Result<Vec<UserChoice>, Self::Error> {
582 Ok(self
583 .pending_user_choices
584 .lock()
585 .expect("bug: mutex poisoned")
586 .get(thread_id)
587 .cloned()
588 .unwrap_or_default())
589 }
590}