zai-rs 0.1.15

一个 Rust SDK, 用于调用 智普AI API
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
//! # Tool Definitions & Configurations
//!
//! Defines the tool types that can be attached to chat requests, including
//! function calling, web search integration, retrieval tools, and the
//! [`ThinkingType`] configuration.
//!
//! # Key Types
//!
//! - [`ThinkingType`] — Controls reasoning mode for thinking-capable models
//! - [`FunctionTool`] — Defines a callable function with JSON-schema parameters
//! - [`WebSearchTool`] — Enables live web search within chat
//! - [`Retrieval`] — Enables knowledge-base retrieval
//! - [`ToolChoice`] — Controls tool-selection behaviour (`auto`, `none`, or
//!   specific function)

use std::collections::HashMap;

use serde::Serialize;
use validator::*;

use super::model_validate::validate_json_schema_value;
use crate::tool::web_search::request::{ContentSize, SearchEngine, SearchRecencyFilter};

/// Controls thinking/reasoning capabilities in AI models.
///
/// This structure determines whether a model should engage in step-by-step
/// reasoning when processing requests, and whether to preserve reasoning
/// content across turns via `clear_thinking`. Thinking mode can improve
/// accuracy for complex tasks but may increase response time and token usage.
///
/// ## Fields
///
/// - `mode` - Whether thinking is enabled or disabled
/// - `clear_thinking` - When `false`, preserves `reasoning_content` across
///   turns (recommended for Coding / Agent scenarios)
///
/// ## Usage
///
/// ```rust,ignore
/// let client = ChatCompletion::new(model, messages, api_key)
///     .with_thinking(ThinkingType::enabled());
///
/// // Preserve reasoning content across turns (Coding / Agent)
/// let client = ChatCompletion::new(model, messages, api_key)
///     .with_thinking(ThinkingType::enabled().with_clear_thinking(false));
/// ```
///
/// ## Model Compatibility
///
/// Thinking capabilities are available only on models that implement the
/// `ThinkEnable` trait, such as GLM-5.1, GLM-5, GLM-4.7, and GLM-4.5 series
/// models.
#[derive(Debug, Clone, Serialize)]
pub struct ThinkingType {
    /// Whether thinking is enabled or disabled.
    #[serde(rename = "type")]
    pub mode: ThinkingMode,

    /// Whether to clear historical `reasoning_content`.
    ///
    /// - `true` (default for standard API): Clears reasoning content each turn.
    /// - `false` (recommended for Coding / Agent): Preserves reasoning content
    ///   across turns, enabling better context for multi-step tool calls.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub clear_thinking: Option<bool>,
}

/// Thinking mode variants.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingMode {
    Enabled,
    Disabled,
}

impl ThinkingType {
    /// Create a new thinking configuration with enabled mode.
    pub fn enabled() -> Self {
        Self {
            mode: ThinkingMode::Enabled,
            clear_thinking: None,
        }
    }

    /// Create a new thinking configuration with disabled mode.
    pub fn disabled() -> Self {
        Self {
            mode: ThinkingMode::Disabled,
            clear_thinking: None,
        }
    }

    /// Set whether to clear historical reasoning content.
    ///
    /// Use `false` for Coding / Agent scenarios where reasoning content should
    /// be preserved across turns.
    pub fn with_clear_thinking(mut self, clear: bool) -> Self {
        self.clear_thinking = Some(clear);
        self
    }
}

/// Available tools that AI assistants can invoke during conversations.
///
/// This enum defines the different categories of external tools and
/// capabilities that can be made available to AI models. Each tool type serves
/// specific purposes and has its own configuration requirements.
///
/// ## Tool Categories
///
/// ### Function Tools
/// Custom user-defined functions that the AI can call with structured
/// parameters. Useful for integrating external APIs, databases, or business
/// logic.
///
/// ### Retrieval Tools
/// Access to knowledge bases, document collections, or information retrieval
/// systems. Enables the AI to query structured knowledge sources.
///
/// ### Web Search Tools
/// Internet search capabilities for accessing current information.
/// Allows the AI to perform web searches and retrieve up-to-date information.
///
/// ### MCP Tools
/// Model Context Protocol tools for standardized tool integration.
/// Provides a standardized interface for tool communication.
///
/// ## Usage
///
/// ```rust,ignore
/// // Function tool
/// let function_tool = Tools::Function {
///     function: Function::new("get_weather", "Get weather data", parameters)
/// };
///
/// // Web search tool
/// let search_tool = Tools::WebSearch {
///     web_search: WebSearch::new(SearchEngine::SearchPro)
///         .with_enable(true)
///         .with_count(10)
/// };
/// ```
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")]
#[serde(rename_all = "snake_case")]
pub enum Tools {
    /// Custom function calling tool with parameters.
    ///
    /// Allows the AI to invoke user-defined functions with structured
    /// arguments. Functions must be pre-defined with JSON schemas for
    /// parameter validation.
    Function { function: Function },

