1use std::collections::BTreeMap;
8
9use reqwest::Method;
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13use crate::client::SessionApi;
14use crate::models::{PromptRequest, Session};
15use crate::sse::{EventStream, event_stream_from_response};
16use crate::{Client, Error};
17
18fn current_url(
19 client: &Client,
20 path: &str,
21 pairs: impl IntoIterator<Item = (String, String)>,
22) -> Result<url::Url, Error> {
23 let mut url = client.url(path)?;
24 {
25 let mut query = url.query_pairs_mut();
26 if let Some(directory) = client.configured_directory() {
27 query.append_pair("directory", directory);
28 }
29 if let Some(workspace) = client.configured_workspace() {
30 query.append_pair("workspace", workspace);
31 }
32 for (key, value) in pairs {
33 query.append_pair(&key, &value);
34 }
35 }
36 Ok(url)
37}
38
39async fn decode_value(client: &Client, response: reqwest::Response) -> Result<Value, Error> {
40 client.decode(response).await
41}
42
43async fn decode_bool(client: &Client, response: reqwest::Response) -> Result<bool, Error> {
44 client.decode(response).await
45}
46
47#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
48pub struct SessionListOptions {
49 #[serde(default)]
50 pub scope: Option<String>,
51 #[serde(default)]
52 pub path: Option<String>,
53 #[serde(default)]
54 pub roots: Option<bool>,
55 #[serde(default)]
56 pub start: Option<u64>,
57 #[serde(default)]
58 pub search: Option<String>,
59 #[serde(default)]
60 pub limit: Option<u32>,
61}
62
63#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
64pub struct SessionMessagesOptions {
65 #[serde(default)]
66 pub limit: Option<u32>,
67 #[serde(default)]
68 pub before: Option<String>,
69}
70
71#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
72pub struct PtyCreateRequest {
73 #[serde(default, skip_serializing_if = "Option::is_none")]
74 pub command: Option<String>,
75 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub args: Option<Vec<String>>,
77 #[serde(default, skip_serializing_if = "Option::is_none")]
78 pub cwd: Option<String>,
79 #[serde(default, skip_serializing_if = "Option::is_none")]
80 pub title: Option<String>,
81 #[serde(default, skip_serializing_if = "Option::is_none")]
82 pub env: Option<BTreeMap<String, String>>,
83}
84
85#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
86pub struct PtyUpdateRequest {
87 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub title: Option<String>,
89 #[serde(default, skip_serializing_if = "Option::is_none")]
90 pub size: Option<Value>,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
94pub struct ToolListOptions {
95 pub provider: String,
96 pub model: String,
97}
98
99#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
100#[serde(rename_all = "lowercase")]
101pub enum VcsDiffMode {
102 Git,
103 Branch,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
107pub struct VcsDiffOptions {
108 pub mode: VcsDiffMode,
109 #[serde(default)]
110 pub context: Option<u32>,
111}
112
113#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
114pub struct FindFilesOptions {
115 pub query: String,
116 #[serde(default)]
117 pub dirs: Option<bool>,
118 #[serde(default, rename = "type")]
119 pub entry_type: Option<String>,
120 #[serde(default)]
121 pub limit: Option<u32>,
122}
123
124#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
125#[serde(rename_all = "lowercase")]
126pub enum LogLevel {
127 Debug,
128 Info,
129 Error,
130 Warn,
131}
132
133#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
134pub struct LogRequest {
135 pub service: String,
136 pub level: LogLevel,
137 pub message: String,
138 #[serde(default, skip_serializing_if = "Option::is_none")]
139 pub extra: Option<Value>,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143pub struct McpAddRequest {
144 pub name: String,
145 pub config: Value,
146}
147
148#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
149pub struct ProviderOauthAuthorizeRequest {
150 pub method: u64,
151 #[serde(default, skip_serializing_if = "Option::is_none")]
152 pub inputs: Option<BTreeMap<String, String>>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
156pub struct ProviderOauthCallbackRequest {
157 pub method: u64,
158 #[serde(default, skip_serializing_if = "Option::is_none")]
159 pub code: Option<String>,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
163pub struct PermissionReplyRequest {
164 pub reply: String,
165 #[serde(default, skip_serializing_if = "Option::is_none")]
166 pub message: Option<String>,
167}
168
169#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
170pub struct QuestionReplyRequest {
171 pub answers: Vec<Vec<String>>,
172}
173
174#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
175pub struct GlobalHealth {
176 pub healthy: bool,
177 pub version: String,
178}
179
180pub struct GlobalApi<'a> {
181 client: &'a Client,
182}
183
184impl GlobalApi<'_> {
185 pub async fn health(&self) -> Result<GlobalHealth, Error> {
186 let response = self
187 .client
188 .request_base(Method::GET, "global/health")?
189 .send()
190 .await?;
191 self.client.decode(response).await
192 }
193
194 pub async fn config(&self) -> Result<Value, Error> {
195 let response = self
196 .client
197 .request_base(Method::GET, "global/config")?
198 .send()
199 .await?;
200 decode_value(self.client, response).await
201 }
202
203 pub async fn update_config(&self, body: &Value) -> Result<Value, Error> {
204 let response = self
205 .client
206 .request_base(Method::PATCH, "global/config")?
207 .json(body)
208 .send()
209 .await?;
210 decode_value(self.client, response).await
211 }
212
213 pub async fn dispose(&self) -> Result<bool, Error> {
214 let response = self
215 .client
216 .request_base(Method::POST, "global/dispose")?
217 .send()
218 .await?;
219 decode_bool(self.client, response).await
220 }
221
222 pub async fn upgrade(&self, target: &str) -> Result<Value, Error> {
223 let response = self
224 .client
225 .request_base(Method::POST, "global/upgrade")?
226 .json(&serde_json::json!({ "target": target }))
227 .send()
228 .await?;
229 decode_value(self.client, response).await
230 }
231
232 pub async fn events(&self) -> Result<EventStream, Error> {
233 let response = self
234 .client
235 .request_base(Method::GET, "global/event")?
236 .send()
237 .await?;
238 if !response.status().is_success() {
239 self.client.ensure_success(response).await?;
240 unreachable!();
241 }
242 Ok(event_stream_from_response(response))
243 }
244}
245
246pub struct ProjectApi<'a> {
247 client: &'a Client,
248}
249
250impl ProjectApi<'_> {
251 pub async fn list(&self) -> Result<Vec<Value>, Error> {
252 let response = self.client.request(Method::GET, "project")?.send().await?;
253 self.client.decode(response).await
254 }
255
256 pub async fn current(&self) -> Result<Value, Error> {
257 let response = self
258 .client
259 .request(Method::GET, "project/current")?
260 .send()
261 .await?;
262 decode_value(self.client, response).await
263 }
264
265 pub async fn init_git(&self) -> Result<Value, Error> {
266 let response = self
267 .client
268 .request(Method::POST, "project/git/init")?
269 .send()
270 .await?;
271 decode_value(self.client, response).await
272 }
273
274 pub async fn update(&self, project_id: &str, body: &Value) -> Result<Value, Error> {
275 let response = self
276 .client
277 .request(Method::PATCH, &format!("project/{project_id}"))?
278 .json(body)
279 .send()
280 .await?;
281 decode_value(self.client, response).await
282 }
283
284 pub async fn directories(&self, project_id: &str) -> Result<Value, Error> {
285 let response = self
286 .client
287 .request(Method::GET, &format!("project/{project_id}/directories"))?
288 .send()
289 .await?;
290 decode_value(self.client, response).await
291 }
292}
293
294pub struct PtyApi<'a> {
295 client: &'a Client,
296}
297
298impl PtyApi<'_> {
299 pub async fn list(&self) -> Result<Vec<Value>, Error> {
300 let response = self.client.request(Method::GET, "pty")?.send().await?;
301 self.client.decode(response).await
302 }
303
304 pub async fn create(&self, body: &PtyCreateRequest) -> Result<Value, Error> {
305 let response = self
306 .client
307 .request(Method::POST, "pty")?
308 .json(body)
309 .send()
310 .await?;
311 decode_value(self.client, response).await
312 }
313
314 pub async fn get(&self, pty_id: &str) -> Result<Value, Error> {
315 let response = self
316 .client
317 .request(Method::GET, &format!("pty/{pty_id}"))?
318 .send()
319 .await?;
320 decode_value(self.client, response).await
321 }
322
323 pub async fn update(&self, pty_id: &str, body: &PtyUpdateRequest) -> Result<Value, Error> {
324 let response = self
325 .client
326 .request(Method::PUT, &format!("pty/{pty_id}"))?
327 .json(body)
328 .send()
329 .await?;
330 decode_value(self.client, response).await
331 }
332
333 pub async fn remove(&self, pty_id: &str) -> Result<bool, Error> {
334 let response = self
335 .client
336 .request(Method::DELETE, &format!("pty/{pty_id}"))?
337 .send()
338 .await?;
339 decode_bool(self.client, response).await
340 }
341
342 pub async fn shells(&self) -> Result<Vec<Value>, Error> {
343 let response = self
344 .client
345 .request(Method::GET, "pty/shells")?
346 .send()
347 .await?;
348 self.client.decode(response).await
349 }
350
351 pub async fn connect_token(&self, pty_id: &str) -> Result<Value, Error> {
352 let response = self
353 .client
354 .request(Method::POST, &format!("pty/{pty_id}/connect-token"))?
355 .send()
356 .await?;
357 decode_value(self.client, response).await
358 }
359
360 pub async fn connect(
361 &self,
362 pty_id: &str,
363 cursor: Option<&str>,
364 ticket: Option<&str>,
365 ) -> Result<bool, Error> {
366 let mut pairs = Vec::new();
367 if let Some(cursor) = cursor {
368 pairs.push(("cursor".to_owned(), cursor.to_owned()));
369 }
370 if let Some(ticket) = ticket {
371 pairs.push(("ticket".to_owned(), ticket.to_owned()));
372 }
373 let url = current_url(self.client, &format!("pty/{pty_id}/connect"), pairs)?;
374 let response = self.client.request_url(Method::GET, url).send().await?;
375 decode_bool(self.client, response).await
376 }
377}
378
379pub struct ConfigApi<'a> {
380 client: &'a Client,
381}
382
383impl ConfigApi<'_> {
384 pub async fn get(&self) -> Result<Value, Error> {
385 let response = self.client.request(Method::GET, "config")?.send().await?;
386 decode_value(self.client, response).await
387 }
388
389 pub async fn update(&self, body: &Value) -> Result<Value, Error> {
390 let response = self
391 .client
392 .request(Method::PATCH, "config")?
393 .json(body)
394 .send()
395 .await?;
396 decode_value(self.client, response).await
397 }
398
399 pub async fn providers(&self) -> Result<Value, Error> {
400 let response = self
401 .client
402 .request(Method::GET, "config/providers")?
403 .send()
404 .await?;
405 decode_value(self.client, response).await
406 }
407}
408
409pub struct ToolApi<'a> {
410 client: &'a Client,
411}
412
413impl ToolApi<'_> {
414 pub async fn ids(&self) -> Result<Value, Error> {
415 let response = self
416 .client
417 .request(Method::GET, "experimental/tool/ids")?
418 .send()
419 .await?;
420 decode_value(self.client, response).await
421 }
422
423 pub async fn list(&self, options: &ToolListOptions) -> Result<Value, Error> {
424 let url = current_url(
425 self.client,
426 "experimental/tool",
427 [
428 ("provider".to_owned(), options.provider.clone()),
429 ("model".to_owned(), options.model.clone()),
430 ],
431 )?;
432 let response = self.client.request_url(Method::GET, url).send().await?;
433 decode_value(self.client, response).await
434 }
435}
436
437pub struct InstanceApi<'a> {
438 client: &'a Client,
439}
440
441impl InstanceApi<'_> {
442 pub async fn dispose(&self) -> Result<bool, Error> {
443 let response = self
444 .client
445 .request(Method::POST, "instance/dispose")?
446 .send()
447 .await?;
448 decode_bool(self.client, response).await
449 }
450}
451
452pub struct PathApi<'a> {
453 client: &'a Client,
454}
455
456impl PathApi<'_> {
457 pub async fn get(&self) -> Result<Value, Error> {
458 let response = self.client.request(Method::GET, "path")?.send().await?;
459 decode_value(self.client, response).await
460 }
461}
462
463pub struct VcsApi<'a> {
464 client: &'a Client,
465}
466
467impl VcsApi<'_> {
468 pub async fn get(&self) -> Result<Value, Error> {
469 let response = self.client.request(Method::GET, "vcs")?.send().await?;
470 decode_value(self.client, response).await
471 }
472
473 pub async fn status(&self) -> Result<Vec<Value>, Error> {
474 let response = self
475 .client
476 .request(Method::GET, "vcs/status")?
477 .send()
478 .await?;
479 self.client.decode(response).await
480 }
481
482 pub async fn diff(&self, options: &VcsDiffOptions) -> Result<Vec<Value>, Error> {
483 let mut pairs = vec![(
484 "mode".to_owned(),
485 match options.mode {
486 VcsDiffMode::Git => "git",
487 VcsDiffMode::Branch => "branch",
488 }
489 .to_owned(),
490 )];
491 if let Some(context) = options.context {
492 pairs.push(("context".to_owned(), context.to_string()));
493 }
494 let url = current_url(self.client, "vcs/diff", pairs)?;
495 let response = self.client.request_url(Method::GET, url).send().await?;
496 self.client.decode(response).await
497 }
498
499 pub async fn diff_raw(&self) -> Result<String, Error> {
500 let response = self
501 .client
502 .request(Method::GET, "vcs/diff/raw")?
503 .send()
504 .await?;
505 if !response.status().is_success() {
506 self.client.ensure_success(response).await?;
507 unreachable!();
508 }
509 Ok(response.text().await?)
510 }
511
512 pub async fn apply(&self, patch: &str) -> Result<bool, Error> {
513 #[derive(Deserialize)]
514 struct ApplyResponse {
515 applied: bool,
516 }
517 let response = self
518 .client
519 .request(Method::POST, "vcs/apply")?
520 .json(&serde_json::json!({ "patch": patch }))
521 .send()
522 .await?;
523 let value: ApplyResponse = self.client.decode(response).await?;
524 Ok(value.applied)
525 }
526}
527
528pub struct CommandApi<'a> {
529 client: &'a Client,
530}
531
532impl CommandApi<'_> {
533 pub async fn list(&self) -> Result<Vec<Value>, Error> {
534 let response = self.client.request(Method::GET, "command")?.send().await?;
535 self.client.decode(response).await
536 }
537}
538
539pub struct ProviderApi<'a> {
540 client: &'a Client,
541}
542
543impl ProviderApi<'_> {
544 pub async fn list(&self) -> Result<Value, Error> {
545 let response = self.client.request(Method::GET, "provider")?.send().await?;
546 decode_value(self.client, response).await
547 }
548
549 pub async fn auth(&self) -> Result<Value, Error> {
550 let response = self
551 .client
552 .request(Method::GET, "provider/auth")?
553 .send()
554 .await?;
555 decode_value(self.client, response).await
556 }
557
558 pub fn oauth(&self) -> ProviderOauthApi<'_> {
559 ProviderOauthApi {
560 client: self.client,
561 }
562 }
563}
564
565pub struct ProviderOauthApi<'a> {
566 client: &'a Client,
567}
568
569impl ProviderOauthApi<'_> {
570 pub async fn authorize(
571 &self,
572 provider_id: &str,
573 body: &ProviderOauthAuthorizeRequest,
574 ) -> Result<Value, Error> {
575 let response = self
576 .client
577 .request(
578 Method::POST,
579 &format!("provider/{provider_id}/oauth/authorize"),
580 )?
581 .json(body)
582 .send()
583 .await?;
584 decode_value(self.client, response).await
585 }
586
587 pub async fn callback(
588 &self,
589 provider_id: &str,
590 body: &ProviderOauthCallbackRequest,
591 ) -> Result<bool, Error> {
592 let response = self
593 .client
594 .request(
595 Method::POST,
596 &format!("provider/{provider_id}/oauth/callback"),
597 )?
598 .json(body)
599 .send()
600 .await?;
601 decode_bool(self.client, response).await
602 }
603}
604
605pub struct FindApi<'a> {
606 client: &'a Client,
607}
608
609impl FindApi<'_> {
610 pub async fn text(&self, pattern: &str) -> Result<Vec<Value>, Error> {
611 let url = current_url(
612 self.client,
613 "find",
614 [("pattern".to_owned(), pattern.to_owned())],
615 )?;
616 let response = self.client.request_url(Method::GET, url).send().await?;
617 self.client.decode(response).await
618 }
619
620 pub async fn files(&self, options: &FindFilesOptions) -> Result<Vec<String>, Error> {
621 let mut pairs = vec![("query".to_owned(), options.query.clone())];
622 if let Some(dirs) = options.dirs {
623 pairs.push(("dirs".to_owned(), dirs.to_string()));
624 }
625 if let Some(entry_type) = &options.entry_type {
626 pairs.push(("type".to_owned(), entry_type.clone()));
627 }
628 if let Some(limit) = options.limit {
629 pairs.push(("limit".to_owned(), limit.to_string()));
630 }
631 let url = current_url(self.client, "find/file", pairs)?;
632 let response = self.client.request_url(Method::GET, url).send().await?;
633 self.client.decode(response).await
634 }
635
636 pub async fn symbols(&self, query: &str) -> Result<Vec<Value>, Error> {
637 let url = current_url(
638 self.client,
639 "find/symbol",
640 [("query".to_owned(), query.to_owned())],
641 )?;
642 let response = self.client.request_url(Method::GET, url).send().await?;
643 self.client.decode(response).await
644 }
645}
646
647pub struct FileApi<'a> {
648 client: &'a Client,
649}
650
651impl FileApi<'_> {
652 pub async fn list(&self, path: &str) -> Result<Vec<Value>, Error> {
653 let url = current_url(self.client, "file", [("path".to_owned(), path.to_owned())])?;
654 let response = self.client.request_url(Method::GET, url).send().await?;
655 self.client.decode(response).await
656 }
657
658 pub async fn read(&self, path: &str) -> Result<Value, Error> {
659 let url = current_url(
660 self.client,
661 "file/content",
662 [("path".to_owned(), path.to_owned())],
663 )?;
664 let response = self.client.request_url(Method::GET, url).send().await?;
665 decode_value(self.client, response).await
666 }
667
668 pub async fn status(&self) -> Result<Vec<Value>, Error> {
669 let response = self
670 .client
671 .request(Method::GET, "file/status")?
672 .send()
673 .await?;
674 self.client.decode(response).await
675 }
676}
677
678pub struct AppApi<'a> {
679 client: &'a Client,
680}
681
682impl AppApi<'_> {
683 pub async fn log(&self, body: &LogRequest) -> Result<bool, Error> {
684 let response = self
685 .client
686 .request(Method::POST, "log")?
687 .json(body)
688 .send()
689 .await?;
690 decode_bool(self.client, response).await
691 }
692
693 pub async fn agents(&self) -> Result<Vec<Value>, Error> {
694 let response = self.client.request(Method::GET, "agent")?.send().await?;
695 self.client.decode(response).await
696 }
697
698 pub async fn skills(&self) -> Result<Vec<Value>, Error> {
699 let response = self.client.request(Method::GET, "skill")?.send().await?;
700 self.client.decode(response).await
701 }
702}
703
704pub struct McpApi<'a> {
705 client: &'a Client,
706}
707
708impl McpApi<'_> {
709 pub async fn status(&self) -> Result<BTreeMap<String, Value>, Error> {
710 let response = self.client.request(Method::GET, "mcp")?.send().await?;
711 self.client.decode(response).await
712 }
713
714 pub async fn add(&self, body: &McpAddRequest) -> Result<BTreeMap<String, Value>, Error> {
715 let response = self
716 .client
717 .request(Method::POST, "mcp")?
718 .json(body)
719 .send()
720 .await?;
721 self.client.decode(response).await
722 }
723
724 pub async fn connect(&self, name: &str) -> Result<bool, Error> {
725 let response = self
726 .client
727 .request(Method::POST, &format!("mcp/{name}/connect"))?
728 .send()
729 .await?;
730 decode_bool(self.client, response).await
731 }
732
733 pub async fn disconnect(&self, name: &str) -> Result<bool, Error> {
734 let response = self
735 .client
736 .request(Method::POST, &format!("mcp/{name}/disconnect"))?
737 .send()
738 .await?;
739 decode_bool(self.client, response).await
740 }
741
742 pub fn auth(&self) -> McpAuthApi<'_> {
743 McpAuthApi {
744 client: self.client,
745 }
746 }
747}
748
749pub struct McpAuthApi<'a> {
750 client: &'a Client,
751}
752
753impl McpAuthApi<'_> {
754 pub async fn start(&self, name: &str) -> Result<Value, Error> {
755 let response = self
756 .client
757 .request(Method::POST, &format!("mcp/{name}/auth"))?
758 .send()
759 .await?;
760 decode_value(self.client, response).await
761 }
762
763 pub async fn remove(&self, name: &str) -> Result<Value, Error> {
764 let response = self
765 .client
766 .request(Method::DELETE, &format!("mcp/{name}/auth"))?
767 .send()
768 .await?;
769 decode_value(self.client, response).await
770 }
771
772 pub async fn callback(&self, name: &str, code: &str) -> Result<Value, Error> {
773 let response = self
774 .client
775 .request(Method::POST, &format!("mcp/{name}/auth/callback"))?
776 .json(&serde_json::json!({ "code": code }))
777 .send()
778 .await?;
779 decode_value(self.client, response).await
780 }
781
782 pub async fn authenticate(&self, name: &str) -> Result<Value, Error> {
783 let response = self
784 .client
785 .request(Method::POST, &format!("mcp/{name}/auth/authenticate"))?
786 .send()
787 .await?;
788 decode_value(self.client, response).await
789 }
790}
791
792pub struct LspApi<'a> {
793 client: &'a Client,
794}
795
796impl LspApi<'_> {
797 pub async fn status(&self) -> Result<Vec<Value>, Error> {
798 let response = self.client.request(Method::GET, "lsp")?.send().await?;
799 self.client.decode(response).await
800 }
801}
802
803pub struct FormatterApi<'a> {
804 client: &'a Client,
805}
806
807impl FormatterApi<'_> {
808 pub async fn status(&self) -> Result<Vec<Value>, Error> {
809 let response = self
810 .client
811 .request(Method::GET, "formatter")?
812 .send()
813 .await?;
814 self.client.decode(response).await
815 }
816}
817
818pub struct AuthApi<'a> {
819 client: &'a Client,
820}
821
822impl AuthApi<'_> {
823 pub async fn set(&self, provider_id: &str, body: &Value) -> Result<bool, Error> {
824 let response = self
825 .client
826 .request_base(Method::PUT, &format!("auth/{provider_id}"))?
827 .json(body)
828 .send()
829 .await?;
830 decode_bool(self.client, response).await
831 }
832
833 pub async fn remove(&self, provider_id: &str) -> Result<bool, Error> {
834 let response = self
835 .client
836 .request_base(Method::DELETE, &format!("auth/{provider_id}"))?
837 .send()
838 .await?;
839 decode_bool(self.client, response).await
840 }
841}
842
843pub struct PermissionApi<'a> {
844 client: &'a Client,
845}
846
847impl PermissionApi<'_> {
848 pub async fn list(&self) -> Result<Vec<Value>, Error> {
849 let response = self
850 .client
851 .request(Method::GET, "permission")?
852 .send()
853 .await?;
854 self.client.decode(response).await
855 }
856
857 pub async fn reply(
858 &self,
859 request_id: &str,
860 body: &PermissionReplyRequest,
861 ) -> Result<bool, Error> {
862 let response = self
863 .client
864 .request(Method::POST, &format!("permission/{request_id}/reply"))?
865 .json(body)
866 .send()
867 .await?;
868 decode_bool(self.client, response).await
869 }
870}
871
872pub struct QuestionApi<'a> {
873 client: &'a Client,
874}
875
876impl QuestionApi<'_> {
877 pub async fn list(&self) -> Result<Vec<Value>, Error> {
878 let response = self.client.request(Method::GET, "question")?.send().await?;
879 self.client.decode(response).await
880 }
881
882 pub async fn reply(
883 &self,
884 request_id: &str,
885 body: &QuestionReplyRequest,
886 ) -> Result<bool, Error> {
887 let response = self
888 .client
889 .request(Method::POST, &format!("question/{request_id}/reply"))?
890 .json(body)
891 .send()
892 .await?;
893 decode_bool(self.client, response).await
894 }
895
896 pub async fn reject(&self, request_id: &str) -> Result<bool, Error> {
897 let response = self
898 .client
899 .request(Method::POST, &format!("question/{request_id}/reject"))?
900 .send()
901 .await?;
902 decode_bool(self.client, response).await
903 }
904}
905
906pub struct TuiApi<'a> {
907 client: &'a Client,
908}
909
910impl TuiApi<'_> {
911 async fn post_bool(&self, path: &str, body: Option<&Value>) -> Result<bool, Error> {
912 let mut request = self.client.request(Method::POST, path)?;
913 if let Some(body) = body {
914 request = request.json(body);
915 }
916 decode_bool(self.client, request.send().await?).await
917 }
918
919 pub async fn append_prompt(&self, text: &str) -> Result<bool, Error> {
920 self.post_bool(
921 "tui/append-prompt",
922 Some(&serde_json::json!({ "text": text })),
923 )
924 .await
925 }
926
927 pub async fn open_help(&self) -> Result<bool, Error> {
928 self.post_bool("tui/open-help", None).await
929 }
930
931 pub async fn open_sessions(&self) -> Result<bool, Error> {
932 self.post_bool("tui/open-sessions", None).await
933 }
934
935 pub async fn open_themes(&self) -> Result<bool, Error> {
936 self.post_bool("tui/open-themes", None).await
937 }
938
939 pub async fn open_models(&self) -> Result<bool, Error> {
940 self.post_bool("tui/open-models", None).await
941 }
942
943 pub async fn submit_prompt(&self) -> Result<bool, Error> {
944 self.post_bool("tui/submit-prompt", None).await
945 }
946
947 pub async fn clear_prompt(&self) -> Result<bool, Error> {
948 self.post_bool("tui/clear-prompt", None).await
949 }
950
951 pub async fn execute_command(&self, command: &str) -> Result<bool, Error> {
952 self.post_bool(
953 "tui/execute-command",
954 Some(&serde_json::json!({ "command": command })),
955 )
956 .await
957 }
958
959 pub async fn show_toast(&self, body: &Value) -> Result<bool, Error> {
960 self.post_bool("tui/show-toast", Some(body)).await
961 }
962
963 pub async fn publish(&self, body: &Value) -> Result<bool, Error> {
964 self.post_bool("tui/publish", Some(body)).await
965 }
966
967 pub async fn select_session(&self, session_id: &str) -> Result<bool, Error> {
968 self.post_bool(
969 "tui/select-session",
970 Some(&serde_json::json!({ "sessionID": session_id })),
971 )
972 .await
973 }
974
975 pub fn control(&self) -> TuiControlApi<'_> {
976 TuiControlApi {
977 client: self.client,
978 }
979 }
980}
981
982pub struct TuiControlApi<'a> {
983 client: &'a Client,
984}
985
986impl TuiControlApi<'_> {
987 pub async fn next(&self) -> Result<Value, Error> {
988 let response = self
989 .client
990 .request(Method::GET, "tui/control/next")?
991 .send()
992 .await?;
993 decode_value(self.client, response).await
994 }
995
996 pub async fn response(&self, body: &Value) -> Result<bool, Error> {
997 let response = self
998 .client
999 .request(Method::POST, "tui/control/response")?
1000 .json(body)
1001 .send()
1002 .await?;
1003 decode_bool(self.client, response).await
1004 }
1005}
1006
1007impl SessionApi<'_> {
1008 pub async fn list_with(&self, options: &SessionListOptions) -> Result<Vec<Session>, Error> {
1009 let mut pairs = Vec::new();
1010 if let Some(scope) = &options.scope {
1011 pairs.push(("scope".to_owned(), scope.clone()));
1012 }
1013 if let Some(path) = &options.path {
1014 pairs.push(("path".to_owned(), path.clone()));
1015 }
1016 if let Some(roots) = options.roots {
1017 pairs.push(("roots".to_owned(), roots.to_string()));
1018 }
1019 if let Some(start) = options.start {
1020 pairs.push(("start".to_owned(), start.to_string()));
1021 }
1022 if let Some(search) = &options.search {
1023 pairs.push(("search".to_owned(), search.clone()));
1024 }
1025 if let Some(limit) = options.limit {
1026 pairs.push(("limit".to_owned(), limit.to_string()));
1027 }
1028 let url = current_url(self.client, "session", pairs)?;
1029 let response = self.client.request_url(Method::GET, url).send().await?;
1030 self.client.decode(response).await
1031 }
1032
1033 pub async fn status(&self) -> Result<BTreeMap<String, Value>, Error> {
1034 let response = self
1035 .client
1036 .request(Method::GET, "session/status")?
1037 .send()
1038 .await?;
1039 self.client.decode(response).await
1040 }
1041
1042 pub async fn delete(&self, session_id: &str) -> Result<bool, Error> {
1043 let response = self
1044 .client
1045 .request(Method::DELETE, &format!("session/{session_id}"))?
1046 .send()
1047 .await?;
1048 decode_bool(self.client, response).await
1049 }
1050
1051 pub async fn update(&self, session_id: &str, body: &Value) -> Result<Session, Error> {
1052 let response = self
1053 .client
1054 .request(Method::PATCH, &format!("session/{session_id}"))?
1055 .json(body)
1056 .send()
1057 .await?;
1058 self.client.decode(response).await
1059 }
1060
1061 pub async fn children(&self, session_id: &str) -> Result<Vec<Session>, Error> {
1062 let response = self
1063 .client
1064 .request(Method::GET, &format!("session/{session_id}/children"))?
1065 .send()
1066 .await?;
1067 self.client.decode(response).await
1068 }
1069
1070 pub async fn todo(&self, session_id: &str) -> Result<Vec<Value>, Error> {
1071 let response = self
1072 .client
1073 .request(Method::GET, &format!("session/{session_id}/todo"))?
1074 .send()
1075 .await?;
1076 self.client.decode(response).await
1077 }
1078
1079 pub async fn init(&self, session_id: &str, body: &Value) -> Result<bool, Error> {
1080 let response = self
1081 .client
1082 .request(Method::POST, &format!("session/{session_id}/init"))?
1083 .json(body)
1084 .send()
1085 .await?;
1086 decode_bool(self.client, response).await
1087 }
1088
1089 pub async fn fork(&self, session_id: &str, message_id: Option<&str>) -> Result<Session, Error> {
1090 let body = match message_id {
1091 Some(message_id) => serde_json::json!({ "messageID": message_id }),
1092 None => serde_json::json!({}),
1093 };
1094 let response = self
1095 .client
1096 .request(Method::POST, &format!("session/{session_id}/fork"))?
1097 .json(&body)
1098 .send()
1099 .await?;
1100 self.client.decode(response).await
1101 }
1102
1103 pub async fn share(&self, session_id: &str) -> Result<Session, Error> {
1104 let response = self
1105 .client
1106 .request(Method::POST, &format!("session/{session_id}/share"))?
1107 .send()
1108 .await?;
1109 self.client.decode(response).await
1110 }
1111
1112 pub async fn unshare(&self, session_id: &str) -> Result<Session, Error> {
1113 let response = self
1114 .client
1115 .request(Method::DELETE, &format!("session/{session_id}/share"))?
1116 .send()
1117 .await?;
1118 self.client.decode(response).await
1119 }
1120
1121 pub async fn diff(
1122 &self,
1123 session_id: &str,
1124 message_id: Option<&str>,
1125 ) -> Result<Vec<Value>, Error> {
1126 let mut pairs = Vec::new();
1127 if let Some(message_id) = message_id {
1128 pairs.push(("messageID".to_owned(), message_id.to_owned()));
1129 }
1130 let url = current_url(self.client, &format!("session/{session_id}/diff"), pairs)?;
1131 let response = self.client.request_url(Method::GET, url).send().await?;
1132 self.client.decode(response).await
1133 }
1134
1135 pub async fn summarize(&self, session_id: &str, body: &Value) -> Result<bool, Error> {
1136 let response = self
1137 .client
1138 .request(Method::POST, &format!("session/{session_id}/summarize"))?
1139 .json(body)
1140 .send()
1141 .await?;
1142 decode_bool(self.client, response).await
1143 }
1144
1145 pub async fn messages(
1146 &self,
1147 session_id: &str,
1148 options: &SessionMessagesOptions,
1149 ) -> Result<Vec<Value>, Error> {
1150 let mut pairs = Vec::new();
1151 if let Some(limit) = options.limit {
1152 pairs.push(("limit".to_owned(), limit.to_string()));
1153 }
1154 if let Some(before) = &options.before {
1155 pairs.push(("before".to_owned(), before.clone()));
1156 }
1157 let url = current_url(self.client, &format!("session/{session_id}/message"), pairs)?;
1158 let response = self.client.request_url(Method::GET, url).send().await?;
1159 self.client.decode(response).await
1160 }
1161
1162 pub async fn message(&self, session_id: &str, message_id: &str) -> Result<Value, Error> {
1163 let response = self
1164 .client
1165 .request(
1166 Method::GET,
1167 &format!("session/{session_id}/message/{message_id}"),
1168 )?
1169 .send()
1170 .await?;
1171 decode_value(self.client, response).await
1172 }
1173
1174 pub async fn prompt_async(&self, session_id: &str, body: &PromptRequest) -> Result<(), Error> {
1175 let response = self
1176 .client
1177 .request(Method::POST, &format!("session/{session_id}/prompt_async"))?
1178 .json(body)
1179 .send()
1180 .await?;
1181 self.client.ensure_success(response).await
1182 }
1183
1184 pub async fn command(&self, session_id: &str, body: &Value) -> Result<Value, Error> {
1185 let response = self
1186 .client
1187 .request(Method::POST, &format!("session/{session_id}/command"))?
1188 .json(body)
1189 .send()
1190 .await?;
1191 decode_value(self.client, response).await
1192 }
1193
1194 pub async fn shell(&self, session_id: &str, body: &Value) -> Result<Value, Error> {
1195 let response = self
1196 .client
1197 .request(Method::POST, &format!("session/{session_id}/shell"))?
1198 .json(body)
1199 .send()
1200 .await?;
1201 decode_value(self.client, response).await
1202 }
1203
1204 pub async fn revert(&self, session_id: &str, body: &Value) -> Result<Session, Error> {
1205 let response = self
1206 .client
1207 .request(Method::POST, &format!("session/{session_id}/revert"))?
1208 .json(body)
1209 .send()
1210 .await?;
1211 self.client.decode(response).await
1212 }
1213
1214 pub async fn unrevert(&self, session_id: &str) -> Result<Session, Error> {
1215 let response = self
1216 .client
1217 .request(Method::POST, &format!("session/{session_id}/unrevert"))?
1218 .send()
1219 .await?;
1220 self.client.decode(response).await
1221 }
1222
1223 pub async fn delete_message(&self, session_id: &str, message_id: &str) -> Result<bool, Error> {
1224 let response = self
1225 .client
1226 .request(
1227 Method::DELETE,
1228 &format!("session/{session_id}/message/{message_id}"),
1229 )?
1230 .send()
1231 .await?;
1232 decode_bool(self.client, response).await
1233 }
1234
1235 pub async fn update_part(
1236 &self,
1237 session_id: &str,
1238 message_id: &str,
1239 part_id: &str,
1240 body: &Value,
1241 ) -> Result<Value, Error> {
1242 let response = self
1243 .client
1244 .request(
1245 Method::PATCH,
1246 &format!("session/{session_id}/message/{message_id}/part/{part_id}"),
1247 )?
1248 .json(body)
1249 .send()
1250 .await?;
1251 decode_value(self.client, response).await
1252 }
1253
1254 pub async fn delete_part(
1255 &self,
1256 session_id: &str,
1257 message_id: &str,
1258 part_id: &str,
1259 ) -> Result<bool, Error> {
1260 let response = self
1261 .client
1262 .request(
1263 Method::DELETE,
1264 &format!("session/{session_id}/message/{message_id}/part/{part_id}"),
1265 )?
1266 .send()
1267 .await?;
1268 decode_bool(self.client, response).await
1269 }
1270
1271 pub async fn respond_permission(
1272 &self,
1273 session_id: &str,
1274 permission_id: &str,
1275 response_value: &str,
1276 ) -> Result<bool, Error> {
1277 let response = self
1278 .client
1279 .request(
1280 Method::POST,
1281 &format!("session/{session_id}/permissions/{permission_id}"),
1282 )?
1283 .json(&serde_json::json!({ "response": response_value }))
1284 .send()
1285 .await?;
1286 decode_bool(self.client, response).await
1287 }
1288}
1289
1290impl Client {
1291 pub fn global(&self) -> GlobalApi<'_> {
1292 GlobalApi { client: self }
1293 }
1294
1295 pub fn project(&self) -> ProjectApi<'_> {
1296 ProjectApi { client: self }
1297 }
1298
1299 pub fn pty(&self) -> PtyApi<'_> {
1300 PtyApi { client: self }
1301 }
1302
1303 pub fn config(&self) -> ConfigApi<'_> {
1304 ConfigApi { client: self }
1305 }
1306
1307 pub fn tool(&self) -> ToolApi<'_> {
1308 ToolApi { client: self }
1309 }
1310
1311 pub fn instance(&self) -> InstanceApi<'_> {
1312 InstanceApi { client: self }
1313 }
1314
1315 pub fn path(&self) -> PathApi<'_> {
1316 PathApi { client: self }
1317 }
1318
1319 pub fn vcs(&self) -> VcsApi<'_> {
1320 VcsApi { client: self }
1321 }
1322
1323 pub fn command(&self) -> CommandApi<'_> {
1324 CommandApi { client: self }
1325 }
1326
1327 pub fn provider(&self) -> ProviderApi<'_> {
1328 ProviderApi { client: self }
1329 }
1330
1331 pub fn find(&self) -> FindApi<'_> {
1332 FindApi { client: self }
1333 }
1334
1335 pub fn file(&self) -> FileApi<'_> {
1336 FileApi { client: self }
1337 }
1338
1339 pub fn app(&self) -> AppApi<'_> {
1340 AppApi { client: self }
1341 }
1342
1343 pub fn mcp(&self) -> McpApi<'_> {
1344 McpApi { client: self }
1345 }
1346
1347 pub fn lsp(&self) -> LspApi<'_> {
1348 LspApi { client: self }
1349 }
1350
1351 pub fn formatter(&self) -> FormatterApi<'_> {
1352 FormatterApi { client: self }
1353 }
1354
1355 pub fn tui(&self) -> TuiApi<'_> {
1356 TuiApi { client: self }
1357 }
1358
1359 pub fn auth(&self) -> AuthApi<'_> {
1360 AuthApi { client: self }
1361 }
1362
1363 pub fn permission(&self) -> PermissionApi<'_> {
1364 PermissionApi { client: self }
1365 }
1366
1367 pub fn question(&self) -> QuestionApi<'_> {
1368 QuestionApi { client: self }
1369 }
1370
1371 pub fn event(&self) -> crate::client::EventsApi<'_> {
1372 self.events()
1373 }
1374}