cognisagent 0.2.1

Batteries-included agent framework built on cognis and cognisgraph
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
//! Pre-configured agent presets for common use cases.
//!
//! This module provides ready-to-use agent configurations via [`AgentPreset`],
//! a [`PresetRegistry`] for managing and discovering presets, and a
//! [`PresetCustomizer`] for tweaking presets before use.
//!
//! # Built-in Presets
//!
//! - [`coding_assistant`] -- code generation, review, and filesystem tools
//! - [`research_assistant`] -- web search and summarization with higher token limits
//! - [`data_analyst`] -- structured output and data analysis tooling
//! - [`writing_assistant`] -- creative writing with lower temperature
//! - [`task_planner`] -- planning and sub-agent delegation
//! - [`customer_support`] -- memory-enabled FAQ-focused agent
//! - [`code_reviewer`] -- filesystem tools with review-focused prompting
//!
//! # Example
//!
//! ```rust
//! use cognisagent::presets::{PresetRegistry, PresetCustomizer};
//!
//! let registry = PresetRegistry::default();
//! let preset = registry.get("coding-assistant").unwrap();
//!
//! let customized = PresetCustomizer::from_preset(preset)
//!     .with_model("openai", "gpt-4")
//!     .with_temperature(0.5)
//!     .build();
//! ```

use std::collections::HashMap;

use crate::config::{AgentConfig, AgentConfigBuilder};

// ---------------------------------------------------------------------------
// AgentPreset
// ---------------------------------------------------------------------------

/// A pre-configured agent template for a specific use case.
#[derive(Debug, Clone)]
pub struct AgentPreset {
    /// Short identifier for this preset (e.g. `"coding-assistant"`).
    pub name: String,
    /// Human-readable description of what this preset is optimized for.
    pub description: String,
    /// The underlying agent configuration.
    pub config: AgentConfig,
    /// System prompt tailored for this preset's use case.
    pub system_prompt: String,
    /// Names of tools that are recommended for this preset.
    pub suggested_tools: Vec<String>,
    /// Arbitrary tags for categorization and filtering.
    pub tags: HashMap<String, String>,
}

impl AgentPreset {
    /// Create a new preset with the given name and description.
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            config: AgentConfig::default(),
            system_prompt: String::new(),
            suggested_tools: Vec::new(),
            tags: HashMap::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// PresetRegistry
// ---------------------------------------------------------------------------

/// A registry of named agent presets with tag-based filtering.
#[derive(Debug, Clone)]
pub struct PresetRegistry {
    presets: Vec<AgentPreset>,
}

impl PresetRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            presets: Vec::new(),
        }
    }

    /// Register a preset. If a preset with the same name exists, it is replaced.
    pub fn register(&mut self, preset: AgentPreset) {
        self.presets.retain(|p| p.name != preset.name);
        self.presets.push(preset);
    }

    /// Look up a preset by name.
    pub fn get(&self, name: &str) -> Option<&AgentPreset> {
        self.presets.iter().find(|p| p.name == name)
    }

    /// Return all registered presets.
    pub fn list(&self) -> Vec<&AgentPreset> {
        self.presets.iter().collect()
    }

    /// Return presets that have a tag with the given key (any value).
    pub fn list_by_tag(&self, tag: &str) -> Vec<&AgentPreset> {
        self.presets
            .iter()
            .filter(|p| p.tags.contains_key(tag))
            .collect()
    }
}

impl Default for PresetRegistry {
    /// Create a registry pre-populated with all built-in presets.
    fn default() -> Self {
        let mut registry = Self::new();
        registry.register(coding_assistant());
        registry.register(research_assistant());
        registry.register(data_analyst());
        registry.register(writing_assistant());
        registry.register(task_planner());
        registry.register(customer_support());
        registry.register(code_reviewer());
        registry
    }
}

// ---------------------------------------------------------------------------
// PresetCustomizer
// ---------------------------------------------------------------------------

/// Builder for modifying an existing preset before use.
#[derive(Debug, Clone)]
pub struct PresetCustomizer {
    preset: AgentPreset,
}

impl PresetCustomizer {
    /// Start customizing from an existing preset (cloned).
    pub fn from_preset(preset: &AgentPreset) -> Self {
        Self {
            preset: preset.clone(),
        }
    }

    /// Override the model provider and model name.
    pub fn with_model(mut self, provider: &str, model: &str) -> Self {
        self.preset.config.model.provider = provider.to_string();
        self.preset.config.model.model_name = model.to_string();
        self
    }

