marbots-sdk 0.3.0

Typed Rust SDK for Marbots, the multi-agent collaboration platform (Boss Man orchestration, per-bot models, skills, MCP, A2A). By Gravicode Studios.
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use std::io::{BufRead, BufReader};
use std::time::Duration;

use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::{json, Value};
use ureq::Agent;

use crate::types::*;

/// Errors returned by the SDK.
#[derive(Debug)]
pub enum Error {
    /// The server answered with an error status (message from Problem Details).
    Api { status: u16, message: String },
    /// Transport or decoding failure.
    Transport(String),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Api { status, message } => write!(f, "HTTP {status}: {message}"),
            Error::Transport(m) => write!(f, "transport error: {m}"),
        }
    }
}

impl std::error::Error for Error {}

impl Error {
    /// The HTTP status for API errors.
    pub fn status(&self) -> Option<u16> {
        match self {
            Error::Api { status, .. } => Some(*status),
            Error::Transport(_) => None,
        }
    }
}

impl From<ureq::Error> for Error {
    fn from(e: ureq::Error) -> Self {
        Error::Transport(e.to_string())
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Error::Transport(e.to_string())
    }
}

/// SDK result type.
pub type Result<T> = std::result::Result<T, Error>;

fn esc(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'~') {
            out.push(b as char);
        } else {
            out.push_str(&format!("%{b:02X}"));
        }
    }
    out
}

/// A synchronous client for a Marbots server. Cheap to clone; safe to share between threads.
#[derive(Clone)]
pub struct Client {
    base: String,
    api_key: Option<String>,
    tenant: Option<String>,
    agent: Agent,
}

impl Client {
    /// A client for `base_url` (e.g. `http://localhost:5170`).
    pub fn new(base_url: impl Into<String>) -> Self {
        let agent: Agent = Agent::config_builder()
            .http_status_as_error(false)
            .timeout_global(Some(Duration::from_secs(30 * 60)))
            .build()
            .into();
        Self {
            base: base_url.into().trim_end_matches('/').to_string(),
            api_key: None,
            tenant: None,
            agent,
        }
    }

    /// The credential: a tenant key (`mbk_…`), the platform key (Marbots:ApiKey) or an OIDC access token.
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Tenant to act in with the platform key or a token (or end the base URL in `/t/<tenant>`).
    pub fn with_tenant(mut self, tenant: impl Into<String>) -> Self {
        self.tenant = Some(tenant.into());
        self
    }

    /// Adds the credential and tenant headers. OIDC tokens (JWT) go in `Authorization`, keys in `X-Api-Key`.
    pub(crate) fn auth<B>(&self, mut req: ureq::RequestBuilder<B>) -> ureq::RequestBuilder<B> {
        if let Some(k) = &self.api_key {
            req = if k.matches('.').count() == 2 && !k.starts_with("mbk_") {
                req.header("Authorization", format!("Bearer {k}"))
            } else {
                req.header("X-Api-Key", k)
            };
        }
        if let Some(t) = &self.tenant {
            req = req.header("X-Marbots-Tenant", t);
        }
        req
    }

    pub fn base_url(&self) -> &str {
        &self.base
    }

    fn finish(resp: ureq::http::Response<ureq::Body>) -> Result<Vec<u8>> {
        let status = resp.status().as_u16();
        let bytes = resp.into_body().read_to_vec()?;
        if status >= 300 {
            let text = String::from_utf8_lossy(&bytes).to_string();
            let message = serde_json::from_str::<Value>(&text)
                .ok()
                .and_then(|v| {
                    v.get("detail")
                        .or_else(|| v.get("title"))
                        .and_then(Value::as_str)
                        .map(str::to_string)
                })
                .unwrap_or(text);
            return Err(Error::Api { status, message });
        }
        Ok(bytes)
    }

    fn decode<T: DeserializeOwned>(bytes: &[u8]) -> Result<T> {
        Ok(serde_json::from_slice(if bytes.is_empty() {
            b"null"
        } else {
            bytes
        })?)
    }

