1use serde_json::{json, Map, Value};
9
10use crate::client::Client;
11use crate::errors::Result;
12use crate::protocol::{new_disconnect, new_request_with_id, new_subscribe};
13
14fn opt_request_id(request_id: &[&str]) -> String {
17 request_id
18 .iter()
19 .copied()
20 .find(|s| !s.is_empty())
21 .unwrap_or_default()
22 .to_string()
23}
24
25impl Client {
26 pub async fn send_command(&self, cmd: &str) -> Result<()> {
28 let mut params = Map::new();
29 params.insert("cmd".into(), json!(cmd));
30 self.notify("slash_command", params).await
31 }
32
33 pub async fn send_detach(&self) -> Result<()> {
35 self.send_envelope(new_disconnect()).await
36 }
37
38 pub async fn send_daemon_status(&self, request_id: &[&str]) -> Result<()> {
40 let rid = opt_request_id(request_id);
41 self.send_envelope(new_request_with_id("daemon_status", Map::new(), rid))
42 .await
43 }
44
45 pub async fn send_daemon_shutdown(&self, request_id: &[&str]) -> Result<()> {
47 let rid = opt_request_id(request_id);
48 self.send_envelope(new_request_with_id("daemon_shutdown", Map::new(), rid))
49 .await
50 }
51
52 pub async fn send_config_get(&self, section: &str, request_id: &[&str]) -> Result<()> {
54 let rid = opt_request_id(request_id);
55 let mut params = Map::new();
56 params.insert("section".into(), json!(section));
57 self.send_envelope(new_request_with_id("config_get", params, rid))
58 .await
59 }
60
61 pub async fn send_config_reload(&self, request_id: &[&str]) -> Result<()> {
63 let rid = opt_request_id(request_id);
64 self.send_envelope(new_request_with_id("config_reload", Map::new(), rid))
65 .await
66 }
67
68 pub async fn send_skills_list(&self, request_id: &[&str]) -> Result<()> {
70 let rid = opt_request_id(request_id);
71 self.send_envelope(new_request_with_id("skills_list", Map::new(), rid))
72 .await
73 }
74
75 pub async fn send_models_list(&self, request_id: &[&str]) -> Result<()> {
77 let rid = opt_request_id(request_id);
78 self.send_envelope(new_request_with_id("models_list", Map::new(), rid))
79 .await
80 }
81
82 pub async fn send_invoke_skill(
84 &self,
85 skill: &str,
86 args: &str,
87 interaction_mode: Option<&str>,
88 request_id: &[&str],
89 ) -> Result<()> {
90 let rid = opt_request_id(request_id);
91 let mut params = Map::new();
92 params.insert("skill".into(), json!(skill));
93 if !args.is_empty() {
94 params.insert("args".into(), json!(args));
95 }
96 if let Some(v) = interaction_mode {
97 params.insert("interaction_mode".into(), json!(v));
98 }
99 self.send_envelope(new_request_with_id("invoke_skill", params, rid))
100 .await
101 }
102
103 pub async fn send_mcp_status(&self, request_id: &[&str]) -> Result<()> {
105 let rid = opt_request_id(request_id);
106 self.send_envelope(new_request_with_id("mcp_status", Map::new(), rid))
107 .await
108 }
109
110 pub async fn send_loop_list(
112 &self,
113 filter: Option<Map<String, Value>>,
114 limit: u32,
115 request_id: &[&str],
116 ) -> Result<()> {
117 let rid = opt_request_id(request_id);
118 let mut params = Map::new();
119 if let Some(f) = filter {
120 params.insert("filter".into(), Value::Object(f));
121 }
122 if limit > 0 {
123 params.insert("limit".into(), json!(limit));
124 }
125 self.send_envelope(new_request_with_id("loop_list", params, rid))
126 .await
127 }
128
129 pub async fn send_loop_get(
131 &self,
132 loop_id: &str,
133 verbose: bool,
134 request_id: &[&str],
135 ) -> Result<()> {
136 let rid = opt_request_id(request_id);
137 let mut params = Map::new();
138 params.insert("loop_id".into(), json!(loop_id));
139 if verbose {
140 params.insert("verbose".into(), json!(true));
141 }
142 self.send_envelope(new_request_with_id("loop_get", params, rid))
143 .await
144 }
145
146 pub async fn send_loop_delete(&self, loop_id: &str, request_id: &[&str]) -> Result<()> {
148 let rid = opt_request_id(request_id);
149 let mut params = Map::new();
150 params.insert("loop_id".into(), json!(loop_id));
151 self.send_envelope(new_request_with_id("loop_delete", params, rid))
152 .await
153 }
154
155 pub async fn send_loop_reattach(&self, loop_id: &str, request_id: &[&str]) -> Result<()> {
157 let rid = opt_request_id(request_id);
158 let mut params = Map::new();
159 params.insert("loop_id".into(), json!(loop_id));
160 self.send_envelope(new_request_with_id("loop_reattach", params, rid))
161 .await
162 }
163
164 pub async fn send_follow_on_confirm(
170 &self,
171 loop_id: &str,
172 follow_on_id: &str,
173 as_mode: Option<&str>,
174 ) -> Result<()> {
175 let mode = as_mode
176 .map(str::trim)
177 .filter(|s| !s.is_empty())
178 .unwrap_or("agent");
179 let mut params = Map::new();
180 params.insert("loop_id".into(), json!(loop_id));
181 params.insert("follow_on_id".into(), json!(follow_on_id));
182 params.insert("action".into(), json!("confirm"));
183 params.insert("as_mode".into(), json!(mode));
184 self.notify("loop_follow_on", params).await
185 }
186
187 pub async fn send_follow_on_dismiss(&self, loop_id: &str, follow_on_id: &str) -> Result<()> {
190 let mut params = Map::new();
191 params.insert("loop_id".into(), json!(loop_id));
192 params.insert("follow_on_id".into(), json!(follow_on_id));
193 params.insert("action".into(), json!("dismiss"));
194 self.notify("loop_follow_on", params).await
195 }
196
197 pub async fn send_loop_subscribe(
201 &self,
202 loop_id: &str,
203 wire_tier: &str,
204 stream_delivery: &str,
205 request_id: &[&str],
206 ) -> Result<()> {
207 let rid = opt_request_id(request_id);
208 let mut params = Map::new();
209 params.insert("loop_id".into(), json!(loop_id));
210 if !wire_tier.is_empty() {
211 params.insert("wire_tier".into(), json!(wire_tier));
212 }
213 if !stream_delivery.is_empty() {
214 params.insert("stream_delivery".into(), json!(stream_delivery));
215 }
216 let env = if rid.is_empty() {
217 new_subscribe("loop_events", params)
218 } else {
219 let mut e = new_subscribe("loop_events", params);
221 e.id = Some(rid);
222 e
223 };
224 self.send_envelope(env).await
225 }
226
227 pub async fn send_loop_detach(&self, loop_id: &str, request_id: &[&str]) -> Result<()> {
231 let rid = opt_request_id(request_id);
232 let id = if rid.is_empty() {
233 loop_id.to_string()
234 } else {
235 rid
236 };
237 self.unsubscribe(&id).await
238 }
239
240 pub async fn send_loop_new(
242 &self,
243 client_workspace: &str,
244 user_id: &str,
245 client_workspace_id: &str,
246 is_ephemeral: bool,
247 request_id: &[&str],
248 ) -> Result<()> {
249 let rid = opt_request_id(request_id);
250 let mut params = Map::new();
251 if !client_workspace.is_empty() {
252 params.insert("client_workspace".into(), json!(client_workspace));
253 }
254 if !user_id.is_empty() {
255 params.insert("user_id".into(), json!(user_id));
256 }
257 if !client_workspace_id.is_empty() {
258 params.insert("client_workspace_id".into(), json!(client_workspace_id));
259 }
260 if is_ephemeral {
261 params.insert("is_ephemeral".into(), json!(true));
262 }
263 self.send_envelope(new_request_with_id("loop_new", params, rid))
264 .await
265 }
266
267 pub async fn send_loop_input(&self, loop_id: &str, content: &str) -> Result<()> {
269 let mut params = Map::new();
270 params.insert("loop_id".into(), json!(loop_id));
271 params.insert("content".into(), json!(content));
272 self.notify("loop_input", params).await
273 }
274
275 pub async fn send_loop_messages(
277 &self,
278 loop_id: &str,
279 limit: u32,
280 offset: u32,
281 include_events: bool,
282 request_id: &[&str],
283 ) -> Result<()> {
284 let rid = opt_request_id(request_id);
285 let mut params = Map::new();
286 params.insert("loop_id".into(), json!(loop_id));
287 if limit > 0 {
288 params.insert("limit".into(), json!(limit));
289 }
290 if offset > 0 {
291 params.insert("offset".into(), json!(offset));
292 }
293 if include_events {
294 params.insert("include_events".into(), json!(true));
295 }
296 self.send_envelope(new_request_with_id("loop_messages", params, rid))
297 .await
298 }
299
300 pub async fn send_loop_state_get(&self, loop_id: &str, request_id: &[&str]) -> Result<()> {
302 let rid = opt_request_id(request_id);
303 let mut params = Map::new();
304 params.insert("loop_id".into(), json!(loop_id));
305 self.send_envelope(new_request_with_id("loop_state_get", params, rid))
306 .await
307 }
308
309 pub async fn send_loop_state_update(
311 &self,
312 loop_id: &str,
313 values: Map<String, Value>,
314 as_node: &str,
315 request_id: &[&str],
316 ) -> Result<()> {
317 let rid = opt_request_id(request_id);
318 let mut params = Map::new();
319 params.insert("loop_id".into(), json!(loop_id));
320 params.insert("values".into(), Value::Object(values));
321 if !as_node.is_empty() {
322 params.insert("as_node".into(), json!(as_node));
323 }
324 self.send_envelope(new_request_with_id("loop_state_update", params, rid))
325 .await
326 }
327
328 pub async fn send_loop_history_fetch(&self, loop_id: &str, request_id: &[&str]) -> Result<()> {
330 let rid = opt_request_id(request_id);
331 let mut params = Map::new();
332 params.insert("loop_id".into(), json!(loop_id));
333 self.send_envelope(new_request_with_id("loop_history_fetch", params, rid))
334 .await
335 }
336
337 pub async fn send_loop_execution_state_fetch(
340 &self,
341 loop_id: &str,
342 request_id: &[&str],
343 ) -> Result<()> {
344 let rid = opt_request_id(request_id);
345 let mut params = Map::new();
346 params.insert("loop_id".into(), json!(loop_id));
347 self.send_envelope(new_request_with_id(
348 "loop_execution_state_fetch",
349 params,
350 rid,
351 ))
352 .await
353 }
354
355 pub async fn send_auth(
357 &self,
358 access_key: &str,
359 secret_key: &str,
360 request_id: &[&str],
361 ) -> Result<()> {
362 let rid = opt_request_id(request_id);
363 let mut params = Map::new();
364 params.insert("access_key".into(), json!(access_key));
365 params.insert("secret_key".into(), json!(secret_key));
366 self.send_envelope(new_request_with_id("auth", params, rid))
367 .await
368 }
369
370 pub async fn send_auth_refresh(&self, refresh_token: &str, request_id: &[&str]) -> Result<()> {
372 let rid = opt_request_id(request_id);
373 let mut params = Map::new();
374 params.insert("refresh_token".into(), json!(refresh_token));
375 self.send_envelope(new_request_with_id("auth_refresh", params, rid))
376 .await
377 }
378
379 pub async fn send_cron_add(
381 &self,
382 text: &str,
383 priority: i32,
384 request_id: &[&str],
385 ) -> Result<()> {
386 let rid = opt_request_id(request_id);
387 let mut params = Map::new();
388 params.insert("text".into(), json!(text));
389 if priority > 0 {
390 params.insert("priority".into(), json!(priority));
391 }
392 self.send_envelope(new_request_with_id("cron_add", params, rid))
393 .await
394 }
395
396 pub async fn send_cron_list(&self, status: &str, request_id: &[&str]) -> Result<()> {
398 let rid = opt_request_id(request_id);
399 let mut params = Map::new();
400 if !status.is_empty() {
401 params.insert("status".into(), json!(status));
402 }
403 self.send_envelope(new_request_with_id("cron_list", params, rid))
404 .await
405 }
406
407 pub async fn send_cron_show(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
409 let rid = opt_request_id(request_id);
410 let mut params = Map::new();
411 params.insert("job_id".into(), json!(job_id));
412 self.send_envelope(new_request_with_id("cron_show", params, rid))
413 .await
414 }
415
416 pub async fn send_cron_cancel(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
418 let rid = opt_request_id(request_id);
419 let mut params = Map::new();
420 params.insert("job_id".into(), json!(job_id));
421 self.send_envelope(new_request_with_id("cron_cancel", params, rid))
422 .await
423 }
424
425 pub async fn send_job_create(
428 &self,
429 goal: &str,
430 rail_id: &str,
431 workspace: Option<&str>,
432 request_id: &[&str],
433 ) -> Result<()> {
434 if goal.is_empty() {
435 return Err(crate::errors::Error::msg("goal is required"));
436 }
437 if rail_id.is_empty() {
438 return Err(crate::errors::Error::msg("rail_id is required"));
439 }
440 let rid = opt_request_id(request_id);
441 let mut params = Map::new();
442 params.insert("goal".into(), json!(goal));
443 params.insert("rail_id".into(), json!(rail_id));
444 if let Some(ws) = workspace {
445 if !ws.is_empty() {
446 params.insert("workspace".into(), json!(ws));
447 }
448 }
449 self.send_envelope(new_request_with_id("job_create", params, rid))
450 .await
451 }
452
453 pub async fn send_job_status(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
455 if job_id.is_empty() {
456 return Err(crate::errors::Error::msg("job_id is required"));
457 }
458 let rid = opt_request_id(request_id);
459 let mut params = Map::new();
460 params.insert("job_id".into(), json!(job_id));
461 self.send_envelope(new_request_with_id("job_status", params, rid))
462 .await
463 }
464
465 pub async fn send_job_pause(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
467 if job_id.is_empty() {
468 return Err(crate::errors::Error::msg("job_id is required"));
469 }
470 let rid = opt_request_id(request_id);
471 let mut params = Map::new();
472 params.insert("job_id".into(), json!(job_id));
473 self.send_envelope(new_request_with_id("job_pause", params, rid))
474 .await
475 }
476
477 pub async fn send_job_resume(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
479 if job_id.is_empty() {
480 return Err(crate::errors::Error::msg("job_id is required"));
481 }
482 let rid = opt_request_id(request_id);
483 let mut params = Map::new();
484 params.insert("job_id".into(), json!(job_id));
485 self.send_envelope(new_request_with_id("job_resume", params, rid))
486 .await
487 }
488
489 pub async fn send_job_cancel(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
491 if job_id.is_empty() {
492 return Err(crate::errors::Error::msg("job_id is required"));
493 }
494 let rid = opt_request_id(request_id);
495 let mut params = Map::new();
496 params.insert("job_id".into(), json!(job_id));
497 self.send_envelope(new_request_with_id("job_cancel", params, rid))
498 .await
499 }
500
501 pub async fn send_job_dag(&self, job_id: &str, request_id: &[&str]) -> Result<()> {
503 if job_id.is_empty() {
504 return Err(crate::errors::Error::msg("job_id is required"));
505 }
506 let rid = opt_request_id(request_id);
507 let mut params = Map::new();
508 params.insert("job_id".into(), json!(job_id));
509 self.send_envelope(new_request_with_id("job_dag", params, rid))
510 .await
511 }
512}
513
514#[cfg(test)]
515mod tests {
516 use super::*;
517
518 #[test]
519 fn opt_request_id_picks_first_non_empty() {
520 assert_eq!(opt_request_id(&[]), "");
521 assert_eq!(opt_request_id(&[""]), "");
522 assert_eq!(opt_request_id(&["abc"]), "abc");
523 assert_eq!(opt_request_id(&["", "def"]), "def");
524 }
525}