cognis 0.2.1

LLM application framework built on cognis-core
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
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
use std::collections::HashSet;
use std::sync::Arc;

use async_trait::async_trait;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use cognis_core::error::{CognisError, Result};
use cognis_core::language_models::chat_model::BaseChatModel;
use cognis_core::messages::{HumanMessage, Message};
use cognis_core::runnables::base::Runnable;
use cognis_core::runnables::config::RunnableConfig;

// ---------------------------------------------------------------------------
// EndpointSpec & APISpec
// ---------------------------------------------------------------------------

/// Describes a single parameter for an API endpoint.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ParameterSpec {
    /// Parameter name.
    pub name: String,
    /// Parameter type (e.g. `"string"`, `"integer"`, `"boolean"`).
    pub param_type: String,
    /// Whether the parameter is required.
    pub required: bool,
    /// Human-readable description.
    pub description: String,
}

/// Builder for [`ParameterSpec`].
pub struct ParameterSpecBuilder {
    name: String,
    param_type: String,
    required: bool,
    description: String,
}

impl ParameterSpecBuilder {
    /// Create a builder with the required parameter name and type.
    pub fn new(name: impl Into<String>, param_type: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            required: false,
            description: String::new(),
        }
    }

    /// Mark this parameter as required.
    pub fn required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }

    /// Set the parameter description.
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Build the [`ParameterSpec`].
    pub fn build(self) -> ParameterSpec {
        ParameterSpec {
            name: self.name,
            param_type: self.param_type,
            required: self.required,
            description: self.description,
        }
    }
}

/// Describes a single API endpoint.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EndpointSpec {
    /// HTTP method (e.g. `"GET"`, `"POST"`).
    pub method: String,
    /// URL path (e.g. `"/users/{id}"`).
    pub path: String,
    /// Human-readable description of what this endpoint does.
    pub description: String,
    /// Parameters for this endpoint.
    pub parameters: Vec<ParameterSpec>,
}

/// Builder for [`EndpointSpec`].
pub struct EndpointSpecBuilder {
    method: String,
    path: String,
    description: String,
    parameters: Vec<ParameterSpec>,
}

impl EndpointSpecBuilder {
    /// Create a builder with the required method and path.
    pub fn new(method: impl Into<String>, path: impl Into<String>) -> Self {
        Self {
            method: method.into(),
            path: path.into(),
            description: String::new(),
            parameters: Vec::new(),
        }
    }

    /// Set the endpoint description.
    pub fn description(mut self, desc: impl Into<String>) -> Self {
        self.description = desc.into();
        self
    }

    /// Add a parameter to this endpoint.
    pub fn parameter(mut self, param: ParameterSpec) -> Self {
        self.parameters.push(param);
        self
    }

    /// Build the [`EndpointSpec`].
    pub fn build(self) -> EndpointSpec {
        EndpointSpec {
            method: self.method,
            path: self.path,
            description: self.description,
            parameters: self.parameters,
        }
    }
}

/// Describes an API with a base URL and a collection of endpoints.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct APISpec {
    /// Base URL for the API (e.g. `"https://api.example.com"`).
    pub base_url: String,
    /// Available endpoints.
    pub endpoints: Vec<EndpointSpec>,
}

/// Builder for [`APISpec`].
pub struct APISpecBuilder {
    base_url: String,
    endpoints: Vec<EndpointSpec>,
}

impl APISpecBuilder {
    /// Create a builder with the required base URL.
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            endpoints: Vec::new(),
        }
    }

    /// Add an endpoint to the API spec.
    pub fn endpoint(mut self, endpoint: EndpointSpec) -> Self {
        self.endpoints.push(endpoint);
        self
    }

    /// Build the [`APISpec`].
    pub fn build(self) -> APISpec {
        APISpec {
            base_url: self.base_url,
            endpoints: self.endpoints,
        }
    }
}

impl APISpec {
    /// Start a new builder.
    pub fn builder(base_url: impl Into<String>) -> APISpecBuilder {
        APISpecBuilder::new(base_url)
    }