    pub(crate) fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        Self::decode(&self.get_raw(path)?)
    }

    pub(crate) fn get_raw(&self, path: &str) -> Result<Vec<u8>> {
        let mut req = self.agent.get(format!("{}{}", self.base, path));
        req = self.auth(req);
        Self::finish(req.call()?)
    }

    pub(crate) fn send<B: Serialize, T: DeserializeOwned>(
        &self,
        method: &str,
        path: &str,
        body: Option<&B>,
    ) -> Result<T> {
        let url = format!("{}{}", self.base, path);
        let resp = match method {
            "DELETE" => {
                let mut req = self.agent.delete(url);
                req = self.auth(req);
                req.call()?
            }
            _ => {
                let mut req = match method {
                    "PUT" => self.agent.put(url),
                    _ => self.agent.post(url),
                };
                req = self.auth(req);
                match body {
                    Some(b) => req.send_json(b)?,
                    None => req
                        .header("Content-Type", "application/json")
                        .send(&b""[..])?,
                }
            }
        };
        Self::decode(&Self::finish(resp)?)
    }

    pub(crate) fn post_bytes(
        &self,
        path: &str,
        body: &[u8],
        content_type: &str,
    ) -> Result<Vec<u8>> {
        let mut req = self
            .agent
            .post(format!("{}{}", self.base, path))
            .header("Content-Type", content_type);
        req = self.auth(req);
        Self::finish(req.send(body)?)
    }

    /// Server information.
    pub fn system(&self) -> Result<SystemInfo> {
        self.get("/api/v1/system")
    }

    /// Machines that run bots.
    pub fn hosts(&self) -> Result<Vec<HostInfo>> {
        self.get("/api/v1/hosts")
    }

    /// Creates a thread with `bot`, sends `text`, waits, and returns the reply text.
    pub fn chat(&self, bot: &str, text: &str) -> Result<String> {
        let thread = self.threads().create(bot, None)?;
        Ok(self
            .threads()
            .send(&thread.id, text, true)?
            .text()
            .to_string())
    }

    pub fn bots(&self) -> Bots<'_> {
        Bots(self)
    }
    pub fn templates(&self) -> Templates<'_> {
        Templates(self)
    }
    pub fn models(&self) -> Models<'_> {
        Models(self)
    }
    pub fn threads(&self) -> Threads<'_> {
        Threads(self)
    }
    pub fn tasks(&self) -> Tasks<'_> {
        Tasks(self)
    }
    pub fn approvals(&self) -> Approvals<'_> {
        Approvals(self)
    }
    pub fn skills(&self) -> Skills<'_> {
        Skills(self)
    }
    pub fn mcp(&self) -> Mcp<'_> {
        Mcp(self)
    }
    pub fn schedules(&self) -> Schedules<'_> {
        Schedules(self)
    }
    pub fn memory(&self) -> Memory<'_> {
        Memory(self)
    }
    /// Computers that run bots' tools.
    pub fn agent_hosts(&self) -> AgentHosts<'_> {
        AgentHosts(self)
    }
    /// Who am I, tenants (platform admins), this tenant's API keys and members (owners).
    pub fn tenancy(&self) -> Tenancy<'_> {
        Tenancy(self)
    }
    pub fn events(&self) -> Events<'_> {
        Events(self)
    }
}

const NONE: Option<&Value> = None;

/// Bot management.
pub struct Bots<'a>(&'a Client);

