1use async_trait::async_trait;
2use infinity_provider_protocol::message::AssistantContent;
3
4use rap_protocol::{ChoiceId, ThreadId, ToolCallId};
5
6use crate::message::{InfinityMessage, InputMessage};
7use crate::system::UserChoice;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum SpawnContext {
13 InheritUpTo(usize),
19 Fresh,
23}
24
25#[async_trait]
27pub trait ConversationStore: Send + Sync + Clone {
28 type Error: std::error::Error + Send + Sync + 'static;
29
30 async fn ensure_root_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error>;
31
32 async fn thread_exists(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error>;
35
36 async fn load_history_up_to(
50 &self,
51 session_id: &ThreadId<str>,
52 start_from: Option<i64>,
53 up_to: Option<i64>,
54 ) -> Result<Vec<InfinityMessage>, Self::Error>;
55
56 async fn load_history_with_ancestors(
64 &self,
65 thread_id: &ThreadId<str>,
66 ) -> Result<(Vec<InfinityMessage>, Option<i64>, usize), Self::Error> {
67 if let Ok(Some((summary, compacted_up_to))) = self
69 .load_latest_compaction_summary_up_to(thread_id, None)
70 .await
71 {
72 let mut combined = vec![InfinityMessage::Assistant {
73 content: AssistantContent::text(format!(
74 "[Compacted conversation summary]\n{}",
75 summary
76 )),
77 }];
78 combined.extend(
79 self.load_history_up_to(thread_id, Some(compacted_up_to), None)
80 .await?,
81 );
82 return Ok((combined, Some(compacted_up_to), 0));
83 }
84
85 let mut ancestors = self.get_ancestor_chain(thread_id).await?;
86
87 if self.is_fresh_context_thread(thread_id).await? {
93 ancestors.clear();
94 } else {
95 for i in (0..ancestors.len()).rev() {
96 if self.is_fresh_context_thread(&ancestors[i].0).await? {
97 ancestors.drain(..i);
98 break;
99 }
100 }
101 }
102
103 let mut compaction_idx = None;
105 let mut compaction_summary = None;
106 for i in (0..ancestors.len()).rev() {
107 let (ref tid, cutoff) = ancestors[i];
108 if let Ok(Some((summary, compacted_up_to))) = self
109 .load_latest_compaction_summary_up_to(tid, Some(cutoff))
110 .await
111 {
112 compaction_idx = Some(i);
113 compaction_summary = Some((summary, compacted_up_to));
114 break;
115 }
116 }
117
118 let mut combined = Vec::new();
119
120 if let (Some(idx), Some((summary, compacted_up_to))) = (compaction_idx, compaction_summary)
121 {
122 combined.push(InfinityMessage::Assistant {
124 content: AssistantContent::text(format!(
125 "[Compacted conversation summary]\n{}",
126 summary
127 )),
128 });
129 let (_, cutoff) = &ancestors[idx];
131 combined.extend(
132 self.load_history_up_to(&ancestors[idx].0, Some(compacted_up_to), Some(*cutoff))
133 .await?,
134 );
135 for (tid, cutoff) in &ancestors[idx + 1..] {
137 combined.extend(self.load_history_up_to(tid, None, Some(*cutoff)).await?);
138 }
139 } else {
140 for (tid, cutoff) in &ancestors {
142 combined.extend(self.load_history_up_to(tid, None, Some(*cutoff)).await?);
143 }
144 }
145
146 let ancestor_prefix_len = combined.len();
147 combined.extend(self.load_history_up_to(thread_id, None, None).await?);
148 Ok((combined, None, ancestor_prefix_len))
149 }
150
151 async fn append_messages(
152 &self,
153 session_id: &ThreadId<str>,
154 messages: Vec<(InfinityMessage, String)>,
155 ) -> Result<(), Self::Error>;
156
157 async fn spawn_thread(
158 &self,
159 parent_thread_id: &ThreadId<str>,
160 spawn_tool_call_id: &ToolCallId<str>,
161 is_for_subscription_event: bool,
162 context: SpawnContext,
163 ) -> Result<ThreadId, Self::Error>;
164
165 async fn is_thread_closed(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error>;
166
167 async fn close_thread(&self, thread_id: &ThreadId<str>) -> Result<(), Self::Error>;
168
169 async fn is_subscription_event_thread(
170 &self,
171 thread_id: &ThreadId<str>,
172 ) -> Result<bool, Self::Error>;
173
174 async fn is_fresh_context_thread(&self, thread_id: &ThreadId<str>)
180 -> Result<bool, Self::Error>;
181
182 async fn get_thread_parent_info(
183 &self,
184 thread_id: &ThreadId<str>,
185 ) -> Result<Option<(ThreadId, ToolCallId)>, Self::Error>;
186
187 async fn get_ancestor_chain(
188 &self,
189 thread_id: &ThreadId<str>,
190 ) -> Result<Vec<(ThreadId, i64)>, Self::Error>;
191
192 async fn mark_thread_as_compaction(&self, thread_id: &ThreadId<str>)
195 -> Result<(), Self::Error>;
196
197 async fn is_compaction_thread(&self, thread_id: &ThreadId<str>) -> Result<bool, Self::Error>;
198
199 async fn get_thread_spawn_order(
200 &self,
201 thread_id: &ThreadId<str>,
202 ) -> Result<Option<i64>, Self::Error>;
203
204 async fn save_compaction_summary(
205 &self,
206 thread_id: &ThreadId<str>,
207 summary: &str,
208 up_to_order: i64,
209 ) -> Result<(), Self::Error>;
210
211 async fn load_latest_compaction_summary_up_to(
214 &self,
215 thread_id: &ThreadId<str>,
216 up_to_order: Option<i64>,
217 ) -> Result<Option<(String, i64)>, Self::Error>;
218}
219
220#[async_trait]
223pub trait StateStore: Send + Sync + Clone {
224 type Error: std::error::Error + Send + Sync + 'static;
225
226 async fn get_processed_ids(
230 &self,
231 thread_id: &ThreadId<str>,
232 ) -> Result<std::collections::HashSet<String>, Self::Error>;
233
234 async fn add_processed_message_ids(
235 &self,
236 thread_id: &ThreadId<str>,
237 message_ids: Vec<String>,
238 ) -> Result<(), Self::Error>;
239
240 async fn get_metadata(
241 &self,
242 root_thread_id: &ThreadId<str>,
243 ) -> Result<Option<serde_json::Value>, Self::Error>;
244
245 async fn set_metadata(
246 &self,
247 root_thread_id: &ThreadId<str>,
248 metadata: serde_json::Value,
249 ) -> Result<(), Self::Error>;
250
251 async fn get_active_subscriptions(
253 &self,
254 thread_id: &ThreadId<str>,
255 ) -> Result<Vec<ToolCallId>, Self::Error>;
256
257 async fn add_active_subscription(
259 &self,
260 thread_id: &ThreadId<str>,
261 tool_call_id: &ToolCallId<str>,
262 ) -> Result<(), Self::Error>;
263
264 async fn remove_active_subscription(
266 &self,
267 thread_id: &ThreadId<str>,
268 tool_call_id: &ToolCallId<str>,
269 ) -> Result<(), Self::Error>;
270
271 async fn add_pending_user_choice(
273 &self,
274 thread_id: &ThreadId<str>,
275 choice: UserChoice,
276 ) -> Result<(), Self::Error>;
277
278 async fn remove_pending_user_choice(
280 &self,
281 thread_id: &ThreadId<str>,
282 choice_id: &ChoiceId<str>,
283 ) -> Result<(), Self::Error>;
284
285 async fn get_pending_user_choices(
287 &self,
288 thread_id: &ThreadId<str>,
289 ) -> Result<Vec<UserChoice>, Self::Error>;
290
291 async fn is_thread_stopped(&self, _thread_id: &ThreadId<str>) -> Result<bool, Self::Error> {
305 Ok(false)
306 }
307}
308
309#[async_trait]
311pub trait InputSender: Send + Sync + Clone {
312 type Error: std::error::Error + Send + Sync + 'static;
313
314 async fn send_to_input_queue(
316 &self,
317 message: InputMessage,
318 dedup_id: &str,
319 ) -> Result<(), Self::Error>;
320}