oxyde 0.1.10

AI Agent SDK for Game NPCs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! Agent module for the Oxyde SDK
//!
//! This module provides the core Agent type, which represents an AI-driven NPC
//! in a game environment. Agents have behaviors, memory, and can interact with players.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use regex::RegexSet;
use tokio::sync::RwLock;
use uuid::Uuid;

use crate::audio::{AudioData, TTSError, TTSService};
use crate::config::AgentConfig;
use crate::inference::InferenceEngine;
use crate::memory::{Memory, MemoryCategory, MemorySystem};
use crate::oxyde_game::behavior::{Behavior, BehaviorResult};
use crate::oxyde_game::emotion::EmotionalState;
use crate::oxyde_game::intent::Intent;
use crate::Result;

// Re-export AgentContext from oxyde-core so it's available as agent::AgentContext
pub use crate::AgentContext;

/// Callback for agent events
pub type AgentCallback = Box<dyn Fn(&Agent, &str) + Send + Sync>;

/// Wrapper for agent callbacks to make them Debug-able
pub struct CallbackWrapper(AgentCallback);

impl std::fmt::Debug for CallbackWrapper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("<AgentCallback>")
    }
}

impl CallbackWrapper {
    /// Create a new callback wrapper
    pub fn new(callback: AgentCallback) -> Self {
        Self(callback)
    }

    /// Call the underlying callback
    pub fn call(&self, agent: &Agent, data: &str) {
        (self.0)(agent, data);
    }
}

/// Agent state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentState {
    /// Agent is initializing
    Initializing,
    /// Agent is idle
    Idle,
    /// Agent is processing input
    Processing,
    /// Agent is generating a response
    Generating,
    /// Agent is executing a behavior
    Executing,
    /// Agent is paused
    Paused,
    /// Agent is stopped
    Stopped,
    /// Agent has encountered an error
    Error,
}

/// Agent event types for callbacks
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AgentEvent {
    /// Agent has started
    Start,
    /// Agent has stopped
    Stop,
    /// Agent is performing an action
    Action,
    /// Agent has generated a response
    Response,
    /// Agent state has changed
    StateChange,
    /// Agent encountered an error
    Error,
}

impl AgentEvent {
    /// Convert to string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Start => "start",
            Self::Stop => "stop",
            Self::Action => "action",
            Self::Response => "response",
            Self::StateChange => "state_change",
            Self::Error => "error",
        }
    }

    /// Convert from string representation
    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "start" => Some(Self::Start),
            "stop" => Some(Self::Stop),
            "action" => Some(Self::Action),
            "response" => Some(Self::Response),
            "state_change" | "statechange" => Some(Self::StateChange),
            "error" => Some(Self::Error),
            _ => None,
        }
    }
}

impl std::fmt::Display for AgentEvent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Agent represents an AI-powered NPC in a game
pub struct Agent {
    /// Unique identifier for the agent
    id: Uuid,

    /// Agent name
    name: String,

    /// Agent configuration
    config: AgentConfig,

    /// Current state of the agent
    state: RwLock<AgentState>,

    /// Inference engine for generating responses
    inference: Arc<InferenceEngine>,

    /// Memory system for storing and retrieving context
    memory: Arc<MemorySystem>,

    /// Context data (current environment state)
    context: RwLock<AgentContext>,

    /// Behaviors available to the agent
    behaviors: RwLock<Vec<Box<dyn Behavior>>>,

    /// TTS service for generating speech
    tts_service: Option<Arc<TTSService>>,

    /// Callbacks for agent events
    callbacks: Mutex<HashMap<String, Vec<CallbackWrapper>>>,

    /// Emotional state of the agent
    emotional_state: RwLock<EmotionalState>,

    /// Moderation patterns for content filtering
    moderation_patterns: Option<RegexSet>,
}

