1use std::{any::Any, path::Path, rc::Rc, sync::Arc};
2
3use agent_servers::{AgentServer, AgentServerDelegate};
4use anyhow::Result;
5use collections::HashMap;
6use fs::Fs;
7use gpui::{App, Entity, SharedString, Task};
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 logo(&self) -> ui::IconName {
34 ui::IconName::ZedAgent
35 }
36
37 fn connect(
38 &self,
39 _root_dir: Option<&Path>,
40 delegate: AgentServerDelegate,
41 cx: &mut App,
42 ) -> Task<
43 Result<(
44 Rc<dyn acp_thread::AgentConnection>,
45 HashMap<String, task::SpawnInTerminal>,
46 )>,
47 > {
48 log::debug!(
49 "NativeAgentServer::connect called for path: {:?}",
50 _root_dir
51 );
52 let project = delegate.project().clone();
53 let fs = self.fs.clone();
54 let history = self.history.clone();
55 let prompt_store = PromptStore::global(cx);
56 cx.spawn(async move |cx| {
57 log::debug!("Creating templates for native agent");
58 let templates = Templates::new();
59 let prompt_store = prompt_store.await?;
60
61 log::debug!("Creating native agent entity");
62 let agent =
63 NativeAgent::new(project, history, templates, Some(prompt_store), fs, cx).await?;
64
65 // Create the connection wrapper
66 let connection = NativeAgentConnection(agent);
67 log::debug!("NativeAgentServer connection established successfully");
68
69 Ok((
70 Rc::new(connection) as Rc<dyn acp_thread::AgentConnection>,
71 HashMap::default(),
72 ))
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}