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