impl Agent {
    /// Create a new agent with the given configuration
    ///
    /// # Arguments
    ///
    /// * `config` - Agent configuration
    ///
    /// # Returns
    ///
    /// A new Agent instance
    pub fn new(config: AgentConfig) -> Self {
        let inference = Arc::new(InferenceEngine::new(&config.inference));
        let memory = Arc::new(MemorySystem::new(config.memory.clone()));

        // Load moderation patterns if enabled
        let moderation_patterns = if config.moderation.enabled {
            crate::utils::load_moderation_patterns("assets/badwords_regex.txt").ok()
        } else {
            None
        };

        Self {
            id: Uuid::new_v4(),
            name: config.agent.name.clone(),
            config,
            state: RwLock::new(AgentState::Initializing),
            inference,
            memory,
            tts_service: None, // TTS service is optional ..... REMOVE IF TTS WILL ALWAYS BE REQUIRED
            context: RwLock::new(HashMap::new()),
            behaviors: RwLock::new(Vec::new()),
            callbacks: Mutex::new(HashMap::new()),
            emotional_state: RwLock::new(EmotionalState::new()),
            moderation_patterns,
        }
    }

    /// Create a new agent with TTS service
    pub fn new_with_tts(config: AgentConfig) -> Self {
        let inference = Arc::new(InferenceEngine::new(&config.inference));
        let memory = Arc::new(MemorySystem::new(config.memory.clone()));

        let moderation_patterns = if config.moderation.enabled {
            crate::utils::load_moderation_patterns("assets/badwords_regex.txt").ok()
        } else {
            None
        };

        // Initialize TTS if configured
        let tts_service = config.tts.as_ref().map(|tts_config| {
            Arc::new(TTSService::new(
                tts_config.default_provider.clone(),
                tts_config.clone(),
            ))
        });

        Self {
            id: Uuid::new_v4(),
            name: config.agent.name.clone(),
            config,
            state: RwLock::new(AgentState::Initializing),
            inference,
            memory,
            tts_service, // Add TTS service field
            context: RwLock::new(HashMap::new()),
            behaviors: RwLock::new(Vec::new()),
            callbacks: Mutex::new(HashMap::new()),
            emotional_state: RwLock::new(EmotionalState::new()),
            moderation_patterns,
        }
    }

    /// Generate speech for agent response
    pub async fn speak(
        &self,
        text: &str,
        emotions: &EmotionalState,
        urgency: f32,
    ) -> Result<AudioData> {
        if let Some(tts) = &self.tts_service {
            tts.synthesize_npc_speech(&self.name, text, emotions, urgency)
                .await
                .map_err(|e| {
                    crate::OxydeError::AudioError(TTSError::AudioProcessingError(e.to_string()))
                })
        } else {
            Err(crate::OxydeError::ConfigurationError(
                "TTS not configured".to_string(),
            ))
        }
    }

    /// Get the agent's unique ID
    pub fn id(&self) -> Uuid {
        self.id
    }

    /// Get the agent's name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the agent's current state
    pub async fn state(&self) -> AgentState {
        *self.state.read().await
    }

    /// Get a copy of the agent's current emotional state
    pub async fn emotional_state(&self) -> EmotionalState {
        self.emotional_state.read().await.clone()
    }

    /// Get the agent's emotion vector as a float array
    pub async fn emotion_vector(&self) -> [f32; 8] {
        let emotion_state = self.emotional_state.read().await;
        emotion_state.as_vector()
    }

    /// Update a specific emotion by a delta value
    ///
    /// # Arguments
    ///
    /// * `emotion` - Name of the emotion to update (e.g., "joy", "fear")
    /// * `delta` - Amount to change the emotion by (-1.0 to 1.0)
    pub async fn update_emotion(&self, emotion: &str, delta: f32) {
        let mut state = self.emotional_state.write().await;
        state.update_emotion(emotion, delta);
    }

    /// Apply emotional decay to all emotions
    ///
    /// This should be called periodically (e.g., every frame or tick)
    /// to allow emotions to naturally fade over time
    pub async fn decay_emotions(&self) {
        let mut state = self.emotional_state.write().await;
        state.decay();
    }

    /// Get the current emotional valence (-1.0 to 1.0)
    ///
    /// Valence represents how positive or negative the agent feels
    pub async fn emotional_valence(&self) -> f32 {
        self.emotional_state.read().await.valence()
    }

    /// Get the current emotional arousal (0.0 to 1.0)
    ///
    /// Arousal represents how intensely the agent is feeling
    pub async fn emotional_arousal(&self) -> f32 {
        self.emotional_state.read().await.arousal()
    }