    /// Override the sampling temperature.
    pub fn with_temperature(mut self, temp: f64) -> Self {
        self.preset.config.model.temperature = Some(temp);
        self
    }

    /// Override the suggested and configured tools.
    pub fn with_tools(mut self, tools: Vec<String>) -> Self {
        self.preset.suggested_tools = tools.clone();
        self.preset.config.tools = tools;
        self
    }

    /// Override the system prompt.
    pub fn with_system_prompt(mut self, prompt: &str) -> Self {
        self.preset.system_prompt = prompt.to_string();
        self.preset.config.system_prompt = Some(prompt.to_string());
        self
    }

    /// Consume the customizer and return the modified preset.
    pub fn build(self) -> AgentPreset {
        self.preset
    }
}

// ---------------------------------------------------------------------------
// Built-in preset factory functions
// ---------------------------------------------------------------------------

/// Preset optimized for code generation, review, and filesystem operations.
pub fn coding_assistant() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("coding-assistant")
        .description("Code generation and review agent")
        .system_prompt(
            "You are an expert software engineer. Write clean, well-documented, \
             and tested code. Use the filesystem tools to read and write files. \
             Follow best practices for the language being used.",
        )
        .temperature(0.2)
        .max_tokens(4096)
        .enable_filesystem(true)
        .enable_memory(true)
        .enable_summarization(false)
        .enable_planning(false)
        .tag("category", "development")
        .tag("tools", "filesystem")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "development".to_string());
    tags.insert("tools".to_string(), "filesystem".to_string());

    AgentPreset {
        name: "coding-assistant".to_string(),
        description: "Optimized for code generation, review, and filesystem operations."
            .to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec![
            "read_file".to_string(),
            "write_file".to_string(),
            "list_directory".to_string(),
            "grep".to_string(),
            "glob".to_string(),
        ],
        config,
        tags,
    }
}

/// Preset optimized for web research, summarization, and higher token limits.
pub fn research_assistant() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("research-assistant")
        .description("Web research and summarization agent")
        .system_prompt(
            "You are a meticulous research assistant. Search the web for information, \
             synthesize findings from multiple sources, and provide well-structured \
             summaries with citations. Always verify claims across sources.",
        )
        .temperature(0.3)
        .max_tokens(8192)
        .enable_filesystem(false)
        .enable_memory(true)
        .enable_summarization(true)
        .enable_planning(false)
        .max_context_tokens(32000)
        .tag("category", "research")
        .tag("tools", "web")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "research".to_string());
    tags.insert("tools".to_string(), "web".to_string());

    AgentPreset {
        name: "research-assistant".to_string(),
        description: "Web search and summarization with higher token limits.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec![
            "web_search".to_string(),
            "web_fetch".to_string(),
            "summarize".to_string(),
        ],
        config,
        tags,
    }
}

/// Preset optimized for structured data analysis.
pub fn data_analyst() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("data-analyst")
        .description("Structured data analysis agent")
        .system_prompt(
            "You are a data analyst. Analyze datasets, generate statistical summaries, \
             and produce structured output. Use code execution tools to run analysis \
             scripts. Present findings clearly with tables and charts when appropriate.",
        )
        .temperature(0.1)
        .max_tokens(4096)
        .enable_filesystem(true)
        .enable_memory(false)
        .enable_summarization(false)
        .enable_planning(true)
        .tag("category", "data")
        .tag("output", "structured")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "data".to_string());
    tags.insert("output".to_string(), "structured".to_string());

    AgentPreset {
        name: "data-analyst".to_string(),
        description: "Structured output and data analysis with code execution.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec![
            "python_repl".to_string(),
            "read_file".to_string(),
            "write_file".to_string(),
        ],
        config,
        tags,
    }
}

/// Preset optimized for creative and technical writing.
pub fn writing_assistant() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("writing-assistant")
        .description("Creative and technical writing agent")
        .system_prompt(
            "You are an expert writer. Produce clear, engaging, and well-structured \
             prose. Adapt your tone and style to the requested format — whether it is \
             a blog post, documentation, email, or creative fiction. Pay attention to \
             grammar, flow, and readability.",
        )
        .temperature(0.7)
        .max_tokens(4096)
        .enable_filesystem(false)
        .enable_memory(false)
        .enable_summarization(false)
        .enable_planning(false)
        .tag("category", "writing")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "writing".to_string());

    AgentPreset {
        name: "writing-assistant".to_string(),
        description: "Creative writing with lower temperature and no tools needed.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: Vec::new(),
        config,
        tags,
    }
}

