| 1 | mod chat_run; |
| 2 | pub mod demo; |
| 3 | #[cfg(any(test, feature = "test-support"))] |
| 4 | pub mod fixtures; |
| 5 | pub mod kills; |
| 6 | pub mod mcp_bridge; |
| 7 | pub mod message_context; |
| 8 | pub mod runs; |
| 9 | pub mod session_options; |
| 10 | pub mod store; |
| 11 | |
| 12 | use std::path::PathBuf; |
| 13 | use std::time::Duration; |
| 14 | |
| 15 | use agents::{AgentAction, AgentProfile, Environment}; |
| 16 | pub use app_base::agents::{lanes, settings_problems}; |
| 17 | pub use app_ui_kit::{settings_controls, spinner}; |
| 18 | use directories::ProjectDirs; |
| 19 | use gpui_kit::*; |
| 20 | |
| 21 | pub use runs::{ |
| 22 | AgentRun, AgentRuns, AgentRunsEvent, AgentRunsHandle, RunState, StartOptions, StartRequest, |
| 23 | needs_dialog, |
| 24 | }; |
| 25 | |
| 26 | use crate::app_state::{AppHandle, AppState}; |
| 27 | |
| 28 | pub fn install(app: Entity<AppState>, cx: &mut App) { |
| 29 | let runs = cx.new(|cx| AgentRuns::new(app, cx)); |
| 30 | cx.set_global(AgentRunsHandle(runs)); |
| 31 | } |
| 32 | |
| 33 | const MCP_POLL_INTERVAL: Duration = Duration::from_millis(100); |
| 34 | const MCP_WAIT_LIMIT: Duration = Duration::from_secs(5); |
| 35 | const MCP_CONFIG_FOLDER: &str = "agent-mcp"; |
| 36 | |
| 37 | fn mcp_config_directory() -> Option<PathBuf> { |
| 38 | ProjectDirs::from("", "", ::store::DATA_DIR_NAME) |
| 39 | .map(|directories| directories.data_local_dir().join(MCP_CONFIG_FOLDER)) |
| 40 | } |
| 41 | |
| 42 | pub fn runs_handle(cx: &App) -> Option<Entity<AgentRuns>> { |
| 43 | cx.try_global::<AgentRunsHandle>() |
| 44 | .map(|handle| handle.0.clone()) |
| 45 | } |
| 46 | |
| 47 | /// Chat profile for «Write with», or `None` when Microsoft Copilot is selected. |
| 48 | pub fn local_writer(cx: &App) -> Option<AgentProfile> { |
| 49 | runs_handle(cx)? |
| 50 | .read(cx) |
| 51 | .settings() |
| 52 | .write_with_profile() |
| 53 | .cloned() |
| 54 | } |
| 55 | |
| 56 | pub fn agents_enabled(cx: &App) -> bool { |
| 57 | runs_handle(cx).is_some_and(|runs| !runs.read(cx).settings().actions.is_empty()) |
| 58 | } |
| 59 | |
| 60 | pub fn raise_notice(text: String, cx: &mut App) { |
| 61 | let Some(handle) = cx.try_global::<AppHandle>() else { |
| 62 | return; |
| 63 | }; |
| 64 | handle |
| 65 | .0 |
| 66 | .clone() |
| 67 | .update(cx, |state, cx| state.raise_notice(text, None, cx)); |
| 68 | } |
| 69 | |
| 70 | pub fn start_run( |
| 71 | action_id: &str, |
| 72 | conversation_id: &str, |
| 73 | message_id: &str, |
| 74 | extra: &str, |
| 75 | options: StartOptions, |
| 76 | cx: &mut App, |
| 77 | ) { |
| 78 | let Some(runs) = runs_handle(cx) else { |
| 79 | return; |
| 80 | }; |
| 81 | let started = runs.update(cx, |runs, cx| { |
| 82 | runs.start_with(action_id, conversation_id, message_id, extra, options, cx) |
| 83 | }); |
| 84 | if let Err(error) = started { |
| 85 | raise_notice(format!("Agent could not start: {error}"), cx); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn start_run_when_mcp_ready( |
| 90 | action_id: String, |
| 91 | conversation_id: String, |
| 92 | message_id: String, |
| 93 | extra: String, |
| 94 | options: StartOptions, |
| 95 | cx: &mut App, |
| 96 | ) { |
| 97 | cx.spawn(async move |cx| { |
| 98 | let mut waited = Duration::ZERO; |
| 99 | while waited < MCP_WAIT_LIMIT && !cx.update(|cx| mcp_bridge::endpoint(cx).is_some()) { |
| 100 | cx.background_executor().timer(MCP_POLL_INTERVAL).await; |
| 101 | waited += MCP_POLL_INTERVAL; |
| 102 | } |
| 103 | cx.update(|cx| { |
| 104 | if mcp_bridge::endpoint(cx).is_none() { |
| 105 | raise_notice( |
| 106 | "MCP server is not running - the agent cannot reply".to_owned(), |
| 107 | cx, |
| 108 | ); |
| 109 | } |
| 110 | start_run( |
| 111 | &action_id, |
| 112 | &conversation_id, |
| 113 | &message_id, |
| 114 | &extra, |
| 115 | options, |
| 116 | cx, |
| 117 | ); |
| 118 | }); |
| 119 | }) |
| 120 | .detach(); |
| 121 | } |
| 122 | |
| 123 | pub fn retry_run(run_id: u64, cx: &mut App) { |
| 124 | let Some(runs) = runs_handle(cx) else { |
| 125 | return; |
| 126 | }; |
| 127 | let retried = runs.update(cx, |runs, cx| runs.retry(run_id, cx)); |
| 128 | if let Err(error) = retried { |
| 129 | raise_notice(format!("Agent could not start: {error}"), cx); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | pub fn delegate(request: StartRequest, cx: &mut App) { |
| 134 | let Some(runs) = runs_handle(cx) else { |
| 135 | return; |
| 136 | }; |
| 137 | let action = runs |
| 138 | .read(cx) |
| 139 | .settings() |
| 140 | .actions |
| 141 | .iter() |
| 142 | .find(|action| action.id == request.action_id) |
| 143 | .cloned(); |
| 144 | let Some(action) = action else { |
| 145 | return; |
| 146 | }; |
| 147 | let allowed = mcp_bridge::chat_allowed(&request.conversation_id, cx); |
| 148 | if needs_dialog(&action, allowed) { |
| 149 | runs.update(cx, |_, cx| cx.emit(AgentRunsEvent::StartRequested(request))); |
| 150 | } else { |
| 151 | start_run( |
| 152 | &request.action_id, |
| 153 | &request.conversation_id, |
| 154 | &request.message_id, |
| 155 | "", |
| 156 | StartOptions::default(), |
| 157 | cx, |
| 158 | ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | pub fn open_settings(cx: &mut App) { |
| 163 | if let Some(runs) = runs_handle(cx) { |
| 164 | runs.update(cx, |_, cx| cx.emit(AgentRunsEvent::OpenSettings)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub fn has_session(session_id: &str, cx: &App) -> bool { |
| 169 | runs_handle(cx).is_some_and(|runs| runs.read(cx).by_session(session_id).is_some()) |
| 170 | } |
| 171 | |
| 172 | pub fn open_session(session_id: &str, cx: &mut App) { |
| 173 | let run = runs_handle(cx).and_then(|runs| runs.read(cx).by_session(session_id).cloned()); |
| 174 | match run { |
| 175 | Some(run) => open_run_session(&run, cx), |
| 176 | None => raise_notice( |
| 177 | "This session was not started from this app, open it in your terminal".to_owned(), |
| 178 | cx, |
| 179 | ), |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | fn open_run_session(run: &AgentRun, cx: &mut App) { |
| 184 | let Some(handle) = cx.try_global::<AppHandle>() else { |
| 185 | return; |
| 186 | }; |
| 187 | let settings = store::load_settings(&handle.0.read(cx).store); |
| 188 | let Some(profile) = settings |
| 189 | .agents |
| 190 | .iter() |
| 191 | .find(|profile| profile.id == run.agent_id) |
| 192 | else { |
| 193 | raise_notice("The agent of this run no longer exists".to_owned(), cx); |
| 194 | return; |
| 195 | }; |
| 196 | let action = AgentAction { |
| 197 | working_dir: run.working_dir.clone(), |
| 198 | ..AgentAction::default() |
| 199 | }; |
| 200 | let spawned = agents::open_session_invocation(profile, &action, &run.session_id) |
| 201 | .map_err(|error| error.to_string()) |
| 202 | .and_then(|invocation| { |
| 203 | std::process::Command::new(&invocation.program) |
| 204 | .args(&invocation.args) |
| 205 | .spawn() |
| 206 | .map(drop) |
| 207 | .map_err(|error| error.to_string()) |
| 208 | }); |
| 209 | if let Err(error) = spawned { |
| 210 | raise_notice(format!("Could not open the session: {error}"), cx); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | pub fn environment_label(environment: &Environment) -> String { |
| 215 | match environment { |
| 216 | Environment::Host => { |
| 217 | if cfg!(windows) { |
| 218 | "Windows".to_owned() |
| 219 | } else { |
| 220 | "This computer".to_owned() |
| 221 | } |
| 222 | } |
| 223 | Environment::Wsl { distro: None } => "WSL".to_owned(), |
| 224 | Environment::Wsl { |
| 225 | distro: Some(distro), |
| 226 | } => format!("WSL - {distro}"), |
| 227 | } |
| 228 | } |