From 3a9897bf4def736c26a15627f514ba062f8ed636 Mon Sep 17 00:00:00 2001 From: Severin Klingler Date: Tue, 22 Oct 2024 12:19:37 +0200 Subject: [PATCH] Allow overwriting default event parameters. --- nemoguardrails/utils.py | 14 ++++++-------- tests/v2_x/test_event_mechanics.py | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/nemoguardrails/utils.py b/nemoguardrails/utils.py index fda3d43bf..dfa12363f 100644 --- a/nemoguardrails/utils.py +++ b/nemoguardrails/utils.py @@ -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"), @@ -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"] diff --git a/tests/v2_x/test_event_mechanics.py b/tests/v2_x/test_event_mechanics.py index f6f0b943f..2c87f02ba 100644 --- a/tests/v2_x/test_event_mechanics.py +++ b/tests/v2_x/test_event_mechanics.py @@ -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."""