impl Bots<'_> {
    pub fn list(&self) -> Result<Vec<Bot>> {
        self.0.get("/api/v1/bots")
    }
    pub fn get(&self, id_or_name: &str) -> Result<Bot> {
        self.0.get(&format!("/api/v1/bots/{}", esc(id_or_name)))
    }
    pub fn create(&self, spec: BotSpec) -> Result<Bot> {
        self.0.send("POST", "/api/v1/bots", Some(&spec))
    }
    pub fn update(&self, id: &str, spec: BotSpec) -> Result<Bot> {
        self.0.send(
            "PUT",
            &format!("/api/v1/bots/{}", esc(id)),
            Some(&spec.with_id(id)),
        )
    }
    pub fn delete(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("DELETE", &format!("/api/v1/bots/{}", esc(id)), NONE)
            .map(|_| ())
    }
    /// Creates a bot from a gallery template.
    pub fn hire(&self, template_id: &str, name: Option<&str>) -> Result<Bot> {
        self.0.send(
            "POST",
            &format!("/api/v1/bots/from-template/{}", esc(template_id)),
            Some(&json!({ "name": name })),
        )
    }
    pub fn get_model(&self, id: &str) -> Result<BotModelInfo> {
        self.0.get(&format!("/api/v1/bots/{}/model", esc(id)))
    }
    /// Sets the bot's model: [`ModelRef::DEFAULT`], [`ModelRef::of`] or a profile name.
    pub fn set_model(&self, id: &str, model: &str) -> Result<BotModelInfo> {
        self.0.send(
            "PUT",
            &format!("/api/v1/bots/{}/model", esc(id)),
            Some(&json!({ "model": model })),
        )
    }
    pub fn pause(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("POST", &format!("/api/v1/bots/{}/pause", esc(id)), NONE)
            .map(|_| ())
    }
    pub fn resume(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("POST", &format!("/api/v1/bots/{}/resume", esc(id)), NONE)
            .map(|_| ())
    }
    /// Downloads a .marbot package (secrets are never included).
    pub fn export(&self, id: &str, include_memory: bool) -> Result<Vec<u8>> {
        self.0.get_raw(&format!(
            "/api/v1/bots/{}/export?includeMemory={include_memory}",
            esc(id)
        ))
    }
    pub fn import_package(&self, package: &[u8]) -> Result<Bot> {
        Client::decode(
            &self
                .0
                .post_bytes("/api/v1/bots/import", package, "application/zip")?,
        )
    }
}

/// Template gallery.
pub struct Templates<'a>(&'a Client);

impl Templates<'_> {
    pub fn list(&self, query: &str, category: &str) -> Result<Vec<BotTemplate>> {
        self.0.get(&format!(
            "/api/v1/templates?q={}&category={}",
            esc(query),
            esc(category)
        ))
    }
    pub fn get(&self, id: &str) -> Result<BotTemplate> {
        self.0.get(&format!("/api/v1/templates/{}", esc(id)))
    }
}

/// Workspace default model and model choices.
pub struct Models<'a>(&'a Client);

impl Models<'_> {
    pub fn list(&self) -> Result<ModelCatalog> {
        self.0.get("/api/v1/models")
    }
    /// Changes the model used by every bot whose model is [`ModelRef::DEFAULT`]; returns the new default.
    pub fn set_default(&self, model: &str) -> Result<String> {
        let v: Value = self.0.send(
            "PUT",
            "/api/v1/models/default",
            Some(&json!({ "model": model })),
        )?;
        Ok(v.get("default")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string())
    }
}

/// Conversations.
pub struct Threads<'a>(&'a Client);

impl Threads<'_> {
    pub fn list(&self, bot_id: Option<&str>) -> Result<Vec<ChatThread>> {
        match bot_id {
            Some(b) => self.0.get(&format!("/api/v1/threads?botId={}", esc(b))),
            None => self.0.get("/api/v1/threads"),
        }
    }
    pub fn create(&self, bot_id: &str, title: Option<&str>) -> Result<ChatThread> {
        self.0.send(
            "POST",
            "/api/v1/threads",
            Some(&json!({ "botId": bot_id, "title": title })),
        )
    }
    /// Sends a message; with `wait` the call returns after the bot finished (10 minute limit).
    pub fn send(&self, thread_id: &str, text: &str, wait: bool) -> Result<SendResult> {
        self.send_with_timeout(thread_id, text, wait, 600)
    }
    pub fn send_with_timeout(
        &self,
        thread_id: &str,
        text: &str,
        wait: bool,
        timeout_seconds: u32,
    ) -> Result<SendResult> {
        let body = json!({ "text": text, "wait": wait, "timeoutSeconds": timeout_seconds });
        self.0.send(
            "POST",
            &format!("/api/v1/threads/{}/messages", esc(thread_id)),
            Some(&body),
        )
    }
    pub fn messages(&self, thread_id: &str) -> Result<Vec<ChatMessage>> {
        self.0
            .get(&format!("/api/v1/threads/{}/messages", esc(thread_id)))
    }
    pub fn files(&self, thread_id: &str) -> Result<Vec<WorkspaceFile>> {
        self.0
            .get(&format!("/api/v1/threads/{}/files", esc(thread_id)))
    }
    pub fn download(&self, thread_id: &str, path: &str) -> Result<Vec<u8>> {
        self.0.get_raw(&format!(
            "/api/v1/threads/{}/files/{}",
            esc(thread_id),
            path
        ))
    }
    pub fn delete(&self, thread_id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>(
                "DELETE",
                &format!("/api/v1/threads/{}", esc(thread_id)),
                NONE,
            )
            .map(|_| ())
    }
}

