1use std::{any::Any, path::Path, rc::Rc, sync::Arc};
2
3use agent_servers::AgentServer;
4use anyhow::Result;
5use fs::Fs;
6use gpui::{App, Entity, SharedString, Task};
7use project::Project;
8use prompt_store::PromptStore;
9
10use crate::{HistoryStore, NativeAgent, NativeAgentConnection, templates::Templates};
11
12#[derive(Clone)]
13pub struct NativeAgentServer {
14 fs: Arc<dyn Fs>,
15 history: Entity<HistoryStore>,
16}
17
18impl NativeAgentServer {
19 pub fn new(fs: Arc<dyn Fs>, history: Entity<HistoryStore>) -> Self {
20 Self { fs, history }
21 }
22}
23
24impl AgentServer for NativeAgentServer {
25 fn telemetry_id(&self) -> &'static str {
26 "zed"
27 }
28
29 fn name(&self) -> SharedString {
30 "Zed Agent".into()
31 }
32
33 fn empty_state_headline(&self) -> SharedString {
34 self.name()
35 }
36
37 fn empty_state_message(&self) -> SharedString {
38 "".into()
39 }
40
41 fn logo(&self) -> ui::IconName {
42 ui::IconName::ZedAgent
43 }
44
45 fn connect(
46 &self,
47 _root_dir: &Path,
48 project: &Entity<Project>,
49 cx: &mut App,
50 ) -> Task<Result<Rc<dyn acp_thread::AgentConnection>>> {
51 log::debug!(
52 "NativeAgentServer::connect called for path: {:?}",
53 _root_dir
54 );
55 let project = project.clone();
56 let fs = self.fs.clone();
57 let history = self.history.clone();
58 let prompt_store = PromptStore::global(cx);
59 cx.spawn(async move |cx| {
60 log::debug!("Creating templates for native agent");
61 let templates = Templates::new();
62 let prompt_store = prompt_store.await?;
63
64 log::debug!("Creating native agent entity");
65 let agent =
66 NativeAgent::new(project, history, templates, Some(prompt_store), fs, cx).await?;
67
68 // Create the connection wrapper
69 let connection = NativeAgentConnection(agent);
70 log::debug!("NativeAgentServer connection established successfully");
71
72 Ok(Rc::new(connection) as Rc<dyn acp_thread::AgentConnection>)
73 })
74 }
75
76 fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
77 self
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 use assistant_context::ContextStore;
86 use gpui::AppContext;
87
88 agent_servers::e2e_tests::common_e2e_tests!(
89 async |fs, project, cx| {
90 let auth = cx.update(|cx| {
91 prompt_store::init(cx);
92 terminal::init(cx);
93
94 let registry = language_model::LanguageModelRegistry::read_global(cx);
95 let auth = registry
96 .provider(&language_model::ANTHROPIC_PROVIDER_ID)
97 .unwrap()
98 .authenticate(cx);
99
100 cx.spawn(async move |_| auth.await)
101 });
102
103 auth.await.unwrap();
104
105 cx.update(|cx| {
106 let registry = language_model::LanguageModelRegistry::global(cx);
107
108 registry.update(cx, |registry, cx| {
109 registry.select_default_model(
110 Some(&language_model::SelectedModel {
111 provider: language_model::ANTHROPIC_PROVIDER_ID,
112 model: language_model::LanguageModelId("claude-sonnet-4-latest".into()),
113 }),
114 cx,
115 );
116 });
117 });
118
119 let history = cx.update(|cx| {
120 let context_store = cx.new(move |cx| ContextStore::fake(project.clone(), cx));
121 cx.new(move |cx| HistoryStore::new(context_store, cx))
122 });
123
124 NativeAgentServer::new(fs.clone(), history)
125 },
126 allow_option_id = "allow"
127 );
128}