    /// Add a behavior to the agent
    ///
    /// # Arguments
    ///
    /// * `behavior` - A behavior to add to the agent
    pub async fn add_behavior<B: Behavior + 'static>(&self, behavior: B) {
        let mut behaviors = self.behaviors.write().await;
        behaviors.push(Box::new(behavior));
    }

    /// Add a boxed behavior to the agent
    ///
    /// # Arguments
    ///
    /// * `behavior` - A boxed behavior to add to the agent
    pub async fn add_boxed_behavior(&self, behavior: Box<dyn Behavior>) {
        let mut behaviors = self.behaviors.write().await;
        behaviors.push(behavior);
    }

    /// Update the agent's context with new data
    ///
    /// # Arguments
    ///
    /// * `context` - New context data to merge with existing context
    pub async fn update_context(&self, context: AgentContext) {
        let mut current_context = self.context.write().await;
        for (key, value) in context {
            current_context.insert(key, value);
        }
    }

    /// Start the agent
    ///
    /// This initializes the agent and prepares it for operation
    pub async fn start(&self) -> Result<()> {
        let mut state = self.state.write().await;
        *state = AgentState::Idle;
        log::info!("Agent {} started", self.name);

        // Initialize memory with agent's backstory and knowledge
        self.memory
            .add(Memory::new(
                MemoryCategory::Semantic,
                &serde_json::to_string(&self.config.agent.backstory)?,
                f64::INFINITY,
                None,
            ))
            .await?;

        self.trigger_event(AgentEvent::Start, "Agent started").await;

        Ok(())
    }

    /// Stop the agent
    pub async fn stop(&self) -> Result<()> {
        let mut state = self.state.write().await;
        *state = AgentState::Stopped;
        log::info!("Agent {} stopped", self.name);

        self.trigger_event(AgentEvent::Stop, "Agent stopped").await;

        Ok(())
    }

    /// Check if content should be moderated
    ///
    /// # Arguments
    ///
    /// * `input` - Content to check for inappropriate material
    ///
    /// # Returns
    ///
    /// `Some(response_message)` if content should be moderated, `None` if content is acceptable
    async fn check_moderation(&self, input: &str) -> Option<String> {
        if !self.config.moderation.enabled {
            return None;
        }

        // Quick regex check first (instant)
        let regex_flagged = if let Some(ref patterns) = self.moderation_patterns {
            patterns.is_match(&input.to_lowercase())
        } else {
            false
        };
        
        // If regex already flagged it, no need for cloud check - return immediately
        if regex_flagged {
            log::warn!("Agent {} moderated inappropriate content (regex): {}", self.name, input);
            return Some(self.config.moderation.response_message.clone());
        }
        
        // Only do cloud check if regex didn't catch it and cloud moderation is enabled
        if self.config.moderation.use_cloud_moderation {
            let api_key = self.config.moderation.cloud_moderation_api_key.clone()
                .or_else(|| self.config.inference.api_key.clone())
                .or_else(|| std::env::var("OPENAI_API_KEY").ok());
            
            if let Some(key) = api_key {
                match crate::utils::check_cloud_moderation(input, &key).await {
                    Ok(true) => {
                        log::warn!("Agent {} moderated inappropriate content (cloud): {}", self.name, input);
                        return Some(self.config.moderation.response_message.clone());
                    },
                    Ok(false) => {
                        // Content is clean, continue processing
                    },
                    Err(e) => {
                        log::warn!("Cloud moderation failed, continuing without it: {}", e);
                    }
                }
            }
        }

        None
    }

    /// Process player input and generate a response
    ///
    /// # Arguments
    ///
    /// * `input` - Player input to process
    ///
    /// # Returns
    ///
    /// A result containing the agent's response
    pub async fn process_input(&self, input: &str) -> Result<String> {
        {
            let mut state = self.state.write().await;
            *state = AgentState::Processing;
        }

        log::debug!("Agent {} processing input: {}", self.name, input);

        // Check for inappropriate content if moderation is enabled
        if let Some(moderation_response) = self.check_moderation(input).await {
            {
                let mut state = self.state.write().await;
                *state = AgentState::Idle;
            }
            self.trigger_callback("response", &moderation_response).await;
            return Ok(moderation_response);
        }

        // Analyze player intent
        let intent = Intent::analyze(input).await?;

        // Update memory with player input, capturing current emotional state
        let emotional_state = self.emotional_state.read().await;
        self.memory.add(Memory::new_emotional(
                MemoryCategory::Episodic,
                input,
                1.0,
                emotional_state.valence() as f64,
                emotional_state.arousal() as f64,
                None
            )).await?;

        // Find behaviors that match the intent
        let behaviors = self.behaviors.read().await;
        let mut response = String::new();

        {
            let mut state = self.state.write().await;
            *state = AgentState::Executing;
        }

        // Get current emotional state for behavior filtering and prioritization
        let current_emotional_state = self.emotional_state.read().await.clone();

        // Filter and sort behaviors by priority (considering emotional modifiers)
        let mut candidate_behaviors: Vec<_> = behaviors
            .iter()
            .filter(|b| {
                // Check if behavior's emotion trigger is satisfied
                if let Some(trigger) = b.emotion_trigger() {
                    trigger.matches(&current_emotional_state)
                } else {
                    true
                }
            })
            .collect();

        // Sort by priority (base + emotional modifier), highest first
        candidate_behaviors.sort_by(|a, b| {
            let a_priority = a.priority() as i32 + a.emotional_priority_modifier(&current_emotional_state);
            let b_priority = b.priority() as i32 + b.emotional_priority_modifier(&current_emotional_state);
            b_priority.cmp(&a_priority) // Descending order
        });

        // Execute matching behaviors in priority order
        for behavior in candidate_behaviors {
            if behavior.matches_intent(&intent).await {
                let context = self.context.read().await.clone();
                let behavior_result = behavior.execute(&intent, &context).await?;

                // Apply emotional influences from the behavior
                let influences = behavior.emotion_influences();
                if !influences.is_empty() {
                    let mut emotional_state = self.emotional_state.write().await;
                    for influence in influences {
                        emotional_state.update_emotion(&influence.emotion, influence.delta);
                    }
                }

                match behavior_result {
                    BehaviorResult::Response(text) => {
                        response = text;
                        break;
                    }
                    BehaviorResult::Action(action) => {
                        // Trigger action callback
                        self.trigger_event(AgentEvent::Action, &action).await;
                    },
                    BehaviorResult::None => {
                        // Continue to next behavior
                    }
                }
            }
        }

        // If no behavior provided a response, generate one with inference
        if response.is_empty() {
            {
                let mut state = self.state.write().await;
                *state = AgentState::Generating;
            }

            // Get relevant memories
            let memories = self.memory.retrieve_relevant(input, 5, None).await?;

            // Generate response using inference engine
            let context = self.context.read().await.clone();
            response = self
                .inference
                .generate_response(input, &memories, &context)
                .await?;

            // Store the response in memory with current emotional state
            let emotional_state = self.emotional_state.read().await;
            self.memory.add(Memory::new_emotional(
                MemoryCategory::Semantic,
                &response,
                1.0,
                emotional_state.valence() as f64,
                emotional_state.arousal() as f64,
                None
            )).await?;
        }

        {
            let mut state = self.state.write().await;
            *state = AgentState::Idle;
        }


        // Trigger response callback
        self.trigger_event(AgentEvent::Response, &response).await;

        Ok(response)
    }

    /// Register a callback for agent events using typed events
    ///
    /// # Arguments
    ///
    /// * `event` - Event type to trigger the callback
    /// * `callback` - Callback function
    ///
    /// # Example
    ///
    /// ```ignore
    /// agent.on_event(AgentEvent::Start, |agent, data| {
    ///     println!("Agent {} started: {}", agent.name(), data);
    /// });
    /// ```
    pub fn on_event<F>(&self, event: AgentEvent, callback: F)
    where
        F: Fn(&Agent, &str) + Send + Sync + 'static,
    {
        self.register_callback(event.as_str(), callback);
    }

    /// Register a callback for agent events (deprecated, use on_event)
    ///
    /// # Arguments
    ///
    /// * `event` - Event name to trigger the callback
    /// * `callback` - Callback function
    #[deprecated(since = "0.1.5", note = "Use on_event() with AgentEvent enum instead")]
    pub fn register_callback<F>(&self, event: &str, callback: F)
    where
        F: Fn(&Agent, &str) + Send + Sync + 'static,
    {
        // Lock the callbacks mutex, recovering from poison if necessary
        let mut callbacks = self.callbacks.lock().unwrap_or_else(|poisoned| {
            log::warn!("Callback mutex was poisoned, recovering");
            poisoned.into_inner()
        });
        let event_callbacks = callbacks.entry(event.to_string()).or_insert(Vec::new());
        event_callbacks.push(CallbackWrapper::new(Box::new(callback)));
    }

    /// Trigger a callback for a typed event
    ///
    /// # Arguments
    ///
    /// * `event` - Event type
    /// * `data` - Event data
    async fn trigger_event(&self, event: AgentEvent, data: &str) {
        self.trigger_callback(event.as_str(), data).await;
    }

    /// Trigger a callback for an event
    ///
    /// # Arguments
    ///
    /// * `event` - Event name
    /// * `data` - Event data
    async fn trigger_callback(&self, event: &str, data: &str) {
        // Lock the callbacks mutex, recovering from poison if necessary
        let callbacks = self.callbacks.lock().unwrap_or_else(|poisoned| {
            log::warn!("Callback mutex was poisoned during trigger, recovering");
            poisoned.into_inner()
        });
        if let Some(event_callbacks) = callbacks.get(event) {
            for callback in event_callbacks {
                callback.call(self, data);
            }
        }
    }


    /// Create a new agent with the same configuration but new state
    ///
    /// This is a simplified clone method that creates a new agent with the same
    /// configuration but with fresh state. This is useful for creating copies
    /// of agents for engine bindings.
    pub fn clone_for_binding(&self) -> Self {
        Self::new(self.config.clone())
    }

    // ==================== Memory System Wrapper Methods ====================
    // These methods provide direct access to the memory system for FFI bindings

    /// Add a memory to the agent's memory system
    pub async fn add_memory(
        &self,
        category: MemoryCategory,
        content: &str,
        importance: f64,
        tags: Option<Vec<String>>,
    ) -> Result<()> {
        self.memory.add(Memory::new(category, content, importance, tags)).await
    }

    /// Add a memory with emotional context to the agent's memory system
    pub async fn add_emotional_memory(
        &self,
        category: MemoryCategory,
        content: &str,
        importance: f64,
        valence: f64,
        intensity: f64,
        tags: Option<Vec<String>>,
    ) -> Result<()> {
        self.memory.add(Memory::new_emotional(
            category,
            content,
            importance,
            valence,
            intensity,
            tags
        )).await
    }

    /// Get the total number of memories stored
    pub async fn memory_count(&self) -> usize {
        self.memory.count().await
    }

    /// Clear all non-permanent memories
    pub async fn clear_memories(&self) -> usize {
        self.memory.clear().await
    }

    /// Get all memories of a specific category
    pub async fn get_memories_by_category(&self, category: MemoryCategory) -> Vec<Memory> {
        self.memory.get_by_category(category).await
    }

    /// Retrieve memories relevant to a query
    pub async fn retrieve_relevant_memories(&self, query: &str, limit: usize) -> Result<Vec<Memory>> {
        self.memory.retrieve_relevant(query, limit, None).await
    }

    /// Forget a specific memory by ID
    pub async fn forget_memory(&self, memory_id: &str) -> Result<()> {
        self.memory.forget(memory_id).await
    }

    /// Forget all memories of a specific category
    pub async fn forget_memories_by_category(&self, category: MemoryCategory) -> usize {
        self.memory.forget_by_category(category).await
    }

    /// Check if a memory exists by ID
    pub async fn has_memory(&self, memory_id: &str) -> bool {
        self.memory.get(memory_id).await.is_some()
    }

    /// Get a specific memory by ID
    pub async fn get_memory(&self, memory_id: &str) -> Option<Memory> {
        self.memory.get(memory_id).await
    }
}