    /// Knowledge retrieval system access tools.
    ///
    /// Provides access to knowledge bases, document collections, or other
    /// structured information sources that the AI can query.
    Retrieval { retrieval: Retrieval },

    /// Web search capabilities for internet access.
    ///
    /// Enables the AI to perform web searches and access current information
    /// from the internet. Supports various search engines and configurations.
    WebSearch { web_search: WebSearch },

    /// Model Context Protocol (MCP) tools.
    ///
    /// Standardized tools that follow the Model Context Protocol specification,
    /// providing a consistent interface for tool integration and communication.
    #[serde(rename = "mcp")]
    MCP { mcp: MCP },
}

/// Definition of a callable function tool.
///
/// This structure defines a function that can be called by the assistant,
/// including its name, description, and parameter schema.
///
/// # Validation
///
/// * `name` - Must be between 1 and 64 characters
/// * `parameters` - Must be a valid JSON schema
#[derive(Debug, Clone, Serialize, Validate)]
pub struct Function {
    /// The name of the function. Must be between 1 and 64 characters.
    #[validate(length(min = 1, max = 64))]
    pub name: String,

    /// A description of what the function does.
    pub description: String,

    /// JSON schema describing the function's parameters.
    /// Server expects an object; keep as Value to avoid double-encoding
    /// strings.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[validate(custom(function = "validate_json_schema_value"))]
    pub parameters: Option<serde_json::Value>,
}

impl Function {
    /// Creates a new function call definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the function
    /// * `description` - A description of what the function does
    /// * `parameters` - JSON schema string describing the function parameters
    ///
    /// # Returns
    ///
    /// A new `Function` instance.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let func = Function::new(
    ///     "get_weather",
    ///     "Get current weather for a location",
    ///     r#"{"type": "object", "properties": {"location": {"type": "string"}}}"#
    /// );
    /// ```
    pub fn new(
        name: impl Into<String>,
        description: impl Into<String>,
        parameters: serde_json::Value,
    ) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            parameters: Some(parameters),
        }
    }
}

/// Configuration for retrieval tool capabilities.
///
/// This structure represents a retrieval tool that can access knowledge bases
/// or document collections. Currently a placeholder for future expansion.
#[derive(Debug, Clone, Serialize)]
pub struct Retrieval {
    knowledge_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    prompt_template: Option<String>,
}

impl Retrieval {
    /// Creates a new `Retrieval` instance.
    pub fn new(knowledge_id: impl Into<String>, prompt_template: Option<String>) -> Self {
        Self {
            knowledge_id: knowledge_id.into(),
            prompt_template,
        }
    }
}

/// Configuration for web search tool capabilities.
///
/// The order in which search results are returned.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ResultSequence {
    Before,
    After,
}

/// This structure represents a web search tool that can perform internet
/// searches. Fields mirror the external web_search schema.
#[derive(Debug, Clone, Serialize, Validate)]
pub struct WebSearch {
    /// Search engine type (required). Supported: search_std, search_pro,
    /// search_pro_sogou, search_pro_quark.
    pub search_engine: SearchEngine,

    /// Whether to enable web search. Default is false.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable: Option<bool>,

    /// Force-triggered search query string.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_query: Option<String>,

    /// Whether to perform search intent detection. true: execute only when
    /// intent is detected; false: skip detection and search directly.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_intent: Option<bool>,

    /// Number of results to return (1-50).
    #[serde(skip_serializing_if = "Option::is_none")]
    #[validate(range(min = 1, max = 50))]
    pub count: Option<u32>,

    /// Whitelist domain filter, e.g., "www.example.com".
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_domain_filter: Option<String>,

