BlueGit

ict rusty-teams

Files

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

mod.rs

d39bdfed History
mod.rs 5.9 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 raise_notice(text: String, cx: &mut App) {
46 let Some(handle) = cx.try_global::<AppHandle>() else {
47 return;
48 };
49 handle
50 .0
51 .clone()
52 .update(cx, |state, cx| state.raise_notice(text, None, cx));
53}
54
55pub fn start_run(
56 action_id: &str,
57 conversation_id: &str,
58 message_id: &str,
59 extra: &str,
60 options: StartOptions,
61 cx: &mut App,
62) {
63 let Some(runs) = runs_handle(cx) else {
64 return;
65 };
66 let started = runs.update(cx, |runs, cx| {
67 runs.start_with(action_id, conversation_id, message_id, extra, options, cx)
68 });
69 if let Err(error) = started {
70 raise_notice(format!("Agent could not start: {error}"), cx);
71 }
72}
73
74pub fn start_run_when_mcp_ready(
75 action_id: String,
76 conversation_id: String,
77 message_id: String,
78 extra: String,
79 options: StartOptions,
80 cx: &mut App,
81) {
82 cx.spawn(async move |cx| {
83 let mut waited = Duration::ZERO;
84 while waited < MCP_WAIT_LIMIT && !cx.update(|cx| mcp_bridge::endpoint(cx).is_some()) {
85 cx.background_executor().timer(MCP_POLL_INTERVAL).await;
86 waited += MCP_POLL_INTERVAL;
87 }
88 cx.update(|cx| {
89 if mcp_bridge::endpoint(cx).is_none() {
90 raise_notice(
91 "MCP server is not running - the agent cannot propose a reply".to_owned(),
92 cx,
93 );
94 }
95 start_run(
96 &action_id,
97 &conversation_id,
98 &message_id,
99 &extra,
100 options,
101 cx,
102 );
103 });
104 })
105 .detach();
106}
107
108pub fn retry_run(run_id: u64, cx: &mut App) {
109 let Some(runs) = runs_handle(cx) else {
110 return;
111 };
112 let retried = runs.update(cx, |runs, cx| runs.retry(run_id, cx));
113 if let Err(error) = retried {
114 raise_notice(format!("Agent could not start: {error}"), cx);
115 }
116}
117
118pub fn delegate(request: StartRequest, cx: &mut App) {
119 let Some(runs) = runs_handle(cx) else {
120 return;
121 };
122 let action = runs
123 .read(cx)
124 .settings()
125 .actions
126 .iter()
127 .find(|action| action.id == request.action_id)
128 .cloned();
129 let Some(action) = action else {
130 return;
131 };
132 let allowed = mcp_bridge::chat_allowed(&request.conversation_id, cx);
133 if needs_dialog(&action, allowed) {
134 runs.update(cx, |_, cx| cx.emit(AgentRunsEvent::StartRequested(request)));
135 } else {
136 start_run(
137 &request.action_id,
138 &request.conversation_id,
139 &request.message_id,
140 "",
141 StartOptions::default(),
142 cx,
143 );
144 }
145}
146
147pub fn open_settings(cx: &mut App) {
148 if let Some(runs) = runs_handle(cx) {
149 runs.update(cx, |_, cx| cx.emit(AgentRunsEvent::OpenSettings));
150 }
151}
152
153pub fn has_session(session_id: &str, cx: &App) -> bool {
154 runs_handle(cx).is_some_and(|runs| runs.read(cx).by_session(session_id).is_some())
155}
156
157pub fn open_session(session_id: &str, cx: &mut App) {
158 let run = runs_handle(cx).and_then(|runs| runs.read(cx).by_session(session_id).cloned());
159 match run {
160 Some(run) => open_run_session(&run, cx),
161 None => raise_notice(
162 "This session was not started from this app, open it in your terminal".to_owned(),
163 cx,
164 ),
165 }
166}
167
168fn open_run_session(run: &AgentRun, cx: &mut App) {
169 let Some(handle) = cx.try_global::<AppHandle>() else {
170 return;
171 };
172 let settings = store::load_settings(&handle.0.read(cx).store);
173 let Some(profile) = settings
174 .agents
175 .iter()
176 .find(|profile| profile.id == run.agent_id)
177 else {
178 raise_notice("The agent of this run no longer exists".to_owned(), cx);
179 return;
180 };
181 let action = AgentAction {
182 working_dir: run.working_dir.clone(),
183 ..AgentAction::default()
184 };
185 let spawned = agents::open_session_invocation(profile, &action, &run.session_id)
186 .map_err(|error| error.to_string())
187 .and_then(|invocation| {
188 std::process::Command::new(&invocation.program)
189 .args(&invocation.args)
190 .spawn()
191 .map(drop)
192 .map_err(|error| error.to_string())
193 });
194 if let Err(error) = spawned {
195 raise_notice(format!("Could not open the session: {error}"), cx);
196 }
197}
198
199pub fn environment_label(environment: &Environment) -> String {
200 match environment {
201 Environment::Host => "Windows".to_owned(),
202 Environment::Wsl { distro: None } => "WSL".to_owned(),
203 Environment::Wsl {
204 distro: Some(distro),
205 } => format!("WSL - {distro}"),
206 }
207}