    /// Generate a human-readable description of the API for inclusion in LLM prompts.
    pub fn to_description(&self) -> String {
        let mut desc = format!("API Base URL: {}\n\nEndpoints:\n", self.base_url);
        for endpoint in &self.endpoints {
            desc.push_str(&format!(
                "\n  {} {} - {}\n",
                endpoint.method, endpoint.path, endpoint.description
            ));
            if !endpoint.parameters.is_empty() {
                desc.push_str("    Parameters:\n");
                for param in &endpoint.parameters {
                    let req = if param.required {
                        "required"
                    } else {
                        "optional"
                    };
                    desc.push_str(&format!(
                        "      - {} ({}, {}): {}\n",
                        param.name, param.param_type, req, param.description
                    ));
                }
            }
        }
        desc
    }

    /// Parse an [`APISpec`] from a simplified JSON format.
    ///
    /// Expected format:
    /// ```json
    /// {
    ///   "base_url": "https://api.example.com",
    ///   "endpoints": [
    ///     {
    ///       "method": "GET",
    ///       "path": "/users",
    ///       "description": "List all users",
    ///       "parameters": [
    ///         {
    ///           "name": "limit",
    ///           "type": "integer",
    ///           "required": false,
    ///           "description": "Max results"
    ///         }
    ///       ]
    ///     }
    ///   ]
    /// }
    /// ```
    pub fn from_json(value: &Value) -> Result<Self> {
        let obj = value.as_object().ok_or_else(|| CognisError::TypeMismatch {
            expected: "JSON object".into(),
            got: format!("{}", value),
        })?;

        let base_url = obj
            .get("base_url")
            .and_then(|v| v.as_str())
            .ok_or_else(|| CognisError::InvalidKey("Missing 'base_url' key".into()))?
            .to_string();

        let endpoints_val = obj
            .get("endpoints")
            .and_then(|v| v.as_array())
            .ok_or_else(|| CognisError::InvalidKey("Missing 'endpoints' array".into()))?;

        let mut endpoints = Vec::new();
        for ep in endpoints_val {
            let ep_obj = ep.as_object().ok_or_else(|| CognisError::TypeMismatch {
                expected: "JSON object for endpoint".into(),
                got: format!("{}", ep),
            })?;

            let method = ep_obj
                .get("method")
                .and_then(|v| v.as_str())
                .unwrap_or("GET")
                .to_string();
            let path = ep_obj
                .get("path")
                .and_then(|v| v.as_str())
                .unwrap_or("/")
                .to_string();
            let description = ep_obj
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();

            let mut parameters = Vec::new();
            if let Some(params) = ep_obj.get("parameters").and_then(|v| v.as_array()) {
                for p in params {
                    let p_obj = p.as_object().ok_or_else(|| CognisError::TypeMismatch {
                        expected: "JSON object for parameter".into(),
                        got: format!("{}", p),
                    })?;

                    parameters.push(ParameterSpec {
                        name: p_obj
                            .get("name")
                            .and_then(|v| v.as_str())
                            .unwrap_or("")
                            .to_string(),
                        param_type: p_obj
                            .get("type")
                            .and_then(|v| v.as_str())
                            .unwrap_or("string")
                            .to_string(),
                        required: p_obj
                            .get("required")
                            .and_then(|v| v.as_bool())
                            .unwrap_or(false),
                        description: p_obj
                            .get("description")
                            .and_then(|v| v.as_str())
                            .unwrap_or("")
                            .to_string(),
                    });
                }
            }

            endpoints.push(EndpointSpec {
                method,
                path,
                description,
                parameters,
            });
        }

        Ok(APISpec {
            base_url,
            endpoints,
        })
    }
}

// ---------------------------------------------------------------------------
// RequestValidator
// ---------------------------------------------------------------------------

/// Validates constructed HTTP requests for safety.
#[derive(Debug, Clone)]
pub struct RequestValidator {
    /// The expected base URL that all requests must start with.
    base_url: String,
    /// Set of allowed HTTP methods (uppercased).
    allowed_methods: HashSet<String>,
}

impl RequestValidator {
    /// Create a new validator for the given base URL and allowed methods.
    pub fn new(base_url: impl Into<String>, allowed_methods: &HashSet<String>) -> Self {
        Self {
            base_url: base_url.into(),
            allowed_methods: allowed_methods.clone(),
        }
    }

