BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / agents / mod.rs

mod.rs

mod.rs 6.0 KiB Download Raw
1mod demo;
2mod kills;
3mod lanes;
4mod mcp_bridge;
5mod message_context;
6mod run_chip;
7mod runs;
8mod runs_popover;
9mod session_options;
10mod settings_controls;
11mod settings_forms;
12mod settings_problems;
13mod settings_section;
14mod spinner;
15mod start_dialog;
16mod start_dialog_pickers;
17mod store;
18
19use std::path::PathBuf;
20use std::time::Duration;
21
22use agents::{AgentAction, Environment};
23use directories::ProjectDirs;
24use gpui_kit::*;
25
26pub use run_chip::run_chip;
27pub use runs::{AgentRun, AgentRuns, AgentRunsEvent, AgentRunsHandle, RunState, StartOptions};
28pub use runs_popover::RunsPopover;
29pub use settings_section::AgentsSettings;
30pub use start_dialog::{StartDialog, StartDialogEvent, StartRequest};
31
32use crate::app_state::{AppHandle, AppState};
33
34pub 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
39const MCP_POLL_INTERVAL: Duration = Duration::from_millis(100);
40const MCP_WAIT_LIMIT: Duration = Duration::from_secs(5);
41const MCP_CONFIG_FOLDER: &str = "agent-mcp";
42
43fn 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
48pub fn runs_handle(cx: &App) -> Option<Entity<AgentRuns>> {
49 cx.try_global::<AgentRunsHandle>()
50 .map(|handle| handle.0.clone())
51}
52
53fn 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
63pub 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
82pub 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
116pub 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
126pub 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
155pub 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
161pub 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
165pub 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
176fn 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
207pub 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}