Skip to content

Commit

Permalink
Allow overwriting default event parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
sklinglernv committed Oct 22, 2024
1 parent 81b6ed6 commit 3a9897b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions nemoguardrails/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ def _has_property(e: Dict[str, Any], p: Property) -> bool:
"UtteranceBotAction": ("bot_speech", "replace"),
"UtteranceUserAction": ("user_speech", "replace"),
"TimerBotAction": ("time", "parallel"),
"FacialGestureBotAction": ("bot_gesture", "override"),
"GestureBotAction": ("bot_gesture", "override"),
"FacialGestureBotAction": ("bot_face", "replace"),
"GestureBotAction": ("bot_gesture", "override"),
"PostureBotAction": ("bot_posture", "override"),
"VisualChoiceSceneAction": ("information", "override"),
"VisualInformationSceneAction": ("information", "override"),
Expand All @@ -164,16 +163,15 @@ def _add_modality_info(event_dict: Dict[str, Any]) -> None:

def _update_action_properties(event_dict: Dict[str, Any]) -> None:
"""Update action related even properties and ensure UMIM compliance (very basic)"""

now = datetime.now(timezone.utc).isoformat()
if "Started" in event_dict["type"]:
event_dict["action_started_at"] = datetime.now(timezone.utc).isoformat()
event_dict.setdefault("action_started_at", now)
elif "Start" in event_dict["type"]:
if "action_uid" not in event_dict:
event_dict["action_uid"] = new_uuid()
event_dict.setdefault("action_uid", new_uuid())
elif "Updated" in event_dict["type"]:
event_dict["action_updated_at"] = datetime.now(timezone.utc).isoformat()
event_dict.setdefault("action_updated_at", now)
elif "Finished" in event_dict["type"]:
event_dict["action_finished_at"] = datetime.now(timezone.utc).isoformat()
event_dict.setdefault("action_finished_at", now)
if (
"is_success" in event_dict
and event_dict["is_success"]
Expand Down
24 changes: 24 additions & 0 deletions tests/v2_x/test_event_mechanics.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ def test_send_umim_action_event():
)


def test_send_umim_action_event_overwriting_default_parameters():
"""Test to send an UMIM event but overwrite default parameters."""

content = """
flow main
$fixed_timestamp = "2024-10-22T12:08:18.874224"
$uid = "1234"
send UtteranceBotActionFinished(final_script="Hello world", action_finished_at=$fixed_timestamp, action_uid=$uid, is_success=True)
"""

state = run_to_completion(_init_state(content), start_main_flow_event)
assert is_data_in_events(
state.outgoing_events,
[
{
"type": "UtteranceBotActionFinished",
"final_script": "Hello world",
"action_uid": "1234",
"action_finished_at": "2024-10-22T12:08:18.874224",
}
],
)


def test_match_umim_action_event():
"""Test to match an UMIM event."""

Expand Down

0 comments on commit 3a9897b

Please sign in to comment.