use crate::cognition::detector::extract_topics;
use crate::cognition::emotional::compute_arousal;
use crate::math::clamp;
use crate::types::belief::*;
pub fn update_affect(state: &SharedBeliefState, message: &str) -> AffectState {
let result = compute_arousal(message);
let certainty = if result.arousal >= 0.6 {
HIGH_AROUSAL_CERTAINTY
} else if result.arousal >= 0.3 {
(LOW_AROUSAL_CERTAINTY + HIGH_AROUSAL_CERTAINTY) / 2.0
} else {
LOW_AROUSAL_CERTAINTY
};
let sustained_delta = if result.arousal >= 0.6 {
-0.10
} else if result.arousal >= 0.3 {
-0.05
} else {
0.05
};
let sustained = clamp(state.affect.sustained + sustained_delta, 0.0, 1.0);
AffectState {
arousal: result.arousal,
valence: result.valence,
certainty,
sustained,
}
}
pub fn update_predictions(response_content: &str) -> Predictions {
let topics = extract_topics(response_content);
let next_topics = topics
.into_iter()
.take(PREDICTION_TOPIC_COUNT)
.collect();
Predictions { next_topics }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn update_affect_neutral() {
let state = SharedBeliefState::new("test".into());
let affect = update_affect(&state, "Hello world");
assert!(affect.arousal < 0.3);
assert_eq!(affect.valence, AffectValence::Neutral);
}
#[test]
fn update_affect_high_arousal() {
let state = SharedBeliefState::new("test".into());
let affect = update_affect(&state, "This is TERRIBLE and frustrating!!!");
assert!(affect.arousal > 0.3);
assert_eq!(affect.certainty, HIGH_AROUSAL_CERTAINTY);
}
#[test]
fn predictions_from_response() {
let preds = update_predictions("Here is the solution using Rust and tokio runtime");
assert!(!preds.next_topics.is_empty());
}
}