Repository navigation
Expand file tree
/
Copy pathhandler.rs
More file actions
331 lines (276 loc) · 10 KB
/
Copy pathhandler.rs
File metadata and controls
331 lines (276 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Collaboration tool handlers.
use super::{
AgentControl, AgentRole, AgentStatus, CollabError, Result, ThreadId,
guards::exceeds_thread_spawn_depth_limit, source::next_thread_spawn_depth, timeouts,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};
// ============================================================================
// spawn_agent
// ============================================================================
/// Arguments for spawn_agent tool.
#[derive(Debug, Clone, Deserialize)]
pub struct SpawnAgentArgs {
/// Message/prompt for the new agent.
pub message: String,
/// Type of agent to spawn.
#[serde(default)]
pub agent_type: Option<AgentRole>,
}
/// Result of spawn_agent tool.
#[derive(Debug, Clone, Serialize)]
pub struct SpawnAgentResult {
/// ID of the spawned agent.
pub agent_id: String,
}
// ============================================================================
// send_input
// ============================================================================
/// Arguments for send_input tool.
#[derive(Debug, Clone, Deserialize)]
pub struct SendInputArgs {
/// ID of the target agent.
pub id: String,
/// Message to send.
pub message: String,
/// Whether to interrupt the agent before sending.
#[serde(default)]
pub interrupt: bool,
}
/// Result of send_input tool.
#[derive(Debug, Clone, Serialize)]
pub struct SendInputResult {
/// Submission ID.
pub submission_id: String,
}
// ============================================================================
// wait
// ============================================================================
/// Arguments for wait tool.
#[derive(Debug, Clone, Deserialize)]
pub struct WaitArgs {
/// IDs of agents to wait for.
pub ids: Vec<String>,
/// Timeout in milliseconds.
#[serde(default)]
pub timeout_ms: Option<i64>,
}
/// Result of wait tool.
#[derive(Debug, Clone, Serialize)]
pub struct WaitResult {
/// Status of each agent.
pub status: HashMap<String, AgentStatus>,
/// Whether the wait timed out.
pub timed_out: bool,
}
// ============================================================================
// close_agent
// ============================================================================
/// Arguments for close_agent tool.
#[derive(Debug, Clone, Deserialize)]
pub struct CloseAgentArgs {
/// ID of the agent to close.
pub id: String,
}
/// Result of close_agent tool.
#[derive(Debug, Clone, Serialize)]
pub struct CloseAgentResult {
/// Final status of the agent.
pub status: AgentStatus,
}
// ============================================================================
// CollabHandler
// ============================================================================
/// Handler for collaboration tools.
pub struct CollabHandler {
control: AgentControl,
}
impl CollabHandler {
/// Create a new collaboration handler.
pub fn new(control: AgentControl) -> Self {
Self { control }
}
/// Handle spawn_agent tool call.
pub async fn handle_spawn_agent(
&self,
args: SpawnAgentArgs,
parent_thread_id: ThreadId,
current_depth: i32,
) -> Result<SpawnAgentResult> {
// Validate message
if args.message.trim().is_empty() {
return Err(CollabError::EmptyMessage);
}
// Check depth limit
let child_depth = next_thread_spawn_depth(&super::SessionSource::SubAgent(
super::SubAgentSource::ThreadSpawn {
parent_thread_id,
depth: current_depth,
},
));
if exceeds_thread_spawn_depth_limit(child_depth) {
return Err(CollabError::DepthLimitExceeded);
}
// Build config
let role = args.agent_type.unwrap_or(AgentRole::General);
let config =
self.control
.build_spawn_config(role, &args.message, parent_thread_id, child_depth);
// Spawn the agent
let agent_id = self
.control
.spawn_agent(config, args.message.clone(), None)
.await?;
Ok(SpawnAgentResult {
agent_id: agent_id.to_string(),
})
}
/// Handle send_input tool call.
pub async fn handle_send_input(&self, args: SendInputArgs) -> Result<SendInputResult> {
// Validate message
if args.message.trim().is_empty() {
return Err(CollabError::EmptyMessage);
}
// Parse agent ID
let agent_id =
ThreadId::parse(&args.id).map_err(|_| CollabError::AgentNotFound(ThreadId::new()))?;
// Interrupt if requested
if args.interrupt {
self.control.interrupt_agent(agent_id).await?;
}
// Send the prompt
let submission_id = self.control.send_prompt(agent_id, args.message).await?;
Ok(SendInputResult { submission_id })
}
/// Handle wait tool call.
pub async fn handle_wait(&self, args: WaitArgs) -> Result<WaitResult> {
// Validate IDs
if args.ids.is_empty() {
return Err(CollabError::Internal("ids must be non-empty".to_string()));
}
// Validate and clamp timeout
let timeout_ms = args.timeout_ms.unwrap_or(timeouts::DEFAULT_WAIT_TIMEOUT_MS);
if timeout_ms <= 0 {
return Err(CollabError::InvalidTimeout);
}
let timeout_ms =
timeout_ms.clamp(timeouts::MIN_WAIT_TIMEOUT_MS, timeouts::MAX_WAIT_TIMEOUT_MS);
// Parse agent IDs
let mut agent_ids = Vec::with_capacity(args.ids.len());
for id_str in &args.ids {
match ThreadId::parse(id_str) {
Ok(id) => agent_ids.push(id),
Err(_) => {
// Agent not found, will be reported in status
}
}
}
// Get initial statuses
let mut statuses = HashMap::new();
let mut pending = Vec::new();
for id in &agent_ids {
let status = self.control.get_status(*id).await;
if status.is_final() {
statuses.insert(id.to_string(), status);
} else {
pending.push(*id);
}
}
// Wait for pending agents with timeout
let deadline = Instant::now() + Duration::from_millis(timeout_ms as u64);
let mut timed_out = false;
for id in pending {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
timed_out = true;
statuses.insert(id.to_string(), self.control.get_status(id).await);
continue;
}
match self.control.subscribe_status(id).await {
Ok(mut rx) => loop {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
timed_out = true;
statuses.insert(id.to_string(), rx.borrow().clone());
break;
}
tokio::select! {
_ = tokio::time::sleep(remaining) => {
timed_out = true;
statuses.insert(id.to_string(), rx.borrow().clone());
break;
}
result = rx.changed() => {
if result.is_err() {
statuses.insert(id.to_string(), AgentStatus::NotFound);
break;
}
let status = rx.borrow().clone();
if status.is_final() {
statuses.insert(id.to_string(), status);
break;
}
}
}
},
Err(_) => {
statuses.insert(id.to_string(), AgentStatus::NotFound);
}
}
}
Ok(WaitResult {
status: statuses,
timed_out,
})
}
/// Handle close_agent tool call.
pub async fn handle_close_agent(&self, args: CloseAgentArgs) -> Result<CloseAgentResult> {
// Parse agent ID
let agent_id =
ThreadId::parse(&args.id).map_err(|_| CollabError::AgentNotFound(ThreadId::new()))?;
// Get current status
let status = self.control.get_status(agent_id).await;
// Shutdown if not already
if !matches!(status, AgentStatus::Shutdown | AgentStatus::NotFound) {
self.control.shutdown_agent(agent_id).await?;
}
Ok(CloseAgentResult {
status: self.control.get_status(agent_id).await,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ThreadManagerState;
use std::sync::Arc;
#[tokio::test]
async fn test_spawn_agent() {
let state = Arc::new(ThreadManagerState::new());
let control = AgentControl::new(Arc::downgrade(&state));
let handler = CollabHandler::new(control);
let args = SpawnAgentArgs {
message: "Test task".to_string(),
agent_type: Some(AgentRole::General),
};
let result = handler
.handle_spawn_agent(args, ThreadId::new(), 0)
.await
.unwrap();
assert!(!result.agent_id.is_empty());
}
#[tokio::test]
async fn test_spawn_depth_limit() {
let state = Arc::new(ThreadManagerState::new());
let control = AgentControl::new(Arc::downgrade(&state));
let handler = CollabHandler::new(control);
let args = SpawnAgentArgs {
message: "Test task".to_string(),
agent_type: None,
};
// Should fail at depth 1 (exceeds limit of 1)
let result = handler.handle_spawn_agent(args, ThreadId::new(), 1).await;
assert!(matches!(result, Err(CollabError::DepthLimitExceeded)));
}
}