agentoven-core 0.6.0

Core SDK for AgentOven — the enterprise agent control plane
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
//! AgentOven Client — connects to the AgentOven control plane.
//!
//! Provides a high-level API for agent registration, deployment, and management.

use reqwest::Client;
use url::Url;

use crate::agent::{Agent, AgentMode};
use crate::recipe::Recipe;

/// Client for interacting with the AgentOven control plane.
#[derive(Debug, Clone)]
pub struct AgentOvenClient {
    /// Control plane base URL.
    base_url: Url,

    /// HTTP client.
    http: Client,

    /// API key for authentication.
    api_key: Option<String>,

    /// Current kitchen (workspace) context.
    kitchen_id: Option<String>,
}

impl AgentOvenClient {
    /// Create a new client connected to the control plane.
    pub fn new(base_url: &str) -> anyhow::Result<Self> {
        Ok(Self {
            base_url: Url::parse(base_url)?,
            http: Client::new(),
            api_key: None,
            kitchen_id: None,
        })
    }

    /// Create from config file + environment variables.
    ///
    /// Priority: CLI flags > env vars > `~/.agentoven/config.toml` > defaults.
    pub fn from_env() -> anyhow::Result<Self> {
        let cfg = crate::config::AgentOvenConfig::load();
        Self::from_config(&cfg)
    }

    /// Create from an explicit config struct (no env-var lookup).
    pub fn from_config(cfg: &crate::config::AgentOvenConfig) -> anyhow::Result<Self> {
        let mut client = Self::new(&cfg.url)?;
        client.api_key = cfg.auth_credential().map(|s| s.to_string());
        client.kitchen_id = cfg.kitchen.clone();
        Ok(client)
    }

    /// Set API key.
    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Set the active kitchen (workspace).
    pub fn with_kitchen(mut self, kitchen_id: impl Into<String>) -> Self {
        self.kitchen_id = Some(kitchen_id.into());
        self
    }

    // ── Agent Operations ─────────────────────────────────────