/// Preset optimized for task decomposition, planning, and sub-agent delegation.
pub fn task_planner() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("task-planner")
        .description("Task planning and delegation agent")
        .system_prompt(
            "You are a task planner. Break complex tasks into smaller, actionable steps. \
             Create detailed execution plans, delegate sub-tasks to specialized agents, \
             and track progress. Ensure all dependencies between steps are identified.",
        )
        .temperature(0.3)
        .max_tokens(4096)
        .enable_filesystem(false)
        .enable_memory(true)
        .enable_summarization(true)
        .enable_planning(true)
        .tag("category", "planning")
        .tag("tools", "subagent")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "planning".to_string());
    tags.insert("tools".to_string(), "subagent".to_string());

    AgentPreset {
        name: "task-planner".to_string(),
        description: "Planning middleware with sub-agent delegation.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec!["create_plan".to_string(), "spawn_subagent".to_string()],
        config,
        tags,
    }
}

/// Preset optimized for customer support with memory and concise responses.
pub fn customer_support() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("customer-support")
        .description("Customer support agent with memory")
        .system_prompt(
            "You are a helpful customer support agent. Answer questions concisely and \
             accurately based on available knowledge. Remember previous interactions \
             with the customer. If you do not know the answer, say so and offer to \
             escalate. Always be polite and professional.",
        )
        .temperature(0.2)
        .max_tokens(2048)
        .enable_filesystem(false)
        .enable_memory(true)
        .enable_summarization(false)
        .enable_planning(false)
        .tag("category", "support")
        .tag("memory", "enabled")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "support".to_string());
    tags.insert("memory".to_string(), "enabled".to_string());

    AgentPreset {
        name: "customer-support".to_string(),
        description: "Memory-enabled agent for FAQ-focused customer support.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec!["knowledge_base_search".to_string()],
        config,
        tags,
    }
}

