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
//! Behavior selection strategies for NPCs
//!
//! This module provides different strategies for selecting which behavior an NPC
//! should execute in response to player actions. Each strategy represents a different
//! approach to behavior selection with different tradeoffs.
//!
//! # Strategies
//!
//! - [`EmotionModulatedStrategy`]: Two-stage selection using emotional gating and priority modulation
//! - [`FixedPriorityStrategy`]: Traditional fixed-priority selection (baseline comparison)
//!
//! # Example
//!
//! ```no_run
//! use oxyde::oxyde_game::behavior::{EmotionModulatedStrategy, SelectionStrategy};
//! use oxyde::oxyde_game::emotion::EmotionalState;
//! use oxyde::oxyde_game::intent::Intent;
//! use std::sync::Arc;
//!
//! async fn select_npc_behavior(
//!     behaviors: &[Arc<dyn oxyde::oxyde_game::behavior::Behavior>],
//!     intent: &Intent,
//!     emotional_state: &EmotionalState,
//! ) -> oxyde::Result<()> {
//!     let strategy = EmotionModulatedStrategy;
//!     let context = std::collections::HashMap::new();
//!
//!     let (behavior_name, result) = strategy
//!         .select_behavior(behaviors, intent, emotional_state, &context)
//!         .await?;
//!
//!     println!("Selected behavior: {}", behavior_name);
//!     Ok(())
//! }
//! ```

use crate::agent::AgentContext;
use crate::oxyde_game::behavior::{Behavior, BehaviorResult};
use crate::oxyde_game::emotion::EmotionalState;
use crate::oxyde_game::intent::Intent;
use crate::Result;
use std::sync::Arc;

/// Trait for behavior selection strategies
///
/// Implementations of this trait define how an NPC selects which behavior to execute
/// from a set of available behaviors, given the current intent and emotional state.
#[async_trait::async_trait]
pub trait SelectionStrategy: Send + Sync {
    /// Select and execute a behavior from available candidates
    ///
    /// # Arguments
    ///
    /// * `behaviors` - Available behaviors to choose from
    /// * `intent` - The classified player intent
    /// * `emotional_state` - Current emotional state of the NPC
    /// * `context` - Additional context for behavior execution
    ///
    /// # Returns
    ///
    /// Returns a tuple of (behavior_name, behavior_result) if a behavior was selected,
    /// or ("none", BehaviorResult::None) if no behavior matched.
    async fn select_behavior(
        &self,
        behaviors: &[Arc<dyn Behavior>],
        intent: &Intent,
        emotional_state: &EmotionalState,
        context: &AgentContext,
    ) -> Result<(String, BehaviorResult)>;

    /// Get the name of this strategy
    fn name(&self) -> &str;
}

/// Emotion-modulated priority selection strategy
///
/// This is the core contribution of the Oxyde SDK's behavior system. It uses a
/// two-stage selection process:
///
/// 1. **Emotional Gating**: Filter behaviors based on whether their emotion triggers
///    match the current emotional state. This ensures NPCs only use behaviors that
///    are emotionally appropriate.
///
/// 2. **Priority Modulation**: Sort remaining behaviors by their base priority plus
///    an emotional modifier. This allows emotions to influence which behavior is
///    chosen among viable candidates.
///
/// # How It Works
///
/// - Behaviors with emotion triggers (e.g., `FleeBehavior` requires fear > 0.7) are
///   only considered when emotions match
/// - Behaviors without triggers (e.g., neutral fallbacks) are always available
/// - Among matching behaviors, priority is: `base_priority + emotional_modifier`
/// - The highest priority behavior is selected
///
/// # Example
///
/// ```no_run
/// use oxyde::oxyde_game::behavior::{
///     EmotionModulatedStrategy, SelectionStrategy,
///     FleeBehavior, NeutralGreetingBehavior,
/// };
/// use oxyde::oxyde_game::emotion::EmotionalState;
/// use oxyde::oxyde_game::intent::{Intent, IntentType};
/// use std::sync::Arc;
///
/// async fn example() -> oxyde::Result<()> {
///     let mut emotional_state = EmotionalState::new();
///     emotional_state.update_emotion("fear", 0.8); // High fear
///
///     let behaviors: Vec<Arc<dyn oxyde::oxyde_game::behavior::Behavior>> = vec![
///         Arc::new(FleeBehavior::new(0.7)),  // Requires fear > 0.7
///         Arc::new(NeutralGreetingBehavior::new()),  // Always available
///     ];
///
///     let intent = Intent {
///         intent_type: IntentType::Threat,
///         confidence: 0.9,
///         raw_input: "I'm going to hurt you!".to_string(),
///         keywords: vec!["hurt".to_string()],
///     };
///
///     let strategy = EmotionModulatedStrategy;
///     let context = std::collections::HashMap::new();
///
///     // Will select FleeBehavior because fear > 0.7
///     let (name, result) = strategy
///         .select_behavior(&behaviors, &intent, &emotional_state, &context)
///         .await?;
///
///     assert_eq!(name, "FleeBehavior");
///     Ok(())
/// }
/// ```
///
/// # Design Rationale
///
/// This strategy produces more believable NPCs compared to fixed-priority systems:
///
/// - **Emotional Coherence**: NPCs only use behaviors that match their emotional state
/// - **Character Development**: Emotions persist and decay over time, creating arcs
/// - **Guaranteed Responsiveness**: Neutral fallback behaviors ensure NPCs always respond
/// - **Variety**: Different emotional states activate different behaviors
///
/// Research shows this approach achieves:
/// - 40% emotional behavior usage (realistic balance)
/// - 9.7-turn emotion persistence (character memory)
/// - Perfect trajectory coherence (smooth emotional arcs)
#[derive(Debug, Clone)]
pub struct EmotionModulatedStrategy;