    /// Validate an HTTP request described as a JSON object.
    ///
    /// Checks:
    /// - The `method` is in the allowed set.
    /// - The `url` starts with the expected `base_url`.
    /// - The `url` does not contain injection patterns (newlines, backticks, etc.).
    pub fn validate(&self, request: &Value) -> Result<()> {
        let obj = request
            .as_object()
            .ok_or_else(|| CognisError::TypeMismatch {
                expected: "JSON object".into(),
                got: format!("{}", request),
            })?;

        // Validate method
        let method = obj
            .get("method")
            .and_then(|v| v.as_str())
            .ok_or_else(|| CognisError::InvalidKey("Missing 'method' in request".into()))?
            .to_uppercase();

        if !self.allowed_methods.contains(&method) {
            return Err(CognisError::Other(format!(
                "HTTP method '{}' is not allowed. Allowed methods: {:?}",
                method, self.allowed_methods
            )));
        }

        // Validate URL
        let url = obj
            .get("url")
            .and_then(|v| v.as_str())
            .ok_or_else(|| CognisError::InvalidKey("Missing 'url' in request".into()))?;

        if !url.starts_with(&self.base_url) {
            return Err(CognisError::Other(format!(
                "URL '{}' does not start with expected base URL '{}'",
                url, self.base_url
            )));
        }

        // Check for injection patterns
        Self::check_url_injection(url)?;

        Ok(())
    }

