1use crate::{
2 prompts::PromptBuilder, Context, ContextEvent, ContextId, ContextOperation, ContextVersion,
3 SavedContext, SavedContextMetadata,
4};
5use anyhow::{anyhow, Context as _, Result};
6use client::{proto, telemetry::Telemetry, Client, TypedEnvelope};
7use clock::ReplicaId;
8use fs::Fs;
9use futures::StreamExt;
10use fuzzy::StringMatchCandidate;
11use gpui::{
12 AppContext, AsyncAppContext, Context as _, EventEmitter, Model, ModelContext, Task, WeakModel,
13};
14use language::LanguageRegistry;
15use paths::contexts_dir;
16use project::Project;
17use regex::Regex;
18use rpc::AnyProtoClient;
19use std::{
20 cmp::Reverse,
21 ffi::OsStr,
22 mem,
23 path::{Path, PathBuf},
24 sync::Arc,
25 time::Duration,
26};
27use util::{ResultExt, TryFutureExt};
28
29pub fn init(client: &AnyProtoClient) {
30 client.add_model_message_handler(ContextStore::handle_advertise_contexts);
31 client.add_model_request_handler(ContextStore::handle_open_context);
32 client.add_model_request_handler(ContextStore::handle_create_context);
33 client.add_model_message_handler(ContextStore::handle_update_context);
34 client.add_model_request_handler(ContextStore::handle_synchronize_contexts);
35}
36
37#[derive(Clone)]
38pub struct RemoteContextMetadata {
39 pub id: ContextId,
40 pub summary: Option<String>,
41}
42
43pub struct ContextStore {
44 contexts: Vec<ContextHandle>,
45 contexts_metadata: Vec<SavedContextMetadata>,
46 host_contexts: Vec<RemoteContextMetadata>,
47 fs: Arc<dyn Fs>,
48 languages: Arc<LanguageRegistry>,
49 telemetry: Arc<Telemetry>,
50 _watch_updates: Task<Option<()>>,
51 client: Arc<Client>,
52 project: Model<Project>,
53 project_is_shared: bool,
54 client_subscription: Option<client::Subscription>,
55 _project_subscriptions: Vec<gpui::Subscription>,
56 prompt_builder: Arc<PromptBuilder>,
57}
58
59pub enum ContextStoreEvent {
60 ContextCreated(ContextId),
61}
62
63impl EventEmitter<ContextStoreEvent> for ContextStore {}
64
65enum ContextHandle {
66 Weak(WeakModel<Context>),
67 Strong(Model<Context>),
68}
69
70impl ContextHandle {
71 fn upgrade(&self) -> Option<Model<Context>> {
72 match self {
73 ContextHandle::Weak(weak) => weak.upgrade(),
74 ContextHandle::Strong(strong) => Some(strong.clone()),
75 }
76 }
77
78 fn downgrade(&self) -> WeakModel<Context> {
79 match self {
80 ContextHandle::Weak(weak) => weak.clone(),
81 ContextHandle::Strong(strong) => strong.downgrade(),
82 }
83 }
84}
85
86impl ContextStore {
87 pub fn new(
88 project: Model<Project>,
89 prompt_builder: Arc<PromptBuilder>,
90 cx: &mut AppContext,
91 ) -> Task<Result<Model<Self>>> {
92 let fs = project.read(cx).fs().clone();
93 let languages = project.read(cx).languages().clone();
94 let telemetry = project.read(cx).client().telemetry().clone();
95 cx.spawn(|mut cx| async move {
96 const CONTEXT_WATCH_DURATION: Duration = Duration::from_millis(100);
97 let (mut events, _) = fs.watch(contexts_dir(), CONTEXT_WATCH_DURATION).await;
98
99 let this = cx.new_model(|cx: &mut ModelContext<Self>| {
100 let mut this = Self {
101 contexts: Vec::new(),
102 contexts_metadata: Vec::new(),
103 host_contexts: Vec::new(),
104 fs,
105 languages,
106 telemetry,
107 _watch_updates: cx.spawn(|this, mut cx| {
108 async move {
109 while events.next().await.is_some() {
110 this.update(&mut cx, |this, cx| this.reload(cx))?
111 .await
112 .log_err();
113 }
114 anyhow::Ok(())
115 }
116 .log_err()
117 }),
118 client_subscription: None,
119 _project_subscriptions: vec![
120 cx.observe(&project, Self::handle_project_changed),
121 cx.subscribe(&project, Self::handle_project_event),
122 ],
123 project_is_shared: false,
124 client: project.read(cx).client(),
125 project: project.clone(),
126 prompt_builder,
127 };
128 this.handle_project_changed(project, cx);
129 this.synchronize_contexts(cx);
130 this
131 })?;
132 this.update(&mut cx, |this, cx| this.reload(cx))?
133 .await
134 .log_err();
135 Ok(this)
136 })
137 }
138
139 async fn handle_advertise_contexts(
140 this: Model<Self>,
141 envelope: TypedEnvelope<proto::AdvertiseContexts>,
142 mut cx: AsyncAppContext,
143 ) -> Result<()> {
144 this.update(&mut cx, |this, cx| {
145 this.host_contexts = envelope
146 .payload
147 .contexts
148 .into_iter()
149 .map(|context| RemoteContextMetadata {
150 id: ContextId::from_proto(context.context_id),
151 summary: context.summary,
152 })
153 .collect();
154 cx.notify();
155 })
156 }
157
158 async fn handle_open_context(
159 this: Model<Self>,
160 envelope: TypedEnvelope<proto::OpenContext>,
161 mut cx: AsyncAppContext,
162 ) -> Result<proto::OpenContextResponse> {
163 let context_id = ContextId::from_proto(envelope.payload.context_id);
164 let operations = this.update(&mut cx, |this, cx| {
165 if this.project.read(cx).is_via_collab() {
166 return Err(anyhow!("only the host contexts can be opened"));
167 }
168
169 let context = this
170 .loaded_context_for_id(&context_id, cx)
171 .context("context not found")?;
172 if context.read(cx).replica_id() != ReplicaId::default() {
173 return Err(anyhow!("context must be opened via the host"));
174 }
175
176 anyhow::Ok(
177 context
178 .read(cx)
179 .serialize_ops(&ContextVersion::default(), cx),
180 )
181 })??;
182 let operations = operations.await;
183 Ok(proto::OpenContextResponse {
184 context: Some(proto::Context { operations }),
185 })
186 }
187
188 async fn handle_create_context(
189 this: Model<Self>,
190 _: TypedEnvelope<proto::CreateContext>,
191 mut cx: AsyncAppContext,
192 ) -> Result<proto::CreateContextResponse> {
193 let (context_id, operations) = this.update(&mut cx, |this, cx| {
194 if this.project.read(cx).is_via_collab() {
195 return Err(anyhow!("can only create contexts as the host"));
196 }
197
198 let context = this.create(cx);
199 let context_id = context.read(cx).id().clone();
200 cx.emit(ContextStoreEvent::ContextCreated(context_id.clone()));
201
202 anyhow::Ok((
203 context_id,
204 context
205 .read(cx)
206 .serialize_ops(&ContextVersion::default(), cx),
207 ))
208 })??;
209 let operations = operations.await;
210 Ok(proto::CreateContextResponse {
211 context_id: context_id.to_proto(),
212 context: Some(proto::Context { operations }),
213 })
214 }
215
216 async fn handle_update_context(
217 this: Model<Self>,
218 envelope: TypedEnvelope<proto::UpdateContext>,
219 mut cx: AsyncAppContext,
220 ) -> Result<()> {
221 this.update(&mut cx, |this, cx| {
222 let context_id = ContextId::from_proto(envelope.payload.context_id);
223 if let Some(context) = this.loaded_context_for_id(&context_id, cx) {
224 let operation_proto = envelope.payload.operation.context("invalid operation")?;
225 let operation = ContextOperation::from_proto(operation_proto)?;
226 context.update(cx, |context, cx| context.apply_ops([operation], cx));
227 }
228 Ok(())
229 })?
230 }
231
232 async fn handle_synchronize_contexts(
233 this: Model<Self>,
234 envelope: TypedEnvelope<proto::SynchronizeContexts>,
235 mut cx: AsyncAppContext,
236 ) -> Result<proto::SynchronizeContextsResponse> {
237 this.update(&mut cx, |this, cx| {
238 if this.project.read(cx).is_via_collab() {
239 return Err(anyhow!("only the host can synchronize contexts"));
240 }
241
242 let mut local_versions = Vec::new();
243 for remote_version_proto in envelope.payload.contexts {
244 let remote_version = ContextVersion::from_proto(&remote_version_proto);
245 let context_id = ContextId::from_proto(remote_version_proto.context_id);
246 if let Some(context) = this.loaded_context_for_id(&context_id, cx) {
247 let context = context.read(cx);
248 let operations = context.serialize_ops(&remote_version, cx);
249 local_versions.push(context.version(cx).to_proto(context_id.clone()));
250 let client = this.client.clone();
251 let project_id = envelope.payload.project_id;
252 cx.background_executor()
253 .spawn(async move {
254 let operations = operations.await;
255 for operation in operations {
256 client.send(proto::UpdateContext {
257 project_id,
258 context_id: context_id.to_proto(),
259 operation: Some(operation),
260 })?;
261 }
262 anyhow::Ok(())
263 })
264 .detach_and_log_err(cx);
265 }
266 }
267
268 this.advertise_contexts(cx);
269
270 anyhow::Ok(proto::SynchronizeContextsResponse {
271 contexts: local_versions,
272 })
273 })?
274 }
275
276 fn handle_project_changed(&mut self, _: Model<Project>, cx: &mut ModelContext<Self>) {
277 let is_shared = self.project.read(cx).is_shared();
278 let was_shared = mem::replace(&mut self.project_is_shared, is_shared);
279 if is_shared == was_shared {
280 return;
281 }
282
283 if is_shared {
284 self.contexts.retain_mut(|context| {
285 if let Some(strong_context) = context.upgrade() {
286 *context = ContextHandle::Strong(strong_context);
287 true
288 } else {
289 false
290 }
291 });
292 let remote_id = self.project.read(cx).remote_id().unwrap();
293 self.client_subscription = self
294 .client
295 .subscribe_to_entity(remote_id)
296 .log_err()
297 .map(|subscription| subscription.set_model(&cx.handle(), &mut cx.to_async()));
298 self.advertise_contexts(cx);
299 } else {
300 self.client_subscription = None;
301 }
302 }
303
304 fn handle_project_event(
305 &mut self,
306 _: Model<Project>,
307 event: &project::Event,
308 cx: &mut ModelContext<Self>,
309 ) {
310 match event {
311 project::Event::Reshared => {
312 self.advertise_contexts(cx);
313 }
314 project::Event::HostReshared | project::Event::Rejoined => {
315 self.synchronize_contexts(cx);
316 }
317 project::Event::DisconnectedFromHost => {
318 self.contexts.retain_mut(|context| {
319 if let Some(strong_context) = context.upgrade() {
320 *context = ContextHandle::Weak(context.downgrade());
321 strong_context.update(cx, |context, cx| {
322 if context.replica_id() != ReplicaId::default() {
323 context.set_capability(language::Capability::ReadOnly, cx);
324 }
325 });
326 true
327 } else {
328 false
329 }
330 });
331 self.host_contexts.clear();
332 cx.notify();
333 }
334 _ => {}
335 }
336 }
337
338 pub fn create(&mut self, cx: &mut ModelContext<Self>) -> Model<Context> {
339 let context = cx.new_model(|cx| {
340 Context::local(
341 self.languages.clone(),
342 Some(self.project.clone()),
343 Some(self.telemetry.clone()),
344 self.prompt_builder.clone(),
345 cx,
346 )
347 });
348 self.register_context(&context, cx);
349 context
350 }
351
352 pub fn create_remote_context(
353 &mut self,
354 cx: &mut ModelContext<Self>,
355 ) -> Task<Result<Model<Context>>> {
356 let project = self.project.read(cx);
357 let Some(project_id) = project.remote_id() else {
358 return Task::ready(Err(anyhow!("project was not remote")));
359 };
360
361 let replica_id = project.replica_id();
362 let capability = project.capability();
363 let language_registry = self.languages.clone();
364 let project = self.project.clone();
365 let telemetry = self.telemetry.clone();
366 let prompt_builder = self.prompt_builder.clone();
367 let request = self.client.request(proto::CreateContext { project_id });
368 cx.spawn(|this, mut cx| async move {
369 let response = request.await?;
370 let context_id = ContextId::from_proto(response.context_id);
371 let context_proto = response.context.context("invalid context")?;
372 let context = cx.new_model(|cx| {
373 Context::new(
374 context_id.clone(),
375 replica_id,
376 capability,
377 language_registry,
378 prompt_builder,
379 Some(project),
380 Some(telemetry),
381 cx,
382 )
383 })?;
384 let operations = cx
385 .background_executor()
386 .spawn(async move {
387 context_proto
388 .operations
389 .into_iter()
390 .map(ContextOperation::from_proto)
391 .collect::<Result<Vec<_>>>()
392 })
393 .await?;
394 context.update(&mut cx, |context, cx| context.apply_ops(operations, cx))?;
395 this.update(&mut cx, |this, cx| {
396 if let Some(existing_context) = this.loaded_context_for_id(&context_id, cx) {
397 existing_context
398 } else {
399 this.register_context(&context, cx);
400 this.synchronize_contexts(cx);
401 context
402 }
403 })
404 })
405 }
406
407 pub fn open_local_context(
408 &mut self,
409 path: PathBuf,
410 cx: &ModelContext<Self>,
411 ) -> Task<Result<Model<Context>>> {
412 if let Some(existing_context) = self.loaded_context_for_path(&path, cx) {
413 return Task::ready(Ok(existing_context));
414 }
415
416 let fs = self.fs.clone();
417 let languages = self.languages.clone();
418 let project = self.project.clone();
419 let telemetry = self.telemetry.clone();
420 let load = cx.background_executor().spawn({
421 let path = path.clone();
422 async move {
423 let saved_context = fs.load(&path).await?;
424 SavedContext::from_json(&saved_context)
425 }
426 });
427 let prompt_builder = self.prompt_builder.clone();
428
429 cx.spawn(|this, mut cx| async move {
430 let saved_context = load.await?;
431 let context = cx.new_model(|cx| {
432 Context::deserialize(
433 saved_context,
434 path.clone(),
435 languages,
436 prompt_builder,
437 Some(project),
438 Some(telemetry),
439 cx,
440 )
441 })?;
442 this.update(&mut cx, |this, cx| {
443 if let Some(existing_context) = this.loaded_context_for_path(&path, cx) {
444 existing_context
445 } else {
446 this.register_context(&context, cx);
447 context
448 }
449 })
450 })
451 }
452
453 fn loaded_context_for_path(&self, path: &Path, cx: &AppContext) -> Option<Model<Context>> {
454 self.contexts.iter().find_map(|context| {
455 let context = context.upgrade()?;
456 if context.read(cx).path() == Some(path) {
457 Some(context)
458 } else {
459 None
460 }
461 })
462 }
463
464 pub(super) fn loaded_context_for_id(
465 &self,
466 id: &ContextId,
467 cx: &AppContext,
468 ) -> Option<Model<Context>> {
469 self.contexts.iter().find_map(|context| {
470 let context = context.upgrade()?;
471 if context.read(cx).id() == id {
472 Some(context)
473 } else {
474 None
475 }
476 })
477 }
478
479 pub fn open_remote_context(
480 &mut self,
481 context_id: ContextId,
482 cx: &mut ModelContext<Self>,
483 ) -> Task<Result<Model<Context>>> {
484 let project = self.project.read(cx);
485 let Some(project_id) = project.remote_id() else {
486 return Task::ready(Err(anyhow!("project was not remote")));
487 };
488
489 if let Some(context) = self.loaded_context_for_id(&context_id, cx) {
490 return Task::ready(Ok(context));
491 }
492
493 let replica_id = project.replica_id();
494 let capability = project.capability();
495 let language_registry = self.languages.clone();
496 let project = self.project.clone();
497 let telemetry = self.telemetry.clone();
498 let request = self.client.request(proto::OpenContext {
499 project_id,
500 context_id: context_id.to_proto(),
501 });
502 let prompt_builder = self.prompt_builder.clone();
503 cx.spawn(|this, mut cx| async move {
504 let response = request.await?;
505 let context_proto = response.context.context("invalid context")?;
506 let context = cx.new_model(|cx| {
507 Context::new(
508 context_id.clone(),
509 replica_id,
510 capability,
511 language_registry,
512 prompt_builder,
513 Some(project),
514 Some(telemetry),
515 cx,
516 )
517 })?;
518 let operations = cx
519 .background_executor()
520 .spawn(async move {
521 context_proto
522 .operations
523 .into_iter()
524 .map(ContextOperation::from_proto)
525 .collect::<Result<Vec<_>>>()
526 })
527 .await?;
528 context.update(&mut cx, |context, cx| context.apply_ops(operations, cx))?;
529 this.update(&mut cx, |this, cx| {
530 if let Some(existing_context) = this.loaded_context_for_id(&context_id, cx) {
531 existing_context
532 } else {
533 this.register_context(&context, cx);
534 this.synchronize_contexts(cx);
535 context
536 }
537 })
538 })
539 }
540
541 fn register_context(&mut self, context: &Model<Context>, cx: &mut ModelContext<Self>) {
542 let handle = if self.project_is_shared {
543 ContextHandle::Strong(context.clone())
544 } else {
545 ContextHandle::Weak(context.downgrade())
546 };
547 self.contexts.push(handle);
548 self.advertise_contexts(cx);
549 cx.subscribe(context, Self::handle_context_event).detach();
550 }
551
552 fn handle_context_event(
553 &mut self,
554 context: Model<Context>,
555 event: &ContextEvent,
556 cx: &mut ModelContext<Self>,
557 ) {
558 let Some(project_id) = self.project.read(cx).remote_id() else {
559 return;
560 };
561
562 match event {
563 ContextEvent::SummaryChanged => {
564 self.advertise_contexts(cx);
565 }
566 ContextEvent::Operation(operation) => {
567 let context_id = context.read(cx).id().to_proto();
568 let operation = operation.to_proto();
569 self.client
570 .send(proto::UpdateContext {
571 project_id,
572 context_id,
573 operation: Some(operation),
574 })
575 .log_err();
576 }
577 _ => {}
578 }
579 }
580
581 fn advertise_contexts(&self, cx: &AppContext) {
582 let Some(project_id) = self.project.read(cx).remote_id() else {
583 return;
584 };
585
586 // For now, only the host can advertise their open contexts.
587 if self.project.read(cx).is_via_collab() {
588 return;
589 }
590
591 let contexts = self
592 .contexts
593 .iter()
594 .rev()
595 .filter_map(|context| {
596 let context = context.upgrade()?.read(cx);
597 if context.replica_id() == ReplicaId::default() {
598 Some(proto::ContextMetadata {
599 context_id: context.id().to_proto(),
600 summary: context.summary().map(|summary| summary.text.clone()),
601 })
602 } else {
603 None
604 }
605 })
606 .collect();
607 self.client
608 .send(proto::AdvertiseContexts {
609 project_id,
610 contexts,
611 })
612 .ok();
613 }
614
615 fn synchronize_contexts(&mut self, cx: &mut ModelContext<Self>) {
616 let Some(project_id) = self.project.read(cx).remote_id() else {
617 return;
618 };
619
620 let contexts = self
621 .contexts
622 .iter()
623 .filter_map(|context| {
624 let context = context.upgrade()?.read(cx);
625 if context.replica_id() != ReplicaId::default() {
626 Some(context.version(cx).to_proto(context.id().clone()))
627 } else {
628 None
629 }
630 })
631 .collect();
632
633 let client = self.client.clone();
634 let request = self.client.request(proto::SynchronizeContexts {
635 project_id,
636 contexts,
637 });
638 cx.spawn(|this, cx| async move {
639 let response = request.await?;
640
641 let mut context_ids = Vec::new();
642 let mut operations = Vec::new();
643 this.read_with(&cx, |this, cx| {
644 for context_version_proto in response.contexts {
645 let context_version = ContextVersion::from_proto(&context_version_proto);
646 let context_id = ContextId::from_proto(context_version_proto.context_id);
647 if let Some(context) = this.loaded_context_for_id(&context_id, cx) {
648 context_ids.push(context_id);
649 operations.push(context.read(cx).serialize_ops(&context_version, cx));
650 }
651 }
652 })?;
653
654 let operations = futures::future::join_all(operations).await;
655 for (context_id, operations) in context_ids.into_iter().zip(operations) {
656 for operation in operations {
657 client.send(proto::UpdateContext {
658 project_id,
659 context_id: context_id.to_proto(),
660 operation: Some(operation),
661 })?;
662 }
663 }
664
665 anyhow::Ok(())
666 })
667 .detach_and_log_err(cx);
668 }
669
670 pub fn search(&self, query: String, cx: &AppContext) -> Task<Vec<SavedContextMetadata>> {
671 let metadata = self.contexts_metadata.clone();
672 let executor = cx.background_executor().clone();
673 cx.background_executor().spawn(async move {
674 if query.is_empty() {
675 metadata
676 } else {
677 let candidates = metadata
678 .iter()
679 .enumerate()
680 .map(|(id, metadata)| StringMatchCandidate::new(id, metadata.title.clone()))
681 .collect::<Vec<_>>();
682 let matches = fuzzy::match_strings(
683 &candidates,
684 &query,
685 false,
686 100,
687 &Default::default(),
688 executor,
689 )
690 .await;
691
692 matches
693 .into_iter()
694 .map(|mat| metadata[mat.candidate_id].clone())
695 .collect()
696 }
697 })
698 }
699
700 pub fn host_contexts(&self) -> &[RemoteContextMetadata] {
701 &self.host_contexts
702 }
703
704 fn reload(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
705 let fs = self.fs.clone();
706 cx.spawn(|this, mut cx| async move {
707 fs.create_dir(contexts_dir()).await?;
708
709 let mut paths = fs.read_dir(contexts_dir()).await?;
710 let mut contexts = Vec::<SavedContextMetadata>::new();
711 while let Some(path) = paths.next().await {
712 let path = path?;
713 if path.extension() != Some(OsStr::new("json")) {
714 continue;
715 }
716
717 let pattern = r" - \d+.zed.json$";
718 let re = Regex::new(pattern).unwrap();
719
720 let metadata = fs.metadata(&path).await?;
721 if let Some((file_name, metadata)) = path
722 .file_name()
723 .and_then(|name| name.to_str())
724 .zip(metadata)
725 {
726 // This is used to filter out contexts saved by the new assistant.
727 if !re.is_match(file_name) {
728 continue;
729 }
730
731 if let Some(title) = re.replace(file_name, "").lines().next() {
732 contexts.push(SavedContextMetadata {
733 title: title.to_string(),
734 path,
735 mtime: metadata.mtime.into(),
736 });
737 }
738 }
739 }
740 contexts.sort_unstable_by_key(|context| Reverse(context.mtime));
741
742 this.update(&mut cx, |this, cx| {
743 this.contexts_metadata = contexts;
744 cx.notify();
745 })
746 })
747 }
748}