zoey-core 0.1.1

ZoeyAI core runtime and types — privacy-first AI agent framework optimized for local models
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
//! Character configuration loader from XML files

use crate::types::*;
use crate::Result;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Load character from XML file
pub fn load_character_from_xml(path: impl AsRef<Path>) -> Result<Character> {
    let xml_content = fs::read_to_string(path)
        .map_err(|e| crate::ZoeyError::config(format!("Failed to read character file: {}", e)))?;

    parse_character_xml(&xml_content)
}

/// Parse character from XML string
pub fn parse_character_xml(xml: &str) -> Result<Character> {
    // Simple XML parsing (in production, would use xml-rs or quick-xml crate)
    let mut character = Character::default();

    // Extract name
    if let Some(name) = extract_tag_content(xml, "name") {
        character.name = name;
    }

    // Extract username
    if let Some(username) = extract_tag_content(xml, "username") {
        character.username = Some(username);
    }

    // Extract bio entries
    character.bio = extract_multiple_tags(xml, "bio", "entry");

    // Extract lore entries
    character.lore = extract_multiple_tags(xml, "lore", "entry");

    // Extract knowledge entries
    character.knowledge = extract_multiple_tags(xml, "knowledge", "entry");

    // Extract adjectives
    character.adjectives = extract_multiple_tags(xml, "adjectives", "adjective");

    // Extract topics
    character.topics = extract_multiple_tags(xml, "topics", "topic");

    // Extract style
    character.style = CharacterStyle {
        all: extract_multiple_tags(xml, "style/all", "guideline"),
        chat: extract_multiple_tags(xml, "style/chat", "guideline"),
        post: extract_multiple_tags(xml, "style/post", "guideline"),
    };

    // Extract post examples
    character.post_examples = extract_multiple_tags(xml, "postExamples", "post");

    // Extract message examples (conversations)
    character.message_examples = extract_message_examples(xml);

    // Extract plugins
    character.plugins = extract_multiple_tags(xml, "plugins", "plugin");

    // Extract clients
    character.clients = extract_multiple_tags(xml, "clients", "client");

    // Extract templates
    if let Some(templates_section) = extract_section(xml, "templates") {
        let mut templates = CharacterTemplates {
            message_handler_template: None,
            post_creation_template: None,
            custom: HashMap::new(),
        };

        if let Some(msg_handler) = extract_cdata_content(&templates_section, "messageHandler") {
            templates.message_handler_template = Some(msg_handler);
        }

        if let Some(post_creation) = extract_cdata_content(&templates_section, "postCreation") {
            templates.post_creation_template = Some(post_creation);
        }

        if let Some(should_respond) = extract_cdata_content(&templates_section, "shouldRespond") {
            templates
                .custom
                .insert("shouldRespond".to_string(), should_respond);
        }

        character.templates = Some(templates);
    }

    // Extract settings
    let mut settings = HashMap::new();
    if let Some(settings_section) = extract_section(xml, "settings") {
        // Model provider
        if let Some(model_provider) = extract_tag_content(&settings_section, "model_provider") {
            settings.insert(
                "model_provider".to_string(),
                serde_json::json!(model_provider),
            );
            settings.insert(
                "MODEL_PROVIDER".to_string(),
                serde_json::json!(model_provider),
            );
            character.model_provider = Some(model_provider.clone());
        }

        // OpenAI settings
        if let Some(openai_model) = extract_tag_content(&settings_section, "openai_model") {
            settings.insert("OPENAI_MODEL".to_string(), serde_json::json!(openai_model));
            settings.insert("openai_model".to_string(), serde_json::json!(openai_model));
        }
        if let Some(openai_key) = extract_tag_content(&settings_section, "openai_api_key") {
            if !openai_key.is_empty() {
                settings.insert("OPENAI_API_KEY".to_string(), serde_json::json!(openai_key));
            }
        }

        // Anthropic settings
        if let Some(anthropic_model) = extract_tag_content(&settings_section, "anthropic_model") {
            settings.insert(
                "ANTHROPIC_MODEL".to_string(),
                serde_json::json!(anthropic_model),
            );
        }
        if let Some(anthropic_key) = extract_tag_content(&settings_section, "anthropic_api_key") {
            if !anthropic_key.is_empty() {
                settings.insert(
                    "ANTHROPIC_API_KEY".to_string(),
                    serde_json::json!(anthropic_key),
                );
            }
        }

        // Local LLM settings
        if let Some(local_model) = extract_tag_content(&settings_section, "local_llm_model") {
            settings.insert(
                "LOCAL_LLM_MODEL".to_string(),
                serde_json::json!(local_model),
            );
            settings.insert(
                "local_llm_model".to_string(),
                serde_json::json!(local_model),
            );
        }
        if let Some(endpoint) = extract_tag_content(&settings_section, "local_llm_endpoint") {
            settings.insert(
                "LOCAL_LLM_ENDPOINT".to_string(),
                serde_json::json!(endpoint),
            );
        }

        // Fallback provider
        if let Some(fallback) = extract_tag_content(&settings_section, "fallback_provider") {
            settings.insert("fallback_provider".to_string(), serde_json::json!(fallback));
        }

        // General settings
        if let Some(temp) = extract_tag_content(&settings_section, "temperature") {
            if let Ok(temp_f) = temp.parse::<f32>() {
                settings.insert("temperature".to_string(), serde_json::json!(temp_f));
            }
        }
        if let Some(max_tokens) = extract_tag_content(&settings_section, "max_tokens") {
            if let Ok(tokens) = max_tokens.parse::<usize>() {
                settings.insert("max_tokens".to_string(), serde_json::json!(tokens));
            }
        }

        // Training settings
        if let Some(training_enabled) = extract_tag_content(&settings_section, "training_enabled") {
            if let Ok(enabled) = training_enabled.parse::<bool>() {
                settings.insert("training_enabled".to_string(), serde_json::json!(enabled));
            }
        }
        if let Some(min_quality) = extract_tag_content(&settings_section, "training_min_quality") {
            if let Ok(quality) = min_quality.parse::<f32>() {
                settings.insert(
                    "training_min_quality".to_string(),
                    serde_json::json!(quality),
                );
            }
        }
        if let Some(rlhf) = extract_tag_content(&settings_section, "rlhf_enabled") {
            if let Ok(enabled) = rlhf.parse::<bool>() {
                settings.insert("rlhf_enabled".to_string(), serde_json::json!(enabled));
            }
        }
        
        // Training backend settings (for MCP auto-configuration)
        if let Some(training_backend) = extract_tag_content(&settings_section, "training_backend") {
            settings.insert("training_backend".to_string(), serde_json::json!(training_backend));
        }
        if let Some(base_model) = extract_tag_content(&settings_section, "training_base_model") {
            settings.insert("training_base_model".to_string(), serde_json::json!(base_model));
        }
        if let Some(method) = extract_tag_content(&settings_section, "training_method") {
            settings.insert("training_method".to_string(), serde_json::json!(method));
        }
        if let Some(use_gpu) = extract_tag_content(&settings_section, "training_use_gpu") {
            if let Ok(gpu) = use_gpu.parse::<bool>() {
                settings.insert("training_use_gpu".to_string(), serde_json::json!(gpu));
            }
        }
        if let Some(output_dir) = extract_tag_content(&settings_section, "training_output_dir") {
            settings.insert("training_output_dir".to_string(), serde_json::json!(output_dir));
        }
        if let Some(num_epochs) = extract_tag_content(&settings_section, "training_num_epochs") {
            if let Ok(epochs) = num_epochs.parse::<u32>() {
                settings.insert("training_num_epochs".to_string(), serde_json::json!(epochs));
            }
        }
        if let Some(lora_rank) = extract_tag_content(&settings_section, "training_lora_rank") {
            if let Ok(rank) = lora_rank.parse::<u32>() {
                settings.insert("training_lora_rank".to_string(), serde_json::json!(rank));
            }
        }

        // Dynamic prompt settings
        if let Some(max_entries) =
            extract_tag_content(&settings_section, "dynamic_prompt_max_entries")
        {
            if let Ok(entries) = max_entries.parse::<usize>() {
                settings.insert(
                    "dynamic_prompt_max_entries".to_string(),
                    serde_json::json!(entries),
                );
            }
        }
        if let Some(validation) = extract_tag_content(&settings_section, "validation_level") {
            settings.insert(
                "validation_level".to_string(),
                serde_json::json!(validation),
            );
        }

        // Cache settings
        if let Some(cache_ttl) = extract_tag_content(&settings_section, "entity_cache_ttl") {
            if let Ok(ttl) = cache_ttl.parse::<u64>() {
                settings.insert("entity_cache_ttl".to_string(), serde_json::json!(ttl));
            }
        }

        // Performance settings
        if let Some(conv_len) = extract_tag_content(&settings_section, "conversation_length") {
            if let Ok(len) = conv_len.parse::<usize>() {
                settings.insert("conversation_length".to_string(), serde_json::json!(len));
            }
        }
        if let Some(retries) = extract_tag_content(&settings_section, "max_retries") {
            if let Ok(r) = retries.parse::<usize>() {
                settings.insert("max_retries".to_string(), serde_json::json!(r));
            }
        }
    }

    // Extract storage configuration
    if let Some(storage_section) = extract_section(xml, "storage") {
        if let Some(adapter) = extract_tag_content(&storage_section, "adapter") {
            if let Ok(storage_type) = adapter.parse::<StorageType>() {
                character.storage.adapter = storage_type;
            }
        }
        if let Some(url) = extract_tag_content(&storage_section, "url") {
            // Support environment variable substitution for sensitive data
            let resolved_url = if url.starts_with("${") && url.ends_with("}") {
                let env_var = &url[2..url.len() - 1];
                std::env::var(env_var).ok()
            } else {
                Some(url)
            };
            character.storage.url = resolved_url;
        }
        if let Some(database) = extract_tag_content(&storage_section, "database") {
            character.storage.database = Some(database);
        }
        if let Some(api_key) = extract_tag_content(&storage_section, "api_key") {
            // Support environment variable substitution
            let resolved_key = if api_key.starts_with("${") && api_key.ends_with("}") {
                let env_var = &api_key[2..api_key.len() - 1];
                std::env::var(env_var).ok()
            } else {
                Some(api_key)
            };
            character.storage.api_key = resolved_key;
        }
        if let Some(dim) = extract_tag_content(&storage_section, "embedding_dimension") {
            if let Ok(d) = dim.parse::<usize>() {
                character.storage.embedding_dimension = Some(d);
            }
        }
    }

    // Extract voice configuration as a nested object
    if let Some(voice_section) = extract_section(xml, "voice") {
        let mut voice_config = serde_json::Map::new();

        // Core settings
        if let Some(enabled) = extract_tag_content(&voice_section, "enabled") {
            voice_config.insert("enabled".to_string(), serde_json::json!(enabled == "true"));
        }
        if let Some(engine) = extract_tag_content(&voice_section, "engine") {
            voice_config.insert("engine".to_string(), serde_json::json!(engine));
        }
        if let Some(model) = extract_tag_content(&voice_section, "model") {
            voice_config.insert("model".to_string(), serde_json::json!(model));
        }
        if let Some(voice_id) = extract_tag_content(&voice_section, "voice_id") {
            voice_config.insert("voice_id".to_string(), serde_json::json!(voice_id));
        }
        if let Some(voice_name) = extract_tag_content(&voice_section, "voice_name") {
            voice_config.insert("voice_name".to_string(), serde_json::json!(voice_name));
        }
        if let Some(speed) = extract_tag_content(&voice_section, "speed") {
            if let Ok(s) = speed.parse::<f32>() {
                voice_config.insert("speed".to_string(), serde_json::json!(s));
            }
        }
        if let Some(output_format) = extract_tag_content(&voice_section, "output_format") {
            voice_config.insert(
                "output_format".to_string(),
                serde_json::json!(output_format),
            );
        }
        if let Some(sample_rate) = extract_tag_content(&voice_section, "sample_rate") {
            if let Ok(sr) = sample_rate.parse::<u32>() {
                voice_config.insert("sample_rate".to_string(), serde_json::json!(sr));
            }
        }
        if let Some(streaming) = extract_tag_content(&voice_section, "streaming") {
            voice_config.insert(
                "streaming".to_string(),
                serde_json::json!(streaming == "true"),
            );
        }

        // Triggers
        if let Some(triggers_section) = extract_section(&voice_section, "triggers") {
            let mut triggers = Vec::new();
            let mut pos = 0;
            while let Some(start) = triggers_section[pos..].find("<trigger>") {
                let content_start = pos + start + "<trigger>".len();
                if let Some(end) = triggers_section[content_start..].find("</trigger>") {
                    let trigger = triggers_section[content_start..content_start + end].trim();
                    if !trigger.is_empty() {
                        triggers.push(serde_json::json!(trigger));
                    }
                    pos = content_start + end + "</trigger>".len();
                } else {
                    break;
                }
            }
            if !triggers.is_empty() {
                voice_config.insert("triggers".to_string(), serde_json::Value::Array(triggers));
            }
        }

        // Discord-specific settings
        if let Some(discord_section) = extract_section(&voice_section, "discord") {
            let mut discord = serde_json::Map::new();
            if let Some(auto_join) = extract_tag_content(&discord_section, "auto_join_voice") {
                discord.insert(
                    "auto_join_voice".to_string(),
                    serde_json::json!(auto_join == "true"),
                );
            }
            if let Some(leave_alone) = extract_tag_content(&discord_section, "leave_when_alone") {
                discord.insert(
                    "leave_when_alone".to_string(),
                    serde_json::json!(leave_alone == "true"),
                );
            }
            if let Some(timeout) = extract_tag_content(&discord_section, "idle_timeout_seconds") {
                if let Ok(t) = timeout.parse::<u64>() {
                    discord.insert("idle_timeout_seconds".to_string(), serde_json::json!(t));
                }
            }
            if let Some(speak) = extract_tag_content(&discord_section, "speak_responses") {
                discord.insert(
                    "speak_responses".to_string(),
                    serde_json::json!(speak == "true"),
                );
            }
            if let Some(listen) = extract_tag_content(&discord_section, "listen_enabled") {
                discord.insert(
                    "listen_enabled".to_string(),
                    serde_json::json!(listen == "true"),
                );
            }
            voice_config.insert("discord".to_string(), serde_json::Value::Object(discord));
        }

        settings.insert("voice".to_string(), serde_json::Value::Object(voice_config));
    }

    character.settings = settings;

    Ok(character)
}

