1use std::cell::{Ref, RefCell};
2use std::path::{Path, PathBuf};
3use std::rc::Rc;
4use std::sync::{Arc, Mutex};
5
6use agent_settings::{AgentProfileId, CompletionMode};
7use anyhow::{Context as _, Result, anyhow};
8use assistant_tool::{ToolId, ToolWorkingSet};
9use chrono::{DateTime, Utc};
10use collections::HashMap;
11use context_server::ContextServerId;
12use futures::channel::{mpsc, oneshot};
13use futures::future::{self, BoxFuture, Shared};
14use futures::{FutureExt as _, StreamExt as _};
15use gpui::{
16 App, BackgroundExecutor, Context, Entity, EventEmitter, Global, ReadGlobal, SharedString,
17 Subscription, Task, prelude::*,
18};
19
20use language_model::{LanguageModelToolResultContent, LanguageModelToolUseId, Role, TokenUsage};
21use project::context_server_store::{ContextServerStatus, ContextServerStore};
22use project::{Project, ProjectItem, ProjectPath, Worktree};
23use prompt_store::{
24 ProjectContext, PromptBuilder, PromptId, PromptStore, PromptsUpdatedEvent, RulesFileContext,
25 UserRulesContext, WorktreeContext,
26};
27use serde::{Deserialize, Serialize};
28use ui::Window;
29use util::ResultExt as _;
30
31use crate::context_server_tool::ContextServerTool;
32use crate::thread::{
33 DetailedSummaryState, ExceededWindowError, MessageId, ProjectSnapshot, Thread, ThreadId,
34};
35use indoc::indoc;
36use sqlez::{
37 bindable::{Bind, Column},
38 connection::Connection,
39 statement::Statement,
40};
41
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
43pub enum DataType {
44 #[serde(rename = "json")]
45 Json,
46 #[serde(rename = "zstd")]
47 Zstd,
48}
49
50impl Bind for DataType {
51 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
52 let value = match self {
53 DataType::Json => "json",
54 DataType::Zstd => "zstd",
55 };
56 value.bind(statement, start_index)
57 }
58}
59
60impl Column for DataType {
61 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
62 let (value, next_index) = String::column(statement, start_index)?;
63 let data_type = match value.as_str() {
64 "json" => DataType::Json,
65 "zstd" => DataType::Zstd,
66 _ => anyhow::bail!("Unknown data type: {}", value),
67 };
68 Ok((data_type, next_index))
69 }
70}
71
72const RULES_FILE_NAMES: [&'static str; 8] = [
73 ".rules",
74 ".cursorrules",
75 ".windsurfrules",
76 ".clinerules",
77 ".github/copilot-instructions.md",
78 "CLAUDE.md",
79 "AGENT.md",
80 "AGENTS.md",
81];
82
83pub fn init(cx: &mut App) {
84 ThreadsDatabase::init(cx);
85}
86
87/// A system prompt shared by all threads created by this ThreadStore
88#[derive(Clone, Default)]
89pub struct SharedProjectContext(Rc<RefCell<Option<ProjectContext>>>);
90
91impl SharedProjectContext {
92 pub fn borrow(&self) -> Ref<Option<ProjectContext>> {
93 self.0.borrow()
94 }
95}
96
97pub type TextThreadStore = assistant_context_editor::ContextStore;
98
99pub struct ThreadStore {
100 project: Entity<Project>,
101 tools: Entity<ToolWorkingSet>,
102 prompt_builder: Arc<PromptBuilder>,
103 prompt_store: Option<Entity<PromptStore>>,
104 context_server_tool_ids: HashMap<ContextServerId, Vec<ToolId>>,
105 threads: Vec<SerializedThreadMetadata>,
106 project_context: SharedProjectContext,
107 reload_system_prompt_tx: mpsc::Sender<()>,
108 _reload_system_prompt_task: Task<()>,
109 _subscriptions: Vec<Subscription>,
110}
111
112pub struct RulesLoadingError {
113 pub message: SharedString,
114}
115
116impl EventEmitter<RulesLoadingError> for ThreadStore {}
117
118impl ThreadStore {
119 pub fn load(
120 project: Entity<Project>,
121 tools: Entity<ToolWorkingSet>,
122 prompt_store: Option<Entity<PromptStore>>,
123 prompt_builder: Arc<PromptBuilder>,
124 cx: &mut App,
125 ) -> Task<Result<Entity<Self>>> {
126 cx.spawn(async move |cx| {
127 let (thread_store, ready_rx) = cx.update(|cx| {
128 let mut option_ready_rx = None;
129 let thread_store = cx.new(|cx| {
130 let (thread_store, ready_rx) =
131 Self::new(project, tools, prompt_builder, prompt_store, cx);
132 option_ready_rx = Some(ready_rx);
133 thread_store
134 });
135 (thread_store, option_ready_rx.take().unwrap())
136 })?;
137 ready_rx.await?;
138 Ok(thread_store)
139 })
140 }
141
142 fn new(
143 project: Entity<Project>,
144 tools: Entity<ToolWorkingSet>,
145 prompt_builder: Arc<PromptBuilder>,
146 prompt_store: Option<Entity<PromptStore>>,
147 cx: &mut Context<Self>,
148 ) -> (Self, oneshot::Receiver<()>) {
149 let mut subscriptions = vec![cx.subscribe(&project, Self::handle_project_event)];
150
151 if let Some(prompt_store) = prompt_store.as_ref() {
152 subscriptions.push(cx.subscribe(
153 prompt_store,
154 |this, _prompt_store, PromptsUpdatedEvent, _cx| {
155 this.enqueue_system_prompt_reload();
156 },
157 ))
158 }
159
160 // This channel and task prevent concurrent and redundant loading of the system prompt.
161 let (reload_system_prompt_tx, mut reload_system_prompt_rx) = mpsc::channel(1);
162 let (ready_tx, ready_rx) = oneshot::channel();
163 let mut ready_tx = Some(ready_tx);
164 let reload_system_prompt_task = cx.spawn({
165 let prompt_store = prompt_store.clone();
166 async move |thread_store, cx| {
167 loop {
168 let Some(reload_task) = thread_store
169 .update(cx, |thread_store, cx| {
170 thread_store.reload_system_prompt(prompt_store.clone(), cx)
171 })
172 .ok()
173 else {
174 return;
175 };
176 reload_task.await;
177 if let Some(ready_tx) = ready_tx.take() {
178 ready_tx.send(()).ok();
179 }
180 reload_system_prompt_rx.next().await;
181 }
182 }
183 });
184
185 let this = Self {
186 project,
187 tools,
188 prompt_builder,
189 prompt_store,
190 context_server_tool_ids: HashMap::default(),
191 threads: Vec::new(),
192 project_context: SharedProjectContext::default(),
193 reload_system_prompt_tx,
194 _reload_system_prompt_task: reload_system_prompt_task,
195 _subscriptions: subscriptions,
196 };
197 this.register_context_server_handlers(cx);
198 this.reload(cx).detach_and_log_err(cx);
199 (this, ready_rx)
200 }
201
202 fn handle_project_event(
203 &mut self,
204 _project: Entity<Project>,
205 event: &project::Event,
206 _cx: &mut Context<Self>,
207 ) {
208 match event {
209 project::Event::WorktreeAdded(_) | project::Event::WorktreeRemoved(_) => {
210 self.enqueue_system_prompt_reload();
211 }
212 project::Event::WorktreeUpdatedEntries(_, items) => {
213 if items.iter().any(|(path, _, _)| {
214 RULES_FILE_NAMES
215 .iter()
216 .any(|name| path.as_ref() == Path::new(name))
217 }) {
218 self.enqueue_system_prompt_reload();
219 }
220 }
221 _ => {}
222 }
223 }
224
225 fn enqueue_system_prompt_reload(&mut self) {
226 self.reload_system_prompt_tx.try_send(()).ok();
227 }
228
229 // Note that this should only be called from `reload_system_prompt_task`.
230 fn reload_system_prompt(
231 &self,
232 prompt_store: Option<Entity<PromptStore>>,
233 cx: &mut Context<Self>,
234 ) -> Task<()> {
235 let worktrees = self
236 .project
237 .read(cx)
238 .visible_worktrees(cx)
239 .collect::<Vec<_>>();
240 let worktree_tasks = worktrees
241 .into_iter()
242 .map(|worktree| {
243 Self::load_worktree_info_for_system_prompt(worktree, self.project.clone(), cx)
244 })
245 .collect::<Vec<_>>();
246 let default_user_rules_task = match prompt_store {
247 None => Task::ready(vec![]),
248 Some(prompt_store) => prompt_store.read_with(cx, |prompt_store, cx| {
249 let prompts = prompt_store.default_prompt_metadata();
250 let load_tasks = prompts.into_iter().map(|prompt_metadata| {
251 let contents = prompt_store.load(prompt_metadata.id, cx);
252 async move { (contents.await, prompt_metadata) }
253 });
254 cx.background_spawn(future::join_all(load_tasks))
255 }),
256 };
257
258 cx.spawn(async move |this, cx| {
259 let (worktrees, default_user_rules) =
260 future::join(future::join_all(worktree_tasks), default_user_rules_task).await;
261
262 let worktrees = worktrees
263 .into_iter()
264 .map(|(worktree, rules_error)| {
265 if let Some(rules_error) = rules_error {
266 this.update(cx, |_, cx| cx.emit(rules_error)).ok();
267 }
268 worktree
269 })
270 .collect::<Vec<_>>();
271
272 let default_user_rules = default_user_rules
273 .into_iter()
274 .flat_map(|(contents, prompt_metadata)| match contents {
275 Ok(contents) => Some(UserRulesContext {
276 uuid: match prompt_metadata.id {
277 PromptId::User { uuid } => uuid,
278 PromptId::EditWorkflow => return None,
279 },
280 title: prompt_metadata.title.map(|title| title.to_string()),
281 contents,
282 }),
283 Err(err) => {
284 this.update(cx, |_, cx| {
285 cx.emit(RulesLoadingError {
286 message: format!("{err:?}").into(),
287 });
288 })
289 .ok();
290 None
291 }
292 })
293 .collect::<Vec<_>>();
294
295 this.update(cx, |this, _cx| {
296 *this.project_context.0.borrow_mut() =
297 Some(ProjectContext::new(worktrees, default_user_rules));
298 })
299 .ok();
300 })
301 }
302
303 fn load_worktree_info_for_system_prompt(
304 worktree: Entity<Worktree>,
305 project: Entity<Project>,
306 cx: &mut App,
307 ) -> Task<(WorktreeContext, Option<RulesLoadingError>)> {
308 let root_name = worktree.read(cx).root_name().into();
309
310 let rules_task = Self::load_worktree_rules_file(worktree, project, cx);
311 let Some(rules_task) = rules_task else {
312 return Task::ready((
313 WorktreeContext {
314 root_name,
315 rules_file: None,
316 },
317 None,
318 ));
319 };
320
321 cx.spawn(async move |_| {
322 let (rules_file, rules_file_error) = match rules_task.await {
323 Ok(rules_file) => (Some(rules_file), None),
324 Err(err) => (
325 None,
326 Some(RulesLoadingError {
327 message: format!("{err}").into(),
328 }),
329 ),
330 };
331 let worktree_info = WorktreeContext {
332 root_name,
333 rules_file,
334 };
335 (worktree_info, rules_file_error)
336 })
337 }
338
339 fn load_worktree_rules_file(
340 worktree: Entity<Worktree>,
341 project: Entity<Project>,
342 cx: &mut App,
343 ) -> Option<Task<Result<RulesFileContext>>> {
344 let worktree_ref = worktree.read(cx);
345 let worktree_id = worktree_ref.id();
346 let selected_rules_file = RULES_FILE_NAMES
347 .into_iter()
348 .filter_map(|name| {
349 worktree_ref
350 .entry_for_path(name)
351 .filter(|entry| entry.is_file())
352 .map(|entry| entry.path.clone())
353 })
354 .next();
355
356 // Note that Cline supports `.clinerules` being a directory, but that is not currently
357 // supported. This doesn't seem to occur often in GitHub repositories.
358 selected_rules_file.map(|path_in_worktree| {
359 let project_path = ProjectPath {
360 worktree_id,
361 path: path_in_worktree.clone(),
362 };
363 let buffer_task =
364 project.update(cx, |project, cx| project.open_buffer(project_path, cx));
365 let rope_task = cx.spawn(async move |cx| {
366 buffer_task.await?.read_with(cx, |buffer, cx| {
367 let project_entry_id = buffer.entry_id(cx).context("buffer has no file")?;
368 anyhow::Ok((project_entry_id, buffer.as_rope().clone()))
369 })?
370 });
371 // Build a string from the rope on a background thread.
372 cx.background_spawn(async move {
373 let (project_entry_id, rope) = rope_task.await?;
374 anyhow::Ok(RulesFileContext {
375 path_in_worktree,
376 text: rope.to_string().trim().to_string(),
377 project_entry_id: project_entry_id.to_usize(),
378 })
379 })
380 })
381 }
382
383 pub fn prompt_store(&self) -> &Option<Entity<PromptStore>> {
384 &self.prompt_store
385 }
386
387 pub fn tools(&self) -> Entity<ToolWorkingSet> {
388 self.tools.clone()
389 }
390
391 /// Returns the number of threads.
392 pub fn thread_count(&self) -> usize {
393 self.threads.len()
394 }
395
396 pub fn reverse_chronological_threads(&self) -> impl Iterator<Item = &SerializedThreadMetadata> {
397 // ordering is from "ORDER BY" in `list_threads`
398 self.threads.iter()
399 }
400
401 pub fn create_thread(&mut self, cx: &mut Context<Self>) -> Entity<Thread> {
402 cx.new(|cx| {
403 Thread::new(
404 self.project.clone(),
405 self.tools.clone(),
406 self.prompt_builder.clone(),
407 self.project_context.clone(),
408 cx,
409 )
410 })
411 }
412
413 pub fn create_thread_from_serialized(
414 &mut self,
415 serialized: SerializedThread,
416 cx: &mut Context<Self>,
417 ) -> Entity<Thread> {
418 cx.new(|cx| {
419 Thread::deserialize(
420 ThreadId::new(),
421 serialized,
422 self.project.clone(),
423 self.tools.clone(),
424 self.prompt_builder.clone(),
425 self.project_context.clone(),
426 None,
427 cx,
428 )
429 })
430 }
431
432 pub fn open_thread(
433 &self,
434 id: &ThreadId,
435 window: &mut Window,
436 cx: &mut Context<Self>,
437 ) -> Task<Result<Entity<Thread>>> {
438 let id = id.clone();
439 let database_future = ThreadsDatabase::global_future(cx);
440 let this = cx.weak_entity();
441 window.spawn(cx, async move |cx| {
442 let database = database_future.await.map_err(|err| anyhow!(err))?;
443 let thread = database
444 .try_find_thread(id.clone())
445 .await?
446 .with_context(|| format!("no thread found with ID: {id:?}"))?;
447
448 let thread = this.update_in(cx, |this, window, cx| {
449 cx.new(|cx| {
450 Thread::deserialize(
451 id.clone(),
452 thread,
453 this.project.clone(),
454 this.tools.clone(),
455 this.prompt_builder.clone(),
456 this.project_context.clone(),
457 Some(window),
458 cx,
459 )
460 })
461 })?;
462
463 Ok(thread)
464 })
465 }
466
467 pub fn save_thread(&self, thread: &Entity<Thread>, cx: &mut Context<Self>) -> Task<Result<()>> {
468 let (metadata, serialized_thread) =
469 thread.update(cx, |thread, cx| (thread.id().clone(), thread.serialize(cx)));
470
471 let database_future = ThreadsDatabase::global_future(cx);
472 cx.spawn(async move |this, cx| {
473 let serialized_thread = serialized_thread.await?;
474 let database = database_future.await.map_err(|err| anyhow!(err))?;
475 database.save_thread(metadata, serialized_thread).await?;
476
477 this.update(cx, |this, cx| this.reload(cx))?.await
478 })
479 }
480
481 pub fn delete_thread(&mut self, id: &ThreadId, cx: &mut Context<Self>) -> Task<Result<()>> {
482 let id = id.clone();
483 let database_future = ThreadsDatabase::global_future(cx);
484 cx.spawn(async move |this, cx| {
485 let database = database_future.await.map_err(|err| anyhow!(err))?;
486 database.delete_thread(id.clone()).await?;
487
488 this.update(cx, |this, cx| {
489 this.threads.retain(|thread| thread.id != id);
490 cx.notify();
491 })
492 })
493 }
494
495 pub fn reload(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
496 let database_future = ThreadsDatabase::global_future(cx);
497 cx.spawn(async move |this, cx| {
498 let threads = database_future
499 .await
500 .map_err(|err| anyhow!(err))?
501 .list_threads()
502 .await?;
503
504 this.update(cx, |this, cx| {
505 this.threads = threads;
506 cx.notify();
507 })
508 })
509 }
510
511 fn register_context_server_handlers(&self, cx: &mut Context<Self>) {
512 let context_server_store = self.project.read(cx).context_server_store();
513 cx.subscribe(&context_server_store, Self::handle_context_server_event)
514 .detach();
515
516 // Check for any servers that were already running before the handler was registered
517 for server in context_server_store.read(cx).running_servers() {
518 self.load_context_server_tools(server.id(), context_server_store.clone(), cx);
519 }
520 }
521
522 fn handle_context_server_event(
523 &mut self,
524 context_server_store: Entity<ContextServerStore>,
525 event: &project::context_server_store::Event,
526 cx: &mut Context<Self>,
527 ) {
528 let tool_working_set = self.tools.clone();
529 match event {
530 project::context_server_store::Event::ServerStatusChanged { server_id, status } => {
531 match status {
532 ContextServerStatus::Starting => {}
533 ContextServerStatus::Running => {
534 self.load_context_server_tools(server_id.clone(), context_server_store, cx);
535 }
536 ContextServerStatus::Stopped | ContextServerStatus::Error(_) => {
537 if let Some(tool_ids) = self.context_server_tool_ids.remove(server_id) {
538 tool_working_set.update(cx, |tool_working_set, _| {
539 tool_working_set.remove(&tool_ids);
540 });
541 }
542 }
543 }
544 }
545 }
546 }
547
548 fn load_context_server_tools(
549 &self,
550 server_id: ContextServerId,
551 context_server_store: Entity<ContextServerStore>,
552 cx: &mut Context<Self>,
553 ) {
554 let Some(server) = context_server_store.read(cx).get_running_server(&server_id) else {
555 return;
556 };
557 let tool_working_set = self.tools.clone();
558 cx.spawn(async move |this, cx| {
559 let Some(protocol) = server.client() else {
560 return;
561 };
562
563 if protocol.capable(context_server::protocol::ServerCapability::Tools) {
564 if let Some(response) = protocol
565 .request::<context_server::types::request::ListTools>(())
566 .await
567 .log_err()
568 {
569 let tool_ids = tool_working_set
570 .update(cx, |tool_working_set, _| {
571 response
572 .tools
573 .into_iter()
574 .map(|tool| {
575 log::info!("registering context server tool: {:?}", tool.name);
576 tool_working_set.insert(Arc::new(ContextServerTool::new(
577 context_server_store.clone(),
578 server.id(),
579 tool,
580 )))
581 })
582 .collect::<Vec<_>>()
583 })
584 .log_err();
585
586 if let Some(tool_ids) = tool_ids {
587 this.update(cx, |this, _| {
588 this.context_server_tool_ids.insert(server_id, tool_ids);
589 })
590 .log_err();
591 }
592 }
593 }
594 })
595 .detach();
596 }
597}
598
599#[derive(Debug, Clone, Serialize, Deserialize)]
600pub struct SerializedThreadMetadata {
601 pub id: ThreadId,
602 pub summary: SharedString,
603 pub updated_at: DateTime<Utc>,
604}
605
606#[derive(Serialize, Deserialize, Debug)]
607pub struct SerializedThread {
608 pub version: String,
609 pub summary: SharedString,
610 pub updated_at: DateTime<Utc>,
611 pub messages: Vec<SerializedMessage>,
612 #[serde(default)]
613 pub initial_project_snapshot: Option<Arc<ProjectSnapshot>>,
614 #[serde(default)]
615 pub cumulative_token_usage: TokenUsage,
616 #[serde(default)]
617 pub request_token_usage: Vec<TokenUsage>,
618 #[serde(default)]
619 pub detailed_summary_state: DetailedSummaryState,
620 #[serde(default)]
621 pub exceeded_window_error: Option<ExceededWindowError>,
622 #[serde(default)]
623 pub model: Option<SerializedLanguageModel>,
624 #[serde(default)]
625 pub completion_mode: Option<CompletionMode>,
626 #[serde(default)]
627 pub tool_use_limit_reached: bool,
628 #[serde(default)]
629 pub profile: Option<AgentProfileId>,
630}
631
632#[derive(Serialize, Deserialize, Debug)]
633pub struct SerializedLanguageModel {
634 pub provider: String,
635 pub model: String,
636}
637
638impl SerializedThread {
639 pub const VERSION: &'static str = "0.2.0";
640
641 pub fn from_json(json: &[u8]) -> Result<Self> {
642 let saved_thread_json = serde_json::from_slice::<serde_json::Value>(json)?;
643 match saved_thread_json.get("version") {
644 Some(serde_json::Value::String(version)) => match version.as_str() {
645 SerializedThreadV0_1_0::VERSION => {
646 let saved_thread =
647 serde_json::from_value::<SerializedThreadV0_1_0>(saved_thread_json)?;
648 Ok(saved_thread.upgrade())
649 }
650 SerializedThread::VERSION => Ok(serde_json::from_value::<SerializedThread>(
651 saved_thread_json,
652 )?),
653 _ => anyhow::bail!("unrecognized serialized thread version: {version:?}"),
654 },
655 None => {
656 let saved_thread =
657 serde_json::from_value::<LegacySerializedThread>(saved_thread_json)?;
658 Ok(saved_thread.upgrade())
659 }
660 version => anyhow::bail!("unrecognized serialized thread version: {version:?}"),
661 }
662 }
663}
664
665#[derive(Serialize, Deserialize, Debug)]
666pub struct SerializedThreadV0_1_0(
667 // The structure did not change, so we are reusing the latest SerializedThread.
668 // When making the next version, make sure this points to SerializedThreadV0_2_0
669 SerializedThread,
670);
671
672impl SerializedThreadV0_1_0 {
673 pub const VERSION: &'static str = "0.1.0";
674
675 pub fn upgrade(self) -> SerializedThread {
676 debug_assert_eq!(SerializedThread::VERSION, "0.2.0");
677
678 let mut messages: Vec<SerializedMessage> = Vec::with_capacity(self.0.messages.len());
679
680 for message in self.0.messages {
681 if message.role == Role::User && !message.tool_results.is_empty() {
682 if let Some(last_message) = messages.last_mut() {
683 debug_assert!(last_message.role == Role::Assistant);
684
685 last_message.tool_results = message.tool_results;
686 continue;
687 }
688 }
689
690 messages.push(message);
691 }
692
693 SerializedThread { messages, ..self.0 }
694 }
695}
696
697#[derive(Debug, Serialize, Deserialize)]
698pub struct SerializedMessage {
699 pub id: MessageId,
700 pub role: Role,
701 #[serde(default)]
702 pub segments: Vec<SerializedMessageSegment>,
703 #[serde(default)]
704 pub tool_uses: Vec<SerializedToolUse>,
705 #[serde(default)]
706 pub tool_results: Vec<SerializedToolResult>,
707 #[serde(default)]
708 pub context: String,
709 #[serde(default)]
710 pub creases: Vec<SerializedCrease>,
711 #[serde(default)]
712 pub is_hidden: bool,
713}
714
715#[derive(Debug, Serialize, Deserialize)]
716#[serde(tag = "type")]
717pub enum SerializedMessageSegment {
718 #[serde(rename = "text")]
719 Text {
720 text: String,
721 },
722 #[serde(rename = "thinking")]
723 Thinking {
724 text: String,
725 #[serde(skip_serializing_if = "Option::is_none")]
726 signature: Option<String>,
727 },
728 RedactedThinking {
729 data: Vec<u8>,
730 },
731}
732
733#[derive(Debug, Serialize, Deserialize)]
734pub struct SerializedToolUse {
735 pub id: LanguageModelToolUseId,
736 pub name: SharedString,
737 pub input: serde_json::Value,
738}
739
740#[derive(Debug, Serialize, Deserialize)]
741pub struct SerializedToolResult {
742 pub tool_use_id: LanguageModelToolUseId,
743 pub is_error: bool,
744 pub content: LanguageModelToolResultContent,
745 pub output: Option<serde_json::Value>,
746}
747
748#[derive(Serialize, Deserialize)]
749struct LegacySerializedThread {
750 pub summary: SharedString,
751 pub updated_at: DateTime<Utc>,
752 pub messages: Vec<LegacySerializedMessage>,
753 #[serde(default)]
754 pub initial_project_snapshot: Option<Arc<ProjectSnapshot>>,
755}
756
757impl LegacySerializedThread {
758 pub fn upgrade(self) -> SerializedThread {
759 SerializedThread {
760 version: SerializedThread::VERSION.to_string(),
761 summary: self.summary,
762 updated_at: self.updated_at,
763 messages: self.messages.into_iter().map(|msg| msg.upgrade()).collect(),
764 initial_project_snapshot: self.initial_project_snapshot,
765 cumulative_token_usage: TokenUsage::default(),
766 request_token_usage: Vec::new(),
767 detailed_summary_state: DetailedSummaryState::default(),
768 exceeded_window_error: None,
769 model: None,
770 completion_mode: None,
771 tool_use_limit_reached: false,
772 profile: None,
773 }
774 }
775}
776
777#[derive(Debug, Serialize, Deserialize)]
778struct LegacySerializedMessage {
779 pub id: MessageId,
780 pub role: Role,
781 pub text: String,
782 #[serde(default)]
783 pub tool_uses: Vec<SerializedToolUse>,
784 #[serde(default)]
785 pub tool_results: Vec<SerializedToolResult>,
786}
787
788impl LegacySerializedMessage {
789 fn upgrade(self) -> SerializedMessage {
790 SerializedMessage {
791 id: self.id,
792 role: self.role,
793 segments: vec![SerializedMessageSegment::Text { text: self.text }],
794 tool_uses: self.tool_uses,
795 tool_results: self.tool_results,
796 context: String::new(),
797 creases: Vec::new(),
798 is_hidden: false,
799 }
800 }
801}
802
803#[derive(Debug, Serialize, Deserialize)]
804pub struct SerializedCrease {
805 pub start: usize,
806 pub end: usize,
807 pub icon_path: SharedString,
808 pub label: SharedString,
809}
810
811struct GlobalThreadsDatabase(
812 Shared<BoxFuture<'static, Result<Arc<ThreadsDatabase>, Arc<anyhow::Error>>>>,
813);
814
815impl Global for GlobalThreadsDatabase {}
816
817pub(crate) struct ThreadsDatabase {
818 executor: BackgroundExecutor,
819 connection: Arc<Mutex<Connection>>,
820}
821
822impl ThreadsDatabase {
823 fn connection(&self) -> Arc<Mutex<Connection>> {
824 self.connection.clone()
825 }
826
827 const COMPRESSION_LEVEL: i32 = 3;
828}
829
830impl Bind for ThreadId {
831 fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
832 self.to_string().bind(statement, start_index)
833 }
834}
835
836impl Column for ThreadId {
837 fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> {
838 let (id_str, next_index) = String::column(statement, start_index)?;
839 Ok((ThreadId::from(id_str.as_str()), next_index))
840 }
841}
842
843impl ThreadsDatabase {
844 fn global_future(
845 cx: &mut App,
846 ) -> Shared<BoxFuture<'static, Result<Arc<ThreadsDatabase>, Arc<anyhow::Error>>>> {
847 GlobalThreadsDatabase::global(cx).0.clone()
848 }
849
850 fn init(cx: &mut App) {
851 let executor = cx.background_executor().clone();
852 let database_future = executor
853 .spawn({
854 let executor = executor.clone();
855 let threads_dir = paths::data_dir().join("threads");
856 async move { ThreadsDatabase::new(threads_dir, executor) }
857 })
858 .then(|result| future::ready(result.map(Arc::new).map_err(Arc::new)))
859 .boxed()
860 .shared();
861
862 cx.set_global(GlobalThreadsDatabase(database_future));
863 }
864
865 pub fn new(threads_dir: PathBuf, executor: BackgroundExecutor) -> Result<Self> {
866 std::fs::create_dir_all(&threads_dir)?;
867
868 let sqlite_path = threads_dir.join("threads.db");
869 let mdb_path = threads_dir.join("threads-db.1.mdb");
870
871 let needs_migration_from_heed = mdb_path.exists();
872
873 let connection = Connection::open_file(&sqlite_path.to_string_lossy());
874
875 connection.exec(indoc! {"
876 CREATE TABLE IF NOT EXISTS threads (
877 id TEXT PRIMARY KEY,
878 summary TEXT NOT NULL,
879 updated_at TEXT NOT NULL,
880 data_type TEXT NOT NULL,
881 data BLOB NOT NULL
882 )
883 "})?()
884 .map_err(|e| anyhow!("Failed to create threads table: {}", e))?;
885
886 let db = Self {
887 executor: executor.clone(),
888 connection: Arc::new(Mutex::new(connection)),
889 };
890
891 if needs_migration_from_heed {
892 let db_connection = db.connection();
893 let executor_clone = executor.clone();
894 executor
895 .spawn(async move {
896 log::info!("Starting threads.db migration");
897 Self::migrate_from_heed(&mdb_path, db_connection, executor_clone)?;
898 std::fs::remove_dir_all(mdb_path)?;
899 log::info!("threads.db migrated to sqlite");
900 Ok::<(), anyhow::Error>(())
901 })
902 .detach();
903 }
904
905 Ok(db)
906 }
907
908 // Remove this migration after 2025-09-01
909 fn migrate_from_heed(
910 mdb_path: &Path,
911 connection: Arc<Mutex<Connection>>,
912 _executor: BackgroundExecutor,
913 ) -> Result<()> {
914 use heed::types::SerdeBincode;
915 struct SerializedThreadHeed(SerializedThread);
916
917 impl heed::BytesEncode<'_> for SerializedThreadHeed {
918 type EItem = SerializedThreadHeed;
919
920 fn bytes_encode(
921 item: &Self::EItem,
922 ) -> Result<std::borrow::Cow<[u8]>, heed::BoxedError> {
923 serde_json::to_vec(&item.0)
924 .map(std::borrow::Cow::Owned)
925 .map_err(Into::into)
926 }
927 }
928
929 impl<'a> heed::BytesDecode<'a> for SerializedThreadHeed {
930 type DItem = SerializedThreadHeed;
931
932 fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, heed::BoxedError> {
933 SerializedThread::from_json(bytes)
934 .map(SerializedThreadHeed)
935 .map_err(Into::into)
936 }
937 }
938
939 const ONE_GB_IN_BYTES: usize = 1024 * 1024 * 1024;
940
941 let env = unsafe {
942 heed::EnvOpenOptions::new()
943 .map_size(ONE_GB_IN_BYTES)
944 .max_dbs(1)
945 .open(mdb_path)?
946 };
947
948 let txn = env.write_txn()?;
949 let threads: heed::Database<SerdeBincode<ThreadId>, SerializedThreadHeed> = env
950 .open_database(&txn, Some("threads"))?
951 .ok_or_else(|| anyhow!("threads database not found"))?;
952
953 for result in threads.iter(&txn)? {
954 let (thread_id, thread_heed) = result?;
955 Self::save_thread_sync(&connection, thread_id, thread_heed.0)?;
956 }
957
958 Ok(())
959 }
960
961 fn save_thread_sync(
962 connection: &Arc<Mutex<Connection>>,
963 id: ThreadId,
964 thread: SerializedThread,
965 ) -> Result<()> {
966 let json_data = serde_json::to_string(&thread)?;
967 let summary = thread.summary.to_string();
968 let updated_at = thread.updated_at.to_rfc3339();
969
970 let connection = connection.lock().unwrap();
971
972 let compressed = zstd::encode_all(json_data.as_bytes(), Self::COMPRESSION_LEVEL)?;
973 let data_type = DataType::Zstd;
974 let data = compressed;
975
976 let mut insert = connection.exec_bound::<(ThreadId, String, String, DataType, Vec<u8>)>(indoc! {"
977 INSERT OR REPLACE INTO threads (id, summary, updated_at, data_type, data) VALUES (?, ?, ?, ?, ?)
978 "})?;
979
980 insert((id, summary, updated_at, data_type, data))?;
981
982 Ok(())
983 }
984
985 pub fn list_threads(&self) -> Task<Result<Vec<SerializedThreadMetadata>>> {
986 let connection = self.connection.clone();
987
988 self.executor.spawn(async move {
989 let connection = connection.lock().unwrap();
990 let mut select =
991 connection.select_bound::<(), (ThreadId, String, String)>(indoc! {"
992 SELECT id, summary, updated_at FROM threads ORDER BY updated_at DESC
993 "})?;
994
995 let rows = select(())?;
996 let mut threads = Vec::new();
997
998 for (id, summary, updated_at) in rows {
999 threads.push(SerializedThreadMetadata {
1000 id,
1001 summary: summary.into(),
1002 updated_at: DateTime::parse_from_rfc3339(&updated_at)?.with_timezone(&Utc),
1003 });
1004 }
1005
1006 Ok(threads)
1007 })
1008 }
1009
1010 pub fn try_find_thread(&self, id: ThreadId) -> Task<Result<Option<SerializedThread>>> {
1011 let connection = self.connection.clone();
1012
1013 self.executor.spawn(async move {
1014 let connection = connection.lock().unwrap();
1015 let mut select = connection.select_bound::<ThreadId, (DataType, Vec<u8>)>(indoc! {"
1016 SELECT data_type, data FROM threads WHERE id = ? LIMIT 1
1017 "})?;
1018
1019 let rows = select(id)?;
1020 if let Some((data_type, data)) = rows.into_iter().next() {
1021 let json_data = match data_type {
1022 DataType::Zstd => {
1023 let decompressed = zstd::decode_all(&data[..])?;
1024 String::from_utf8(decompressed)?
1025 }
1026 DataType::Json => String::from_utf8(data)?,
1027 };
1028
1029 let thread = SerializedThread::from_json(json_data.as_bytes())?;
1030 Ok(Some(thread))
1031 } else {
1032 Ok(None)
1033 }
1034 })
1035 }
1036
1037 pub fn save_thread(&self, id: ThreadId, thread: SerializedThread) -> Task<Result<()>> {
1038 let connection = self.connection.clone();
1039
1040 self.executor
1041 .spawn(async move { Self::save_thread_sync(&connection, id, thread) })
1042 }
1043
1044 pub fn delete_thread(&self, id: ThreadId) -> Task<Result<()>> {
1045 let connection = self.connection.clone();
1046
1047 self.executor.spawn(async move {
1048 let connection = connection.lock().unwrap();
1049
1050 let mut delete = connection.exec_bound::<ThreadId>(indoc! {"
1051 DELETE FROM threads WHERE id = ?
1052 "})?;
1053
1054 delete(id)?;
1055
1056 Ok(())
1057 })
1058 }
1059}