    /// Time range filter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_recency_filter: Option<SearchRecencyFilter>,

    /// Snippet summary size: medium or high.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_size: Option<ContentSize>,

    /// Return sequence for search results: before or after.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result_sequence: Option<ResultSequence>,

    /// Whether to include detailed search source information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_result: Option<bool>,

    /// Whether an answer requires search results to be returned.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_search: Option<bool>,

    /// Custom prompt to post-process search results.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_prompt: Option<String>,
}

impl WebSearch {
    /// Create a WebSearch config with the required search engine; other fields
    /// are optional.
    pub fn new(search_engine: SearchEngine) -> Self {
        Self {
            search_engine,
            enable: None,
            search_query: None,
            search_intent: None,
            count: None,
            search_domain_filter: None,
            search_recency_filter: None,
            content_size: None,
            result_sequence: None,
            search_result: None,
            require_search: None,
            search_prompt: None,
        }
    }

    /// Enable or disable web search.
    pub fn with_enable(mut self, enable: bool) -> Self {
        self.enable = Some(enable);
        self
    }
    /// Set a forced search query.
    pub fn with_search_query(mut self, query: impl Into<String>) -> Self {
        self.search_query = Some(query.into());
        self
    }
    /// Set search intent detection behavior.
    pub fn with_search_intent(mut self, search_intent: bool) -> Self {
        self.search_intent = Some(search_intent);
        self
    }
    /// Set results count (1-50).
    pub fn with_count(mut self, count: u32) -> Self {
        self.count = Some(count);
        self
    }
    /// Restrict to a whitelist domain.
    pub fn with_search_domain_filter(mut self, domain: impl Into<String>) -> Self {
        self.search_domain_filter = Some(domain.into());
        self
    }
    /// Set time range filter.
    pub fn with_search_recency_filter(mut self, filter: SearchRecencyFilter) -> Self {
        self.search_recency_filter = Some(filter);
        self
    }
    /// Set content size.
    pub fn with_content_size(mut self, size: ContentSize) -> Self {
        self.content_size = Some(size);
        self
    }
    /// Set result sequence.
    pub fn with_result_sequence(mut self, seq: ResultSequence) -> Self {
        self.result_sequence = Some(seq);
        self
    }
    /// Toggle returning detailed search source info.
    pub fn with_search_result(mut self, enable: bool) -> Self {
        self.search_result = Some(enable);
        self
    }
    /// Require search results for answering.
    pub fn with_require_search(mut self, require: bool) -> Self {
        self.require_search = Some(require);
        self
    }
    /// Set a custom prompt to post-process search results.
    pub fn with_search_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.search_prompt = Some(prompt.into());
        self
    }
}
/// Represents the MCP connection configuration. When connecting to Zhipu's MCP
/// server using an MCP code, fill `server_label` with that code and leave
/// `server_url` empty.
#[derive(Debug, Clone, Serialize, Validate)]
pub struct MCP {
    /// MCP server identifier (required). If connecting to Zhipu MCP via code,
    /// put the code here.
    #[validate(length(min = 1))]
    pub server_label: String,

    /// MCP server URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[validate(url)]
    pub server_url: Option<String>,

    /// Transport type. Default: streamable-http.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub transport_type: Option<MCPTransportType>,

    /// Allowed tool names.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub allowed_tools: Vec<String>,

    /// Authentication headers required by the MCP server.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub headers: Option<HashMap<String, String>>,
}

impl MCP {
    /// Create a new MCP config with required server_label and default transport
    /// type.
    pub fn new(server_label: impl Into<String>) -> Self {
        Self {
            server_label: server_label.into(),
            server_url: None,
            transport_type: Some(MCPTransportType::StreamableHttp),
            allowed_tools: Vec::new(),
            headers: None,
        }
    }