/// Tasks.
pub struct Tasks<'a>(&'a Client);

impl Tasks<'_> {
    pub fn list(&self, thread_id: Option<&str>) -> Result<Vec<TaskRecord>> {
        match thread_id {
            Some(t) => self.0.get(&format!("/api/v1/tasks?threadId={}", esc(t))),
            None => self.0.get("/api/v1/tasks"),
        }
    }
    pub fn get(&self, id: &str) -> Result<TaskRecord> {
        self.0.get(&format!("/api/v1/tasks/{}", esc(id)))
    }
    pub fn cancel(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("POST", &format!("/api/v1/tasks/{}/cancel", esc(id)), NONE)
            .map(|_| ())
    }
}

/// Human-in-the-loop approvals.
pub struct Approvals<'a>(&'a Client);

impl Approvals<'_> {
    pub fn pending(&self) -> Result<Vec<ApprovalRequest>> {
        self.0.get("/api/v1/approvals?state=pending")
    }
    pub fn approve(&self, id: &str, scope: ApprovalScope) -> Result<ApprovalRequest> {
        self.0.send(
            "POST",
            &format!("/api/v1/approvals/{}/approve", esc(id)),
            Some(&json!({ "scope": scope })),
        )
    }
    pub fn reject(&self, id: &str) -> Result<ApprovalRequest> {
        self.0.send::<Value, _>(
            "POST",
            &format!("/api/v1/approvals/{}/reject", esc(id)),
            NONE,
        )
    }
    /// True when approvals are skipped (dangerous mode).
    pub fn skip_approvals(&self) -> Result<bool> {
        let v: Value = self.0.get("/api/v1/system/approvals")?;
        Ok(v.get("dangerouslySkipApprovals")
            .and_then(Value::as_bool)
            .unwrap_or(false))
    }
    /// Dangerous, like `--dangerously-skip-permissions`: every action that would ask runs without a human.
    /// Turning it on also approves everything pending. Actions a bot's profile denies stay denied.
    pub fn set_skip_approvals(&self, skip: bool) -> Result<bool> {
        let v: Value = self.0.send(
            "PUT",
            "/api/v1/system/approvals",
            Some(&json!({ "dangerouslySkipApprovals": skip })),
        )?;
        Ok(v.get("dangerouslySkipApprovals")
            .and_then(Value::as_bool)
            .unwrap_or(false))
    }
}

/// SKILL.md packages.
pub struct Skills<'a>(&'a Client);

impl Skills<'_> {
    pub fn list(&self) -> Result<Vec<SkillInfo>> {
        self.0.get("/api/v1/skills")
    }
    pub fn install(&self, source: &str) -> Result<Vec<SkillInfo>> {
        self.0.send(
            "POST",
            "/api/v1/skills/install",
            Some(&json!({ "source": source })),
        )
    }
    /// Learning evaluation: outcomes per skill version and a verdict.
    pub fn evaluations(&self) -> Result<Vec<SkillEvaluation>> {
        self.0.get("/api/v1/skills/evaluations")
    }
    /// Restores the previous version of an installed skill; returns the restored version.
    pub fn rollback(&self, name: &str) -> Result<String> {
        let v: Value = self.0.send(
            "POST",
            &format!("/api/v1/skills/{}/rollback", esc(name)),
            NONE,
        )?;
        Ok(v.get("version")
            .and_then(Value::as_str)
            .unwrap_or_default()
            .to_string())
    }
    /// Publishes a skill drafted by auto-learn.
    pub fn promote(&self, name: &str) -> Result<()> {
        self.0
            .send::<Value, Value>(
                "POST",
                &format!("/api/v1/skills/{}/approve", esc(name)),
                NONE,
            )
            .map(|_| ())
    }
    pub fn discard(&self, name: &str) -> Result<()> {
        self.0
            .send::<Value, Value>(
                "POST",
                &format!("/api/v1/skills/{}/reject", esc(name)),
                NONE,
            )
            .map(|_| ())
    }
    pub fn auto_rollback(&self) -> Result<bool> {
        let v: Value = self.0.get("/api/v1/system/learning")?;
        Ok(v.get("autoRollbackSkills")
            .and_then(Value::as_bool)
            .unwrap_or(false))
    }
    pub fn set_auto_rollback(&self, on: bool) -> Result<bool> {
        let v: Value = self.0.send(
            "PUT",
            "/api/v1/system/learning",
            Some(&json!({ "autoRollbackSkills": on })),
        )?;
        Ok(v.get("autoRollbackSkills")
            .and_then(Value::as_bool)
            .unwrap_or(false))
    }
}

