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