Skip to content

Commit

Permalink
PNLP-12553: set_mid_variable
Browse files Browse the repository at this point in the history
  • Loading branch information
SyrexMinus committed Jan 30, 2025
1 parent 791e484 commit 0d5635e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion core/basic_models/actions/variable_actions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations
import collections
import json
from typing import Optional, Dict, Any, Union, List
from typing import Optional, Dict, Any, Union, List, TYPE_CHECKING, AsyncGenerator

from jinja2 import exceptions as jexcept

Expand All @@ -11,6 +12,9 @@
from core.text_preprocessing.base import BaseTextPreprocessingResult
from core.unified_template.unified_template import UnifiedTemplate

if TYPE_CHECKING:
from scenarios.user.user_model import User


class BaseSetVariableAction(Action):
key: str
Expand Down Expand Up @@ -99,3 +103,17 @@ async def run(self, user: BaseUser, text_preprocessing_result: BaseTextPreproces
class SetLocalVariableAction(BaseSetVariableAction):
def _set(self, user, value):
user.local_vars.set(self.key, value)


class SetMidVariableAction(BaseSetVariableAction):
async def run(self, user: User, text_preprocessing_result: BaseTextPreprocessingResult,
params: dict[str, str | float | int] | None = None) -> List[Command]:
try:
value = self.template.render({**(params or {}), **user.parametrizer.collect(text_preprocessing_result)})
except Exception:
value = None
self._set(user, self.loaders[self.loader](value) if self.loader and value else value)
return []

def _set(self, user: User, value: Any) -> None:
user.mid_variables.update(self.key, value)
3 changes: 2 additions & 1 deletion smart_kit/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from core.basic_models.actions.string_actions import StringAction, AfinaAnswerAction, SDKAnswer, \
SDKAnswerToUser, StringFileUnifiedTemplateAction
from core.basic_models.actions.variable_actions import ClearVariablesAction, DeleteVariableAction, \
SetLocalVariableAction, SetVariableAction
SetLocalVariableAction, SetVariableAction, SetMidVariableAction
from core.basic_models.answer_items.answer_items import items_factory, SdkAnswerItem, answer_items, BubbleText, \
ItemCard, PronounceText, SuggestText, SuggestDeepLink, RawItem
from core.basic_models.classifiers.basic_classifiers import classifiers, classifier_factory, Classifier, \
Expand Down Expand Up @@ -307,6 +307,7 @@ def init_actions(self):
actions["sdk_answer_to_user"] = SDKAnswerToUser
actions["self_service_with_state"] = SelfServiceActionWithState
actions["set_local_variable"] = SetLocalVariableAction
actions["set_mid_variable"] = SetMidVariableAction
actions["set_variable"] = SetVariableAction
actions["string"] = StringFileUnifiedTemplateAction
actions["push"] = PushAction
Expand Down

0 comments on commit 0d5635e

Please sign in to comment.