Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scenarios/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ def run(self, user: User, text_preprocessing_result: BaseTextPreprocessingResult
self._clear_scenario(user, last_scenario_id)


class ClearAllScenariosAction(Action):

def run(self, user: User, text_preprocessing_result: BaseTextPreprocessingResult,
params: Optional[Dict[str, Union[str, float, int]]] = None) -> None:
user.last_scenarios.clear_all()


class ClearScenarioByIdAction(ClearCurrentScenarioAction):
version: Optional[int]
parametrizer: BasicParametrizer
Expand Down
14 changes: 5 additions & 9 deletions smart_kit/models/dialogue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,23 @@ def _nothing_found_action(self):
return self.actions.get(self.NOTHING_FOUND_ACTION) or NothingFoundAction()

def run(self, text_preprocessing_result, user):
before_action = user.descriptions["external_actions"].get("before_action")
if before_action:
params = user.parametrizer.collect(text_preprocessing_result)
before_action.run(user, text_preprocessing_result, params)
scenarios_names = user.last_scenarios.scenarios_names
scenario_key = user.message.payload[field.INTENT]

if scenario_key in scenarios_names:
scenario = self.scenarios[scenario_key]
is_form_filling = isinstance(scenario, FormFillingScenario)

if is_form_filling:
params = user.parametrizer.collect(text_preprocessing_result)

if not scenario.text_fits(text_preprocessing_result, user):

params = user.parametrizer.collect(text_preprocessing_result)
if scenario.check_ask_again_requests(text_preprocessing_result, user, params):
reply = scenario.ask_again(text_preprocessing_result, user, params)

return reply, True

smart_kit_metrics.counter_nothing_found(self.app_name, scenario_key, user)

return self._nothing_found_action.run(user, text_preprocessing_result), False

return self.run_scenario(scenario_key, text_preprocessing_result, user), True

def run_scenario(self, scen_id, text_preprocessing_result, user):
Expand Down
4 changes: 3 additions & 1 deletion smart_kit/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
ClearCurrentScenarioFormAction, ClearFormAction, ClearInnerFormAction, ClearScenarioByIdAction,
ClearVariablesAction, CompositeFillFieldAction, DeleteVariableAction, FillFieldAction,
RemoveCompositeFormFieldAction, RemoveFormFieldAction, SaveBehaviorAction, SetVariableAction,
ResetCurrentNodeAction, RunScenarioAction, RunLastScenarioAction, AddHistoryEventAction, SetLocalVariableAction
ResetCurrentNodeAction, RunScenarioAction, RunLastScenarioAction, AddHistoryEventAction, SetLocalVariableAction,
ClearAllScenariosAction
)
from scenarios.actions.action import ProcessBehaviorAction, SelfServiceActionWithState, EmptyAction
from scenarios.behaviors.behavior_descriptions import BehaviorDescriptions
Expand Down Expand Up @@ -276,6 +277,7 @@ def init_actions(self):
actions["choice"] = ChoiceAction
actions["choice_scenario"] = ChoiceScenarioAction
actions["clear_current_scenario"] = ClearCurrentScenarioAction
actions["clear_all_scenarios"] = ClearAllScenariosAction
actions["clear_current_scenario_form"] = ClearCurrentScenarioFormAction
actions["clear_form_by_id"] = ClearFormAction
actions["clear_inner_form_by_id"] = ClearInnerFormAction
Expand Down
10 changes: 10 additions & 0 deletions smart_kit/template/static/references/actions/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@
},
"process_behavior_action": {
"type": "process_behavior"
},
"before_action": {
"type": "requirement",
"action": {
"type": "clear_all_scenarios"
},
"requirement": {
"type": "template",
"requirement": "{{ payload.new_session and settings['template_settings'].get('reset_context_on_new_session')}}"
}
}
}
13 changes: 13 additions & 0 deletions tests/smart_kit_tests/models/test_dialogue_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,25 @@ def setUp(self):
self.test_user1.last_scenarios = PicklableMock()
self.test_user1.last_scenarios.scenarios_names = []
self.test_user1.message.payload = {"skillId": 1, "intent": 2}
self.test_user1.descriptions = {'external_actions': {}}
self.test_user2 = PicklableMock()
self.test_user2.name = "TestName"
self.test_user2.last_scenarios = PicklableMock()
self.test_user2.last_scenarios.scenarios_names = [1, 2]
self.test_user2.message.payload = {"skillId": 1, "intent": 2}
self.test_user2.descriptions = {'external_actions': {}}
self.test_user3 = PicklableMock()
self.test_user3.name = "TestName"
self.test_user3.last_scenarios = PicklableMock()
self.test_user3.last_scenarios.scenarios_names = [1]
self.test_user3.message.payload = {"skillId": 1, "intent": 2}
self.test_user3.descriptions = {'external_actions': {}}
self.test_user4 = PicklableMock()
self.test_user4.name = "TestName"
self.test_user4.last_scenarios = PicklableMock()
self.test_user4.last_scenarios.scenarios_names = []
self.test_user4.message.payload = {"skillId": 1, "intent": 2}
self.test_user4.descriptions = {'external_actions': {'before_action': PicklableMock()}}
self.test_text_preprocessing_result = PicklableMock()
self.test_text_preprocessing_result.name = "Result"
self.test_scenario1 = PicklableMock()
Expand Down Expand Up @@ -86,6 +95,10 @@ def test_dialogue_manager_run(self):
# случай, когда 2-е условие не выполнено
self.assertTrue(obj2.run(self.test_text_preprocessing_result, self.test_user3) == ('TestNameResult', True))

# проверка на вызов before_action, если такой задан в external_actions
obj2.run(self.test_text_preprocessing_result, self.test_user4)
self.assertTrue(self.test_user4.descriptions['external_actions']['before_action'].run.called)

def test_dialogue_manager_run_scenario(self):
obj = dialogue_manager.DialogueManager({'scenarios': self.test_scenarios,
'external_actions': {'nothing_found_action': self.TestAction}},
Expand Down