thread_store.rs

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