    /// Set the MCP server URL.
    pub fn with_server_url(mut self, url: impl Into<String>) -> Self {
        self.server_url = Some(url.into());
        self
    }
    /// Set the MCP transport type.
    pub fn with_transport_type(mut self, transport: MCPTransportType) -> Self {
        self.transport_type = Some(transport);
        self
    }
    /// Replace the allowed tool list.
    pub fn with_allowed_tools(mut self, tools: impl Into<Vec<String>>) -> Self {
        self.allowed_tools = tools.into();
        self
    }
    /// Add a single allowed tool.
    pub fn add_allowed_tool(mut self, tool: impl Into<String>) -> Self {
        self.allowed_tools.push(tool.into());
        self
    }
    /// Set authentication headers map.
    pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
        self.headers = Some(headers);
        self
    }
    /// Add or update a single header entry.
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        let mut map = self.headers.unwrap_or_default();
        map.insert(key.into(), value.into());
        self.headers = Some(map);
        self
    }
}

/// Allowed MCP transport types.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum MCPTransportType {
    Sse,
    StreamableHttp,
}

/// Specifies the format for the model's response.
///
/// This enum controls how the model should structure its output, either as
/// plain text or as a structured JSON object.
///
/// # Variants
///
/// * `Text` - Plain text response format
/// * `JsonObject` - Structured JSON object response format
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "type")]
pub enum ResponseFormat {
    /// Plain text response format.
    Text,
    /// Structured JSON object response format.
    JsonObject,
}

#[cfg(test)]
mod tests {
    use super::*;

    // ThinkingType tests
    #[test]
    fn test_thinking_type_enabled_serialization() {
        let thinking = ThinkingType::enabled();
        let json = serde_json::to_string(&thinking).unwrap();
        assert!(json.contains("\"type\":\"enabled\""));
        assert!(!json.contains("clear_thinking"));
    }

    #[test]
    fn test_thinking_type_disabled_serialization() {
        let thinking = ThinkingType::disabled();
        let json = serde_json::to_string(&thinking).unwrap();
        assert!(json.contains("\"type\":\"disabled\""));
        assert!(!json.contains("clear_thinking"));
    }

    #[test]
    fn test_thinking_type_with_clear_thinking_serialization() {
        let thinking = ThinkingType::enabled().with_clear_thinking(false);
        let json = serde_json::to_string(&thinking).unwrap();
        assert!(json.contains("\"type\":\"enabled\""));
        assert!(json.contains("\"clear_thinking\":false"));
    }

    #[test]
    fn test_thinking_type_disabled_with_clear_thinking() {
        let thinking = ThinkingType::disabled().with_clear_thinking(true);
        let json = serde_json::to_string(&thinking).unwrap();
        assert!(json.contains("\"type\":\"disabled\""));
        assert!(json.contains("\"clear_thinking\":true"));
    }

    // Function tests
    #[test]
    fn test_function_new() {
        let params = serde_json::json!({
            "type": "object",
            "properties": {
                "name": {"type": "string"}
            }
        });
        let func = Function::new("test_func", "A test function", params);

        assert_eq!(func.name, "test_func");
        assert_eq!(func.description, "A test function");
        assert!(func.parameters.is_some());
    }

    #[test]
    fn test_function_serialization() {
        let params = serde_json::json!({
            "type": "object",
            "properties": {
                "value": {"type": "number"}
            }
        });
        let func = Function::new("test_func", "A test function", params);
        let json = serde_json::to_string(&func).unwrap();

        assert!(json.contains("\"name\":\"test_func\""));
        assert!(json.contains("\"description\":\"A test function\""));
        assert!(json.contains("\"properties\""));
    }

    #[test]
    fn test_function_validation() {
        let params = serde_json::json!({
            "type": "object",
            "properties": {}
        });
        let func = Function::new("valid_name", "Description", params.clone());

        // Name length validation: 1-64 characters
        assert!(func.validate().is_ok());

        let invalid_name = Function::new("", "Description", params.clone());
        assert!(invalid_name.validate().is_err());

        let long_name = Function::new("a".repeat(65), "Description", params);
        assert!(long_name.validate().is_err());
    }

    // Retrieval tests
    #[test]
    fn test_retrieval_new() {
        let retrieval = Retrieval::new("kb_123", Some("template".to_string()));
        assert_eq!(retrieval.knowledge_id, "kb_123");
        assert_eq!(retrieval.prompt_template, Some("template".to_string()));
    }

    #[test]
    fn test_retrieval_new_without_template() {
        let retrieval = Retrieval::new("kb_456", None);
        assert_eq!(retrieval.knowledge_id, "kb_456");
        assert!(retrieval.prompt_template.is_none());
    }