/// Computers that run bots' tools (see docs/en/computers.md).
pub struct AgentHosts<'a>(&'a Client);

impl AgentHosts<'_> {
    pub fn list(&self) -> Result<Vec<HostInfo>> {
        self.0.get("/api/v1/hosts")
    }
    /// One-time token for `marbots-host enroll`.
    pub fn create_enrollment(&self, name: &str, valid_minutes: u32) -> Result<EnrollmentToken> {
        self.0.send(
            "POST",
            "/api/v1/hosts/enrollments",
            Some(&json!({ "name": name, "validMinutes": valid_minutes })),
        )
    }
    /// Installs marbots-host over SSH; the password/key is used for this call only.
    pub fn bootstrap(&self, options: &BootstrapOptions) -> Result<BootstrapResult> {
        self.0
            .send("POST", "/api/v1/hosts/bootstrap", Some(options))
    }
    pub fn disable(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("POST", &format!("/api/v1/hosts/{}/disable", esc(id)), NONE)
            .map(|_| ())
    }
    pub fn enable(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("POST", &format!("/api/v1/hosts/{}/enable", esc(id)), NONE)
            .map(|_| ())
    }
    pub fn remove(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("DELETE", &format!("/api/v1/hosts/{}", esc(id)), NONE)
            .map(|_| ())
    }
}

/// Who am I, tenants and this tenant's keys and members (see docs/en/multi-tenant.md).
pub struct Tenancy<'a>(&'a Client);

impl Tenancy<'_> {
    pub fn whoami(&self) -> Result<WhoAmI> {
        self.0.get("/api/v1/whoami")
    }
    pub fn list_tenants(&self) -> Result<Vec<Tenant>> {
        self.0.get("/api/v1/tenants")
    }
    pub fn create_tenant(&self, id: &str, name: Option<&str>) -> Result<Tenant> {
        self.0.send(
            "POST",
            "/api/v1/tenants",
            Some(&json!({ "id": id, "name": name })),
        )
    }
    pub fn disable_tenant(&self, id: &str) -> Result<Tenant> {
        self.0.send::<Value, _>(
            "POST",
            &format!("/api/v1/tenants/{}/disable", esc(id)),
            NONE,
        )
    }
    pub fn enable_tenant(&self, id: &str) -> Result<Tenant> {
        self.0
            .send::<Value, _>("POST", &format!("/api/v1/tenants/{}/enable", esc(id)), NONE)
    }
    /// A key in any tenant (platform admins); the plaintext key is returned once.
    pub fn create_tenant_key(
        &self,
        tenant: &str,
        name: &str,
        role: TenantRole,
    ) -> Result<NewApiKey> {
        self.0.send(
            "POST",
            &format!("/api/v1/tenants/{}/keys", esc(tenant)),
            Some(&json!({ "name": name, "role": role })),
        )
    }
    pub fn list_keys(&self) -> Result<Vec<ApiKeyInfo>> {
        self.0.get("/api/v1/tenant/keys")
    }
    pub fn create_key(&self, name: &str, role: TenantRole) -> Result<NewApiKey> {
        self.0.send(
            "POST",
            "/api/v1/tenant/keys",
            Some(&json!({ "name": name, "role": role })),
        )
    }
    pub fn revoke_key(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("DELETE", &format!("/api/v1/tenant/keys/{}", esc(id)), NONE)
            .map(|_| ())
    }
    pub fn list_members(&self) -> Result<Vec<TenantMember>> {
        self.0.get("/api/v1/tenant/members")
    }
    pub fn set_member(&self, subject: &str, role: TenantRole) -> Result<TenantMember> {
        self.0.send(
            "PUT",
            "/api/v1/tenant/members",
            Some(&json!({ "subject": subject, "role": role })),
        )
    }
    pub fn remove_member(&self, subject: &str) -> Result<()> {
        self.0
            .send::<Value, Value>(
                "DELETE",
                &format!("/api/v1/tenant/members/{}", esc(subject)),
                NONE,
            )
            .map(|_| ())
    }
}