    /// Check a URL for common injection patterns.
    fn check_url_injection(url: &str) -> Result<()> {
        // Disallow newlines (HTTP header injection)
        if url.contains('\n') || url.contains('\r') {
            return Err(CognisError::Other(
                "URL contains newline characters (possible header injection)".into(),
            ));
        }

        // Disallow backticks (command injection in some contexts)
        if url.contains('`') {
            return Err(CognisError::Other(
                "URL contains backtick characters (possible injection)".into(),
            ));
        }

        // Disallow javascript: scheme
        let lower = url.to_lowercase();
        if lower.contains("javascript:") {
            return Err(CognisError::Other(
                "URL contains 'javascript:' scheme (possible injection)".into(),
            ));
        }

        // Disallow data: scheme
        if lower.starts_with("data:") {
            return Err(CognisError::Other(
                "URL uses 'data:' scheme (possible injection)".into(),
            ));
        }

        // Disallow double-encoded characters that could bypass validation
        let double_encode_re = Regex::new(r"%25[0-9a-fA-F]{2}").unwrap();
        if double_encode_re.is_match(url) {
            return Err(CognisError::Other(
                "URL contains double-encoded characters (possible injection)".into(),
            ));
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Default prompt
// ---------------------------------------------------------------------------

const DEFAULT_API_PROMPT: &str = r#"You are an API request constructor. Given the following API specification and a user question, construct the appropriate HTTP request.

{api_description}

User question: {question}

Respond with ONLY a JSON object in this exact format (no markdown, no explanation):
{{"method": "GET", "url": "https://...", "headers": {{}}, "body": null}}"#;

// ---------------------------------------------------------------------------
// APIChain
// ---------------------------------------------------------------------------

/// A chain that uses an LLM to construct HTTP requests from natural-language
/// questions based on an API specification.
///
/// The chain:
/// 1. Formats the API spec into a human-readable description
/// 2. Sends the spec + question to the LLM
/// 3. Parses the LLM response as a JSON request object
/// 4. Validates the request (method, URL, injection checks)
/// 5. Optionally executes the HTTP request
pub struct APIChain {
    model: Arc<dyn BaseChatModel>,
    api_spec: APISpec,
    execute_requests: bool,
    allowed_methods: HashSet<String>,
    prompt_template: String,
}

/// Builder for [`APIChain`].
pub struct APIChainBuilder {
    model: Option<Arc<dyn BaseChatModel>>,
    api_spec: Option<APISpec>,
    execute_requests: bool,
    allowed_methods: HashSet<String>,
    prompt_template: String,
}

impl APIChainBuilder {
    /// Create a new builder with defaults (GET-only, no request execution).
    pub fn new() -> Self {
        let mut allowed = HashSet::new();
        allowed.insert("GET".to_string());
        Self {
            model: None,
            api_spec: None,
            execute_requests: false,
            allowed_methods: allowed,
            prompt_template: DEFAULT_API_PROMPT.to_string(),
        }
    }

    /// Set the chat model (required).
    pub fn model(mut self, model: Arc<dyn BaseChatModel>) -> Self {
        self.model = Some(model);
        self
    }

    /// Set the API specification (required).
    pub fn api_spec(mut self, spec: APISpec) -> Self {
        self.api_spec = Some(spec);
        self
    }

    /// Enable or disable HTTP request execution. Default: `false`.
    pub fn execute_requests(mut self, execute: bool) -> Self {
        self.execute_requests = execute;
        self
    }

    /// Set the allowed HTTP methods. Default: `{"GET"}`.
    pub fn allowed_methods(mut self, methods: HashSet<String>) -> Self {
        self.allowed_methods = methods;
        self
    }

    /// Add an allowed HTTP method.
    pub fn allow_method(mut self, method: impl Into<String>) -> Self {
        self.allowed_methods.insert(method.into().to_uppercase());
        self
    }

    /// Set a custom prompt template.
    ///
    /// The template should contain `{api_description}` and `{question}` placeholders.
    pub fn prompt_template(mut self, template: impl Into<String>) -> Self {
        self.prompt_template = template.into();
        self
    }

    /// Build the [`APIChain`]. Panics if model or api_spec is not set.
    pub fn build(self) -> APIChain {
        APIChain {
            model: self.model.expect("model is required for APIChain"),
            api_spec: self.api_spec.expect("api_spec is required for APIChain"),
            execute_requests: self.execute_requests,
            allowed_methods: self.allowed_methods,
            prompt_template: self.prompt_template,
        }
    }
}

impl Default for APIChainBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl APIChain {
    /// Start a new builder.
    pub fn builder() -> APIChainBuilder {
        APIChainBuilder::new()
    }

    /// Format the prompt template by replacing `{variable}` placeholders.
    fn format_prompt(&self, input: &Value) -> Result<String> {
        let re = Regex::new(r"\{(\w+)\}").unwrap();
        let obj = input.as_object().ok_or_else(|| CognisError::TypeMismatch {
            expected: "JSON object".into(),
            got: format!("{}", input),
        })?;

        let api_description = self.api_spec.to_description();

        let mut missing: Vec<String> = Vec::new();
        let result = re.replace_all(&self.prompt_template, |caps: &regex::Captures| {
            let key = &caps[1];
            match key {
                "api_description" => api_description.clone(),
                _ => match obj.get(key) {
                    Some(Value::String(s)) => s.clone(),
                    Some(v) => v.to_string(),
                    None => {
                        missing.push(key.to_string());
                        String::new()
                    }
                },
            }
        });

        if !missing.is_empty() {
            return Err(CognisError::InvalidKey(format!(
                "Missing input variable(s): {}",
                missing.join(", ")
            )));
        }

        Ok(result.into_owned())
    }

    /// Extract a JSON request object from the LLM response text.
    ///
    /// Handles responses that may include markdown code blocks or extra text.
    fn extract_request(response: &str) -> Result<Value> {
        let trimmed = response.trim();

        // Try to extract from ```json ... ``` or ``` ... ``` code block
        let code_block_re = Regex::new(r"(?is)```(?:json)?\s*\n?(.*?)\n?\s*```").unwrap();
        let extracted = code_block_re
            .captures(trimmed)
            .map(|cap| cap[1].trim().to_string());
        let json_str = extracted.as_deref().unwrap_or(trimmed);

        // Try to find the first JSON object in the text
        let start = json_str.find('{').ok_or_else(|| {
            CognisError::Other(format!(
                "Could not find JSON object in LLM response: {}",
                trimmed
            ))
        })?;

        // Find the matching closing brace
        let mut depth = 0;
        let mut end = start;
        for (i, ch) in json_str[start..].char_indices() {
            match ch {
                '{' => depth += 1,
                '}' => {
                    depth -= 1;
                    if depth == 0 {
                        end = start + i + 1;
                        break;
                    }
                }
                _ => {}
            }
        }

        let json_slice = &json_str[start..end];
        serde_json::from_str(json_slice)
            .map_err(|e| CognisError::Other(format!("Failed to parse LLM response as JSON: {}", e)))
    }

    /// Create a [`RequestValidator`] for this chain's configuration.
    fn validator(&self) -> RequestValidator {
        RequestValidator::new(&self.api_spec.base_url, &self.allowed_methods)
    }
}

#[async_trait]
impl Runnable for APIChain {
    fn name(&self) -> &str {
        "APIChain"
    }

    async fn invoke(&self, input: Value, _config: Option<&RunnableConfig>) -> Result<Value> {
        let question = input
            .as_object()
            .and_then(|o| o.get("question"))
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                CognisError::InvalidKey("Input must be a JSON object with a 'question' key".into())
            })?
            .to_string();

        // Format and send prompt to LLM
        let formatted = self.format_prompt(&input)?;
        let messages = vec![Message::Human(HumanMessage::new(&formatted))];
        let ai_msg = self.model.invoke_messages(&messages, None).await?;
        let raw_text = ai_msg.base.content.text();

        // Parse the LLM response into a request object
        let request = Self::extract_request(&raw_text)?;

        // Validate the request
        let validator = self.validator();
        validator.validate(&request)?;

        // If execute_requests is enabled and reqwest is available, make the call
        if self.execute_requests {
            #[cfg(feature = "openai")] // reqwest is available with any provider feature
            {
                let response = self.execute_http_request(&request).await?;
                return Ok(json!({
                    "question": question,
                    "request": request,
                    "response": response,
                }));
            }

            #[cfg(not(feature = "openai"))]
            {
                return Err(CognisError::Other(
                    "HTTP request execution requires the 'reqwest' dependency (enable a provider feature)".into(),
                ));
            }
        }

        Ok(json!({
            "question": question,
            "request": request,
        }))
    }
}

/// HTTP execution support (available when reqwest is present via a provider feature).
#[cfg(any(
    feature = "openai",
    feature = "anthropic",
    feature = "google",
    feature = "ollama",
    feature = "azure"
))]
impl APIChain {
    /// Execute an HTTP request described by a JSON object.
    async fn execute_http_request(&self, request: &Value) -> Result<Value> {
        let client = reqwest::Client::new();

        let method_str = request["method"].as_str().unwrap_or("GET").to_uppercase();
        let url = request["url"]
            .as_str()
            .ok_or_else(|| CognisError::InvalidKey("Missing 'url' in request".into()))?;

        let method = match method_str.as_str() {
            "GET" => reqwest::Method::GET,
            "POST" => reqwest::Method::POST,
            "PUT" => reqwest::Method::PUT,
            "DELETE" => reqwest::Method::DELETE,
            "PATCH" => reqwest::Method::PATCH,
            "HEAD" => reqwest::Method::HEAD,
            "OPTIONS" => reqwest::Method::OPTIONS,
            other => {
                return Err(CognisError::Other(format!(
                    "Unsupported HTTP method: {}",
                    other
                )));
            }
        };

        let mut req_builder = client.request(method, url);

        // Add headers
        if let Some(headers) = request.get("headers").and_then(|h| h.as_object()) {
            for (key, val) in headers {
                if let Some(v) = val.as_str() {
                    req_builder = req_builder.header(key.as_str(), v);
                }
            }
        }

        // Add body
        if let Some(body) = request.get("body") {
            if !body.is_null() {
                req_builder = req_builder.json(body);
            }
        }

        let response = req_builder
            .send()
            .await
            .map_err(|e| CognisError::Other(format!("HTTP request failed: {}", e)))?;

        let status = response.status().as_u16();
        let body_text = response
            .text()
            .await
            .map_err(|e| CognisError::Other(format!("Failed to read response body: {}", e)))?;

        // Try to parse as JSON, fall back to string
        let body_value =
            serde_json::from_str::<Value>(&body_text).unwrap_or(Value::String(body_text));

        Ok(json!({
            "status": status,
            "body": body_value,
        }))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use cognis_core::language_models::fake::FakeListChatModel;

    fn fake_model(responses: Vec<&str>) -> Arc<dyn BaseChatModel> {
        Arc::new(FakeListChatModel::new(
            responses.into_iter().map(String::from).collect(),
        ))
    }

    fn sample_api_spec() -> APISpec {
        APISpec::builder("https://api.example.com")
            .endpoint(
                EndpointSpecBuilder::new("GET", "/users")
                    .description("List all users")
                    .parameter(
                        ParameterSpecBuilder::new("limit", "integer")
                            .required(false)
                            .description("Maximum number of results")
                            .build(),
                    )
                    .build(),
            )
            .endpoint(
                EndpointSpecBuilder::new("GET", "/users/{id}")
                    .description("Get a specific user by ID")
                    .parameter(
                        ParameterSpecBuilder::new("id", "integer")
                            .required(true)
                            .description("User ID")
                            .build(),
                    )
                    .build(),
            )
            .endpoint(
                EndpointSpecBuilder::new("POST", "/users")
                    .description("Create a new user")
                    .parameter(
                        ParameterSpecBuilder::new("name", "string")
                            .required(true)
                            .description("User name")
                            .build(),
                    )
                    .parameter(
                        ParameterSpecBuilder::new("email", "string")
                            .required(true)
                            .description("User email")
                            .build(),
                    )
                    .build(),
            )
            .build()
    }

    // 1. Basic API request construction
    #[tokio::test]
    async fn test_basic_api_request_construction() {
        let response = r#"{"method": "GET", "url": "https://api.example.com/users?limit=10", "headers": {}, "body": null}"#;
        let chain = APIChain::builder()
            .model(fake_model(vec![response]))
            .api_spec(sample_api_spec())
            .build();

        let result = chain
            .invoke(json!({"question": "List the first 10 users"}), None)
            .await
            .unwrap();

        assert_eq!(result["request"]["method"], "GET");
        assert_eq!(
            result["request"]["url"],
            "https://api.example.com/users?limit=10"
        );
        assert_eq!(result["question"], "List the first 10 users");
        // No response key when execute_requests is false
        assert!(result.get("response").is_none());
    }

    // 2. API spec to_description formatting
    #[test]
    fn test_api_spec_to_description() {
        let spec = sample_api_spec();
        let desc = spec.to_description();

        assert!(desc.contains("API Base URL: https://api.example.com"));
        assert!(desc.contains("GET /users - List all users"));
        assert!(desc.contains("GET /users/{id} - Get a specific user by ID"));
        assert!(desc.contains("POST /users - Create a new user"));
        assert!(desc.contains("limit (integer, optional)"));
        assert!(desc.contains("id (integer, required)"));
        assert!(desc.contains("name (string, required)"));
        assert!(desc.contains("email (string, required)"));
    }

    // 3. Request validation (valid URL)
    #[test]
    fn test_request_validation_valid_url() {
        let mut allowed = HashSet::new();
        allowed.insert("GET".to_string());
        let validator = RequestValidator::new("https://api.example.com", &allowed);

        let request = json!({
            "method": "GET",
            "url": "https://api.example.com/users?limit=10",
            "headers": {},
            "body": null
        });

        assert!(validator.validate(&request).is_ok());
    }

    // 4. Request validation (invalid method rejected)
    #[test]
    fn test_request_validation_invalid_method_rejected() {
        let mut allowed = HashSet::new();
        allowed.insert("GET".to_string());
        let validator = RequestValidator::new("https://api.example.com", &allowed);

        let request = json!({
            "method": "DELETE",
            "url": "https://api.example.com/users/1",
            "headers": {},
            "body": null
        });

        let result = validator.validate(&request);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not allowed"),
            "Error should mention 'not allowed': {err}"
        );
    }