    /// Register a new agent in the oven (menu).
    ///
    /// Sends only the user-provided fields; server-managed fields
    /// (id, status, kitchen, timestamps, a2a_endpoint) are set server-side.
    pub async fn register(&self, agent: &Agent) -> anyhow::Result<Agent> {
        let url = self.url("/api/v1/agents");

        // Build a minimal registration payload — only fields the server expects.
        let mut body = serde_json::json!({
            "name": agent.name,
            "description": agent.description,
            "framework": agent.framework,
        });
        let obj = body.as_object_mut().unwrap();

        if !agent.version.is_empty() {
            obj.insert("version".into(), agent.version.clone().into());
        }
        if !agent.model_provider.is_empty() {
            obj.insert("model_provider".into(), agent.model_provider.clone().into());
        }
        if !agent.model_name.is_empty() {
            obj.insert("model_name".into(), agent.model_name.clone().into());
        }
        if let Some(ref bp) = agent.backup_provider {
            obj.insert("backup_provider".into(), bp.clone().into());
        }
        if let Some(ref bm) = agent.backup_model {
            obj.insert("backup_model".into(), bm.clone().into());
        }
        if let Some(ref sp) = agent.system_prompt {
            obj.insert("system_prompt".into(), sp.clone().into());
        }
        if let Some(mt) = agent.max_turns {
            obj.insert("max_turns".into(), mt.into());
        }
        if !agent.skills.is_empty() {
            obj.insert("skills".into(), serde_json::to_value(&agent.skills)?);
        }
        if !agent.ingredients.is_empty() {
            obj.insert(
                "ingredients".into(),
                serde_json::to_value(&agent.ingredients)?,
            );
        }
        if !agent.tags.is_empty() {
            obj.insert("tags".into(), serde_json::to_value(&agent.tags)?);
        }
        if !agent.guardrails.is_empty() {
            obj.insert(
                "guardrails".into(),
                serde_json::to_value(&agent.guardrails)?,
            );
        }
        if agent.mode != AgentMode::default() {
            obj.insert("mode".into(), serde_json::to_value(&agent.mode)?);
        }

        let resp = self
            .authed_request(self.http.post(url))
            .json(&body)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get an agent by name and optional version.
    pub async fn get_agent(&self, name: &str, version: Option<&str>) -> anyhow::Result<Agent> {
        let path = match version {
            Some(v) => format!("/api/v1/agents/{name}/versions/{v}"),
            None => format!("/api/v1/agents/{name}"),
        };
        let url = self.url(&path);
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// List all agents in the current kitchen.
    pub async fn list_agents(&self) -> anyhow::Result<Vec<Agent>> {
        let url = self.url("/api/v1/agents");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Bake (deploy) an agent to an environment.
    pub async fn bake(&self, agent: &Agent, environment: &str) -> anyhow::Result<Agent> {
        let url = self.url(&format!("/api/v1/agents/{}/bake", agent.name));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&serde_json::json!({
                "version": agent.version,
                "environment": environment,
            }))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Rewarm a cooled agent (transition back to ready).
    pub async fn rewarm(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/rewarm"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Recipe Operations ────────────────────────────────────

    /// Create a new recipe (workflow).
    pub async fn create_recipe(&self, recipe: &Recipe) -> anyhow::Result<Recipe> {
        let url = self.url("/api/v1/recipes");
        let resp = self
            .authed_request(self.http.post(url))
            .json(recipe)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Bake (execute) a recipe with input.
    pub async fn bake_recipe(
        &self,
        recipe_name: &str,
        input: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/recipes/{recipe_name}/bake"));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&input)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Agent Lifecycle ──────────────────────────────────────

    /// Update an existing agent.
    pub async fn update_agent(
        &self,
        name: &str,
        updates: serde_json::Value,
    ) -> anyhow::Result<Agent> {
        let url = self.url(&format!("/api/v1/agents/{name}"));
        let resp = self
            .authed_request(self.http.put(url))
            .json(&updates)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete an agent.
    pub async fn delete_agent(&self, name: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/agents/{name}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// Re-cook an agent with edits.
    pub async fn recook_agent(
        &self,
        name: &str,
        edits: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/recook"));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&edits)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Cool (pause) an agent.
    pub async fn cool_agent(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/cool"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Retire an agent permanently.
    pub async fn retire_agent(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/retire"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Test an agent (one-shot, via /test endpoint).
    pub async fn test_agent(
        &self,
        name: &str,
        message: &str,
        thinking: bool,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/test"));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&serde_json::json!({
                "message": message,
                "thinking_enabled": thinking,
            }))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Invoke a managed agent (full agentic loop with execution trace).
    pub async fn invoke_agent(
        &self,
        name: &str,
        message: &str,
        variables: Option<serde_json::Value>,
        thinking: bool,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/invoke"));
        let mut body = serde_json::json!({
            "message": message,
            "thinking_enabled": thinking,
        });
        if let Some(vars) = variables {
            body["variables"] = vars;
        }
        let resp = self
            .authed_request(self.http.post(url))
            .json(&body)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get the resolved configuration for a baked agent.
    pub async fn agent_config(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/config"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get the A2A Agent Card for an agent.
    pub async fn agent_card(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{name}/card"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// List version history for an agent.
    pub async fn agent_versions(&self, name: &str) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url(&format!("/api/v1/agents/{name}/versions"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Provider Operations ──────────────────────────────────

    /// List all model providers.
    pub async fn list_providers(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/models/providers");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Add a new model provider.
    pub async fn add_provider(
        &self,
        provider: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/models/providers");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&provider)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific model provider.
    pub async fn get_provider(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/models/providers/{name}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Update a model provider.
    pub async fn update_provider(
        &self,
        name: &str,
        provider: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/models/providers/{name}"));
        let resp = self
            .authed_request(self.http.put(url))
            .json(&provider)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a model provider.
    pub async fn delete_provider(&self, name: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/models/providers/{name}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// Test a provider's connectivity and credentials.
    pub async fn test_provider(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/models/providers/{name}/test"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Discover models available from a provider.
    pub async fn discover_provider(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/models/providers/{name}/discover"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Tool Operations ──────────────────────────────────────

    /// List all MCP tools.
    pub async fn list_tools(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/tools");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Add a new MCP tool.
    pub async fn add_tool(&self, tool: serde_json::Value) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/tools");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&tool)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Bulk-add multiple MCP tools in one request.
    pub async fn bulk_add_tools(
        &self,
        payload: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/tools/bulk");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&payload)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific tool.
    pub async fn get_tool(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/tools/{name}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Update a tool.
    pub async fn update_tool(
        &self,
        name: &str,
        tool: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/tools/{name}"));
        let resp = self
            .authed_request(self.http.put(url))
            .json(&tool)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a tool.
    pub async fn delete_tool(&self, name: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/tools/{name}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    // ── Prompt Operations ────────────────────────────────────

    /// List all prompt templates.
    pub async fn list_prompts(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/prompts");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Add a new prompt template.
    pub async fn add_prompt(&self, prompt: serde_json::Value) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/prompts");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&prompt)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific prompt template.
    pub async fn get_prompt(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/prompts/{name}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Update a prompt template.
    pub async fn update_prompt(
        &self,
        name: &str,
        prompt: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/prompts/{name}"));
        let resp = self
            .authed_request(self.http.put(url))
            .json(&prompt)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a prompt template.
    pub async fn delete_prompt(&self, name: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/prompts/{name}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// Validate a prompt template.
    pub async fn validate_prompt(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/prompts/{name}/validate"));
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// List version history for a prompt.
    pub async fn prompt_versions(&self, name: &str) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url(&format!("/api/v1/prompts/{name}/versions"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Kitchen Operations ───────────────────────────────────

    /// List all kitchens.
    pub async fn list_kitchens(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/kitchens");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific kitchen.
    pub async fn get_kitchen(&self, id: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/kitchens/{id}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Create a new kitchen.
    pub async fn create_kitchen(
        &self,
        kitchen: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/kitchens");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&kitchen)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a kitchen.
    pub async fn delete_kitchen(&self, id: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/kitchens/{id}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// Get server info (edition, features, limits).
    /// The CLI calls this to discover what commands are available.
    pub async fn server_info(&self) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/info");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get kitchen settings.
    pub async fn get_settings(&self) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/settings");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Update kitchen settings.
    pub async fn update_settings(
        &self,
        settings: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/settings");
        let resp = self
            .authed_request(self.http.put(url))
            .json(&settings)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Session Operations ───────────────────────────────────

    /// List sessions for an agent.
    pub async fn list_sessions(&self, agent_name: &str) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url(&format!("/api/v1/agents/{agent_name}/sessions"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Create a new session for an agent.
    pub async fn create_session(&self, agent_name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/agents/{agent_name}/sessions"));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&serde_json::json!({}))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific session.
    pub async fn get_session(
        &self,
        agent_name: &str,
        session_id: &str,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!(
            "/api/v1/agents/{agent_name}/sessions/{session_id}"
        ));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a session.
    pub async fn delete_session(&self, agent_name: &str, session_id: &str) -> anyhow::Result<()> {
        let url = self.url(&format!(
            "/api/v1/agents/{agent_name}/sessions/{session_id}"
        ));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// Send a message within a session.
    pub async fn send_session_message(
        &self,
        agent_name: &str,
        session_id: &str,
        message: &str,
        thinking: bool,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!(
            "/api/v1/agents/{agent_name}/sessions/{session_id}/messages"
        ));
        let resp = self
            .authed_request(self.http.post(url))
            .json(&serde_json::json!({
                "message": message,
                "thinking_enabled": thinking,
            }))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── RAG Operations ───────────────────────────────────────

    /// Query the RAG pipeline.
    pub async fn rag_query(&self, query: serde_json::Value) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/rag/query");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&query)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Ingest documents into the RAG pipeline.
    pub async fn rag_ingest(
        &self,
        request: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/rag/ingest");
        let resp = self
            .authed_request(self.http.post(url))
            .json(&request)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Trace Operations ─────────────────────────────────────

    /// List recent traces.
    pub async fn list_traces(
        &self,
        agent: Option<&str>,
        limit: u32,
    ) -> anyhow::Result<Vec<serde_json::Value>> {
        let mut url = self.url("/api/v1/traces");
        {
            let mut query = url.query_pairs_mut();
            query.append_pair("limit", &limit.to_string());
            if let Some(a) = agent {
                query.append_pair("agent", a);
            }
        }
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get a specific trace.
    pub async fn get_trace(&self, trace_id: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/traces/{trace_id}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Model Catalog Operations ─────────────────────────────

    /// List model catalog.
    pub async fn model_catalog(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/models/catalog");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Refresh model catalog from providers.
    pub async fn catalog_refresh(&self) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/models/catalog/refresh");
        let resp = self
            .authed_request(self.http.post(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Get cost summary.
    pub async fn model_cost(&self) -> anyhow::Result<serde_json::Value> {
        let url = self.url("/api/v1/models/cost");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Recipe Extended Operations ───────────────────────────

    /// Get a specific recipe.
    pub async fn get_recipe(&self, name: &str) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!("/api/v1/recipes/{name}"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// List all recipes.
    pub async fn list_recipes(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/recipes");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Delete a recipe.
    pub async fn delete_recipe(&self, name: &str) -> anyhow::Result<()> {
        let url = self.url(&format!("/api/v1/recipes/{name}"));
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    /// List runs for a recipe.
    pub async fn recipe_runs(&self, name: &str) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url(&format!("/api/v1/recipes/{name}/runs"));
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Approve a human gate in a recipe run.
    pub async fn approve_gate(
        &self,
        recipe: &str,
        run_id: &str,
        step_name: &str,
        approved: bool,
        comment: Option<&str>,
    ) -> anyhow::Result<serde_json::Value> {
        let url = self.url(&format!(
            "/api/v1/recipes/{recipe}/runs/{run_id}/gates/{step_name}/approve"
        ));
        let mut body = serde_json::json!({ "approved": approved });
        if let Some(c) = comment {
            body["comment"] = serde_json::json!(c);
        }
        let resp = self
            .authed_request(self.http.post(url))
            .json(&body)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Audit Operations ─────────────────────────────────────

    /// List audit events.
    pub async fn list_audit(&self, limit: u32) -> anyhow::Result<Vec<serde_json::Value>> {
        let mut url = self.url("/api/v1/audit");
        url.query_pairs_mut()
            .append_pair("limit", &limit.to_string());
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Guardrail Operations ─────────────────────────────────

    /// List available guardrail kinds.
    pub async fn guardrail_kinds(&self) -> anyhow::Result<Vec<serde_json::Value>> {
        let url = self.url("/api/v1/guardrails/kinds");
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    // ── Generic HTTP helpers (for Pro-gated commands) ──────

    /// Generic GET that returns deserialized JSON.
    pub async fn raw_get<T: serde::de::DeserializeOwned>(&self, path: &str) -> anyhow::Result<T> {
        let url = self.url(path);
        let resp = self
            .authed_request(self.http.get(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Generic POST that sends JSON and returns deserialized JSON.
    pub async fn raw_post<T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
        body: &serde_json::Value,
    ) -> anyhow::Result<T> {
        let url = self.url(path);
        let resp = self
            .authed_request(self.http.post(url))
            .json(body)
            .send()
            .await?
            .error_for_status()?;
        Ok(resp.json().await?)
    }

    /// Generic DELETE.
    pub async fn raw_delete(&self, path: &str) -> anyhow::Result<()> {
        let url = self.url(path);
        self.authed_request(self.http.delete(url))
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }

    // ── Internal ─────────────────────────────────────────────

    fn url(&self, path: &str) -> Url {
        self.base_url.join(path).expect("Invalid URL path")
    }

    fn authed_request(&self, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        let mut b = builder;
        if let Some(ref key) = self.api_key {
            b = b.bearer_auth(key);
        }
        if let Some(ref kitchen) = self.kitchen_id {
            b = b.header("X-Kitchen-Id", kitchen.as_str());
        }
        b
    }
}