Esempio n. 1
0
    def _check_response(
        self, dialog_state: DialogState, base_args: Dict[str, Any]
    ) -> Optional[List[stopcovid.dialog.models.events.DialogEvent]]:
        prompt = dialog_state.get_prompt()
        if prompt is None:
            return
        events = []
        if prompt.should_advance_with_answer(
                self.content_lower, dialog_state.user_profile.language):
            events.append(
                CompletedPrompt(
                    prompt=prompt,
                    drill_instance_id=dialog_state.
                    drill_instance_id,  # type: ignore
                    response=self.content,
                    **base_args,
                ))
            should_advance = True
        else:
            should_advance = dialog_state.current_prompt_state.failures >= prompt.max_failures
            events.append(
                FailedPrompt(
                    prompt=prompt,
                    response=self.content,
                    drill_instance_id=dialog_state.
                    drill_instance_id,  # type: ignore
                    abandoned=should_advance,
                    **base_args,
                ))

        if should_advance:
            next_prompt = dialog_state.get_next_prompt()
            if next_prompt is not None:
                events.append(
                    AdvancedToNextPrompt(
                        prompt=next_prompt,
                        drill_instance_id=dialog_state.
                        drill_instance_id,  # type: ignore
                        **base_args,
                    ))
                if dialog_state.is_next_prompt_last():
                    # assume the last prompt doesn't wait for an answer
                    events.append(
                        DrillCompleted(
                            drill_instance_id=dialog_state.
                            drill_instance_id,  # type: ignore
                            **base_args,
                        ))
        return events
Esempio n. 2
0
    def _check_response(
            self, dialog_state: DialogState,
            base_args: Dict[str, Any]) -> Optional[List[DialogEvent]]:
        prompt = dialog_state.get_prompt()
        if prompt is None:
            return None
        events: List[DialogEvent] = []
        if prompt.should_advance_with_answer(self.content_lower):
            user_profile_updates = None
            if prompt.response_user_profile_key:
                user_profile_updates = {
                    prompt.response_user_profile_key: self.content
                }
            events.append(
                CompletedPrompt(
                    prompt=prompt,
                    drill_instance_id=dialog_state.drill_instance_id,
                    response=self.content,
                    user_profile_updates=user_profile_updates,
                    **base_args,
                ))
            should_advance = True
        else:
            assert dialog_state.current_prompt_state
            should_advance = dialog_state.current_prompt_state.failures >= (
                prompt.max_failures or 1)
            events.append(
                FailedPrompt(
                    prompt=prompt,
                    response=self.content or None,
                    drill_instance_id=dialog_state.drill_instance_id,
                    abandoned=should_advance,
                    **base_args,
                ))

        if should_advance:
            assert dialog_state.current_drill
            next_prompt = dialog_state.get_next_prompt()
            if next_prompt is not None:
                events.append(
                    AdvancedToNextPrompt(
                        prompt=next_prompt,
                        drill_instance_id=dialog_state.drill_instance_id,
                        **base_args,
                    ))
                if dialog_state.is_next_prompt_last():
                    # assume the last prompt doesn't wait for an answer
                    events.append(
                        DrillCompleted(
                            drill_instance_id=dialog_state.drill_instance_id,
                            **base_args,
                        ))
            elif len(dialog_state.current_drill.prompts) == 1:
                events.append(
                    DrillCompleted(
                        drill_instance_id=dialog_state.drill_instance_id,
                        last_prompt_response=self.content or None,
                        **base_args,
                    ))

        return events