    // 5. APISpec builder
    #[test]
    fn test_api_spec_builder() {
        let spec = APISpec::builder("https://api.test.com")
            .endpoint(
                EndpointSpecBuilder::new("GET", "/health")
                    .description("Health check")
                    .build(),
            )
            .endpoint(
                EndpointSpecBuilder::new("POST", "/data")
                    .description("Submit data")
                    .parameter(
                        ParameterSpecBuilder::new("payload", "object")
                            .required(true)
                            .description("Data payload")
                            .build(),
                    )
                    .build(),
            )
            .build();

        assert_eq!(spec.base_url, "https://api.test.com");
        assert_eq!(spec.endpoints.len(), 2);
        assert_eq!(spec.endpoints[0].method, "GET");
        assert_eq!(spec.endpoints[0].path, "/health");
        assert_eq!(spec.endpoints[1].method, "POST");
        assert_eq!(spec.endpoints[1].parameters.len(), 1);
        assert!(spec.endpoints[1].parameters[0].required);
    }

    // 6. EndpointSpec with parameters
    #[test]
    fn test_endpoint_spec_with_parameters() {
        let endpoint = EndpointSpecBuilder::new("PUT", "/items/{id}")
            .description("Update an item")
            .parameter(
                ParameterSpecBuilder::new("id", "integer")
                    .required(true)
                    .description("Item ID")
                    .build(),
            )
            .parameter(
                ParameterSpecBuilder::new("name", "string")
                    .required(false)
                    .description("New item name")
                    .build(),
            )
            .parameter(
                ParameterSpecBuilder::new("active", "boolean")
                    .required(false)
                    .description("Whether the item is active")
                    .build(),
            )
            .build();

        assert_eq!(endpoint.method, "PUT");
        assert_eq!(endpoint.path, "/items/{id}");
        assert_eq!(endpoint.description, "Update an item");
        assert_eq!(endpoint.parameters.len(), 3);
        assert!(endpoint.parameters[0].required);
        assert!(!endpoint.parameters[1].required);
        assert_eq!(endpoint.parameters[2].param_type, "boolean");
    }

