1use crate::{
2 ContextServerRegistry, Thread, ThreadEvent, ThreadsDatabase, ToolCallAuthorization,
3 UserMessageContent, templates::Templates,
4};
5use crate::{HistoryStore, TitleUpdated, TokenUsageUpdated};
6use acp_thread::{AcpThread, AgentModelSelector};
7use action_log::ActionLog;
8use agent_client_protocol as acp;
9use agent_settings::AgentSettings;
10use anyhow::{Context as _, Result, anyhow};
11use collections::{HashSet, IndexMap};
12use fs::Fs;
13use futures::channel::mpsc;
14use futures::{StreamExt, future};
15use gpui::{
16 App, AppContext, AsyncApp, Context, Entity, SharedString, Subscription, Task, WeakEntity,
17};
18use language_model::{LanguageModel, LanguageModelProvider, LanguageModelRegistry};
19use project::{Project, ProjectItem, ProjectPath, Worktree};
20use prompt_store::{
21 ProjectContext, PromptId, PromptStore, RulesFileContext, UserRulesContext, WorktreeContext,
22};
23use settings::update_settings_file;
24use std::any::Any;
25use std::collections::HashMap;
26use std::path::Path;
27use std::rc::Rc;
28use std::sync::Arc;
29use util::ResultExt;
30
31const RULES_FILE_NAMES: [&str; 9] = [
32 ".rules",
33 ".cursorrules",
34 ".windsurfrules",
35 ".clinerules",
36 ".github/copilot-instructions.md",
37 "CLAUDE.md",
38 "AGENT.md",
39 "AGENTS.md",
40 "GEMINI.md",
41];
42
43pub struct RulesLoadingError {
44 pub message: SharedString,
45}
46
47/// Holds both the internal Thread and the AcpThread for a session
48struct Session {
49 /// The internal thread that processes messages
50 thread: Entity<Thread>,
51 /// The ACP thread that handles protocol communication
52 acp_thread: WeakEntity<acp_thread::AcpThread>,
53 pending_save: Task<()>,
54 _subscriptions: Vec<Subscription>,
55}
56
57pub struct LanguageModels {
58 /// Access language model by ID
59 models: HashMap<acp_thread::AgentModelId, Arc<dyn LanguageModel>>,
60 /// Cached list for returning language model information
61 model_list: acp_thread::AgentModelList,
62 refresh_models_rx: watch::Receiver<()>,
63 refresh_models_tx: watch::Sender<()>,
64}
65
66impl LanguageModels {
67 fn new(cx: &App) -> Self {
68 let (refresh_models_tx, refresh_models_rx) = watch::channel(());
69 let mut this = Self {
70 models: HashMap::default(),
71 model_list: acp_thread::AgentModelList::Grouped(IndexMap::default()),
72 refresh_models_rx,
73 refresh_models_tx,
74 };
75 this.refresh_list(cx);
76 this
77 }
78
79 fn refresh_list(&mut self, cx: &App) {
80 let providers = LanguageModelRegistry::global(cx)
81 .read(cx)
82 .providers()
83 .into_iter()
84 .filter(|provider| provider.is_authenticated(cx))
85 .collect::<Vec<_>>();
86
87 let mut language_model_list = IndexMap::default();
88 let mut recommended_models = HashSet::default();
89
90 let mut recommended = Vec::new();
91 for provider in &providers {
92 for model in provider.recommended_models(cx) {
93 recommended_models.insert(model.id());
94 recommended.push(Self::map_language_model_to_info(&model, provider));
95 }
96 }
97 if !recommended.is_empty() {
98 language_model_list.insert(
99 acp_thread::AgentModelGroupName("Recommended".into()),
100 recommended,
101 );
102 }
103
104 let mut models = HashMap::default();
105 for provider in providers {
106 let mut provider_models = Vec::new();
107 for model in provider.provided_models(cx) {
108 let model_info = Self::map_language_model_to_info(&model, &provider);
109 let model_id = model_info.id.clone();
110 if !recommended_models.contains(&model.id()) {
111 provider_models.push(model_info);
112 }
113 models.insert(model_id, model);
114 }
115 if !provider_models.is_empty() {
116 language_model_list.insert(
117 acp_thread::AgentModelGroupName(provider.name().0.clone()),
118 provider_models,
119 );
120 }
121 }
122
123 self.models = models;
124 self.model_list = acp_thread::AgentModelList::Grouped(language_model_list);
125 self.refresh_models_tx.send(()).ok();
126 }
127
128 fn watch(&self) -> watch::Receiver<()> {
129 self.refresh_models_rx.clone()
130 }
131
132 pub fn model_from_id(
133 &self,
134 model_id: &acp_thread::AgentModelId,
135 ) -> Option<Arc<dyn LanguageModel>> {
136 self.models.get(model_id).cloned()
137 }
138
139 fn map_language_model_to_info(
140 model: &Arc<dyn LanguageModel>,
141 provider: &Arc<dyn LanguageModelProvider>,
142 ) -> acp_thread::AgentModelInfo {
143 acp_thread::AgentModelInfo {
144 id: Self::model_id(model),
145 name: model.name().0,
146 icon: Some(provider.icon()),
147 }
148 }
149
150 fn model_id(model: &Arc<dyn LanguageModel>) -> acp_thread::AgentModelId {
151 acp_thread::AgentModelId(format!("{}/{}", model.provider_id().0, model.id().0).into())
152 }
153}
154
155pub struct NativeAgent {
156 /// Session ID -> Session mapping
157 sessions: HashMap<acp::SessionId, Session>,
158 history: Entity<HistoryStore>,
159 /// Shared project context for all threads
160 project_context: Entity<ProjectContext>,
161 project_context_needs_refresh: watch::Sender<()>,
162 _maintain_project_context: Task<Result<()>>,
163 context_server_registry: Entity<ContextServerRegistry>,
164 /// Shared templates for all threads
165 templates: Arc<Templates>,
166 /// Cached model information
167 models: LanguageModels,
168 project: Entity<Project>,
169 prompt_store: Option<Entity<PromptStore>>,
170 fs: Arc<dyn Fs>,
171 _subscriptions: Vec<Subscription>,
172}
173
174impl NativeAgent {
175 pub async fn new(
176 project: Entity<Project>,
177 history: Entity<HistoryStore>,
178 templates: Arc<Templates>,
179 prompt_store: Option<Entity<PromptStore>>,
180 fs: Arc<dyn Fs>,
181 cx: &mut AsyncApp,
182 ) -> Result<Entity<NativeAgent>> {
183 log::info!("Creating new NativeAgent");
184
185 let project_context = cx
186 .update(|cx| Self::build_project_context(&project, prompt_store.as_ref(), cx))?
187 .await;
188
189 cx.new(|cx| {
190 let mut subscriptions = vec![
191 cx.subscribe(&project, Self::handle_project_event),
192 cx.subscribe(
193 &LanguageModelRegistry::global(cx),
194 Self::handle_models_updated_event,
195 ),
196 ];
197 if let Some(prompt_store) = prompt_store.as_ref() {
198 subscriptions.push(cx.subscribe(prompt_store, Self::handle_prompts_updated_event))
199 }
200
201 let (project_context_needs_refresh_tx, project_context_needs_refresh_rx) =
202 watch::channel(());
203 Self {
204 sessions: HashMap::new(),
205 history,
206 project_context: cx.new(|_| project_context),
207 project_context_needs_refresh: project_context_needs_refresh_tx,
208 _maintain_project_context: cx.spawn(async move |this, cx| {
209 Self::maintain_project_context(this, project_context_needs_refresh_rx, cx).await
210 }),
211 context_server_registry: cx.new(|cx| {
212 ContextServerRegistry::new(project.read(cx).context_server_store(), cx)
213 }),
214 templates,
215 models: LanguageModels::new(cx),
216 project,
217 prompt_store,
218 fs,
219 _subscriptions: subscriptions,
220 }
221 })
222 }
223
224 fn register_session(
225 &mut self,
226 thread_handle: Entity<Thread>,
227 cx: &mut Context<Self>,
228 ) -> Entity<AcpThread> {
229 let connection = Rc::new(NativeAgentConnection(cx.entity()));
230 let registry = LanguageModelRegistry::read_global(cx);
231 let summarization_model = registry.thread_summary_model(cx).map(|c| c.model);
232
233 thread_handle.update(cx, |thread, cx| {
234 thread.set_summarization_model(summarization_model, cx);
235 thread.add_default_tools(cx)
236 });
237
238 let thread = thread_handle.read(cx);
239 let session_id = thread.id().clone();
240 let title = thread.title();
241 let project = thread.project.clone();
242 let action_log = thread.action_log.clone();
243 let acp_thread = cx.new(|_cx| {
244 acp_thread::AcpThread::new(
245 title,
246 connection,
247 project.clone(),
248 action_log.clone(),
249 session_id.clone(),
250 )
251 });
252 let subscriptions = vec![
253 cx.observe_release(&acp_thread, |this, acp_thread, _cx| {
254 this.sessions.remove(acp_thread.session_id());
255 }),
256 cx.subscribe(&thread_handle, Self::handle_thread_title_updated),
257 cx.subscribe(&thread_handle, Self::handle_thread_token_usage_updated),
258 cx.observe(&thread_handle, move |this, thread, cx| {
259 this.save_thread(thread, cx)
260 }),
261 ];
262
263 self.sessions.insert(
264 session_id,
265 Session {
266 thread: thread_handle,
267 acp_thread: acp_thread.downgrade(),
268 _subscriptions: subscriptions,
269 pending_save: Task::ready(()),
270 },
271 );
272 acp_thread
273 }
274
275 pub fn models(&self) -> &LanguageModels {
276 &self.models
277 }
278
279 async fn maintain_project_context(
280 this: WeakEntity<Self>,
281 mut needs_refresh: watch::Receiver<()>,
282 cx: &mut AsyncApp,
283 ) -> Result<()> {
284 while needs_refresh.changed().await.is_ok() {
285 let project_context = this
286 .update(cx, |this, cx| {
287 Self::build_project_context(&this.project, this.prompt_store.as_ref(), cx)
288 })?
289 .await;
290 this.update(cx, |this, cx| {
291 this.project_context = cx.new(|_| project_context);
292 })?;
293 }
294
295 Ok(())
296 }
297
298 fn build_project_context(
299 project: &Entity<Project>,
300 prompt_store: Option<&Entity<PromptStore>>,
301 cx: &mut App,
302 ) -> Task<ProjectContext> {
303 let worktrees = project.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
304 let worktree_tasks = worktrees
305 .into_iter()
306 .map(|worktree| {
307 Self::load_worktree_info_for_system_prompt(worktree, project.clone(), cx)
308 })
309 .collect::<Vec<_>>();
310 let default_user_rules_task = if let Some(prompt_store) = prompt_store.as_ref() {
311 prompt_store.read_with(cx, |prompt_store, cx| {
312 let prompts = prompt_store.default_prompt_metadata();
313 let load_tasks = prompts.into_iter().map(|prompt_metadata| {
314 let contents = prompt_store.load(prompt_metadata.id, cx);
315 async move { (contents.await, prompt_metadata) }
316 });
317 cx.background_spawn(future::join_all(load_tasks))
318 })
319 } else {
320 Task::ready(vec![])
321 };
322
323 cx.spawn(async move |_cx| {
324 let (worktrees, default_user_rules) =
325 future::join(future::join_all(worktree_tasks), default_user_rules_task).await;
326
327 let worktrees = worktrees
328 .into_iter()
329 .map(|(worktree, _rules_error)| {
330 // TODO: show error message
331 // if let Some(rules_error) = rules_error {
332 // this.update(cx, |_, cx| cx.emit(rules_error)).ok();
333 // }
334 worktree
335 })
336 .collect::<Vec<_>>();
337
338 let default_user_rules = default_user_rules
339 .into_iter()
340 .flat_map(|(contents, prompt_metadata)| match contents {
341 Ok(contents) => Some(UserRulesContext {
342 uuid: match prompt_metadata.id {
343 PromptId::User { uuid } => uuid,
344 PromptId::EditWorkflow => return None,
345 },
346 title: prompt_metadata.title.map(|title| title.to_string()),
347 contents,
348 }),
349 Err(_err) => {
350 // TODO: show error message
351 // this.update(cx, |_, cx| {
352 // cx.emit(RulesLoadingError {
353 // message: format!("{err:?}").into(),
354 // });
355 // })
356 // .ok();
357 None
358 }
359 })
360 .collect::<Vec<_>>();
361
362 ProjectContext::new(worktrees, default_user_rules)
363 })
364 }
365
366 fn load_worktree_info_for_system_prompt(
367 worktree: Entity<Worktree>,
368 project: Entity<Project>,
369 cx: &mut App,
370 ) -> Task<(WorktreeContext, Option<RulesLoadingError>)> {
371 let tree = worktree.read(cx);
372 let root_name = tree.root_name().into();
373 let abs_path = tree.abs_path();
374
375 let mut context = WorktreeContext {
376 root_name,
377 abs_path,
378 rules_file: None,
379 };
380
381 let rules_task = Self::load_worktree_rules_file(worktree, project, cx);
382 let Some(rules_task) = rules_task else {
383 return Task::ready((context, None));
384 };
385
386 cx.spawn(async move |_| {
387 let (rules_file, rules_file_error) = match rules_task.await {
388 Ok(rules_file) => (Some(rules_file), None),
389 Err(err) => (
390 None,
391 Some(RulesLoadingError {
392 message: format!("{err}").into(),
393 }),
394 ),
395 };
396 context.rules_file = rules_file;
397 (context, rules_file_error)
398 })
399 }
400
401 fn load_worktree_rules_file(
402 worktree: Entity<Worktree>,
403 project: Entity<Project>,
404 cx: &mut App,
405 ) -> Option<Task<Result<RulesFileContext>>> {
406 let worktree = worktree.read(cx);
407 let worktree_id = worktree.id();
408 let selected_rules_file = RULES_FILE_NAMES
409 .into_iter()
410 .filter_map(|name| {
411 worktree
412 .entry_for_path(name)
413 .filter(|entry| entry.is_file())
414 .map(|entry| entry.path.clone())
415 })
416 .next();
417
418 // Note that Cline supports `.clinerules` being a directory, but that is not currently
419 // supported. This doesn't seem to occur often in GitHub repositories.
420 selected_rules_file.map(|path_in_worktree| {
421 let project_path = ProjectPath {
422 worktree_id,
423 path: path_in_worktree.clone(),
424 };
425 let buffer_task =
426 project.update(cx, |project, cx| project.open_buffer(project_path, cx));
427 let rope_task = cx.spawn(async move |cx| {
428 buffer_task.await?.read_with(cx, |buffer, cx| {
429 let project_entry_id = buffer.entry_id(cx).context("buffer has no file")?;
430 anyhow::Ok((project_entry_id, buffer.as_rope().clone()))
431 })?
432 });
433 // Build a string from the rope on a background thread.
434 cx.background_spawn(async move {
435 let (project_entry_id, rope) = rope_task.await?;
436 anyhow::Ok(RulesFileContext {
437 path_in_worktree,
438 text: rope.to_string().trim().to_string(),
439 project_entry_id: project_entry_id.to_usize(),
440 })
441 })
442 })
443 }
444
445 fn handle_thread_title_updated(
446 &mut self,
447 thread: Entity<Thread>,
448 _: &TitleUpdated,
449 cx: &mut Context<Self>,
450 ) {
451 let session_id = thread.read(cx).id();
452 let Some(session) = self.sessions.get(session_id) else {
453 return;
454 };
455 let thread = thread.downgrade();
456 let acp_thread = session.acp_thread.clone();
457 cx.spawn(async move |_, cx| {
458 let title = thread.read_with(cx, |thread, _| thread.title())?;
459 let task = acp_thread.update(cx, |acp_thread, cx| acp_thread.set_title(title, cx))?;
460 task.await
461 })
462 .detach_and_log_err(cx);
463 }
464
465 fn handle_thread_token_usage_updated(
466 &mut self,
467 thread: Entity<Thread>,
468 usage: &TokenUsageUpdated,
469 cx: &mut Context<Self>,
470 ) {
471 let Some(session) = self.sessions.get(thread.read(cx).id()) else {
472 return;
473 };
474 session
475 .acp_thread
476 .update(cx, |acp_thread, cx| {
477 acp_thread.update_token_usage(usage.0.clone(), cx);
478 })
479 .ok();
480 }
481
482 fn handle_project_event(
483 &mut self,
484 _project: Entity<Project>,
485 event: &project::Event,
486 _cx: &mut Context<Self>,
487 ) {
488 match event {
489 project::Event::WorktreeAdded(_) | project::Event::WorktreeRemoved(_) => {
490 self.project_context_needs_refresh.send(()).ok();
491 }
492 project::Event::WorktreeUpdatedEntries(_, items) => {
493 if items.iter().any(|(path, _, _)| {
494 RULES_FILE_NAMES
495 .iter()
496 .any(|name| path.as_ref() == Path::new(name))
497 }) {
498 self.project_context_needs_refresh.send(()).ok();
499 }
500 }
501 _ => {}
502 }
503 }
504
505 fn handle_prompts_updated_event(
506 &mut self,
507 _prompt_store: Entity<PromptStore>,
508 _event: &prompt_store::PromptsUpdatedEvent,
509 _cx: &mut Context<Self>,
510 ) {
511 self.project_context_needs_refresh.send(()).ok();
512 }
513
514 fn handle_models_updated_event(
515 &mut self,
516 _registry: Entity<LanguageModelRegistry>,
517 _event: &language_model::Event,
518 cx: &mut Context<Self>,
519 ) {
520 self.models.refresh_list(cx);
521
522 let registry = LanguageModelRegistry::read_global(cx);
523 let default_model = registry.default_model().map(|m| m.model);
524 let summarization_model = registry.thread_summary_model(cx).map(|m| m.model);
525
526 for session in self.sessions.values_mut() {
527 session.thread.update(cx, |thread, cx| {
528 if thread.model().is_none()
529 && let Some(model) = default_model.clone()
530 {
531 thread.set_model(model, cx);
532 cx.notify();
533 }
534 thread.set_summarization_model(summarization_model.clone(), cx);
535 });
536 }
537 }
538
539 pub fn open_thread(
540 &mut self,
541 id: acp::SessionId,
542 cx: &mut Context<Self>,
543 ) -> Task<Result<Entity<AcpThread>>> {
544 let database_future = ThreadsDatabase::connect(cx);
545 cx.spawn(async move |this, cx| {
546 let database = database_future.await.map_err(|err| anyhow!(err))?;
547 let db_thread = database
548 .load_thread(id.clone())
549 .await?
550 .with_context(|| format!("no thread found with ID: {id:?}"))?;
551
552 let thread = this.update(cx, |this, cx| {
553 let action_log = cx.new(|_cx| ActionLog::new(this.project.clone()));
554 cx.new(|cx| {
555 Thread::from_db(
556 id.clone(),
557 db_thread,
558 this.project.clone(),
559 this.project_context.clone(),
560 this.context_server_registry.clone(),
561 action_log.clone(),
562 this.templates.clone(),
563 cx,
564 )
565 })
566 })?;
567 let acp_thread =
568 this.update(cx, |this, cx| this.register_session(thread.clone(), cx))?;
569 let events = thread.update(cx, |thread, cx| thread.replay(cx))?;
570 cx.update(|cx| {
571 NativeAgentConnection::handle_thread_events(events, acp_thread.downgrade(), cx)
572 })?
573 .await?;
574 Ok(acp_thread)
575 })
576 }
577
578 pub fn thread_summary(
579 &mut self,
580 id: acp::SessionId,
581 cx: &mut Context<Self>,
582 ) -> Task<Result<SharedString>> {
583 let thread = self.open_thread(id.clone(), cx);
584 cx.spawn(async move |this, cx| {
585 let acp_thread = thread.await?;
586 let result = this
587 .update(cx, |this, cx| {
588 this.sessions
589 .get(&id)
590 .unwrap()
591 .thread
592 .update(cx, |thread, cx| thread.summary(cx))
593 })?
594 .await?;
595 drop(acp_thread);
596 Ok(result)
597 })
598 }
599
600 fn save_thread(&mut self, thread: Entity<Thread>, cx: &mut Context<Self>) {
601 if thread.read(cx).is_empty() {
602 return;
603 }
604
605 let database_future = ThreadsDatabase::connect(cx);
606 let (id, db_thread) =
607 thread.update(cx, |thread, cx| (thread.id().clone(), thread.to_db(cx)));
608 let Some(session) = self.sessions.get_mut(&id) else {
609 return;
610 };
611 let history = self.history.clone();
612 session.pending_save = cx.spawn(async move |_, cx| {
613 let Some(database) = database_future.await.map_err(|err| anyhow!(err)).log_err() else {
614 return;
615 };
616 let db_thread = db_thread.await;
617 database.save_thread(id, db_thread).await.log_err();
618 history.update(cx, |history, cx| history.reload(cx)).ok();
619 });
620 }
621}
622
623/// Wrapper struct that implements the AgentConnection trait
624#[derive(Clone)]
625pub struct NativeAgentConnection(pub Entity<NativeAgent>);
626
627impl NativeAgentConnection {
628 pub fn thread(&self, session_id: &acp::SessionId, cx: &App) -> Option<Entity<Thread>> {
629 self.0
630 .read(cx)
631 .sessions
632 .get(session_id)
633 .map(|session| session.thread.clone())
634 }
635
636 fn run_turn(
637 &self,
638 session_id: acp::SessionId,
639 cx: &mut App,
640 f: impl 'static
641 + FnOnce(Entity<Thread>, &mut App) -> Result<mpsc::UnboundedReceiver<Result<ThreadEvent>>>,
642 ) -> Task<Result<acp::PromptResponse>> {
643 let Some((thread, acp_thread)) = self.0.update(cx, |agent, _cx| {
644 agent
645 .sessions
646 .get_mut(&session_id)
647 .map(|s| (s.thread.clone(), s.acp_thread.clone()))
648 }) else {
649 return Task::ready(Err(anyhow!("Session not found")));
650 };
651 log::debug!("Found session for: {}", session_id);
652
653 let response_stream = match f(thread, cx) {
654 Ok(stream) => stream,
655 Err(err) => return Task::ready(Err(err)),
656 };
657 Self::handle_thread_events(response_stream, acp_thread, cx)
658 }
659
660 fn handle_thread_events(
661 mut events: mpsc::UnboundedReceiver<Result<ThreadEvent>>,
662 acp_thread: WeakEntity<AcpThread>,
663 cx: &App,
664 ) -> Task<Result<acp::PromptResponse>> {
665 cx.spawn(async move |cx| {
666 // Handle response stream and forward to session.acp_thread
667 while let Some(result) = events.next().await {
668 match result {
669 Ok(event) => {
670 log::trace!("Received completion event: {:?}", event);
671
672 match event {
673 ThreadEvent::UserMessage(message) => {
674 acp_thread.update(cx, |thread, cx| {
675 for content in message.content {
676 thread.push_user_content_block(
677 Some(message.id.clone()),
678 content.into(),
679 cx,
680 );
681 }
682 })?;
683 }
684 ThreadEvent::AgentText(text) => {
685 acp_thread.update(cx, |thread, cx| {
686 thread.push_assistant_content_block(
687 acp::ContentBlock::Text(acp::TextContent {
688 text,
689 annotations: None,
690 }),
691 false,
692 cx,
693 )
694 })?;
695 }
696 ThreadEvent::AgentThinking(text) => {
697 acp_thread.update(cx, |thread, cx| {
698 thread.push_assistant_content_block(
699 acp::ContentBlock::Text(acp::TextContent {
700 text,
701 annotations: None,
702 }),
703 true,
704 cx,
705 )
706 })?;
707 }
708 ThreadEvent::ToolCallAuthorization(ToolCallAuthorization {
709 tool_call,
710 options,
711 response,
712 }) => {
713 let recv = acp_thread.update(cx, |thread, cx| {
714 thread.request_tool_call_authorization(tool_call, options, cx)
715 })?;
716 cx.background_spawn(async move {
717 if let Some(recv) = recv.log_err()
718 && let Some(option) = recv
719 .await
720 .context("authorization sender was dropped")
721 .log_err()
722 {
723 response
724 .send(option)
725 .map(|_| anyhow!("authorization receiver was dropped"))
726 .log_err();
727 }
728 })
729 .detach();
730 }
731 ThreadEvent::ToolCall(tool_call) => {
732 acp_thread.update(cx, |thread, cx| {
733 thread.upsert_tool_call(tool_call, cx)
734 })??;
735 }
736 ThreadEvent::ToolCallUpdate(update) => {
737 acp_thread.update(cx, |thread, cx| {
738 thread.update_tool_call(update, cx)
739 })??;
740 }
741 ThreadEvent::Retry(status) => {
742 acp_thread.update(cx, |thread, cx| {
743 thread.update_retry_status(status, cx)
744 })?;
745 }
746 ThreadEvent::Stop(stop_reason) => {
747 log::debug!("Assistant message complete: {:?}", stop_reason);
748 return Ok(acp::PromptResponse { stop_reason });
749 }
750 }
751 }
752 Err(e) => {
753 log::error!("Error in model response stream: {:?}", e);
754 return Err(e);
755 }
756 }
757 }
758
759 log::info!("Response stream completed");
760 anyhow::Ok(acp::PromptResponse {
761 stop_reason: acp::StopReason::EndTurn,
762 })
763 })
764 }
765}
766
767impl AgentModelSelector for NativeAgentConnection {
768 fn list_models(&self, cx: &mut App) -> Task<Result<acp_thread::AgentModelList>> {
769 log::debug!("NativeAgentConnection::list_models called");
770 let list = self.0.read(cx).models.model_list.clone();
771 Task::ready(if list.is_empty() {
772 Err(anyhow::anyhow!("No models available"))
773 } else {
774 Ok(list)
775 })
776 }
777
778 fn select_model(
779 &self,
780 session_id: acp::SessionId,
781 model_id: acp_thread::AgentModelId,
782 cx: &mut App,
783 ) -> Task<Result<()>> {
784 log::info!("Setting model for session {}: {}", session_id, model_id);
785 let Some(thread) = self
786 .0
787 .read(cx)
788 .sessions
789 .get(&session_id)
790 .map(|session| session.thread.clone())
791 else {
792 return Task::ready(Err(anyhow!("Session not found")));
793 };
794
795 let Some(model) = self.0.read(cx).models.model_from_id(&model_id) else {
796 return Task::ready(Err(anyhow!("Invalid model ID {}", model_id)));
797 };
798
799 thread.update(cx, |thread, cx| {
800 thread.set_model(model.clone(), cx);
801 });
802
803 update_settings_file::<AgentSettings>(
804 self.0.read(cx).fs.clone(),
805 cx,
806 move |settings, _cx| {
807 settings.set_model(model);
808 },
809 );
810
811 Task::ready(Ok(()))
812 }
813
814 fn selected_model(
815 &self,
816 session_id: &acp::SessionId,
817 cx: &mut App,
818 ) -> Task<Result<acp_thread::AgentModelInfo>> {
819 let session_id = session_id.clone();
820
821 let Some(thread) = self
822 .0
823 .read(cx)
824 .sessions
825 .get(&session_id)
826 .map(|session| session.thread.clone())
827 else {
828 return Task::ready(Err(anyhow!("Session not found")));
829 };
830 let Some(model) = thread.read(cx).model() else {
831 return Task::ready(Err(anyhow!("Model not found")));
832 };
833 let Some(provider) = LanguageModelRegistry::read_global(cx).provider(&model.provider_id())
834 else {
835 return Task::ready(Err(anyhow!("Provider not found")));
836 };
837 Task::ready(Ok(LanguageModels::map_language_model_to_info(
838 model, &provider,
839 )))
840 }
841
842 fn watch(&self, cx: &mut App) -> watch::Receiver<()> {
843 self.0.read(cx).models.watch()
844 }
845}
846
847impl acp_thread::AgentConnection for NativeAgentConnection {
848 fn new_thread(
849 self: Rc<Self>,
850 project: Entity<Project>,
851 cwd: &Path,
852 cx: &mut App,
853 ) -> Task<Result<Entity<acp_thread::AcpThread>>> {
854 let agent = self.0.clone();
855 log::info!("Creating new thread for project at: {:?}", cwd);
856
857 cx.spawn(async move |cx| {
858 log::debug!("Starting thread creation in async context");
859
860 // Create Thread
861 let thread = agent.update(
862 cx,
863 |agent, cx: &mut gpui::Context<NativeAgent>| -> Result<_> {
864 // Fetch default model from registry settings
865 let registry = LanguageModelRegistry::read_global(cx);
866 // Log available models for debugging
867 let available_count = registry.available_models(cx).count();
868 log::debug!("Total available models: {}", available_count);
869
870 let default_model = registry.default_model().and_then(|default_model| {
871 agent
872 .models
873 .model_from_id(&LanguageModels::model_id(&default_model.model))
874 });
875 Ok(cx.new(|cx| {
876 Thread::new(
877 project.clone(),
878 agent.project_context.clone(),
879 agent.context_server_registry.clone(),
880 agent.templates.clone(),
881 default_model,
882 cx,
883 )
884 }))
885 },
886 )??;
887 agent.update(cx, |agent, cx| agent.register_session(thread, cx))
888 })
889 }
890
891 fn auth_methods(&self) -> &[acp::AuthMethod] {
892 &[] // No auth for in-process
893 }
894
895 fn authenticate(&self, _method: acp::AuthMethodId, _cx: &mut App) -> Task<Result<()>> {
896 Task::ready(Ok(()))
897 }
898
899 fn model_selector(&self) -> Option<Rc<dyn AgentModelSelector>> {
900 Some(Rc::new(self.clone()) as Rc<dyn AgentModelSelector>)
901 }
902
903 fn prompt(
904 &self,
905 id: Option<acp_thread::UserMessageId>,
906 params: acp::PromptRequest,
907 cx: &mut App,
908 ) -> Task<Result<acp::PromptResponse>> {
909 let id = id.expect("UserMessageId is required");
910 let session_id = params.session_id.clone();
911 log::info!("Received prompt request for session: {}", session_id);
912 log::debug!("Prompt blocks count: {}", params.prompt.len());
913
914 self.run_turn(session_id, cx, |thread, cx| {
915 let content: Vec<UserMessageContent> = params
916 .prompt
917 .into_iter()
918 .map(Into::into)
919 .collect::<Vec<_>>();
920 log::info!("Converted prompt to message: {} chars", content.len());
921 log::debug!("Message id: {:?}", id);
922 log::debug!("Message content: {:?}", content);
923
924 thread.update(cx, |thread, cx| thread.send(id, content, cx))
925 })
926 }
927
928 fn prompt_capabilities(&self) -> acp::PromptCapabilities {
929 acp::PromptCapabilities {
930 image: true,
931 audio: false,
932 embedded_context: true,
933 }
934 }
935
936 fn resume(
937 &self,
938 session_id: &acp::SessionId,
939 _cx: &mut App,
940 ) -> Option<Rc<dyn acp_thread::AgentSessionResume>> {
941 Some(Rc::new(NativeAgentSessionResume {
942 connection: self.clone(),
943 session_id: session_id.clone(),
944 }) as _)
945 }
946
947 fn cancel(&self, session_id: &acp::SessionId, cx: &mut App) {
948 log::info!("Cancelling on session: {}", session_id);
949 self.0.update(cx, |agent, cx| {
950 if let Some(agent) = agent.sessions.get(session_id) {
951 agent.thread.update(cx, |thread, cx| thread.cancel(cx));
952 }
953 });
954 }
955
956 fn truncate(
957 &self,
958 session_id: &agent_client_protocol::SessionId,
959 cx: &mut App,
960 ) -> Option<Rc<dyn acp_thread::AgentSessionTruncate>> {
961 self.0.update(cx, |agent, _cx| {
962 agent.sessions.get(session_id).map(|session| {
963 Rc::new(NativeAgentSessionEditor {
964 thread: session.thread.clone(),
965 acp_thread: session.acp_thread.clone(),
966 }) as _
967 })
968 })
969 }
970
971 fn set_title(
972 &self,
973 session_id: &acp::SessionId,
974 _cx: &mut App,
975 ) -> Option<Rc<dyn acp_thread::AgentSessionSetTitle>> {
976 Some(Rc::new(NativeAgentSessionSetTitle {
977 connection: self.clone(),
978 session_id: session_id.clone(),
979 }) as _)
980 }
981
982 fn telemetry(&self) -> Option<Rc<dyn acp_thread::AgentTelemetry>> {
983 Some(Rc::new(self.clone()) as Rc<dyn acp_thread::AgentTelemetry>)
984 }
985
986 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
987 self
988 }
989}
990
991impl acp_thread::AgentTelemetry for NativeAgentConnection {
992 fn agent_name(&self) -> String {
993 "Zed".into()
994 }
995
996 fn thread_data(
997 &self,
998 session_id: &acp::SessionId,
999 cx: &mut App,
1000 ) -> Task<Result<serde_json::Value>> {
1001 let Some(session) = self.0.read(cx).sessions.get(session_id) else {
1002 return Task::ready(Err(anyhow!("Session not found")));
1003 };
1004
1005 let task = session.thread.read(cx).to_db(cx);
1006 cx.background_spawn(async move {
1007 serde_json::to_value(task.await).context("Failed to serialize thread")
1008 })
1009 }
1010}
1011
1012struct NativeAgentSessionEditor {
1013 thread: Entity<Thread>,
1014 acp_thread: WeakEntity<AcpThread>,
1015}
1016
1017impl acp_thread::AgentSessionTruncate for NativeAgentSessionEditor {
1018 fn run(&self, message_id: acp_thread::UserMessageId, cx: &mut App) -> Task<Result<()>> {
1019 match self.thread.update(cx, |thread, cx| {
1020 thread.truncate(message_id.clone(), cx)?;
1021 Ok(thread.latest_token_usage())
1022 }) {
1023 Ok(usage) => {
1024 self.acp_thread
1025 .update(cx, |thread, cx| {
1026 thread.update_token_usage(usage, cx);
1027 })
1028 .ok();
1029 Task::ready(Ok(()))
1030 }
1031 Err(error) => Task::ready(Err(error)),
1032 }
1033 }
1034}
1035
1036struct NativeAgentSessionResume {
1037 connection: NativeAgentConnection,
1038 session_id: acp::SessionId,
1039}
1040
1041impl acp_thread::AgentSessionResume for NativeAgentSessionResume {
1042 fn run(&self, cx: &mut App) -> Task<Result<acp::PromptResponse>> {
1043 self.connection
1044 .run_turn(self.session_id.clone(), cx, |thread, cx| {
1045 thread.update(cx, |thread, cx| thread.resume(cx))
1046 })
1047 }
1048}
1049
1050struct NativeAgentSessionSetTitle {
1051 connection: NativeAgentConnection,
1052 session_id: acp::SessionId,
1053}
1054
1055impl acp_thread::AgentSessionSetTitle for NativeAgentSessionSetTitle {
1056 fn run(&self, title: SharedString, cx: &mut App) -> Task<Result<()>> {
1057 let Some(session) = self.connection.0.read(cx).sessions.get(&self.session_id) else {
1058 return Task::ready(Err(anyhow!("session not found")));
1059 };
1060 let thread = session.thread.clone();
1061 thread.update(cx, |thread, cx| thread.set_title(title, cx));
1062 Task::ready(Ok(()))
1063 }
1064}
1065
1066#[cfg(test)]
1067mod tests {
1068 use crate::HistoryEntryId;
1069
1070 use super::*;
1071 use acp_thread::{
1072 AgentConnection, AgentModelGroupName, AgentModelId, AgentModelInfo, MentionUri,
1073 };
1074 use fs::FakeFs;
1075 use gpui::TestAppContext;
1076 use indoc::indoc;
1077 use language_model::fake_provider::FakeLanguageModel;
1078 use serde_json::json;
1079 use settings::SettingsStore;
1080 use util::path;
1081
1082 #[gpui::test]
1083 async fn test_maintaining_project_context(cx: &mut TestAppContext) {
1084 init_test(cx);
1085 let fs = FakeFs::new(cx.executor());
1086 fs.insert_tree(
1087 "/",
1088 json!({
1089 "a": {}
1090 }),
1091 )
1092 .await;
1093 let project = Project::test(fs.clone(), [], cx).await;
1094 let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1095 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1096 let agent = NativeAgent::new(
1097 project.clone(),
1098 history_store,
1099 Templates::new(),
1100 None,
1101 fs.clone(),
1102 &mut cx.to_async(),
1103 )
1104 .await
1105 .unwrap();
1106 agent.read_with(cx, |agent, cx| {
1107 assert_eq!(agent.project_context.read(cx).worktrees, vec![])
1108 });
1109
1110 let worktree = project
1111 .update(cx, |project, cx| project.create_worktree("/a", true, cx))
1112 .await
1113 .unwrap();
1114 cx.run_until_parked();
1115 agent.read_with(cx, |agent, cx| {
1116 assert_eq!(
1117 agent.project_context.read(cx).worktrees,
1118 vec![WorktreeContext {
1119 root_name: "a".into(),
1120 abs_path: Path::new("/a").into(),
1121 rules_file: None
1122 }]
1123 )
1124 });
1125
1126 // Creating `/a/.rules` updates the project context.
1127 fs.insert_file("/a/.rules", Vec::new()).await;
1128 cx.run_until_parked();
1129 agent.read_with(cx, |agent, cx| {
1130 let rules_entry = worktree.read(cx).entry_for_path(".rules").unwrap();
1131 assert_eq!(
1132 agent.project_context.read(cx).worktrees,
1133 vec![WorktreeContext {
1134 root_name: "a".into(),
1135 abs_path: Path::new("/a").into(),
1136 rules_file: Some(RulesFileContext {
1137 path_in_worktree: Path::new(".rules").into(),
1138 text: "".into(),
1139 project_entry_id: rules_entry.id.to_usize()
1140 })
1141 }]
1142 )
1143 });
1144 }
1145
1146 #[gpui::test]
1147 async fn test_listing_models(cx: &mut TestAppContext) {
1148 init_test(cx);
1149 let fs = FakeFs::new(cx.executor());
1150 fs.insert_tree("/", json!({ "a": {} })).await;
1151 let project = Project::test(fs.clone(), [], cx).await;
1152 let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1153 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1154 let connection = NativeAgentConnection(
1155 NativeAgent::new(
1156 project.clone(),
1157 history_store,
1158 Templates::new(),
1159 None,
1160 fs.clone(),
1161 &mut cx.to_async(),
1162 )
1163 .await
1164 .unwrap(),
1165 );
1166
1167 let models = cx.update(|cx| connection.list_models(cx)).await.unwrap();
1168
1169 let acp_thread::AgentModelList::Grouped(models) = models else {
1170 panic!("Unexpected model group");
1171 };
1172 assert_eq!(
1173 models,
1174 IndexMap::from_iter([(
1175 AgentModelGroupName("Fake".into()),
1176 vec![AgentModelInfo {
1177 id: AgentModelId("fake/fake".into()),
1178 name: "Fake".into(),
1179 icon: Some(ui::IconName::ZedAssistant),
1180 }]
1181 )])
1182 );
1183 }
1184
1185 #[gpui::test]
1186 async fn test_model_selection_persists_to_settings(cx: &mut TestAppContext) {
1187 init_test(cx);
1188 let fs = FakeFs::new(cx.executor());
1189 fs.create_dir(paths::settings_file().parent().unwrap())
1190 .await
1191 .unwrap();
1192 fs.insert_file(
1193 paths::settings_file(),
1194 json!({
1195 "agent": {
1196 "default_model": {
1197 "provider": "foo",
1198 "model": "bar"
1199 }
1200 }
1201 })
1202 .to_string()
1203 .into_bytes(),
1204 )
1205 .await;
1206 let project = Project::test(fs.clone(), [], cx).await;
1207
1208 let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1209 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1210
1211 // Create the agent and connection
1212 let agent = NativeAgent::new(
1213 project.clone(),
1214 history_store,
1215 Templates::new(),
1216 None,
1217 fs.clone(),
1218 &mut cx.to_async(),
1219 )
1220 .await
1221 .unwrap();
1222 let connection = NativeAgentConnection(agent.clone());
1223
1224 // Create a thread/session
1225 let acp_thread = cx
1226 .update(|cx| {
1227 Rc::new(connection.clone()).new_thread(project.clone(), Path::new("/a"), cx)
1228 })
1229 .await
1230 .unwrap();
1231
1232 let session_id = cx.update(|cx| acp_thread.read(cx).session_id().clone());
1233
1234 // Select a model
1235 let model_id = AgentModelId("fake/fake".into());
1236 cx.update(|cx| connection.select_model(session_id.clone(), model_id.clone(), cx))
1237 .await
1238 .unwrap();
1239
1240 // Verify the thread has the selected model
1241 agent.read_with(cx, |agent, _| {
1242 let session = agent.sessions.get(&session_id).unwrap();
1243 session.thread.read_with(cx, |thread, _| {
1244 assert_eq!(thread.model().unwrap().id().0, "fake");
1245 });
1246 });
1247
1248 cx.run_until_parked();
1249
1250 // Verify settings file was updated
1251 let settings_content = fs.load(paths::settings_file()).await.unwrap();
1252 let settings_json: serde_json::Value = serde_json::from_str(&settings_content).unwrap();
1253
1254 // Check that the agent settings contain the selected model
1255 assert_eq!(
1256 settings_json["agent"]["default_model"]["model"],
1257 json!("fake")
1258 );
1259 assert_eq!(
1260 settings_json["agent"]["default_model"]["provider"],
1261 json!("fake")
1262 );
1263 }
1264
1265 #[gpui::test]
1266 #[cfg_attr(target_os = "windows", ignore)] // TODO: Fix this test on Windows
1267 async fn test_save_load_thread(cx: &mut TestAppContext) {
1268 init_test(cx);
1269 let fs = FakeFs::new(cx.executor());
1270 fs.insert_tree(
1271 "/",
1272 json!({
1273 "a": {
1274 "b.md": "Lorem"
1275 }
1276 }),
1277 )
1278 .await;
1279 let project = Project::test(fs.clone(), [path!("/a").as_ref()], cx).await;
1280 let context_store = cx.new(|cx| assistant_context::ContextStore::fake(project.clone(), cx));
1281 let history_store = cx.new(|cx| HistoryStore::new(context_store, cx));
1282 let agent = NativeAgent::new(
1283 project.clone(),
1284 history_store.clone(),
1285 Templates::new(),
1286 None,
1287 fs.clone(),
1288 &mut cx.to_async(),
1289 )
1290 .await
1291 .unwrap();
1292 let connection = Rc::new(NativeAgentConnection(agent.clone()));
1293
1294 let acp_thread = cx
1295 .update(|cx| {
1296 connection
1297 .clone()
1298 .new_thread(project.clone(), Path::new(""), cx)
1299 })
1300 .await
1301 .unwrap();
1302 let session_id = acp_thread.read_with(cx, |thread, _| thread.session_id().clone());
1303 let thread = agent.read_with(cx, |agent, _| {
1304 agent.sessions.get(&session_id).unwrap().thread.clone()
1305 });
1306
1307 // Ensure empty threads are not saved, even if they get mutated.
1308 let model = Arc::new(FakeLanguageModel::default());
1309 let summary_model = Arc::new(FakeLanguageModel::default());
1310 thread.update(cx, |thread, cx| {
1311 thread.set_model(model.clone(), cx);
1312 thread.set_summarization_model(Some(summary_model.clone()), cx);
1313 });
1314 cx.run_until_parked();
1315 assert_eq!(history_entries(&history_store, cx), vec![]);
1316
1317 let send = acp_thread.update(cx, |thread, cx| {
1318 thread.send(
1319 vec![
1320 "What does ".into(),
1321 acp::ContentBlock::ResourceLink(acp::ResourceLink {
1322 name: "b.md".into(),
1323 uri: MentionUri::File {
1324 abs_path: path!("/a/b.md").into(),
1325 }
1326 .to_uri()
1327 .to_string(),
1328 annotations: None,
1329 description: None,
1330 mime_type: None,
1331 size: None,
1332 title: None,
1333 }),
1334 " mean?".into(),
1335 ],
1336 cx,
1337 )
1338 });
1339 let send = cx.foreground_executor().spawn(send);
1340 cx.run_until_parked();
1341
1342 model.send_last_completion_stream_text_chunk("Lorem.");
1343 model.end_last_completion_stream();
1344 cx.run_until_parked();
1345 summary_model.send_last_completion_stream_text_chunk("Explaining /a/b.md");
1346 summary_model.end_last_completion_stream();
1347
1348 send.await.unwrap();
1349 acp_thread.read_with(cx, |thread, cx| {
1350 assert_eq!(
1351 thread.to_markdown(cx),
1352 indoc! {"
1353 ## User
1354
1355 What does [@b.md](file:///a/b.md) mean?
1356
1357 ## Assistant
1358
1359 Lorem.
1360
1361 "}
1362 )
1363 });
1364
1365 cx.run_until_parked();
1366
1367 // Drop the ACP thread, which should cause the session to be dropped as well.
1368 cx.update(|_| {
1369 drop(thread);
1370 drop(acp_thread);
1371 });
1372 agent.read_with(cx, |agent, _| {
1373 assert_eq!(agent.sessions.keys().cloned().collect::<Vec<_>>(), []);
1374 });
1375
1376 // Ensure the thread can be reloaded from disk.
1377 assert_eq!(
1378 history_entries(&history_store, cx),
1379 vec![(
1380 HistoryEntryId::AcpThread(session_id.clone()),
1381 "Explaining /a/b.md".into()
1382 )]
1383 );
1384 let acp_thread = agent
1385 .update(cx, |agent, cx| agent.open_thread(session_id.clone(), cx))
1386 .await
1387 .unwrap();
1388 acp_thread.read_with(cx, |thread, cx| {
1389 assert_eq!(
1390 thread.to_markdown(cx),
1391 indoc! {"
1392 ## User
1393
1394 What does [@b.md](file:///a/b.md) mean?
1395
1396 ## Assistant
1397
1398 Lorem.
1399
1400 "}
1401 )
1402 });
1403 }
1404
1405 fn history_entries(
1406 history: &Entity<HistoryStore>,
1407 cx: &mut TestAppContext,
1408 ) -> Vec<(HistoryEntryId, String)> {
1409 history.read_with(cx, |history, cx| {
1410 history
1411 .entries(cx)
1412 .iter()
1413 .map(|e| (e.id(), e.title().to_string()))
1414 .collect::<Vec<_>>()
1415 })
1416 }
1417
1418 fn init_test(cx: &mut TestAppContext) {
1419 env_logger::try_init().ok();
1420 cx.update(|cx| {
1421 let settings_store = SettingsStore::test(cx);
1422 cx.set_global(settings_store);
1423 Project::init_settings(cx);
1424 agent_settings::init(cx);
1425 language::init(cx);
1426 LanguageModelRegistry::test(cx);
1427 });
1428 }
1429}