connection.rs

  1use crate::AcpThread;
  2use agent_client_protocol::{self as acp};
  3use anyhow::Result;
  4use chrono::{DateTime, Utc};
  5use collections::IndexMap;
  6use gpui::{Entity, SharedString, Task};
  7use language_model::LanguageModelProviderId;
  8use project::Project;
  9use serde::{Deserialize, Serialize};
 10use std::{
 11    any::Any,
 12    error::Error,
 13    fmt,
 14    path::{Path, PathBuf},
 15    rc::Rc,
 16    sync::Arc,
 17};
 18use ui::{App, IconName};
 19use uuid::Uuid;
 20
 21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
 22pub struct UserMessageId(Arc<str>);
 23
 24impl UserMessageId {
 25    pub fn new() -> Self {
 26        Self(Uuid::new_v4().to_string().into())
 27    }
 28}
 29
 30pub trait AgentConnection {
 31    fn telemetry_id(&self) -> SharedString;
 32
 33    fn new_session(
 34        self: Rc<Self>,
 35        project: Entity<Project>,
 36        cwd: &Path,
 37        cx: &mut App,
 38    ) -> Task<Result<Entity<AcpThread>>>;
 39
 40    /// Whether this agent supports loading existing sessions.
 41    fn supports_load_session(&self) -> bool {
 42        false
 43    }
 44
 45    /// Load an existing session by ID.
 46    fn load_session(
 47        self: Rc<Self>,
 48        _session_id: acp::SessionId,
 49        _project: Entity<Project>,
 50        _cwd: &Path,
 51        _title: Option<SharedString>,
 52        _cx: &mut App,
 53    ) -> Task<Result<Entity<AcpThread>>> {
 54        Task::ready(Err(anyhow::Error::msg("Loading sessions is not supported")))
 55    }
 56
 57    /// Whether this agent supports closing existing sessions.
 58    fn supports_close_session(&self) -> bool {
 59        false
 60    }
 61
 62    /// Close an existing session. Allows the agent to free the session from memory.
 63    fn close_session(&self, _session_id: &acp::SessionId, _cx: &mut App) -> Task<Result<()>> {
 64        Task::ready(Err(anyhow::Error::msg("Closing sessions is not supported")))
 65    }
 66
 67    /// Whether this agent supports resuming existing sessions without loading history.
 68    fn supports_resume_session(&self) -> bool {
 69        false
 70    }
 71
 72    /// Resume an existing session by ID without replaying previous messages.
 73    fn resume_session(
 74        self: Rc<Self>,
 75        _session_id: acp::SessionId,
 76        _project: Entity<Project>,
 77        _cwd: &Path,
 78        _title: Option<SharedString>,
 79        _cx: &mut App,
 80    ) -> Task<Result<Entity<AcpThread>>> {
 81        Task::ready(Err(anyhow::Error::msg(
 82            "Resuming sessions is not supported",
 83        )))
 84    }
 85
 86    /// Whether this agent supports showing session history.
 87    fn supports_session_history(&self) -> bool {
 88        self.supports_load_session() || self.supports_resume_session()
 89    }
 90
 91    fn auth_methods(&self) -> &[acp::AuthMethod];
 92
 93    fn authenticate(&self, method: acp::AuthMethodId, cx: &mut App) -> Task<Result<()>>;
 94
 95    fn prompt(
 96        &self,
 97        user_message_id: Option<UserMessageId>,
 98        params: acp::PromptRequest,
 99        cx: &mut App,
100    ) -> Task<Result<acp::PromptResponse>>;
101
102    fn retry(&self, _session_id: &acp::SessionId, _cx: &App) -> Option<Rc<dyn AgentSessionRetry>> {
103        None
104    }
105
106    fn cancel(&self, session_id: &acp::SessionId, cx: &mut App);
107
108    fn truncate(
109        &self,
110        _session_id: &acp::SessionId,
111        _cx: &App,
112    ) -> Option<Rc<dyn AgentSessionTruncate>> {
113        None
114    }
115
116    fn set_title(
117        &self,
118        _session_id: &acp::SessionId,
119        _cx: &App,
120    ) -> Option<Rc<dyn AgentSessionSetTitle>> {
121        None
122    }
123
124    /// Returns this agent as an [Rc<dyn ModelSelector>] if the model selection capability is supported.
125    ///
126    /// If the agent does not support model selection, returns [None].
127    /// This allows sharing the selector in UI components.
128    fn model_selector(&self, _session_id: &acp::SessionId) -> Option<Rc<dyn AgentModelSelector>> {
129        None
130    }
131
132    fn telemetry(&self) -> Option<Rc<dyn AgentTelemetry>> {
133        None
134    }
135
136    fn session_modes(
137        &self,
138        _session_id: &acp::SessionId,
139        _cx: &App,
140    ) -> Option<Rc<dyn AgentSessionModes>> {
141        None
142    }
143
144    fn session_config_options(
145        &self,
146        _session_id: &acp::SessionId,
147        _cx: &App,
148    ) -> Option<Rc<dyn AgentSessionConfigOptions>> {
149        None
150    }
151
152    fn session_list(&self, _cx: &mut App) -> Option<Rc<dyn AgentSessionList>> {
153        None
154    }
155
156    fn into_any(self: Rc<Self>) -> Rc<dyn Any>;
157}
158
159impl dyn AgentConnection {
160    pub fn downcast<T: 'static + AgentConnection + Sized>(self: Rc<Self>) -> Option<Rc<T>> {
161        self.into_any().downcast().ok()
162    }
163}
164
165pub trait AgentSessionTruncate {
166    fn run(&self, message_id: UserMessageId, cx: &mut App) -> Task<Result<()>>;
167}
168
169pub trait AgentSessionRetry {
170    fn run(&self, cx: &mut App) -> Task<Result<acp::PromptResponse>>;
171}
172
173pub trait AgentSessionSetTitle {
174    fn run(&self, title: SharedString, cx: &mut App) -> Task<Result<()>>;
175}
176
177pub trait AgentTelemetry {
178    /// A representation of the current thread state that can be serialized for
179    /// storage with telemetry events.
180    fn thread_data(
181        &self,
182        session_id: &acp::SessionId,
183        cx: &mut App,
184    ) -> Task<Result<serde_json::Value>>;
185}
186
187pub trait AgentSessionModes {
188    fn current_mode(&self) -> acp::SessionModeId;
189
190    fn all_modes(&self) -> Vec<acp::SessionMode>;
191
192    fn set_mode(&self, mode: acp::SessionModeId, cx: &mut App) -> Task<Result<()>>;
193}
194
195pub trait AgentSessionConfigOptions {
196    /// Get all current config options with their state
197    fn config_options(&self) -> Vec<acp::SessionConfigOption>;
198
199    /// Set a config option value
200    /// Returns the full updated list of config options
201    fn set_config_option(
202        &self,
203        config_id: acp::SessionConfigId,
204        value: acp::SessionConfigValueId,
205        cx: &mut App,
206    ) -> Task<Result<Vec<acp::SessionConfigOption>>>;
207
208    /// Whenever the config options are updated the receiver will be notified.
209    /// Optional for agents that don't update their config options dynamically.
210    fn watch(&self, _cx: &mut App) -> Option<watch::Receiver<()>> {
211        None
212    }
213}
214
215#[derive(Debug, Clone, Default)]
216pub struct AgentSessionListRequest {
217    pub cwd: Option<PathBuf>,
218    pub cursor: Option<String>,
219    pub meta: Option<acp::Meta>,
220}
221
222#[derive(Debug, Clone)]
223pub struct AgentSessionListResponse {
224    pub sessions: Vec<AgentSessionInfo>,
225    pub next_cursor: Option<String>,
226    pub meta: Option<acp::Meta>,
227}
228
229impl AgentSessionListResponse {
230    pub fn new(sessions: Vec<AgentSessionInfo>) -> Self {
231        Self {
232            sessions,
233            next_cursor: None,
234            meta: None,
235        }
236    }
237}
238
239#[derive(Debug, Clone, PartialEq)]
240pub struct AgentSessionInfo {
241    pub session_id: acp::SessionId,
242    pub cwd: Option<PathBuf>,
243    pub title: Option<SharedString>,
244    pub updated_at: Option<DateTime<Utc>>,
245    pub created_at: Option<DateTime<Utc>>,
246    pub meta: Option<acp::Meta>,
247}
248
249impl AgentSessionInfo {
250    pub fn new(session_id: impl Into<acp::SessionId>) -> Self {
251        Self {
252            session_id: session_id.into(),
253            cwd: None,
254            title: None,
255            updated_at: None,
256            created_at: None,
257            meta: None,
258        }
259    }
260}
261
262#[derive(Debug, Clone)]
263pub enum SessionListUpdate {
264    Refresh,
265    SessionInfo {
266        session_id: acp::SessionId,
267        update: acp::SessionInfoUpdate,
268    },
269}
270
271pub trait AgentSessionList {
272    fn list_sessions(
273        &self,
274        request: AgentSessionListRequest,
275        cx: &mut App,
276    ) -> Task<Result<AgentSessionListResponse>>;
277
278    fn supports_delete(&self) -> bool {
279        false
280    }
281
282    fn delete_session(&self, _session_id: &acp::SessionId, _cx: &mut App) -> Task<Result<()>> {
283        Task::ready(Err(anyhow::anyhow!("delete_session not supported")))
284    }
285
286    fn delete_sessions(&self, _cx: &mut App) -> Task<Result<()>> {
287        Task::ready(Err(anyhow::anyhow!("delete_sessions not supported")))
288    }
289
290    fn watch(&self, _cx: &mut App) -> Option<smol::channel::Receiver<SessionListUpdate>> {
291        None
292    }
293
294    fn notify_refresh(&self) {}
295
296    fn into_any(self: Rc<Self>) -> Rc<dyn Any>;
297}
298
299impl dyn AgentSessionList {
300    pub fn downcast<T: 'static + AgentSessionList + Sized>(self: Rc<Self>) -> Option<Rc<T>> {
301        self.into_any().downcast().ok()
302    }
303}
304
305#[derive(Debug)]
306pub struct AuthRequired {
307    pub description: Option<String>,
308    pub provider_id: Option<LanguageModelProviderId>,
309}
310
311impl AuthRequired {
312    pub fn new() -> Self {
313        Self {
314            description: None,
315            provider_id: None,
316        }
317    }
318
319    pub fn with_description(mut self, description: String) -> Self {
320        self.description = Some(description);
321        self
322    }
323
324    pub fn with_language_model_provider(mut self, provider_id: LanguageModelProviderId) -> Self {
325        self.provider_id = Some(provider_id);
326        self
327    }
328}
329
330impl Error for AuthRequired {}
331impl fmt::Display for AuthRequired {
332    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
333        write!(f, "Authentication required")
334    }
335}
336
337/// Trait for agents that support listing, selecting, and querying language models.
338///
339/// This is an optional capability; agents indicate support via [AgentConnection::model_selector].
340pub trait AgentModelSelector: 'static {
341    /// Lists all available language models for this agent.
342    ///
343    /// # Parameters
344    /// - `cx`: The GPUI app context for async operations and global access.
345    ///
346    /// # Returns
347    /// A task resolving to the list of models or an error (e.g., if no models are configured).
348    fn list_models(&self, cx: &mut App) -> Task<Result<AgentModelList>>;
349
350    /// Selects a model for a specific session (thread).
351    ///
352    /// This sets the default model for future interactions in the session.
353    /// If the session doesn't exist or the model is invalid, it returns an error.
354    ///
355    /// # Parameters
356    /// - `model`: The model to select (should be one from [list_models]).
357    /// - `cx`: The GPUI app context.
358    ///
359    /// # Returns
360    /// A task resolving to `Ok(())` on success or an error.
361    fn select_model(&self, model_id: acp::ModelId, cx: &mut App) -> Task<Result<()>>;
362
363    /// Retrieves the currently selected model for a specific session (thread).
364    ///
365    /// # Parameters
366    /// - `cx`: The GPUI app context.
367    ///
368    /// # Returns
369    /// A task resolving to the selected model (always set) or an error (e.g., session not found).
370    fn selected_model(&self, cx: &mut App) -> Task<Result<AgentModelInfo>>;
371
372    /// Whenever the model list is updated the receiver will be notified.
373    /// Optional for agents that don't update their model list.
374    fn watch(&self, _cx: &mut App) -> Option<watch::Receiver<()>> {
375        None
376    }
377
378    /// Returns whether the model picker should render a footer.
379    fn should_render_footer(&self) -> bool {
380        false
381    }
382}
383
384/// Icon for a model in the model selector.
385#[derive(Debug, Clone, PartialEq, Eq)]
386pub enum AgentModelIcon {
387    /// A built-in icon from Zed's icon set.
388    Named(IconName),
389    /// Path to a custom SVG icon file.
390    Path(SharedString),
391}
392
393#[derive(Debug, Clone, PartialEq, Eq)]
394pub struct AgentModelInfo {
395    pub id: acp::ModelId,
396    pub name: SharedString,
397    pub description: Option<SharedString>,
398    pub icon: Option<AgentModelIcon>,
399    pub is_latest: bool,
400    pub cost: Option<SharedString>,
401}
402
403impl From<acp::ModelInfo> for AgentModelInfo {
404    fn from(info: acp::ModelInfo) -> Self {
405        Self {
406            id: info.model_id,
407            name: info.name.into(),
408            description: info.description.map(|desc| desc.into()),
409            icon: None,
410            is_latest: false,
411            cost: None,
412        }
413    }
414}
415
416#[derive(Debug, Clone, PartialEq, Eq, Hash)]
417pub struct AgentModelGroupName(pub SharedString);
418
419#[derive(Debug, Clone)]
420pub enum AgentModelList {
421    Flat(Vec<AgentModelInfo>),
422    Grouped(IndexMap<AgentModelGroupName, Vec<AgentModelInfo>>),
423}
424
425impl AgentModelList {
426    pub fn is_empty(&self) -> bool {
427        match self {
428            AgentModelList::Flat(models) => models.is_empty(),
429            AgentModelList::Grouped(groups) => groups.is_empty(),
430        }
431    }
432
433    pub fn is_flat(&self) -> bool {
434        matches!(self, AgentModelList::Flat(_))
435    }
436}
437
438#[derive(Debug, Clone)]
439pub struct PermissionOptionChoice {
440    pub allow: acp::PermissionOption,
441    pub deny: acp::PermissionOption,
442}
443
444impl PermissionOptionChoice {
445    pub fn label(&self) -> SharedString {
446        self.allow.name.clone().into()
447    }
448}
449
450#[derive(Debug, Clone)]
451pub enum PermissionOptions {
452    Flat(Vec<acp::PermissionOption>),
453    Dropdown(Vec<PermissionOptionChoice>),
454}
455
456impl PermissionOptions {
457    pub fn is_empty(&self) -> bool {
458        match self {
459            PermissionOptions::Flat(options) => options.is_empty(),
460            PermissionOptions::Dropdown(options) => options.is_empty(),
461        }
462    }
463
464    pub fn first_option_of_kind(
465        &self,
466        kind: acp::PermissionOptionKind,
467    ) -> Option<&acp::PermissionOption> {
468        match self {
469            PermissionOptions::Flat(options) => options.iter().find(|option| option.kind == kind),
470            PermissionOptions::Dropdown(options) => options.iter().find_map(|choice| {
471                if choice.allow.kind == kind {
472                    Some(&choice.allow)
473                } else if choice.deny.kind == kind {
474                    Some(&choice.deny)
475                } else {
476                    None
477                }
478            }),
479        }
480    }
481
482    pub fn allow_once_option_id(&self) -> Option<acp::PermissionOptionId> {
483        self.first_option_of_kind(acp::PermissionOptionKind::AllowOnce)
484            .map(|option| option.option_id.clone())
485    }
486
487    pub fn deny_once_option_id(&self) -> Option<acp::PermissionOptionId> {
488        self.first_option_of_kind(acp::PermissionOptionKind::RejectOnce)
489            .map(|option| option.option_id.clone())
490    }
491}
492
493#[cfg(feature = "test-support")]
494mod test_support {
495    //! Test-only stubs and helpers for acp_thread.
496    //!
497    //! This module is gated by the `test-support` feature and is not included
498    //! in production builds. It provides:
499    //! - `StubAgentConnection` for mocking agent connections in tests
500    //! - `create_test_png_base64` for generating test images
501
502    use std::sync::Arc;
503    use std::sync::atomic::{AtomicUsize, Ordering};
504
505    use action_log::ActionLog;
506    use collections::HashMap;
507    use futures::{channel::oneshot, future::try_join_all};
508    use gpui::{AppContext as _, WeakEntity};
509    use parking_lot::Mutex;
510
511    use super::*;
512
513    /// Creates a PNG image encoded as base64 for testing.
514    ///
515    /// Generates a solid-color PNG of the specified dimensions and returns
516    /// it as a base64-encoded string suitable for use in `ImageContent`.
517    pub fn create_test_png_base64(width: u32, height: u32, color: [u8; 4]) -> String {
518        use image::ImageEncoder as _;
519
520        let mut png_data = Vec::new();
521        {
522            let encoder = image::codecs::png::PngEncoder::new(&mut png_data);
523            let mut pixels = Vec::with_capacity((width * height * 4) as usize);
524            for _ in 0..(width * height) {
525                pixels.extend_from_slice(&color);
526            }
527            encoder
528                .write_image(&pixels, width, height, image::ExtendedColorType::Rgba8)
529                .expect("Failed to encode PNG");
530        }
531
532        use image::EncodableLayout as _;
533        base64::Engine::encode(
534            &base64::engine::general_purpose::STANDARD,
535            png_data.as_bytes(),
536        )
537    }
538
539    #[derive(Clone, Default)]
540    pub struct StubAgentConnection {
541        sessions: Arc<Mutex<HashMap<acp::SessionId, Session>>>,
542        permission_requests: HashMap<acp::ToolCallId, PermissionOptions>,
543        next_prompt_updates: Arc<Mutex<Vec<acp::SessionUpdate>>>,
544    }
545
546    struct Session {
547        thread: WeakEntity<AcpThread>,
548        response_tx: Option<oneshot::Sender<acp::StopReason>>,
549    }
550
551    impl StubAgentConnection {
552        pub fn new() -> Self {
553            Self {
554                next_prompt_updates: Default::default(),
555                permission_requests: HashMap::default(),
556                sessions: Arc::default(),
557            }
558        }
559
560        pub fn set_next_prompt_updates(&self, updates: Vec<acp::SessionUpdate>) {
561            *self.next_prompt_updates.lock() = updates;
562        }
563
564        pub fn with_permission_requests(
565            mut self,
566            permission_requests: HashMap<acp::ToolCallId, PermissionOptions>,
567        ) -> Self {
568            self.permission_requests = permission_requests;
569            self
570        }
571
572        pub fn send_update(
573            &self,
574            session_id: acp::SessionId,
575            update: acp::SessionUpdate,
576            cx: &mut App,
577        ) {
578            assert!(
579                self.next_prompt_updates.lock().is_empty(),
580                "Use either send_update or set_next_prompt_updates"
581            );
582
583            self.sessions
584                .lock()
585                .get(&session_id)
586                .unwrap()
587                .thread
588                .update(cx, |thread, cx| {
589                    thread.handle_session_update(update, cx).unwrap();
590                })
591                .unwrap();
592        }
593
594        pub fn end_turn(&self, session_id: acp::SessionId, stop_reason: acp::StopReason) {
595            self.sessions
596                .lock()
597                .get_mut(&session_id)
598                .unwrap()
599                .response_tx
600                .take()
601                .expect("No pending turn")
602                .send(stop_reason)
603                .unwrap();
604        }
605    }
606
607    impl AgentConnection for StubAgentConnection {
608        fn telemetry_id(&self) -> SharedString {
609            "stub".into()
610        }
611
612        fn auth_methods(&self) -> &[acp::AuthMethod] {
613            &[]
614        }
615
616        fn model_selector(
617            &self,
618            _session_id: &acp::SessionId,
619        ) -> Option<Rc<dyn AgentModelSelector>> {
620            Some(self.model_selector_impl())
621        }
622
623        fn new_session(
624            self: Rc<Self>,
625            project: Entity<Project>,
626            cwd: &Path,
627            cx: &mut gpui::App,
628        ) -> Task<gpui::Result<Entity<AcpThread>>> {
629            static NEXT_SESSION_ID: AtomicUsize = AtomicUsize::new(0);
630            let session_id =
631                acp::SessionId::new(NEXT_SESSION_ID.fetch_add(1, Ordering::SeqCst).to_string());
632            let action_log = cx.new(|_| ActionLog::new(project.clone()));
633            let thread = cx.new(|cx| {
634                AcpThread::new(
635                    None,
636                    "Test",
637                    Some(cwd.to_path_buf()),
638                    self.clone(),
639                    project,
640                    action_log,
641                    session_id.clone(),
642                    watch::Receiver::constant(
643                        acp::PromptCapabilities::new()
644                            .image(true)
645                            .audio(true)
646                            .embedded_context(true),
647                    ),
648                    cx,
649                )
650            });
651            self.sessions.lock().insert(
652                session_id,
653                Session {
654                    thread: thread.downgrade(),
655                    response_tx: None,
656                },
657            );
658            Task::ready(Ok(thread))
659        }
660
661        fn authenticate(
662            &self,
663            _method_id: acp::AuthMethodId,
664            _cx: &mut App,
665        ) -> Task<gpui::Result<()>> {
666            unimplemented!()
667        }
668
669        fn prompt(
670            &self,
671            _id: Option<UserMessageId>,
672            params: acp::PromptRequest,
673            cx: &mut App,
674        ) -> Task<gpui::Result<acp::PromptResponse>> {
675            let mut sessions = self.sessions.lock();
676            let Session {
677                thread,
678                response_tx,
679            } = sessions.get_mut(&params.session_id).unwrap();
680            let mut tasks = vec![];
681            if self.next_prompt_updates.lock().is_empty() {
682                let (tx, rx) = oneshot::channel();
683                response_tx.replace(tx);
684                cx.spawn(async move |_| {
685                    let stop_reason = rx.await?;
686                    Ok(acp::PromptResponse::new(stop_reason))
687                })
688            } else {
689                for update in self.next_prompt_updates.lock().drain(..) {
690                    let thread = thread.clone();
691                    let update = update.clone();
692                    let permission_request = if let acp::SessionUpdate::ToolCall(tool_call) =
693                        &update
694                        && let Some(options) = self.permission_requests.get(&tool_call.tool_call_id)
695                    {
696                        Some((tool_call.clone(), options.clone()))
697                    } else {
698                        None
699                    };
700                    let task = cx.spawn(async move |cx| {
701                        if let Some((tool_call, options)) = permission_request {
702                            thread
703                                .update(cx, |thread, cx| {
704                                    thread.request_tool_call_authorization(
705                                        tool_call.clone().into(),
706                                        options.clone(),
707                                        cx,
708                                    )
709                                })??
710                                .await;
711                        }
712                        thread.update(cx, |thread, cx| {
713                            thread.handle_session_update(update.clone(), cx).unwrap();
714                        })?;
715                        anyhow::Ok(())
716                    });
717                    tasks.push(task);
718                }
719
720                cx.spawn(async move |_| {
721                    try_join_all(tasks).await?;
722                    Ok(acp::PromptResponse::new(acp::StopReason::EndTurn))
723                })
724            }
725        }
726
727        fn cancel(&self, session_id: &acp::SessionId, _cx: &mut App) {
728            if let Some(end_turn_tx) = self
729                .sessions
730                .lock()
731                .get_mut(session_id)
732                .unwrap()
733                .response_tx
734                .take()
735            {
736                end_turn_tx.send(acp::StopReason::Cancelled).unwrap();
737            }
738        }
739
740        fn set_title(
741            &self,
742            _session_id: &acp::SessionId,
743            _cx: &App,
744        ) -> Option<Rc<dyn AgentSessionSetTitle>> {
745            Some(Rc::new(StubAgentSessionSetTitle))
746        }
747
748        fn truncate(
749            &self,
750            _session_id: &agent_client_protocol::SessionId,
751            _cx: &App,
752        ) -> Option<Rc<dyn AgentSessionTruncate>> {
753            Some(Rc::new(StubAgentSessionEditor))
754        }
755
756        fn into_any(self: Rc<Self>) -> Rc<dyn Any> {
757            self
758        }
759    }
760
761    struct StubAgentSessionSetTitle;
762
763    impl AgentSessionSetTitle for StubAgentSessionSetTitle {
764        fn run(&self, _title: SharedString, _cx: &mut App) -> Task<Result<()>> {
765            Task::ready(Ok(()))
766        }
767    }
768
769    struct StubAgentSessionEditor;
770
771    impl AgentSessionTruncate for StubAgentSessionEditor {
772        fn run(&self, _: UserMessageId, _: &mut App) -> Task<Result<()>> {
773            Task::ready(Ok(()))
774        }
775    }
776
777    #[derive(Clone)]
778    struct StubModelSelector {
779        selected_model: Arc<Mutex<AgentModelInfo>>,
780    }
781
782    impl StubModelSelector {
783        fn new() -> Self {
784            Self {
785                selected_model: Arc::new(Mutex::new(AgentModelInfo {
786                    id: acp::ModelId::new("visual-test-model"),
787                    name: "Visual Test Model".into(),
788                    description: Some("A stub model for visual testing".into()),
789                    icon: Some(AgentModelIcon::Named(ui::IconName::ZedAssistant)),
790                    is_latest: false,
791                    cost: None,
792                })),
793            }
794        }
795    }
796
797    impl AgentModelSelector for StubModelSelector {
798        fn list_models(&self, _cx: &mut App) -> Task<Result<AgentModelList>> {
799            let model = self.selected_model.lock().clone();
800            Task::ready(Ok(AgentModelList::Flat(vec![model])))
801        }
802
803        fn select_model(&self, model_id: acp::ModelId, _cx: &mut App) -> Task<Result<()>> {
804            self.selected_model.lock().id = model_id;
805            Task::ready(Ok(()))
806        }
807
808        fn selected_model(&self, _cx: &mut App) -> Task<Result<AgentModelInfo>> {
809            Task::ready(Ok(self.selected_model.lock().clone()))
810        }
811    }
812
813    impl StubAgentConnection {
814        /// Returns a model selector for this stub connection.
815        pub fn model_selector_impl(&self) -> Rc<dyn AgentModelSelector> {
816            Rc::new(StubModelSelector::new())
817        }
818    }
819}
820
821#[cfg(feature = "test-support")]
822pub use test_support::*;