    // 7. Allowed methods filtering
    #[tokio::test]
    async fn test_allowed_methods_filtering() {
        // LLM returns a POST request but only GET is allowed
        let response = r#"{"method": "POST", "url": "https://api.example.com/users", "headers": {}, "body": {"name": "test"}}"#;
        let chain = APIChain::builder()
            .model(fake_model(vec![response]))
            .api_spec(sample_api_spec())
            .build();

        let result = chain
            .invoke(json!({"question": "Create a new user named test"}), None)
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("not allowed"),
            "Error should mention method not allowed: {err}"
        );
    }

    // 8. from_json parsing
    #[test]
    fn test_from_json_parsing() {
        let json_spec = json!({
            "base_url": "https://api.weather.com",
            "endpoints": [
                {
                    "method": "GET",
                    "path": "/forecast",
                    "description": "Get weather forecast",
                    "parameters": [
                        {
                            "name": "city",
                            "type": "string",
                            "required": true,
                            "description": "City name"
                        },
                        {
                            "name": "days",
                            "type": "integer",
                            "required": false,
                            "description": "Number of forecast days"
                        }
                    ]
                }
            ]
        });

        let spec = APISpec::from_json(&json_spec).unwrap();
        assert_eq!(spec.base_url, "https://api.weather.com");
        assert_eq!(spec.endpoints.len(), 1);
        assert_eq!(spec.endpoints[0].method, "GET");
        assert_eq!(spec.endpoints[0].path, "/forecast");
        assert_eq!(spec.endpoints[0].parameters.len(), 2);
        assert_eq!(spec.endpoints[0].parameters[0].name, "city");
        assert!(spec.endpoints[0].parameters[0].required);
        assert_eq!(spec.endpoints[0].parameters[1].param_type, "integer");
        assert!(!spec.endpoints[0].parameters[1].required);
    }

    // 9. Runnable trait implementation
    #[tokio::test]
    async fn test_runnable_trait_implementation() {
        let response = r#"{"method": "GET", "url": "https://api.example.com/users/42", "headers": {}, "body": null}"#;
        let chain = APIChain::builder()
            .model(fake_model(vec![response]))
            .api_spec(sample_api_spec())
            .build();

        let runnable: &dyn Runnable = &chain;
        assert_eq!(runnable.name(), "APIChain");

        let result = runnable
            .invoke(json!({"question": "Get user 42"}), None)
            .await
            .unwrap();
        assert_eq!(result["request"]["method"], "GET");
        assert_eq!(result["request"]["url"], "https://api.example.com/users/42");
    }

    // 10. Request extraction from LLM response
    #[test]
    fn test_request_extraction_from_llm_response() {
        // Plain JSON
        let plain = r#"{"method": "GET", "url": "https://api.example.com/users", "headers": {}, "body": null}"#;
        let parsed = APIChain::extract_request(plain).unwrap();
        assert_eq!(parsed["method"], "GET");

        // Wrapped in markdown code block
        let markdown = "Here is the request:\n```json\n{\"method\": \"GET\", \"url\": \"https://api.example.com/users\", \"headers\": {}, \"body\": null}\n```";
        let parsed = APIChain::extract_request(markdown).unwrap();
        assert_eq!(parsed["method"], "GET");
        assert_eq!(parsed["url"], "https://api.example.com/users");

        // With extra text around it
        let noisy = "Sure! Here you go: {\"method\": \"POST\", \"url\": \"https://api.example.com/data\", \"headers\": {\"Content-Type\": \"application/json\"}, \"body\": {\"key\": \"value\"}} Hope that helps!";
        let parsed = APIChain::extract_request(noisy).unwrap();
        assert_eq!(parsed["method"], "POST");
        assert_eq!(parsed["body"]["key"], "value");
    }

    // 11. URL injection prevention
    #[test]
    fn test_url_injection_prevention() {
        let mut allowed = HashSet::new();
        allowed.insert("GET".to_string());
        let validator = RequestValidator::new("https://api.example.com", &allowed);

        // Newline injection
        let req_newline = json!({
            "method": "GET",
            "url": "https://api.example.com/users\r\nX-Injected: header",
            "headers": {},
            "body": null
        });
        let result = validator.validate(&req_newline);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("newline"));

        // Backtick injection
        let req_backtick = json!({
            "method": "GET",
            "url": "https://api.example.com/users`whoami`",
            "headers": {},
            "body": null
        });
        let result = validator.validate(&req_backtick);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("backtick"));

        // Wrong base URL
        let req_wrong_base = json!({
            "method": "GET",
            "url": "https://evil.com/api",
            "headers": {},
            "body": null
        });
        let result = validator.validate(&req_wrong_base);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("base URL"));

        // JavaScript scheme
        let req_js = json!({
            "method": "GET",
            "url": "https://api.example.com/users?redirect=javascript:alert(1)",
            "headers": {},
            "body": null
        });
        let result = validator.validate(&req_js);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("javascript"));
    }

    // 12. Allow multiple methods
    #[tokio::test]
    async fn test_allow_multiple_methods() {
        let response = r#"{"method": "POST", "url": "https://api.example.com/users", "headers": {"Content-Type": "application/json"}, "body": {"name": "Alice"}}"#;
        let chain = APIChain::builder()
            .model(fake_model(vec![response]))
            .api_spec(sample_api_spec())
            .allow_method("POST")
            .build();

        let result = chain
            .invoke(json!({"question": "Create user Alice"}), None)
            .await
            .unwrap();

        assert_eq!(result["request"]["method"], "POST");
        assert_eq!(result["request"]["body"]["name"], "Alice");
    }

    // 13. from_json missing base_url
    #[test]
    fn test_from_json_missing_base_url() {
        let json_spec = json!({
            "endpoints": []
        });
        let result = APISpec::from_json(&json_spec);
        assert!(result.is_err());
    }

    // 14. Double-encoded URL rejection
    #[test]
    fn test_double_encoded_url_rejection() {
        let mut allowed = HashSet::new();
        allowed.insert("GET".to_string());
        let validator = RequestValidator::new("https://api.example.com", &allowed);

        let req = json!({
            "method": "GET",
            "url": "https://api.example.com/path%252F..%252Fetc%252Fpasswd",
            "headers": {},
            "body": null
        });
        let result = validator.validate(&req);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("double-encoded"));
    }
}