context_store.rs

  1use std::ops::Range;
  2use std::path::Path;
  3use std::sync::Arc;
  4
  5use anyhow::{Context as _, Result, anyhow};
  6use collections::{BTreeMap, HashMap, HashSet};
  7use futures::future::join_all;
  8use futures::{self, Future, FutureExt, future};
  9use gpui::{App, AppContext as _, Context, Entity, SharedString, Task, WeakEntity};
 10use language::{Buffer, File};
 11use project::{Project, ProjectItem, ProjectPath, Worktree};
 12use rope::Rope;
 13use text::{Anchor, BufferId, OffsetRangeExt};
 14use util::{ResultExt as _, maybe};
 15
 16use crate::ThreadStore;
 17use crate::context::{
 18    AssistantContext, ContextBuffer, ContextId, ContextSymbol, ContextSymbolId, DirectoryContext,
 19    FetchedUrlContext, FileContext, SymbolContext, ThreadContext,
 20};
 21use crate::context_strip::SuggestedContext;
 22use crate::thread::{Thread, ThreadId};
 23
 24pub struct ContextStore {
 25    project: WeakEntity<Project>,
 26    context: Vec<AssistantContext>,
 27    thread_store: Option<WeakEntity<ThreadStore>>,
 28    // TODO: If an EntityId is used for all context types (like BufferId), can remove ContextId.
 29    next_context_id: ContextId,
 30    files: BTreeMap<BufferId, ContextId>,
 31    directories: HashMap<ProjectPath, ContextId>,
 32    symbols: HashMap<ContextSymbolId, ContextId>,
 33    symbol_buffers: HashMap<ContextSymbolId, Entity<Buffer>>,
 34    symbols_by_path: HashMap<ProjectPath, Vec<ContextSymbolId>>,
 35    threads: HashMap<ThreadId, ContextId>,
 36    thread_summary_tasks: Vec<Task<()>>,
 37    fetched_urls: HashMap<String, ContextId>,
 38}
 39
 40impl ContextStore {
 41    pub fn new(
 42        project: WeakEntity<Project>,
 43        thread_store: Option<WeakEntity<ThreadStore>>,
 44    ) -> Self {
 45        Self {
 46            project,
 47            thread_store,
 48            context: Vec::new(),
 49            next_context_id: ContextId(0),
 50            files: BTreeMap::default(),
 51            directories: HashMap::default(),
 52            symbols: HashMap::default(),
 53            symbol_buffers: HashMap::default(),
 54            symbols_by_path: HashMap::default(),
 55            threads: HashMap::default(),
 56            thread_summary_tasks: Vec::new(),
 57            fetched_urls: HashMap::default(),
 58        }
 59    }
 60
 61    pub fn context(&self) -> &Vec<AssistantContext> {
 62        &self.context
 63    }
 64
 65    pub fn context_for_id(&self, id: ContextId) -> Option<&AssistantContext> {
 66        self.context().iter().find(|context| context.id() == id)
 67    }
 68
 69    pub fn clear(&mut self) {
 70        self.context.clear();
 71        self.files.clear();
 72        self.directories.clear();
 73        self.threads.clear();
 74        self.fetched_urls.clear();
 75    }
 76
 77    pub fn add_file_from_path(
 78        &mut self,
 79        project_path: ProjectPath,
 80        remove_if_exists: bool,
 81        cx: &mut Context<Self>,
 82    ) -> Task<Result<()>> {
 83        let Some(project) = self.project.upgrade() else {
 84            return Task::ready(Err(anyhow!("failed to read project")));
 85        };
 86
 87        cx.spawn(async move |this, cx| {
 88            let open_buffer_task = project.update(cx, |project, cx| {
 89                project.open_buffer(project_path.clone(), cx)
 90            })?;
 91
 92            let buffer = open_buffer_task.await?;
 93            let buffer_id = this.update(cx, |_, cx| buffer.read(cx).remote_id())?;
 94
 95            let already_included = this.update(cx, |this, cx| {
 96                match this.will_include_buffer(buffer_id, &project_path) {
 97                    Some(FileInclusion::Direct(context_id)) => {
 98                        if remove_if_exists {
 99                            this.remove_context(context_id, cx);
100                        }
101                        true
102                    }
103                    Some(FileInclusion::InDirectory(_)) => true,
104                    None => false,
105                }
106            })?;
107
108            if already_included {
109                return anyhow::Ok(());
110            }
111
112            let (buffer_info, text_task) =
113                this.update(cx, |_, cx| collect_buffer_info_and_text(buffer, None, cx))??;
114
115            let text = text_task.await;
116
117            this.update(cx, |this, cx| {
118                this.insert_file(make_context_buffer(buffer_info, text), cx);
119            })?;
120
121            anyhow::Ok(())
122        })
123    }
124
125    pub fn add_file_from_buffer(
126        &mut self,
127        buffer: Entity<Buffer>,
128        cx: &mut Context<Self>,
129    ) -> Task<Result<()>> {
130        cx.spawn(async move |this, cx| {
131            let (buffer_info, text_task) =
132                this.update(cx, |_, cx| collect_buffer_info_and_text(buffer, None, cx))??;
133
134            let text = text_task.await;
135
136            this.update(cx, |this, cx| {
137                this.insert_file(make_context_buffer(buffer_info, text), cx)
138            })?;
139
140            anyhow::Ok(())
141        })
142    }
143
144    fn insert_file(&mut self, context_buffer: ContextBuffer, cx: &mut Context<Self>) {
145        let id = self.next_context_id.post_inc();
146        self.files.insert(context_buffer.id, id);
147        self.context
148            .push(AssistantContext::File(FileContext { id, context_buffer }));
149        cx.notify();
150    }
151
152    pub fn add_directory(
153        &mut self,
154        project_path: ProjectPath,
155        remove_if_exists: bool,
156        cx: &mut Context<Self>,
157    ) -> Task<Result<()>> {
158        let Some(project) = self.project.upgrade() else {
159            return Task::ready(Err(anyhow!("failed to read project")));
160        };
161
162        let already_included = match self.includes_directory(&project_path) {
163            Some(FileInclusion::Direct(context_id)) => {
164                if remove_if_exists {
165                    self.remove_context(context_id, cx);
166                }
167                true
168            }
169            Some(FileInclusion::InDirectory(_)) => true,
170            None => false,
171        };
172        if already_included {
173            return Task::ready(Ok(()));
174        }
175
176        let worktree_id = project_path.worktree_id;
177        cx.spawn(async move |this, cx| {
178            let worktree = project.update(cx, |project, cx| {
179                project
180                    .worktree_for_id(worktree_id, cx)
181                    .ok_or_else(|| anyhow!("no worktree found for {worktree_id:?}"))
182            })??;
183
184            let files = worktree.update(cx, |worktree, _cx| {
185                collect_files_in_path(worktree, &project_path.path)
186            })?;
187
188            let open_buffers_task = project.update(cx, |project, cx| {
189                let tasks = files.iter().map(|file_path| {
190                    project.open_buffer(
191                        ProjectPath {
192                            worktree_id,
193                            path: file_path.clone(),
194                        },
195                        cx,
196                    )
197                });
198                future::join_all(tasks)
199            })?;
200
201            let buffers = open_buffers_task.await;
202
203            let mut buffer_infos = Vec::new();
204            let mut text_tasks = Vec::new();
205            this.update(cx, |_, cx| {
206                // Skip all binary files and other non-UTF8 files
207                for buffer in buffers.into_iter().flatten() {
208                    if let Some((buffer_info, text_task)) =
209                        collect_buffer_info_and_text(buffer, None, cx).log_err()
210                    {
211                        buffer_infos.push(buffer_info);
212                        text_tasks.push(text_task);
213                    }
214                }
215                anyhow::Ok(())
216            })??;
217
218            let buffer_texts = future::join_all(text_tasks).await;
219            let context_buffers = buffer_infos
220                .into_iter()
221                .zip(buffer_texts)
222                .map(|(info, text)| make_context_buffer(info, text))
223                .collect::<Vec<_>>();
224
225            if context_buffers.is_empty() {
226                let full_path = cx.update(|cx| worktree.read(cx).full_path(&project_path.path))?;
227                return Err(anyhow!("No text files found in {}", &full_path.display()));
228            }
229
230            this.update(cx, |this, cx| {
231                this.insert_directory(worktree, project_path, context_buffers, cx);
232            })?;
233
234            anyhow::Ok(())
235        })
236    }
237
238    fn insert_directory(
239        &mut self,
240        worktree: Entity<Worktree>,
241        project_path: ProjectPath,
242        context_buffers: Vec<ContextBuffer>,
243        cx: &mut Context<Self>,
244    ) {
245        let id = self.next_context_id.post_inc();
246        let path = project_path.path.clone();
247        self.directories.insert(project_path, id);
248
249        self.context
250            .push(AssistantContext::Directory(DirectoryContext {
251                id,
252                worktree,
253                path,
254                context_buffers,
255            }));
256        cx.notify();
257    }
258
259    pub fn add_symbol(
260        &mut self,
261        buffer: Entity<Buffer>,
262        symbol_name: SharedString,
263        symbol_range: Range<Anchor>,
264        symbol_enclosing_range: Range<Anchor>,
265        remove_if_exists: bool,
266        cx: &mut Context<Self>,
267    ) -> Task<Result<bool>> {
268        let buffer_ref = buffer.read(cx);
269        let Some(project_path) = buffer_ref.project_path(cx) else {
270            return Task::ready(Err(anyhow!("buffer has no path")));
271        };
272
273        if let Some(symbols_for_path) = self.symbols_by_path.get(&project_path) {
274            let mut matching_symbol_id = None;
275            for symbol in symbols_for_path {
276                if &symbol.name == &symbol_name {
277                    let snapshot = buffer_ref.snapshot();
278                    if symbol.range.to_offset(&snapshot) == symbol_range.to_offset(&snapshot) {
279                        matching_symbol_id = self.symbols.get(symbol).cloned();
280                        break;
281                    }
282                }
283            }
284
285            if let Some(id) = matching_symbol_id {
286                if remove_if_exists {
287                    self.remove_context(id, cx);
288                }
289                return Task::ready(Ok(false));
290            }
291        }
292
293        let (buffer_info, collect_content_task) =
294            match collect_buffer_info_and_text(buffer, Some(symbol_enclosing_range.clone()), cx) {
295                Ok((buffer_info, collect_context_task)) => (buffer_info, collect_context_task),
296                Err(err) => return Task::ready(Err(err)),
297            };
298
299        cx.spawn(async move |this, cx| {
300            let content = collect_content_task.await;
301
302            this.update(cx, |this, cx| {
303                this.insert_symbol(
304                    make_context_symbol(
305                        buffer_info,
306                        project_path,
307                        symbol_name,
308                        symbol_range,
309                        symbol_enclosing_range,
310                        content,
311                    ),
312                    cx,
313                )
314            })?;
315            anyhow::Ok(true)
316        })
317    }
318
319    fn insert_symbol(&mut self, context_symbol: ContextSymbol, cx: &mut Context<Self>) {
320        let id = self.next_context_id.post_inc();
321        self.symbols.insert(context_symbol.id.clone(), id);
322        self.symbols_by_path
323            .entry(context_symbol.id.path.clone())
324            .or_insert_with(Vec::new)
325            .push(context_symbol.id.clone());
326        self.symbol_buffers
327            .insert(context_symbol.id.clone(), context_symbol.buffer.clone());
328        self.context.push(AssistantContext::Symbol(SymbolContext {
329            id,
330            context_symbol,
331        }));
332        cx.notify();
333    }
334
335    pub fn add_thread(
336        &mut self,
337        thread: Entity<Thread>,
338        remove_if_exists: bool,
339        cx: &mut Context<Self>,
340    ) {
341        if let Some(context_id) = self.includes_thread(&thread.read(cx).id()) {
342            if remove_if_exists {
343                self.remove_context(context_id, cx);
344            }
345        } else {
346            self.insert_thread(thread, cx);
347        }
348    }
349
350    pub fn wait_for_summaries(&mut self, cx: &App) -> Task<()> {
351        let tasks = std::mem::take(&mut self.thread_summary_tasks);
352
353        cx.spawn(async move |_cx| {
354            join_all(tasks).await;
355        })
356    }
357
358    fn insert_thread(&mut self, thread: Entity<Thread>, cx: &mut Context<Self>) {
359        if let Some(summary_task) =
360            thread.update(cx, |thread, cx| thread.generate_detailed_summary(cx))
361        {
362            let thread = thread.clone();
363            let thread_store = self.thread_store.clone();
364
365            self.thread_summary_tasks.push(cx.spawn(async move |_, cx| {
366                summary_task.await;
367
368                if let Some(thread_store) = thread_store {
369                    // Save thread so its summary can be reused later
370                    let save_task = thread_store
371                        .update(cx, |thread_store, cx| thread_store.save_thread(&thread, cx));
372
373                    if let Some(save_task) = save_task.ok() {
374                        save_task.await.log_err();
375                    }
376                }
377            }));
378        }
379
380        let id = self.next_context_id.post_inc();
381
382        let text = thread.read(cx).latest_detailed_summary_or_text();
383
384        self.threads.insert(thread.read(cx).id().clone(), id);
385        self.context
386            .push(AssistantContext::Thread(ThreadContext { id, thread, text }));
387        cx.notify();
388    }
389
390    pub fn add_fetched_url(
391        &mut self,
392        url: String,
393        text: impl Into<SharedString>,
394        cx: &mut Context<ContextStore>,
395    ) {
396        if self.includes_url(&url).is_none() {
397            self.insert_fetched_url(url, text, cx);
398        }
399    }
400
401    fn insert_fetched_url(
402        &mut self,
403        url: String,
404        text: impl Into<SharedString>,
405        cx: &mut Context<ContextStore>,
406    ) {
407        let id = self.next_context_id.post_inc();
408
409        self.fetched_urls.insert(url.clone(), id);
410        self.context
411            .push(AssistantContext::FetchedUrl(FetchedUrlContext {
412                id,
413                url: url.into(),
414                text: text.into(),
415            }));
416        cx.notify();
417    }
418
419    pub fn accept_suggested_context(
420        &mut self,
421        suggested: &SuggestedContext,
422        cx: &mut Context<ContextStore>,
423    ) -> Task<Result<()>> {
424        match suggested {
425            SuggestedContext::File {
426                buffer,
427                icon_path: _,
428                name: _,
429            } => {
430                if let Some(buffer) = buffer.upgrade() {
431                    return self.add_file_from_buffer(buffer, cx);
432                };
433            }
434            SuggestedContext::Thread { thread, name: _ } => {
435                if let Some(thread) = thread.upgrade() {
436                    self.insert_thread(thread, cx);
437                };
438            }
439        }
440        Task::ready(Ok(()))
441    }
442
443    pub fn remove_context(&mut self, id: ContextId, cx: &mut Context<Self>) {
444        let Some(ix) = self.context.iter().position(|context| context.id() == id) else {
445            return;
446        };
447
448        match self.context.remove(ix) {
449            AssistantContext::File(_) => {
450                self.files.retain(|_, context_id| *context_id != id);
451            }
452            AssistantContext::Directory(_) => {
453                self.directories.retain(|_, context_id| *context_id != id);
454            }
455            AssistantContext::Symbol(symbol) => {
456                if let Some(symbols_in_path) =
457                    self.symbols_by_path.get_mut(&symbol.context_symbol.id.path)
458                {
459                    symbols_in_path.retain(|s| {
460                        self.symbols
461                            .get(s)
462                            .map_or(false, |context_id| *context_id != id)
463                    });
464                }
465                self.symbol_buffers.remove(&symbol.context_symbol.id);
466                self.symbols.retain(|_, context_id| *context_id != id);
467            }
468            AssistantContext::FetchedUrl(_) => {
469                self.fetched_urls.retain(|_, context_id| *context_id != id);
470            }
471            AssistantContext::Thread(_) => {
472                self.threads.retain(|_, context_id| *context_id != id);
473            }
474        }
475
476        cx.notify();
477    }
478
479    /// Returns whether the buffer is already included directly in the context, or if it will be
480    /// included in the context via a directory. Directory inclusion is based on paths rather than
481    /// buffer IDs as the directory will be re-scanned.
482    pub fn will_include_buffer(
483        &self,
484        buffer_id: BufferId,
485        project_path: &ProjectPath,
486    ) -> Option<FileInclusion> {
487        if let Some(context_id) = self.files.get(&buffer_id) {
488            return Some(FileInclusion::Direct(*context_id));
489        }
490
491        self.will_include_file_path_via_directory(project_path)
492    }
493
494    /// Returns whether this file path is already included directly in the context, or if it will be
495    /// included in the context via a directory.
496    pub fn will_include_file_path(
497        &self,
498        project_path: &ProjectPath,
499        cx: &App,
500    ) -> Option<FileInclusion> {
501        if !self.files.is_empty() {
502            let found_file_context = self.context.iter().find(|context| match &context {
503                AssistantContext::File(file_context) => {
504                    let buffer = file_context.context_buffer.buffer.read(cx);
505                    if let Some(context_path) = buffer.project_path(cx) {
506                        &context_path == project_path
507                    } else {
508                        false
509                    }
510                }
511                _ => false,
512            });
513            if let Some(context) = found_file_context {
514                return Some(FileInclusion::Direct(context.id()));
515            }
516        }
517
518        self.will_include_file_path_via_directory(project_path)
519    }
520
521    fn will_include_file_path_via_directory(
522        &self,
523        project_path: &ProjectPath,
524    ) -> Option<FileInclusion> {
525        if self.directories.is_empty() {
526            return None;
527        }
528
529        let mut path_buf = project_path.path.to_path_buf();
530
531        while path_buf.pop() {
532            // TODO: This isn't very efficient. Consider using a better representation of the
533            // directories map.
534            let directory_project_path = ProjectPath {
535                worktree_id: project_path.worktree_id,
536                path: path_buf.clone().into(),
537            };
538            if let Some(_) = self.directories.get(&directory_project_path) {
539                return Some(FileInclusion::InDirectory(directory_project_path));
540            }
541        }
542
543        None
544    }
545
546    pub fn includes_directory(&self, project_path: &ProjectPath) -> Option<FileInclusion> {
547        if let Some(context_id) = self.directories.get(project_path) {
548            return Some(FileInclusion::Direct(*context_id));
549        }
550
551        self.will_include_file_path_via_directory(project_path)
552    }
553
554    pub fn included_symbol(&self, symbol_id: &ContextSymbolId) -> Option<ContextId> {
555        self.symbols.get(symbol_id).copied()
556    }
557
558    pub fn included_symbols_by_path(&self) -> &HashMap<ProjectPath, Vec<ContextSymbolId>> {
559        &self.symbols_by_path
560    }
561
562    pub fn buffer_for_symbol(&self, symbol_id: &ContextSymbolId) -> Option<Entity<Buffer>> {
563        self.symbol_buffers.get(symbol_id).cloned()
564    }
565
566    pub fn includes_thread(&self, thread_id: &ThreadId) -> Option<ContextId> {
567        self.threads.get(thread_id).copied()
568    }
569
570    pub fn includes_url(&self, url: &str) -> Option<ContextId> {
571        self.fetched_urls.get(url).copied()
572    }
573
574    /// Replaces the context that matches the ID of the new context, if any match.
575    fn replace_context(&mut self, new_context: AssistantContext) {
576        let id = new_context.id();
577        for context in self.context.iter_mut() {
578            if context.id() == id {
579                *context = new_context;
580                break;
581            }
582        }
583    }
584
585    pub fn file_paths(&self, cx: &App) -> HashSet<ProjectPath> {
586        self.context
587            .iter()
588            .filter_map(|context| match context {
589                AssistantContext::File(file) => {
590                    let buffer = file.context_buffer.buffer.read(cx);
591                    buffer.project_path(cx)
592                }
593                AssistantContext::Directory(_)
594                | AssistantContext::Symbol(_)
595                | AssistantContext::FetchedUrl(_)
596                | AssistantContext::Thread(_) => None,
597            })
598            .collect()
599    }
600
601    pub fn thread_ids(&self) -> HashSet<ThreadId> {
602        self.threads.keys().cloned().collect()
603    }
604}
605
606pub enum FileInclusion {
607    Direct(ContextId),
608    InDirectory(ProjectPath),
609}
610
611// ContextBuffer without text.
612struct BufferInfo {
613    id: BufferId,
614    buffer: Entity<Buffer>,
615    file: Arc<dyn File>,
616    version: clock::Global,
617}
618
619fn make_context_buffer(info: BufferInfo, text: SharedString) -> ContextBuffer {
620    ContextBuffer {
621        id: info.id,
622        buffer: info.buffer,
623        file: info.file,
624        version: info.version,
625        text,
626    }
627}
628
629fn make_context_symbol(
630    info: BufferInfo,
631    path: ProjectPath,
632    name: SharedString,
633    range: Range<Anchor>,
634    enclosing_range: Range<Anchor>,
635    text: SharedString,
636) -> ContextSymbol {
637    ContextSymbol {
638        id: ContextSymbolId { name, range, path },
639        buffer_version: info.version,
640        enclosing_range,
641        buffer: info.buffer,
642        text,
643    }
644}
645
646fn collect_buffer_info_and_text(
647    buffer: Entity<Buffer>,
648    range: Option<Range<Anchor>>,
649    cx: &App,
650) -> Result<(BufferInfo, Task<SharedString>)> {
651    let buffer_ref = buffer.read(cx);
652    let file = buffer_ref.file().context("file context must have a path")?;
653
654    // Important to collect version at the same time as content so that staleness logic is correct.
655    let version = buffer_ref.version();
656    let content = if let Some(range) = range {
657        buffer_ref.text_for_range(range).collect::<Rope>()
658    } else {
659        buffer_ref.as_rope().clone()
660    };
661
662    let buffer_info = BufferInfo {
663        buffer,
664        id: buffer_ref.remote_id(),
665        file: file.clone(),
666        version,
667    };
668
669    let full_path = file.full_path(cx);
670    let text_task = cx.background_spawn(async move { to_fenced_codeblock(&full_path, content) });
671
672    Ok((buffer_info, text_task))
673}
674
675fn to_fenced_codeblock(path: &Path, content: Rope) -> SharedString {
676    let path_extension = path.extension().and_then(|ext| ext.to_str());
677    let path_string = path.to_string_lossy();
678    let capacity = 3
679        + path_extension.map_or(0, |extension| extension.len() + 1)
680        + path_string.len()
681        + 1
682        + content.len()
683        + 5;
684    let mut buffer = String::with_capacity(capacity);
685
686    buffer.push_str("```");
687
688    if let Some(extension) = path_extension {
689        buffer.push_str(extension);
690        buffer.push(' ');
691    }
692    buffer.push_str(&path_string);
693
694    buffer.push('\n');
695    for chunk in content.chunks() {
696        buffer.push_str(&chunk);
697    }
698
699    if !buffer.ends_with('\n') {
700        buffer.push('\n');
701    }
702
703    buffer.push_str("```\n");
704
705    debug_assert!(
706        buffer.len() == capacity - 1 || buffer.len() == capacity,
707        "to_fenced_codeblock calculated capacity of {}, but length was {}",
708        capacity,
709        buffer.len(),
710    );
711
712    buffer.into()
713}
714
715fn collect_files_in_path(worktree: &Worktree, path: &Path) -> Vec<Arc<Path>> {
716    let mut files = Vec::new();
717
718    for entry in worktree.child_entries(path) {
719        if entry.is_dir() {
720            files.extend(collect_files_in_path(worktree, &entry.path));
721        } else if entry.is_file() {
722            files.push(entry.path.clone());
723        }
724    }
725
726    files
727}
728
729pub fn refresh_context_store_text(
730    context_store: Entity<ContextStore>,
731    changed_buffers: &HashSet<Entity<Buffer>>,
732    cx: &App,
733) -> impl Future<Output = Vec<ContextId>> + use<> {
734    let mut tasks = Vec::new();
735
736    for context in &context_store.read(cx).context {
737        let id = context.id();
738
739        let task = maybe!({
740            match context {
741                AssistantContext::File(file_context) => {
742                    if changed_buffers.is_empty()
743                        || changed_buffers.contains(&file_context.context_buffer.buffer)
744                    {
745                        let context_store = context_store.clone();
746                        return refresh_file_text(context_store, file_context, cx);
747                    }
748                }
749                AssistantContext::Directory(directory_context) => {
750                    let directory_path = directory_context.project_path(cx);
751                    let should_refresh = changed_buffers.is_empty()
752                        || changed_buffers.iter().any(|buffer| {
753                            let Some(buffer_path) = buffer.read(cx).project_path(cx) else {
754                                return false;
755                            };
756                            buffer_path.starts_with(&directory_path)
757                        });
758
759                    if should_refresh {
760                        let context_store = context_store.clone();
761                        return refresh_directory_text(context_store, directory_context, cx);
762                    }
763                }
764                AssistantContext::Symbol(symbol_context) => {
765                    if changed_buffers.is_empty()
766                        || changed_buffers.contains(&symbol_context.context_symbol.buffer)
767                    {
768                        let context_store = context_store.clone();
769                        return refresh_symbol_text(context_store, symbol_context, cx);
770                    }
771                }
772                AssistantContext::Thread(thread_context) => {
773                    if changed_buffers.is_empty() {
774                        let context_store = context_store.clone();
775                        return Some(refresh_thread_text(context_store, thread_context, cx));
776                    }
777                }
778                // Intentionally omit refreshing fetched URLs as it doesn't seem all that useful,
779                // and doing the caching properly could be tricky (unless it's already handled by
780                // the HttpClient?).
781                AssistantContext::FetchedUrl(_) => {}
782            }
783
784            None
785        });
786
787        if let Some(task) = task {
788            tasks.push(task.map(move |_| id));
789        }
790    }
791
792    future::join_all(tasks)
793}
794
795fn refresh_file_text(
796    context_store: Entity<ContextStore>,
797    file_context: &FileContext,
798    cx: &App,
799) -> Option<Task<()>> {
800    let id = file_context.id;
801    let task = refresh_context_buffer(&file_context.context_buffer, cx);
802    if let Some(task) = task {
803        Some(cx.spawn(async move |cx| {
804            let context_buffer = task.await;
805            context_store
806                .update(cx, |context_store, _| {
807                    let new_file_context = FileContext { id, context_buffer };
808                    context_store.replace_context(AssistantContext::File(new_file_context));
809                })
810                .ok();
811        }))
812    } else {
813        None
814    }
815}
816
817fn refresh_directory_text(
818    context_store: Entity<ContextStore>,
819    directory_context: &DirectoryContext,
820    cx: &App,
821) -> Option<Task<()>> {
822    let mut stale = false;
823    let futures = directory_context
824        .context_buffers
825        .iter()
826        .map(|context_buffer| {
827            if let Some(refresh_task) = refresh_context_buffer(context_buffer, cx) {
828                stale = true;
829                future::Either::Left(refresh_task)
830            } else {
831                future::Either::Right(future::ready((*context_buffer).clone()))
832            }
833        })
834        .collect::<Vec<_>>();
835
836    if !stale {
837        return None;
838    }
839
840    let context_buffers = future::join_all(futures);
841
842    let id = directory_context.id;
843    let worktree = directory_context.worktree.clone();
844    let path = directory_context.path.clone();
845    Some(cx.spawn(async move |cx| {
846        let context_buffers = context_buffers.await;
847        context_store
848            .update(cx, |context_store, _| {
849                let new_directory_context = DirectoryContext {
850                    id,
851                    worktree,
852                    path,
853                    context_buffers,
854                };
855                context_store.replace_context(AssistantContext::Directory(new_directory_context));
856            })
857            .ok();
858    }))
859}
860
861fn refresh_symbol_text(
862    context_store: Entity<ContextStore>,
863    symbol_context: &SymbolContext,
864    cx: &App,
865) -> Option<Task<()>> {
866    let id = symbol_context.id;
867    let task = refresh_context_symbol(&symbol_context.context_symbol, cx);
868    if let Some(task) = task {
869        Some(cx.spawn(async move |cx| {
870            let context_symbol = task.await;
871            context_store
872                .update(cx, |context_store, _| {
873                    let new_symbol_context = SymbolContext { id, context_symbol };
874                    context_store.replace_context(AssistantContext::Symbol(new_symbol_context));
875                })
876                .ok();
877        }))
878    } else {
879        None
880    }
881}
882
883fn refresh_thread_text(
884    context_store: Entity<ContextStore>,
885    thread_context: &ThreadContext,
886    cx: &App,
887) -> Task<()> {
888    let id = thread_context.id;
889    let thread = thread_context.thread.clone();
890    cx.spawn(async move |cx| {
891        context_store
892            .update(cx, |context_store, cx| {
893                let text = thread.read(cx).latest_detailed_summary_or_text();
894                context_store.replace_context(AssistantContext::Thread(ThreadContext {
895                    id,
896                    thread,
897                    text,
898                }));
899            })
900            .ok();
901    })
902}
903
904fn refresh_context_buffer(
905    context_buffer: &ContextBuffer,
906    cx: &App,
907) -> Option<impl Future<Output = ContextBuffer> + use<>> {
908    let buffer = context_buffer.buffer.read(cx);
909    if buffer.version.changed_since(&context_buffer.version) {
910        let (buffer_info, text_task) =
911            collect_buffer_info_and_text(context_buffer.buffer.clone(), None, cx).log_err()?;
912        Some(text_task.map(move |text| make_context_buffer(buffer_info, text)))
913    } else {
914        None
915    }
916}
917
918fn refresh_context_symbol(
919    context_symbol: &ContextSymbol,
920    cx: &App,
921) -> Option<impl Future<Output = ContextSymbol> + use<>> {
922    let buffer = context_symbol.buffer.read(cx);
923    let project_path = buffer.project_path(cx)?;
924    if buffer.version.changed_since(&context_symbol.buffer_version) {
925        let (buffer_info, text_task) = collect_buffer_info_and_text(
926            context_symbol.buffer.clone(),
927            Some(context_symbol.enclosing_range.clone()),
928            cx,
929        )
930        .log_err()?;
931        let name = context_symbol.id.name.clone();
932        let range = context_symbol.id.range.clone();
933        let enclosing_range = context_symbol.enclosing_range.clone();
934        Some(text_task.map(move |text| {
935            make_context_symbol(
936                buffer_info,
937                project_path,
938                name,
939                range,
940                enclosing_range,
941                text,
942            )
943        }))
944    } else {
945        None
946    }
947}