Complete API documentation for the Aifeels library.
Main class for managing emotional state.
from aifeels import EmotionalState
state = EmotionalState(
frustration=0.0, # Optional, default: 0.0
trust=0.0, # Optional, default: 0.0
urgency=0.0, # Optional, default: 0.0
stress=0.0, # Optional, default: 0.0
caution=0.0 # Optional, default: 0.0
)| Attribute | Type | Range | Description |
|---|---|---|---|
frustration |
float | 0.0-1.0 | Accumulated setbacks and failures |
trust |
float | 0.0-1.0 | Confidence in own capabilities |
urgency |
float | 0.0-1.0 | Time pressure and priority |
stress |
float | 0.0-1.0 | Cognitive load and resource constraints |
caution |
float | 0.0-1.0 | Need for verification and care |
last_update_time |
float | - | Unix timestamp of last update |
history |
List[StateTransition] | - | List of state transitions |
Process an event and update emotional state.
Parameters:
event(Event): The event to processapply_decay(bool): Whether to apply decay before processing (default: True)
Example:
from aifeels.events import StandardEvents
state.process_event(StandardEvents.TASK_FAILED)Get current emotional state as a dictionary.
Parameters:
apply_decay(bool): Whether to apply decay before returning (default: True)
Returns:
- Dictionary mapping emotion names to current values
Example:
current = state.get_current_state()
# {'frustration': 0.3, 'trust': 0.0, 'urgency': 0.0, 'stress': 0.1, 'caution': 0.0}Get recommended action based on current state.
Parameters:
apply_decay(bool): Whether to apply decay before selecting action (default: True)
Returns:
- Action string:
'escalate_to_human','request_confirmation','increase_caution', or'continue_autonomy'
Example:
action = state.recommended_action()
if action == 'escalate_to_human':
# Stop and get human help
passGet detailed action recommendation with reasoning.
Parameters:
apply_decay(bool): Whether to apply decay before selecting action (default: True)
Returns:
- ActionRecommendation object with action, reason, and triggered_by
Example:
rec = state.get_action_recommendation()
print(rec.action) # Action.ESCALATE_TO_HUMAN
print(rec.reason) # "High frustration level..."
print(rec.triggered_by) # {'frustration': 0.9}Reset all emotional primitives to 0.0 and clear history.
Example:
state.reset()Get complete history of state transitions.
Returns:
- List of StateTransition objects in chronological order
Example:
history = state.get_history()
for transition in history:
print(transition.event_name, transition.deltas)Represents an event that affects emotional state.
from aifeels.events import Event
event = Event(
name="custom:my_event",
deltas={"frustration": 0.1, "stress": 0.2}
)| Attribute | Type | Description |
|---|---|---|
name |
str | Unique event identifier (with namespace) |
deltas |
Dict[str, float] | Emotional state changes |
Note: Events are immutable (frozen).
Class containing all 13 standard events defined in Aifeels v0.1.0.
Task Lifecycle:
StandardEvents.TASK_STARTEDStandardEvents.TASK_COMPLETEDStandardEvents.TASK_FAILEDStandardEvents.TASK_BLOCKED
User Interaction:
StandardEvents.USER_FEEDBACK_POSITIVEStandardEvents.USER_FEEDBACK_NEGATIVEStandardEvents.USER_INTERVENTION_REQUIREDStandardEvents.USER_INTERVENTION_COMPLETED
System Events:
StandardEvents.RESOURCE_CONSTRAINTStandardEvents.UNCERTAINTY_DETECTEDStandardEvents.CONFIDENCE_INCREASEDStandardEvents.DEADLINE_APPROACHINGStandardEvents.IDLE_TIME
Get all standard events as a dictionary.
Returns:
- Dictionary mapping event names to Event objects
Example:
events = StandardEvents.get_all_events()
print(len(events)) # 13Retrieve a standard event by its name.
Parameters:
name(str): The event name (e.g., 'std:task_failed')
Returns:
- The Event object
Raises:
- ValueError: If event name is not found
Example:
event = StandardEvents.get_event_by_name('std:task_failed')Calculates temporal decay for emotional primitives.
| Constant | Value | Description |
|---|---|---|
DECAY_RATE |
0.05 | Amount of decay per interval |
DECAY_INTERVAL |
300.0 | Interval in seconds (5 minutes) |
calculate_decay(current_value: float, last_update_time: float, current_time: float | None = None) -> float
Calculate decayed value based on elapsed time.
Parameters:
current_value(float): Current emotional primitive value (0.0-1.0)last_update_time(float): Unix timestamp of last updatecurrent_time(float | None): Unix timestamp to calculate decay to (defaults to now)
Returns:
- Decayed value, clamped to [0.0, 1.0]
Example:
from aifeels.decay import DecayCalculator
import time
value = 0.5
start_time = time.time()
end_time = start_time + 300 # 5 minutes later
decayed = DecayCalculator.calculate_decay(value, start_time, end_time)
print(decayed) # 0.45 (0.5 - 0.05)apply_decay_to_state(state: Dict[str, float], last_update_time: float, current_time: float | None = None) -> Dict[str, float]
Apply decay to all primitives in a state dictionary.
Parameters:
state(Dict[str, float]): Dictionary of emotion names to valueslast_update_time(float): Unix timestamp of last updatecurrent_time(float | None): Unix timestamp to calculate decay to (defaults to now)
Returns:
- New state dictionary with decayed values
Enum of possible actions.
from aifeels.actions import Action
Action.ESCALATE_TO_HUMAN # 'escalate_to_human'
Action.REQUEST_CONFIRMATION # 'request_confirmation'
Action.INCREASE_CAUTION # 'increase_caution'
Action.CONTINUE_AUTONOMY # 'continue_autonomy'Recommendation for action based on emotional state.
| Attribute | Type | Description |
|---|---|---|
action |
Action | The recommended action |
reason |
str | Human-readable explanation |
triggered_by |
Dict[str, float] | Emotional primitives that triggered this |
Selects appropriate actions based on emotional state thresholds.
| Constant | Value | Description |
|---|---|---|
ESCALATE_THRESHOLD |
0.8 | Frustration threshold for escalation |
CONFIRM_THRESHOLD |
0.7 | Frustration threshold for confirmation |
CAUTION_THRESHOLD |
0.7 | Caution threshold for increased care |
Select the appropriate action based on emotional state.
Parameters:
state(Dict[str, float]): Dictionary of emotion names to values
Returns:
- ActionRecommendation with selected action and explanation
Example:
from aifeels.actions import ActionSelector
state = {"frustration": 0.9, "trust": 0.2, "urgency": 0.5, "stress": 0.3, "caution": 0.1}
rec = ActionSelector.select_action(state)
print(rec.action) # Action.ESCALATE_TO_HUMANGet the list of actions in precedence order.
Returns:
- List of actions from highest to lowest priority
Record of a single state transition.
| Attribute | Type | Description |
|---|---|---|
timestamp |
float | Unix timestamp when transition occurred |
event_name |
str | Name of the event that triggered the transition |
deltas |
Dict[str, float] | Changes applied to emotional primitives |
state_before |
Dict[str, float] | State values before the transition |
state_after |
Dict[str, float] | State values after the transition |
Start the Aifeels MCP server.
Parameters:
port(int): Port number (currently unused for stdio server)
Main entry point for the aifeels-mcp command.
The MCP server provides these tools:
-
get_emotional_state
- Get current emotional state
- Parameters:
apply_decay(bool)
-
process_event
- Process an event
- Parameters:
event_name(str)
-
get_recommended_action
- Get recommended action
- Parameters:
apply_decay(bool)
-
reset_emotional_state
- Reset all primitives to 0.0
- Parameters: none
-
get_state_history
- Get complete history
- Parameters: none
All public APIs include complete type hints for use with mypy and other type checkers.
from aifeels import EmotionalState
from aifeels.events import Event
# Type checking works
def process_failure(state: EmotionalState) -> str:
state.process_event(StandardEvents.TASK_FAILED)
return state.recommended_action()All data classes use Pydantic for runtime validation:
from aifeels import EmotionalState
# Validation happens automatically
state = EmotionalState(frustration=1.5) # Raises ValidationError
state = EmotionalState(frustration=-0.1) # Raises ValidationError
state = EmotionalState(frustration=0.5) # OKimport aifeels
print(aifeels.__version__) # '0.1.0'