thread_store.rs

   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::requests::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, 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: Vec<u8>,
 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}