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