/// MCP servers.
pub struct Mcp<'a>(&'a Client);

impl Mcp<'_> {
    pub fn list(&self) -> Result<Vec<McpServer>> {
        self.0.get("/api/v1/mcp")
    }
    pub fn install(&self, id: &str) -> Result<McpServer> {
        self.0
            .send::<Value, _>("POST", &format!("/api/v1/mcp/{}/install", esc(id)), NONE)
    }
}

/// Cron and one-off jobs.
pub struct Schedules<'a>(&'a Client);

impl Schedules<'_> {
    pub fn list(&self) -> Result<Vec<ScheduleJob>> {
        self.0.get("/api/v1/schedules")
    }
    pub fn create(&self, spec: &ScheduleSpec) -> Result<ScheduleJob> {
        self.0.send("POST", "/api/v1/schedules", Some(spec))
    }
    pub fn delete(&self, id: &str) -> Result<()> {
        self.0
            .send::<Value, Value>("DELETE", &format!("/api/v1/schedules/{}", esc(id)), NONE)
            .map(|_| ())
    }
}

/// Long-term memory.
pub struct Memory<'a>(&'a Client);

impl Memory<'_> {
    pub fn list(&self, owner: &str) -> Result<Vec<MemoryRecord>> {
        self.0.get(&format!("/api/v1/memory/{}", esc(owner)))
    }
    pub fn remember(&self, owner: &str, content: &str, kind: MemoryKind) -> Result<MemoryRecord> {
        let body = json!({ "owner": owner, "content": content, "kind": kind, "source": "sdk:rust", "confidence": 1.0 });
        self.0.send("POST", "/api/v1/memory", Some(&body))
    }
}

/// Live events (Server-Sent Events).
pub struct Events<'a>(&'a Client);

impl Events<'_> {
    /// Opens the event stream (one thread, or everything when `thread_id` is `None`). Iterate it on a background
    /// thread if you need to keep working; dropping the iterator closes the connection.
    pub fn stream(&self, thread_id: Option<&str>) -> Result<EventStream> {
        let path = match thread_id {
            Some(t) => format!("/api/v1/threads/{}/events", esc(t)),
            None => "/api/v1/events".to_string(),
        };
        let agent: Agent = Agent::config_builder()
            .http_status_as_error(false)
            .build()
            .into();
        let mut req = agent
            .get(format!("{}{}", self.0.base, path))
            .header("Accept", "text/event-stream");
        req = self.0.auth(req);
        let resp = req.call()?;
        if resp.status().as_u16() >= 300 {
            return Err(Error::Api {
                status: resp.status().as_u16(),
                message: "event stream unavailable".into(),
            });
        }
        Ok(EventStream {
            lines: Box::new(BufReader::new(resp.into_body().into_reader())),
        })
    }
}

/// Iterator over live events.
pub struct EventStream {
    lines: Box<dyn BufRead + Send>,
}

impl Iterator for EventStream {
    type Item = Result<AgentEvent>;

    fn next(&mut self) -> Option<Self::Item> {
        let mut line = String::new();
        loop {
            line.clear();
            match self.lines.read_line(&mut line) {
                Ok(0) => return None,
                Ok(_) => {
                    if let Some(data) = line.trim_end().strip_prefix("data: ") {
                        return Some(serde_json::from_str(data).map_err(Error::from));
                    }
                }
                Err(e) => return Some(Err(Error::Transport(e.to_string()))),
            }
        }
    }
}