/// Extract message examples (conversations)
fn extract_message_examples(xml: &str) -> Vec<Vec<MessageExample>> {
    let mut conversations = Vec::new();

    if let Some(section) = extract_section(xml, "messageExamples") {
        // Extract all conversation blocks
        let mut search_pos = 0;
        while let Some(start) = section[search_pos..].find("<conversation>") {
            let actual_start = search_pos + start;
            if let Some(end) = section[actual_start..].find("</conversation>") {
                let content_start = actual_start + "<conversation>".len();
                let content_end = actual_start + end;

                if content_start < content_end {
                    let conv_section = &section[content_start..content_end];

                    // Extract messages in this conversation
                    let mut messages = Vec::new();
                    let mut msg_pos = 0;

                    while let Some(msg_start) = conv_section[msg_pos..].find("<message ") {
                        let msg_actual_start = msg_pos + msg_start;
                        if let Some(msg_end) = conv_section[msg_actual_start..].find("/>") {
                            let msg_tag =
                                &conv_section[msg_actual_start..msg_actual_start + msg_end + 2];

                            // Parse name and text attributes
                            if let (Some(name), Some(text)) = (
                                extract_attribute(msg_tag, "name"),
                                extract_attribute(msg_tag, "text"),
                            ) {
                                messages.push(MessageExample { name, text });
                            }

                            msg_pos = msg_actual_start + msg_end + 2;
                        } else {
                            break;
                        }
                    }

                    if !messages.is_empty() {
                        conversations.push(messages);
                    }
                }

                search_pos = content_end + "</conversation>".len();
            } else {
                break;
            }
        }
    }

    conversations
}

