BlueGit

ict rusty-teams

Files

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

mod.rs

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