Allow attaching text threads as context (#29947)

Release Notes:

- N/A

---------

Co-authored-by: Michael Sloan <mgsloan@gmail.com>
This commit is contained in:
Max Brunsfeld 2025-05-05 13:59:21 -07:00 committed by GitHub
parent 7f868a2eff
commit dd79c29af9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 784 additions and 245 deletions

View file

@ -1,6 +1,8 @@
use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use chrono::{DateTime, Utc};
use fuzzy::StringMatchCandidate;
use gpui::{App, DismissEvent, Entity, FocusHandle, Focusable, Task, WeakEntity};
use picker::{Picker, PickerDelegate};
@ -9,7 +11,7 @@ use ui::{ListItem, prelude::*};
use crate::context_picker::ContextPicker;
use crate::context_store::{self, ContextStore};
use crate::thread::ThreadId;
use crate::thread_store::ThreadStore;
use crate::thread_store::{TextThreadStore, ThreadStore};
pub struct ThreadContextPicker {
picker: Entity<Picker<ThreadContextPickerDelegate>>,
@ -18,13 +20,18 @@ pub struct ThreadContextPicker {
impl ThreadContextPicker {
pub fn new(
thread_store: WeakEntity<ThreadStore>,
text_thread_context_store: WeakEntity<TextThreadStore>,
context_picker: WeakEntity<ContextPicker>,
context_store: WeakEntity<context_store::ContextStore>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let delegate =
ThreadContextPickerDelegate::new(thread_store, context_picker, context_store);
let delegate = ThreadContextPickerDelegate::new(
thread_store,
text_thread_context_store,
context_picker,
context_store,
);
let picker = cx.new(|cx| Picker::uniform_list(delegate, window, cx));
ThreadContextPicker { picker }
@ -44,13 +51,29 @@ impl Render for ThreadContextPicker {
}
#[derive(Debug, Clone)]
pub struct ThreadContextEntry {
pub id: ThreadId,
pub summary: SharedString,
pub enum ThreadContextEntry {
Thread {
id: ThreadId,
title: SharedString,
},
Context {
path: Arc<Path>,
title: SharedString,
},
}
impl ThreadContextEntry {
pub fn title(&self) -> &SharedString {
match self {
Self::Thread { title, .. } => title,
Self::Context { title, .. } => title,
}
}
}
pub struct ThreadContextPickerDelegate {
thread_store: WeakEntity<ThreadStore>,
text_thread_store: WeakEntity<TextThreadStore>,
context_picker: WeakEntity<ContextPicker>,
context_store: WeakEntity<context_store::ContextStore>,
matches: Vec<ThreadContextEntry>,
@ -60,6 +83,7 @@ pub struct ThreadContextPickerDelegate {
impl ThreadContextPickerDelegate {
pub fn new(
thread_store: WeakEntity<ThreadStore>,
text_thread_store: WeakEntity<TextThreadStore>,
context_picker: WeakEntity<ContextPicker>,
context_store: WeakEntity<context_store::ContextStore>,
) -> Self {
@ -67,6 +91,7 @@ impl ThreadContextPickerDelegate {
thread_store,
context_picker,
context_store,
text_thread_store,
matches: Vec::new(),
selected_index: 0,
}
@ -103,11 +128,21 @@ impl PickerDelegate for ThreadContextPickerDelegate {
window: &mut Window,
cx: &mut Context<Picker<Self>>,
) -> Task<()> {
let Some(thread_store) = self.thread_store.upgrade() else {
let Some((thread_store, text_thread_context_store)) = self
.thread_store
.upgrade()
.zip(self.text_thread_store.upgrade())
else {
return Task::ready(());
};
let search_task = search_threads(query, Arc::new(AtomicBool::default()), thread_store, cx);
let search_task = search_threads(
query,
Arc::new(AtomicBool::default()),
thread_store,
text_thread_context_store,
cx,
);
cx.spawn_in(window, async move |this, cx| {
let matches = search_task.await;
this.update(cx, |this, cx| {
@ -124,24 +159,48 @@ impl PickerDelegate for ThreadContextPickerDelegate {
return;
};
let Some(thread_store) = self.thread_store.upgrade() else {
return;
};
match entry {
ThreadContextEntry::Thread { id, .. } => {
let Some(thread_store) = self.thread_store.upgrade() else {
return;
};
let open_thread_task =
thread_store.update(cx, |this, cx| this.open_thread(&id, cx));
let open_thread_task = thread_store.update(cx, |this, cx| this.open_thread(&entry.id, cx));
cx.spawn(async move |this, cx| {
let thread = open_thread_task.await?;
this.update(cx, |this, cx| {
this.delegate
.context_store
.update(cx, |context_store, cx| {
context_store.add_thread(thread, true, cx)
cx.spawn(async move |this, cx| {
let thread = open_thread_task.await?;
this.update(cx, |this, cx| {
this.delegate
.context_store
.update(cx, |context_store, cx| {
context_store.add_thread(thread, true, cx)
})
.ok();
})
.ok();
})
})
.detach_and_log_err(cx);
})
.detach_and_log_err(cx);
}
ThreadContextEntry::Context { path, .. } => {
let Some(text_thread_store) = self.text_thread_store.upgrade() else {
return;
};
let task = text_thread_store
.update(cx, |this, cx| this.open_local_context(path.clone(), cx));
cx.spawn(async move |this, cx| {
let thread = task.await?;
this.update(cx, |this, cx| {
this.delegate
.context_store
.update(cx, |context_store, cx| {
context_store.add_text_thread(thread, true, cx)
})
.ok();
})
})
.detach_and_log_err(cx);
}
}
}
fn dismissed(&mut self, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
@ -168,13 +227,20 @@ impl PickerDelegate for ThreadContextPickerDelegate {
}
pub fn render_thread_context_entry(
thread: &ThreadContextEntry,
entry: &ThreadContextEntry,
context_store: WeakEntity<ContextStore>,
cx: &mut App,
) -> Div {
let added = context_store.upgrade().map_or(false, |ctx_store| {
ctx_store.read(cx).includes_thread(&thread.id)
});
let is_added = match entry {
ThreadContextEntry::Thread { id, .. } => context_store
.upgrade()
.map_or(false, |ctx_store| ctx_store.read(cx).includes_thread(&id)),
ThreadContextEntry::Context { path, .. } => {
context_store.upgrade().map_or(false, |ctx_store| {
ctx_store.read(cx).includes_text_thread(path)
})
}
};
h_flex()
.gap_1p5()
@ -189,9 +255,9 @@ pub fn render_thread_context_entry(
.size(IconSize::XSmall)
.color(Color::Muted),
)
.child(Label::new(thread.summary.clone()).truncate()),
.child(Label::new(entry.title().clone()).truncate()),
)
.when(added, |el| {
.when(is_added, |el| {
el.child(
h_flex()
.gap_1()
@ -211,28 +277,54 @@ pub struct ThreadMatch {
pub is_recent: bool,
}
pub fn unordered_thread_entries(
thread_store: Entity<ThreadStore>,
text_thread_store: Entity<TextThreadStore>,
cx: &App,
) -> impl Iterator<Item = (DateTime<Utc>, ThreadContextEntry)> {
let threads = thread_store.read(cx).unordered_threads().map(|thread| {
(
thread.updated_at,
ThreadContextEntry::Thread {
id: thread.id.clone(),
title: thread.summary.clone(),
},
)
});
let text_threads = text_thread_store
.read(cx)
.unordered_contexts()
.map(|context| {
(
context.mtime.to_utc(),
ThreadContextEntry::Context {
path: context.path.clone(),
title: context.title.clone().into(),
},
)
});
threads.chain(text_threads)
}
pub(crate) fn search_threads(
query: String,
cancellation_flag: Arc<AtomicBool>,
thread_store: Entity<ThreadStore>,
text_thread_store: Entity<TextThreadStore>,
cx: &mut App,
) -> Task<Vec<ThreadMatch>> {
let threads = thread_store
.read(cx)
.reverse_chronological_threads()
.into_iter()
.map(|thread| ThreadContextEntry {
id: thread.id,
summary: thread.summary,
})
.collect::<Vec<_>>();
let mut threads =
unordered_thread_entries(thread_store, text_thread_store, cx).collect::<Vec<_>>();
threads.sort_unstable_by_key(|(updated_at, _)| std::cmp::Reverse(*updated_at));
let executor = cx.background_executor().clone();
cx.background_spawn(async move {
if query.is_empty() {
threads
.into_iter()
.map(|thread| ThreadMatch {
.map(|(_, thread)| ThreadMatch {
thread,
is_recent: false,
})
@ -241,7 +333,7 @@ pub(crate) fn search_threads(
let candidates = threads
.iter()
.enumerate()
.map(|(id, thread)| StringMatchCandidate::new(id, &thread.summary))
.map(|(id, (_, thread))| StringMatchCandidate::new(id, &thread.title()))
.collect::<Vec<_>>();
let matches = fuzzy::match_strings(
&candidates,
@ -256,7 +348,7 @@ pub(crate) fn search_threads(
matches
.into_iter()
.map(|mat| ThreadMatch {
thread: threads[mat.candidate_id].clone(),
thread: threads[mat.candidate_id].1.clone(),
is_recent: false,
})
.collect()