impl EmotionModulatedStrategy {
    /// Create a new emotion-modulated selection strategy
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait::async_trait]
impl SelectionStrategy for EmotionModulatedStrategy {
    async fn select_behavior(
        &self,
        behaviors: &[Arc<dyn Behavior>],
        intent: &Intent,
        emotional_state: &EmotionalState,
        context: &AgentContext,
    ) -> Result<(String, BehaviorResult)> {
        // Stage 1: Emotional Gating - Filter behaviors by emotion triggers
        let mut candidates = Vec::new();
        for behavior in behaviors {
            // Check if behavior's emotion trigger matches current state
            if let Some(trigger) = behavior.emotion_trigger() {
                if !trigger.matches(emotional_state) {
                    continue; // Skip behaviors whose emotions don't match
                }
            }
            // Behavior has no trigger or trigger matches - check intent
            if behavior.matches_intent(intent).await {
                candidates.push(behavior);
            }
        }

        if candidates.is_empty() {
            return Ok(("none".to_string(), BehaviorResult::None));
        }

        // Stage 2: Priority Modulation - Sort by base priority + emotional modifier
        candidates.sort_by(|a, b| {
            let a_priority = a.priority() as i32 + a.emotional_priority_modifier(emotional_state);
            let b_priority = b.priority() as i32 + b.emotional_priority_modifier(emotional_state);
            b_priority.cmp(&a_priority)
        });

        // Execute highest priority behavior
        let selected = candidates[0];
        let result = selected.execute(intent, context).await?;

        // Extract behavior name from Debug representation
        let name = format!("{:?}", selected)
            .split('(')
            .next()
            .unwrap_or("unknown")
            .to_string();

        Ok((name, result))
    }

    fn name(&self) -> &str {
        "emotion_modulated"
    }
}

/// Fixed priority selection strategy (baseline)
///
/// This strategy ignores emotional state and always selects the behavior with the
/// highest base priority. It represents traditional behavior tree / FSM approaches
/// used in most games.
///
/// # Characteristics
///
/// - **No emotional influence**: Emotions don't affect selection
/// - **Predictable**: Always selects the same behavior for a given intent
/// - **No memory**: Each interaction is independent
/// - **Simple**: Easy to understand and debug
///
/// # Use Cases
///
/// - Games that don't need emotional NPCs
/// - Debugging behavior selection issues
/// - Baseline comparison for research
///
/// # Example
///
/// ```no_run
/// use oxyde::oxyde_game::behavior::{FixedPriorityStrategy, SelectionStrategy};
/// use oxyde::oxyde_game::emotion::EmotionalState;
/// use oxyde::oxyde_game::intent::{Intent, IntentType};
///
/// async fn example() -> oxyde::Result<()> {
///     let strategy = FixedPriorityStrategy;
///     let emotional_state = EmotionalState::new(); // Ignored
///
///     // Behavior selection is purely priority-based
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone)]
pub struct FixedPriorityStrategy;