impl std::fmt::Debug for Agent {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let callbacks_count = self.callbacks.lock()
            .map(|cb| cb.len())
            .unwrap_or(0);

        f.debug_struct("Agent")
            .field("id", &self.id)
            .field("name", &self.name)
            .field("config", &self.config)
            // Don't debug the behaviors or callbacks directly as they don't implement Debug
            .field("behaviors_count", &format!("<{} behaviors>", self.behaviors.try_read().map(|b| b.len()).unwrap_or(0)))
            .field("callbacks_count", &format!("<{} callback types>", callbacks_count))
            .finish()
    }
}

/// AgentBuilder for fluent construction of Agents
#[derive(Default)]
pub struct AgentBuilder {
    config: Option<AgentConfig>,
    behaviors: Vec<Box<dyn Behavior>>,
}

impl AgentBuilder {
    /// Create a new AgentBuilder
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the agent configuration
    pub fn with_config(mut self, config: AgentConfig) -> Self {
        self.config = Some(config);
        self
    }

    /// Add a behavior to the agent
    pub fn with_behavior<B: Behavior + 'static>(mut self, behavior: B) -> Self {
        self.behaviors.push(Box::new(behavior));
        self
    }

    /// Build the agent
    pub async fn build(self) -> Result<Agent> {
        let config = self.config.ok_or_else(|| {
            crate::OxydeError::ConfigurationError("Agent configuration is required".to_string())
        })?;

        // Validate the configuration before building
        config.validate()?;

        let agent = Agent::new(config);

        // Add all behaviors provided via the builder
        for behavior in self.behaviors {
            agent.add_boxed_behavior(behavior).await;
        }

        Ok(agent)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{AgentPersonality, InferenceConfig, MemoryConfig};

    #[tokio::test]
    async fn test_agent_creation() {
        let config = AgentConfig {
            agent: AgentPersonality {
                name: "Test Agent".to_string(),
                role: "Tester".to_string(),
                backstory: vec!["A test agent".to_string()],
                knowledge: vec!["Testing knowledge".to_string()],
            },
            memory: MemoryConfig::default(),
            inference: InferenceConfig::default(),
            behavior: HashMap::new(),
            tts: None, // No TTS for this test
            moderation: crate::config::ModerationConfig::default(),
        };

        let agent = Agent::new(config);
        assert_eq!(agent.name(), "Test Agent");

        agent.start().await.unwrap();
        assert_eq!(agent.state().await, AgentState::Idle);

        agent.stop().await.unwrap();
        assert_eq!(agent.state().await, AgentState::Stopped);
    }


    #[tokio::test]
    
    async fn test_agent_builder_with_behaviors() {
        use crate::oxyde_game::behavior::GreetingBehavior;

        let config = AgentConfig {
            agent: AgentPersonality {
                name: "Builder Test".to_string(),
                role: "Tester".to_string(),
                backstory: vec!["Built with builder".to_string()],
                knowledge: vec![],
            },
            memory: MemoryConfig::default(),
            inference: InferenceConfig::default(),
            behavior: HashMap::new(),
            moderation: crate::config::ModerationConfig::default(),
            tts: None, // No TTS for this test
        };

        // Create agent with builder and add behaviors
        let greeting1 = GreetingBehavior::new("Hello!");
        let greeting2 = GreetingBehavior::new("Greetings!");

        let agent = AgentBuilder::new()
            .with_config(config)
            .with_behavior(greeting1)
            .with_behavior(greeting2)
            .build()
            .await
            .unwrap();

        assert_eq!(agent.name(), "Builder Test");

        // Verify behaviors were added (check the count)
        let behaviors = agent.behaviors.read().await;
        assert_eq!(behaviors.len(), 2, "Builder should add all provided behaviors");
    }

    #[tokio::test]
    async fn test_agent_builder_without_config_fails() {
        use crate::oxyde_game::behavior::GreetingBehavior;

        let greeting = GreetingBehavior::new("Hello!");

        // Attempt to build without config should fail
        let result = AgentBuilder::new()
            .with_behavior(greeting)
            .build()
            .await;

        assert!(result.is_err(), "Building without config should fail");
        if let Err(crate::OxydeError::ConfigurationError(msg)) = result {
            assert!(msg.contains("required"), "Error should mention config is required");
        } else {
            panic!("Expected ConfigurationError");
        }
    }

    #[tokio::test]
    async fn test_content_moderation() {
        let config = AgentConfig {
            agent: AgentPersonality {
                name: "Test Agent".to_string(),
                role: "Tester".to_string(),
                backstory: vec!["A test agent".to_string()],
                knowledge: vec!["Testing knowledge".to_string()],
            },
            memory: MemoryConfig::default(),
            inference: InferenceConfig::default(),
            behavior: HashMap::new(),
            moderation: crate::config::ModerationConfig {
                enabled: true,
                response_message: "Sorry, I can't respond to that.".to_string(),
                use_cloud_moderation: false,
                cloud_moderation_api_key: None,
            },
            tts: None, // No TTS for this test
        };

        let agent = Agent::new(config);
        agent.start().await.unwrap();

        // Test that bad words trigger moderation response
        let response = agent.process_input("Fuck you").await.unwrap();
        assert_eq!(response, "Sorry, I can't respond to that.");
    }
}