/// Preset optimized for code review with filesystem tools and structured output.
pub fn code_reviewer() -> AgentPreset {
    let config = AgentConfigBuilder::new()
        .name("code-reviewer")
        .description("Code review agent with structured feedback")
        .system_prompt(
            "You are an expert code reviewer. Read the provided code carefully and \
             provide structured feedback covering: correctness, performance, security, \
             readability, and adherence to best practices. Categorize each finding by \
             severity (critical, warning, suggestion). Always explain why something \
             is an issue and suggest a concrete fix.",
        )
        .temperature(0.1)
        .max_tokens(4096)
        .enable_filesystem(true)
        .enable_memory(false)
        .enable_summarization(false)
        .enable_planning(false)
        .tag("category", "development")
        .tag("output", "structured")
        .tag("tools", "filesystem")
        .build();

    let mut tags = HashMap::new();
    tags.insert("category".to_string(), "development".to_string());
    tags.insert("output".to_string(), "structured".to_string());
    tags.insert("tools".to_string(), "filesystem".to_string());

    AgentPreset {
        name: "code-reviewer".to_string(),
        description: "Filesystem tools with review-focused structured output.".to_string(),
        system_prompt: config.system_prompt.clone().unwrap_or_default(),
        suggested_tools: vec![
            "read_file".to_string(),
            "list_directory".to_string(),
            "grep".to_string(),
            "glob".to_string(),
        ],
        config,
        tags,
    }
}

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

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

    // -- AgentPreset construction tests --

    #[test]
    fn test_agent_preset_new() {
        let preset = AgentPreset::new("test", "A test preset");
        assert_eq!(preset.name, "test");
        assert_eq!(preset.description, "A test preset");
        assert!(preset.system_prompt.is_empty());
        assert!(preset.suggested_tools.is_empty());
        assert!(preset.tags.is_empty());
    }

    #[test]
    fn test_coding_assistant_preset() {
        let preset = coding_assistant();
        assert_eq!(preset.name, "coding-assistant");
        assert!(!preset.description.is_empty());
        assert!(!preset.system_prompt.is_empty());
        assert!(preset.suggested_tools.contains(&"read_file".to_string()));
        assert!(preset.suggested_tools.contains(&"write_file".to_string()));
        assert_eq!(preset.config.model.temperature, Some(0.2));
        assert!(preset.config.middleware.enable_filesystem);
        assert!(preset.tags.contains_key("category"));
        assert_eq!(
            preset.tags.get("category"),
            Some(&"development".to_string())
        );
    }

    #[test]
    fn test_research_assistant_preset() {
        let preset = research_assistant();
        assert_eq!(preset.name, "research-assistant");
        assert!(preset.config.middleware.enable_summarization);
        assert_eq!(preset.config.model.max_tokens, Some(8192));
        assert_eq!(preset.config.middleware.max_context_tokens, Some(32000));
        assert!(preset.suggested_tools.contains(&"web_search".to_string()));
        assert_eq!(preset.tags.get("category"), Some(&"research".to_string()));
    }

    #[test]
    fn test_data_analyst_preset() {
        let preset = data_analyst();
        assert_eq!(preset.name, "data-analyst");
        assert_eq!(preset.config.model.temperature, Some(0.1));
        assert!(preset.config.middleware.enable_planning);
        assert!(!preset.config.middleware.enable_memory);
        assert!(preset.suggested_tools.contains(&"python_repl".to_string()));
        assert_eq!(preset.tags.get("output"), Some(&"structured".to_string()));
    }

    #[test]
    fn test_writing_assistant_preset() {
        let preset = writing_assistant();
        assert_eq!(preset.name, "writing-assistant");
        assert_eq!(preset.config.model.temperature, Some(0.7));
        assert!(!preset.config.middleware.enable_filesystem);
        assert!(preset.suggested_tools.is_empty());
        assert_eq!(preset.tags.get("category"), Some(&"writing".to_string()));
    }

    #[test]
    fn test_task_planner_preset() {
        let preset = task_planner();
        assert_eq!(preset.name, "task-planner");
        assert!(preset.config.middleware.enable_planning);
        assert!(preset.config.middleware.enable_summarization);
        assert!(preset
            .suggested_tools
            .contains(&"spawn_subagent".to_string()));
        assert_eq!(preset.tags.get("tools"), Some(&"subagent".to_string()));
    }

    #[test]
    fn test_customer_support_preset() {
        let preset = customer_support();
        assert_eq!(preset.name, "customer-support");
        assert!(preset.config.middleware.enable_memory);
        assert_eq!(preset.config.model.temperature, Some(0.2));
        assert_eq!(preset.config.model.max_tokens, Some(2048));
        assert_eq!(preset.tags.get("memory"), Some(&"enabled".to_string()));
    }

    #[test]
    fn test_code_reviewer_preset() {
        let preset = code_reviewer();
        assert_eq!(preset.name, "code-reviewer");
        assert_eq!(preset.config.model.temperature, Some(0.1));
        assert!(preset.config.middleware.enable_filesystem);
        assert!(preset.suggested_tools.contains(&"grep".to_string()));
        assert!(preset.tags.contains_key("output"));
    }

    // -- PresetRegistry tests --

    #[test]
    fn test_registry_new_is_empty() {
        let registry = PresetRegistry::new();
        assert!(registry.list().is_empty());
    }

    #[test]
    fn test_registry_register_and_get() {
        let mut registry = PresetRegistry::new();
        registry.register(coding_assistant());
        let preset = registry.get("coding-assistant");
        assert!(preset.is_some());
        assert_eq!(preset.unwrap().name, "coding-assistant");
    }

    #[test]
    fn test_registry_get_nonexistent() {
        let registry = PresetRegistry::new();
        assert!(registry.get("nonexistent").is_none());
    }

    #[test]
    fn test_registry_default_has_all_builtins() {
        let registry = PresetRegistry::default();
        let names: Vec<&str> = registry.list().iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"coding-assistant"));
        assert!(names.contains(&"research-assistant"));
        assert!(names.contains(&"data-analyst"));
        assert!(names.contains(&"writing-assistant"));
        assert!(names.contains(&"task-planner"));
        assert!(names.contains(&"customer-support"));
        assert!(names.contains(&"code-reviewer"));
        assert_eq!(registry.list().len(), 7);
    }

    #[test]
    fn test_registry_list_by_tag() {
        let registry = PresetRegistry::default();
        let dev_presets = registry.list_by_tag("category");
        // All built-in presets have a "category" tag
        assert_eq!(dev_presets.len(), 7);
    }

    #[test]
    fn test_registry_list_by_tag_specific() {
        let registry = PresetRegistry::default();
        let memory_presets = registry.list_by_tag("memory");
        assert_eq!(memory_presets.len(), 1);
        assert_eq!(memory_presets[0].name, "customer-support");
    }

    #[test]
    fn test_registry_list_by_tag_no_match() {
        let registry = PresetRegistry::default();
        let presets = registry.list_by_tag("nonexistent-tag");
        assert!(presets.is_empty());
    }

    #[test]
    fn test_registry_register_replaces_duplicate() {
        let mut registry = PresetRegistry::new();
        let mut preset1 = coding_assistant();
        preset1.description = "first".to_string();
        registry.register(preset1);

        let mut preset2 = coding_assistant();
        preset2.description = "second".to_string();
        registry.register(preset2);

        assert_eq!(registry.list().len(), 1);
        assert_eq!(
            registry.get("coding-assistant").unwrap().description,
            "second"
        );
    }

    // -- PresetCustomizer tests --

    #[test]
    fn test_customizer_from_preset_clones() {
        let preset = coding_assistant();
        let customizer = PresetCustomizer::from_preset(&preset);
        let customized = customizer.build();
        assert_eq!(customized.name, preset.name);
        assert_eq!(customized.description, preset.description);
    }

    #[test]
    fn test_customizer_with_model() {
        let preset = coding_assistant();
        let customized = PresetCustomizer::from_preset(&preset)
            .with_model("openai", "gpt-4")
            .build();
        assert_eq!(customized.config.model.provider, "openai");
        assert_eq!(customized.config.model.model_name, "gpt-4");
    }

    #[test]
    fn test_customizer_with_temperature() {
        let preset = coding_assistant();
        let customized = PresetCustomizer::from_preset(&preset)
            .with_temperature(0.9)
            .build();
        assert_eq!(customized.config.model.temperature, Some(0.9));
    }

    #[test]
    fn test_customizer_with_tools() {
        let preset = coding_assistant();
        let tools = vec!["custom_tool".to_string()];
        let customized = PresetCustomizer::from_preset(&preset)
            .with_tools(tools.clone())
            .build();
        assert_eq!(customized.suggested_tools, tools);
        assert_eq!(customized.config.tools, tools);
    }

    #[test]
    fn test_customizer_with_system_prompt() {
        let preset = coding_assistant();
        let customized = PresetCustomizer::from_preset(&preset)
            .with_system_prompt("Be concise.")
            .build();
        assert_eq!(customized.system_prompt, "Be concise.");
        assert_eq!(
            customized.config.system_prompt,
            Some("Be concise.".to_string())
        );
    }

    #[test]
    fn test_customizer_chained() {
        let preset = writing_assistant();
        let customized = PresetCustomizer::from_preset(&preset)
            .with_model("anthropic", "claude-opus-4-20250514")
            .with_temperature(0.5)
            .with_system_prompt("Write poetry.")
            .with_tools(vec!["rhyme_finder".to_string()])
            .build();

        assert_eq!(customized.config.model.provider, "anthropic");
        assert_eq!(customized.config.model.model_name, "claude-opus-4-20250514");
        assert_eq!(customized.config.model.temperature, Some(0.5));
        assert_eq!(customized.system_prompt, "Write poetry.");
        assert_eq!(customized.suggested_tools, vec!["rhyme_finder"]);
    }

    #[test]
    fn test_customizer_does_not_mutate_original() {
        let preset = coding_assistant();
        let original_temp = preset.config.model.temperature;
        let _ = PresetCustomizer::from_preset(&preset)
            .with_temperature(0.99)
            .build();
        assert_eq!(preset.config.model.temperature, original_temp);
    }

    // -- Preset system prompt / config consistency --

    #[test]
    fn test_all_presets_have_system_prompt_in_config() {
        let presets = vec![
            coding_assistant(),
            research_assistant(),
            data_analyst(),
            writing_assistant(),
            task_planner(),
            customer_support(),
            code_reviewer(),
        ];
        for preset in &presets {
            assert!(
                preset.config.system_prompt.is_some(),
                "Preset '{}' should have a system prompt in its config",
                preset.name,
            );
            assert_eq!(
                preset.system_prompt,
                preset.config.system_prompt.clone().unwrap(),
                "Preset '{}' system_prompt field should match config.system_prompt",
                preset.name,
            );
        }
    }

    #[test]
    fn test_all_presets_have_nonempty_description() {
        let registry = PresetRegistry::default();
        for preset in registry.list() {
            assert!(
                !preset.description.is_empty(),
                "Preset '{}' should have a non-empty description",
                preset.name,
            );
        }
    }

    #[test]
    fn test_preset_config_name_matches_preset_name() {
        let registry = PresetRegistry::default();
        for preset in registry.list() {
            assert_eq!(
                preset.name, preset.config.name,
                "Preset name '{}' should match config name '{}'",
                preset.name, preset.config.name,
            );
        }
    }
}