    #[test]
    fn test_retrieval_serialization() {
        let retrieval = Retrieval::new("kb_789", None);
        let json = serde_json::to_string(&retrieval).unwrap();
        assert!(json.contains("\"knowledge_id\":\"kb_789\""));
        // prompt_template should be omitted when None
        assert!(!json.contains("prompt_template"));
    }

    // WebSearch tests
    #[test]
    fn test_web_search_new() {
        let web_search = WebSearch::new(SearchEngine::SearchPro);
        assert_eq!(web_search.search_engine, SearchEngine::SearchPro);
        assert!(web_search.enable.is_none());
    }

    #[test]
    fn test_web_search_with_enable() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_enable(true);
        assert_eq!(web_search.enable, Some(true));
    }

    #[test]
    fn test_web_search_with_search_query() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_search_query("test query");
        assert_eq!(web_search.search_query, Some("test query".to_string()));
    }

    #[test]
    fn test_web_search_with_search_intent() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_search_intent(true);
        assert_eq!(web_search.search_intent, Some(true));
    }

    #[test]
    fn test_web_search_with_count() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_count(10);
        assert_eq!(web_search.count, Some(10));
    }

    #[test]
    fn test_web_search_with_search_domain_filter() {
        let web_search =
            WebSearch::new(SearchEngine::SearchPro).with_search_domain_filter("example.com");
        assert_eq!(
            web_search.search_domain_filter,
            Some("example.com".to_string())
        );
    }

    #[test]
    fn test_web_search_with_search_recency_filter() {
        let filter = SearchRecencyFilter::OneDay;
        let web_search =
            WebSearch::new(SearchEngine::SearchPro).with_search_recency_filter(filter.clone());
        assert_eq!(web_search.search_recency_filter, Some(filter));
    }

    #[test]
    fn test_web_search_with_content_size() {
        let size = ContentSize::Medium;
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_content_size(size.clone());
        assert_eq!(web_search.content_size, Some(size));
    }

    #[test]
    fn test_web_search_with_result_sequence() {
        let seq = ResultSequence::After;
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_result_sequence(seq.clone());
        assert_eq!(web_search.result_sequence, Some(seq));
    }

    #[test]
    fn test_web_search_with_search_result() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_search_result(true);
        assert_eq!(web_search.search_result, Some(true));
    }

    #[test]
    fn test_web_search_with_require_search() {
        let web_search = WebSearch::new(SearchEngine::SearchPro).with_require_search(true);
        assert_eq!(web_search.require_search, Some(true));
    }

    #[test]
    fn test_web_search_with_search_prompt() {
        let web_search =
            WebSearch::new(SearchEngine::SearchPro).with_search_prompt("custom prompt");
        assert_eq!(web_search.search_prompt, Some("custom prompt".to_string()));
    }

    #[test]
    fn test_web_search_serialization() {
        let web_search = WebSearch::new(SearchEngine::SearchPro)
            .with_enable(true)
            .with_count(5);
        let json = serde_json::to_string(&web_search).unwrap();
        assert!(json.contains("\"search_engine\""));
        assert!(json.contains("\"enable\":true"));
        assert!(json.contains("\"count\":5"));
    }

    // MCP tests
    #[test]
    fn test_mcp_new() {
        let mcp = MCP::new("server_label");
        assert_eq!(mcp.server_label, "server_label");
        assert_eq!(mcp.transport_type, Some(MCPTransportType::StreamableHttp));
        assert!(mcp.allowed_tools.is_empty());
    }

    #[test]
    fn test_mcp_with_server_url() {
        let mcp = MCP::new("server_label").with_server_url("https://example.com");
        assert_eq!(mcp.server_url, Some("https://example.com".to_string()));
    }

    #[test]
    fn test_mcp_with_transport_type() {
        let mcp = MCP::new("server_label").with_transport_type(MCPTransportType::Sse);
        assert_eq!(mcp.transport_type, Some(MCPTransportType::Sse));
    }

    #[test]
    fn test_mcp_with_allowed_tools() {
        let mcp = MCP::new("server_label")
            .with_allowed_tools(vec!["tool1".to_string(), "tool2".to_string()]);
        assert_eq!(mcp.allowed_tools.len(), 2);
        assert!(mcp.allowed_tools.contains(&"tool1".to_string()));
    }

    #[test]
    fn test_mcp_add_allowed_tool() {
        let mcp = MCP::new("server_label")
            .add_allowed_tool("tool1")
            .add_allowed_tool("tool2");
        assert_eq!(mcp.allowed_tools.len(), 2);
    }

    #[test]
    fn test_mcp_with_headers() {
        let mut headers = HashMap::new();
        headers.insert("Authorization".to_string(), "Bearer token".to_string());
        let mcp = MCP::new("server_label").with_headers(headers.clone());
        assert_eq!(mcp.headers, Some(headers));
    }

    #[test]
    fn test_mcp_with_header() {
        let mcp = MCP::new("server_label").with_header("Authorization", "Bearer token");
        let headers = mcp.headers.unwrap();
        assert_eq!(
            headers.get("Authorization"),
            Some(&"Bearer token".to_string())
        );
    }

    #[test]
    fn test_mcp_serialization() {
        let mcp = MCP::new("server_label")
            .with_server_url("https://example.com")
            .with_transport_type(MCPTransportType::Sse);
        let json = serde_json::to_string(&mcp).unwrap();
        assert!(json.contains("\"server_label\":\"server_label\""));
        assert!(json.contains("\"server_url\":\"https://example.com\""));
        assert!(json.contains("\"transport_type\":\"sse\""));
        // allowed_tools should be omitted when empty
        assert!(!json.contains("allowed_tools"));
    }

    // MCPTransportType tests
    #[test]
    fn test_mcp_transport_type_sse_serialization() {
        let transport = MCPTransportType::Sse;
        let json = serde_json::to_string(&transport).unwrap();
        assert!(json.contains("\"sse\""));
    }

    #[test]
    fn test_mcp_transport_type_streamable_http_serialization() {
        let transport = MCPTransportType::StreamableHttp;
        let json = serde_json::to_string(&transport).unwrap();
        assert!(json.contains("\"streamable-http\""));
    }

    // ResponseFormat tests
    #[test]
    fn test_response_format_text_serialization() {
        let format = ResponseFormat::Text;
        let json = serde_json::to_string(&format).unwrap();
        assert!(json.contains("\"type\":\"text\""));
    }

    #[test]
    fn test_response_format_json_object_serialization() {
        let format = ResponseFormat::JsonObject;
        let json = serde_json::to_string(&format).unwrap();
        assert!(json.contains("\"type\":\"json_object\""));
    }

    // Tools enum tests
    #[test]
    fn test_tools_function_serialization() {
        let func = Function::new("test_func", "test", serde_json::json!({}));
        let tools = Tools::Function { function: func };
        let json = serde_json::to_string(&tools).unwrap();
        assert!(json.contains("\"type\":\"function\""));
        assert!(json.contains("\"name\":\"test_func\""));
    }

    #[test]
    fn test_tools_retrieval_serialization() {
        let retrieval = Retrieval::new("kb_123", None);
        let tools = Tools::Retrieval { retrieval };
        let json = serde_json::to_string(&tools).unwrap();
        assert!(json.contains("\"type\":\"retrieval\""));
        assert!(json.contains("\"knowledge_id\":\"kb_123\""));
    }

    #[test]
    fn test_tools_web_search_serialization() {
        let web_search = WebSearch::new(SearchEngine::SearchPro);
        let tools = Tools::WebSearch { web_search };
        let json = serde_json::to_string(&tools).unwrap();
        assert!(json.contains("\"type\":\"web_search\""));
        assert!(json.contains("\"search_engine\""));
    }

    #[test]
    fn test_tools_mcp_serialization() {
        let mcp = MCP::new("server_label");
        let tools = Tools::MCP { mcp };
        let json = serde_json::to_string(&tools).unwrap();
        eprintln!("JSON: {}", json);
        assert!(json.contains("\"type\":\"mcp\""));
        assert!(json.contains("\"server_label\":\"server_label\""));
    }

    // ResultSequence tests
    #[test]
    fn test_result_sequence_before_serialization() {
        let seq = ResultSequence::Before;
        let json = serde_json::to_string(&seq).unwrap();
        assert!(json.contains("\"before\""));
    }

    #[test]
    fn test_result_sequence_after_serialization() {
        let seq = ResultSequence::After;
        let json = serde_json::to_string(&seq).unwrap();
        assert!(json.contains("\"after\""));
    }
}