Skip to content

Commit

Permalink
dict -> list
Browse files Browse the repository at this point in the history
  • Loading branch information
pseusys committed Mar 13, 2024
1 parent 087dfbf commit f4c4eeb
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
10 changes: 8 additions & 2 deletions dff/messengers/common/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class MessengerInterface(abc.ABC):
It is responsible for connection between user and pipeline, as well as for request-response transactions.
"""

def __init__(self, name: Optional[str] = None):
self.name = name if name is not None else str(type(self))


@abc.abstractmethod
async def connect(self, pipeline_runner: PipelineRunnerFunction, iface_id: str):
"""
Expand Down Expand Up @@ -121,8 +125,9 @@ class CallbackMessengerInterface(MessengerInterface):
Callback message interface is waiting for user input and answers once it gets one.
"""

def __init__(self):
def __init__(self, name: Optional[str]):
self._pipeline_runner: Optional[PipelineRunnerFunction] = None
MessengerInterface.__init__(self, name)

async def connect(self, pipeline_runner: PipelineRunnerFunction, iface_id: str):
self._pipeline_runner = pipeline_runner
Expand Down Expand Up @@ -159,8 +164,9 @@ def __init__(
prompt_request: str = "request: ",
prompt_response: str = "response: ",
out_descriptor: Optional[TextIO] = None,
name: Optional[str] = None
):
super().__init__()
super().__init__(name)
self._ctx_id: Optional[Hashable] = None
self._intro: Optional[str] = intro
self._prompt_request: str = prompt_request
Expand Down
2 changes: 2 additions & 0 deletions dff/messengers/telegram/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def __init__(
timeout: int = 20,
long_polling_timeout: int = 20,
messenger: Optional[TelegramMessenger] = None,
name: Optional[str] = None
):
super().__init__(name)
self.messenger = (
messenger if messenger is not None else TelegramMessenger(token, suppress_middleware_excepions=True)
)
Expand Down
18 changes: 7 additions & 11 deletions dff/pipeline/pipeline/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from dff.context_storages import DBContextStorage
from dff.script import Script, Context, ActorStage
from dff.script import NodeLabel2Type, Message, DEFAULT_INTERFACE_ID
from dff.script import NodeLabel2Type, Message
from dff.utils.turn_caching import cache_clear

from dff.messengers.common import MessengerInterface, CLIMessengerInterface
Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(
condition_handler: Optional[Callable] = None,
verbose: bool = True,
handlers: Optional[Dict[ActorStage, List[Callable]]] = None,
messenger_interfaces: Optional[Union[MessengerInterface, Iterable[MessengerInterface], Dict[str, MessengerInterface]]] = None,
messenger_interfaces: Optional[Iterable[MessengerInterface]] = None,
context_storage: Optional[Union[DBContextStorage, Dict]] = None,
before_handler: Optional[ExtraHandlerBuilder] = None,
after_handler: Optional[ExtraHandlerBuilder] = None,
Expand All @@ -110,15 +110,11 @@ def __init__(
timeout=timeout,
)

if messenger_interfaces is not None and not isinstance(messenger_interfaces, MessengerInterface):
if isinstance(messenger_interfaces, Iterable):
self.messenger_interfaces = {str(uuid4()): iface for iface in messenger_interfaces}
elif isinstance(messenger_interfaces, Dict):
self.messenger_interfaces = messenger_interfaces
else:
raise RuntimeError(f"Unexpected type of 'messenger_interfaces': {type(messenger_interfaces)}")
if self.messenger_interfaces is None:
interface = CLIMessengerInterface()
self.messenger_interfaces = {interface.name: interface}
else:
self.messenger_interfaces = {DEFAULT_INTERFACE_ID: CLIMessengerInterface()}
self.messenger_interfaces = {iface.name: iface for iface in messenger_interfaces}

self._services_pipeline.name = "pipeline"
self._services_pipeline.path = ".pipeline"
Expand Down Expand Up @@ -229,7 +225,7 @@ def from_script(
parallelize_processing: bool = False,
handlers: Optional[Dict[ActorStage, List[Callable]]] = None,
context_storage: Optional[Union[DBContextStorage, Dict]] = None,
messenger_interfaces: Optional[Union[MessengerInterface, Iterable[MessengerInterface], Dict[str, MessengerInterface]]] = None,
messenger_interfaces: Optional[Iterable[MessengerInterface]] = None,
pre_services: Optional[List[Union[ServiceBuilder, ServiceGroupBuilder]]] = None,
post_services: Optional[List[Union[ServiceBuilder, ServiceGroupBuilder]]] = None,
) -> "Pipeline":
Expand Down
1 change: 0 additions & 1 deletion dff/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
)
from .core.script import Node, Script
from .core.types import (
DEFAULT_INTERFACE_ID,
LabelType,
NodeLabel1Type,
NodeLabel2Type,
Expand Down
3 changes: 0 additions & 3 deletions dff/script/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
# TODO: change example


DEFAULT_INTERFACE_ID = "default"


class ActorStage(Enum):
"""
The class which holds keys for the handlers. These keys are used
Expand Down
4 changes: 2 additions & 2 deletions tests/pipeline/test_messenger_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_cli_messenger_interface(monkeypatch):
sys.path.append(str(pathlib.Path(__file__).parent.absolute()))

interface = CLIMessengerInterface(intro="Hi, it's DFF powered bot, let's chat!")
pipeline.messenger_interfaces = {DEFAULT_INTERFACE_ID: interface}
pipeline.messenger_interfaces = {interface.name: interface}

def loop() -> bool:
loop.runs_left -= 1
Expand All @@ -56,7 +56,7 @@ def loop() -> bool:

def test_callback_messenger_interface(monkeypatch):
interface = CallbackMessengerInterface()
pipeline.messenger_interfaces = {DEFAULT_INTERFACE_ID: interface}
pipeline.messenger_interfaces = {interface.name: interface}

pipeline.run()

Expand Down
2 changes: 1 addition & 1 deletion tutorials/pipeline/2_pre_and_post_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def pong_processor(ctx: Context):
context_storage={}, # `context_storage` - a dictionary or
# a `DBContextStorage` instance,
# a place to store dialog contexts
messenger_interfaces=CLIMessengerInterface(),
messenger_interfaces=[CLIMessengerInterface()],
# `messenger_interface` - a message channel adapter,
# it's not used in this tutorial
pre_services=[ping_processor],
Expand Down
4 changes: 2 additions & 2 deletions tutorials/pipeline/3_pipeline_dict_with_services_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ def postprocess(ctx: Context, pl: Pipeline):
"script": TOY_SCRIPT,
"start_label": ("greeting_flow", "start_node"),
"fallback_label": ("greeting_flow", "fallback_node"),
"messenger_interfaces": CLIMessengerInterface(
"messenger_interfaces": [CLIMessengerInterface(
intro="Hi, this is a brand new Pipeline running!",
prompt_request="Request: ",
prompt_response="Response: ",
), # `CLIMessengerInterface` has the following constructor parameters:
)], # `CLIMessengerInterface` has the following constructor parameters:
# `intro` - a string that will be displayed
# on connection to interface (on `pipeline.run`)
# `prompt_request` - a string that will be displayed before user input
Expand Down

0 comments on commit f4c4eeb

Please sign in to comment.