BlueGit

ict rusty-teams

Files

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

mod.rs

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