/// Extract attribute value from XML tag
fn extract_attribute(tag: &str, attr: &str) -> Option<String> {
    let pattern = format!(r#"{}=""#, attr);
    if let Some(start) = tag.find(&pattern) {
        let value_start = start + pattern.len();
        if let Some(quote_end) = tag[value_start..].find('"') {
            return Some(tag[value_start..value_start + quote_end].to_string());
        }
    }
    None
}

/// Extract CDATA content from XML tag
fn extract_cdata_content(xml: &str, tag: &str) -> Option<String> {
    let start_tag = format!("<{}>", tag);
    let end_tag = format!("</{}>", tag);

    if let Some(start) = xml.find(&start_tag) {
        if let Some(end) = xml.find(&end_tag) {
            let content_start = start + start_tag.len();
            if content_start < end {
                let content = xml[content_start..end].trim();

                // Check for CDATA
                if content.starts_with("<![CDATA[") && content.ends_with("]]>") {
                    return Some(content[9..content.len() - 3].trim().to_string());
                }

                // Return regular content
                return Some(content.to_string());
            }
        }
    }

    None
}

/// Extract content from a simple XML tag
fn extract_tag_content(xml: &str, tag: &str) -> Option<String> {
    let start_tag = format!("<{}>", tag);
    let end_tag = format!("</{}>", tag);

    if let Some(start) = xml.find(&start_tag) {
        if let Some(end) = xml.find(&end_tag) {
            let content_start = start + start_tag.len();
            if content_start < end {
                return Some(xml[content_start..end].trim().to_string());
            }
        }
    }

    None
}

/// Extract multiple entries from a parent tag
fn extract_multiple_tags(xml: &str, parent_tag: &str, child_tag: &str) -> Vec<String> {
    let mut results = Vec::new();

    // Extract parent section first
    if let Some(section) = extract_section(xml, parent_tag) {
        let start_tag = format!("<{}>", child_tag);
        let end_tag = format!("</{}>", child_tag);

        let mut search_pos = 0;
        while let Some(start) = section[search_pos..].find(&start_tag) {
            let actual_start = search_pos + start;
            if let Some(end) = section[actual_start..].find(&end_tag) {
                let content_start = actual_start + start_tag.len();
                let content_end = actual_start + end;

                if content_start < content_end {
                    results.push(section[content_start..content_end].trim().to_string());
                }

                search_pos = content_end + end_tag.len();
            } else {
                break;
            }
        }
    }

    results
}

/// Extract a section between opening and closing tags
fn extract_section(xml: &str, tag: &str) -> Option<String> {
    // Handle nested tags like "style/all"
    let parts: Vec<&str> = tag.split('/').collect();

    if parts.len() == 1 {
        let start_tag = format!("<{}>", tag);
        let end_tag = format!("</{}>", tag);

        if let Some(start) = xml.find(&start_tag) {
            if let Some(end) = xml.find(&end_tag) {
                let content_start = start + start_tag.len();
                if content_start < end {
                    return Some(xml[content_start..end].to_string());
                }
            }
        }
    } else {
        // Handle nested tags
        let mut current_section = xml.to_string();
        for part in parts {
            if let Some(section) = extract_section(&current_section, part) {
                current_section = section;
            } else {
                return None;
            }
        }
        return Some(current_section);
    }

    None
}

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

    #[test]
    fn test_extract_tag_content() {
        let xml = "<name>TestBot</name>";
        assert_eq!(
            extract_tag_content(xml, "name"),
            Some("TestBot".to_string())
        );
    }

    #[test]
    fn test_extract_multiple_tags() {
        let xml = r#"<bio><entry>First</entry><entry>Second</entry></bio>"#;
        let entries = extract_multiple_tags(xml, "bio", "entry");
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0], "First");
        assert_eq!(entries[1], "Second");
    }

    #[test]
    fn test_parse_character() {
        let xml = r#"
        <character>
            <name>TestBot</name>
            <username>testbot</username>
            <bio>
                <entry>I am a test bot</entry>
            </bio>
        </character>
        "#;

        let character = parse_character_xml(xml).unwrap();
        assert_eq!(character.name, "TestBot");
        assert_eq!(character.username, Some("testbot".to_string()));
        assert_eq!(character.bio.len(), 1);
    }

    #[test]
    fn test_parse_complete_character() {
        let xml = r#"
        <character>
            <name>TestBot</name>
            <username>testbot</username>
            <bio>
                <entry>First bio</entry>
                <entry>Second bio</entry>
            </bio>
            <lore>
                <entry>Origin story</entry>
            </lore>
            <knowledge>
                <entry>Fact 1</entry>
                <entry>Fact 2</entry>
            </knowledge>
            <postExamples>
                <post>Example post 1</post>
                <post>Example post 2</post>
            </postExamples>
            <messageExamples>
                <conversation>
                    <message name="User" text="Hello" />
                    <message name="Bot" text="Hi there!" />
                </conversation>
            </messageExamples>
            <topics>
                <topic>AI</topic>
                <topic>Rust</topic>
            </topics>
            <adjectives>
                <adjective>helpful</adjective>
                <adjective>smart</adjective>
            </adjectives>
            <style>
                <all>
                    <guideline>Be helpful</guideline>
                </all>
                <chat>
                    <guideline>Be friendly</guideline>
                </chat>
                <post>
                    <guideline>Be concise</guideline>
                </post>
            </style>
            <plugins>
                <plugin>plugin1</plugin>
                <plugin>plugin2</plugin>
            </plugins>
            <clients>
                <client>terminal</client>
            </clients>
            <templates>
                <messageHandler><![CDATA[Template content here]]></messageHandler>
                <postCreation><![CDATA[Post template]]></postCreation>
            </templates>
            <settings>
                <model_provider>openai</model_provider>
                <temperature>0.7</temperature>
                <max_tokens>1000</max_tokens>
            </settings>
        </character>
        "#;

        let character = parse_character_xml(xml).unwrap();

        // Verify all fields
        assert_eq!(character.name, "TestBot");
        assert_eq!(character.username, Some("testbot".to_string()));
        assert_eq!(character.bio.len(), 2);
        assert_eq!(character.lore.len(), 1);
        assert_eq!(character.knowledge.len(), 2);
        assert_eq!(character.post_examples.len(), 2);
        assert_eq!(character.message_examples.len(), 1);
        assert_eq!(character.message_examples[0].len(), 2);
        assert_eq!(character.topics.len(), 2);
        assert_eq!(character.adjectives.len(), 2);
        assert_eq!(character.plugins.len(), 2);
        assert_eq!(character.clients.len(), 1);
        assert_eq!(character.style.all.len(), 1);
        assert_eq!(character.style.chat.len(), 1);
        assert_eq!(character.style.post.len(), 1);
        assert!(character.templates.is_some());
        assert_eq!(character.model_provider, Some("openai".to_string()));
        assert!(character.settings.contains_key("temperature"));
    }

    #[test]
    fn test_extract_attribute() {
        let tag = r#"<message name="User" text="Hello world" />"#;
        assert_eq!(extract_attribute(tag, "name"), Some("User".to_string()));
        assert_eq!(
            extract_attribute(tag, "text"),
            Some("Hello world".to_string())
        );
        assert_eq!(extract_attribute(tag, "missing"), None);
    }

    #[test]
    fn test_extract_cdata() {
        let xml = "<template><![CDATA[Content here]]></template>";
        let content = extract_cdata_content(xml, "template");
        assert_eq!(content, Some("Content here".to_string()));
    }
}