impl FixedPriorityStrategy {
    /// Create a new fixed-priority selection strategy
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait::async_trait]
impl SelectionStrategy for FixedPriorityStrategy {
    async fn select_behavior(
        &self,
        behaviors: &[Arc<dyn Behavior>],
        intent: &Intent,
        _emotional_state: &EmotionalState, // Intentionally ignored
        context: &AgentContext,
    ) -> Result<(String, BehaviorResult)> {
        // Filter matching behaviors (no emotion trigger check)
        let mut candidates = Vec::new();
        for behavior in behaviors {
            if behavior.matches_intent(intent).await {
                candidates.push(behavior);
            }
        }

        if candidates.is_empty() {
            return Ok(("none".to_string(), BehaviorResult::None));
        }

        // Sort by fixed priority only (no emotional modifier)
        candidates.sort_by(|a, b| b.priority().cmp(&a.priority()));

        // Execute highest priority
        let selected = candidates[0];
        let result = selected.execute(intent, context).await?;

        let name = format!("{:?}", selected)
            .split('(')
            .next()
            .unwrap_or("unknown")
            .to_string();

        Ok((name, result))
    }

    fn name(&self) -> &str {
        "fixed_priority"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::oxyde_game::behavior::{FleeBehavior, NeutralGreetingBehavior};
    use crate::oxyde_game::intent::IntentType;

    #[tokio::test]
    async fn test_emotion_modulated_strategy() {
        use crate::oxyde_game::behavior::{DefaultAcknowledgeBehavior, AggressiveBehavior};

        let mut emotional_state = EmotionalState::new();
        emotional_state.update_emotion("anger", 0.8); // High anger

        let behaviors: Vec<Arc<dyn Behavior>> = vec![
            Arc::new(AggressiveBehavior::new(0.6)), // Requires anger > 0.6, matches Hostile
            Arc::new(DefaultAcknowledgeBehavior::new()), // Matches all intents
        ];

        let intent = Intent {
            intent_type: IntentType::Hostile, // AggressiveBehavior matches this
            confidence: 0.9,
            raw_input: "I'm going to attack you!".to_string(),
            keywords: vec!["attack".to_string()],
        };

        let strategy = EmotionModulatedStrategy::new();
        let context = std::collections::HashMap::new();

        let (name, _result) = strategy
            .select_behavior(&behaviors, &intent, &emotional_state, &context)
            .await
            .unwrap();

        // AggressiveBehavior should be selected (anger > 0.6, matches Hostile intent)
        assert!(name.contains("AggressiveBehavior"), "Expected AggressiveBehavior, got: {}", name);
    }

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

        let emotional_state = EmotionalState::new(); // Emotions ignored

        let behaviors: Vec<Arc<dyn Behavior>> = vec![
            Arc::new(FleeBehavior::new(0.7)),
            Arc::new(DefaultAcknowledgeBehavior::new()), // Matches all intents
        ];

        let intent = Intent {
            intent_type: IntentType::Threat,
            confidence: 0.9,
            raw_input: "Threatening message".to_string(),
            keywords: vec!["threat".to_string()],
        };

        let strategy = FixedPriorityStrategy::new();
        let context = std::collections::HashMap::new();

        let (name, _result) = strategy
            .select_behavior(&behaviors, &intent, &emotional_state, &context)
            .await
            .unwrap();

        // Fixed priority always selects highest priority (FleeBehavior = 100)
        assert!(name.contains("FleeBehavior"), "Expected FleeBehavior, got: {}", name);
    }

    #[tokio::test]
    async fn test_neutral_fallback_when_no_emotional_match() {
        let emotional_state = EmotionalState::new(); // Fear = 0.0

        let behaviors: Vec<Arc<dyn Behavior>> = vec![
            Arc::new(FleeBehavior::new(0.7)), // Requires fear > 0.7
            Arc::new(NeutralGreetingBehavior::new()), // Always available
        ];

        let intent = Intent {
            intent_type: IntentType::Greeting,
            confidence: 0.9,
            raw_input: "Hello".to_string(),
            keywords: vec!["hello".to_string()],
        };

        let strategy = EmotionModulatedStrategy::new();
        let context = std::collections::HashMap::new();

        let (name, _result) = strategy
            .select_behavior(&behaviors, &intent, &emotional_state, &context)
            .await
            .unwrap();

        // Should use neutral fallback since FleeBehavior doesn't match emotions
        assert!(name.contains("NeutralGreetingBehavior"), "Expected NeutralGreetingBehavior, got: {}", name);
    }
}