Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API #253

Merged
merged 36 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a110e62
Move updates from merge/slots; ensure passing tests
ruthenian8 Oct 13, 2023
bd67607
move run_response from Node object to Actor object; execute response …
ruthenian8 Oct 13, 2023
12eed6e
update documentation && change the signature of processing-related ac…
ruthenian8 Oct 13, 2023
43533b5
remove overwrite_current_node_in_processing method
ruthenian8 Oct 17, 2023
bb42016
allow asynchronous condition functions
ruthenian8 Oct 25, 2023
039840b
merge branch 'dev'
ruthenian8 Oct 25, 2023
5dc75a1
update tutorial
ruthenian8 Oct 25, 2023
b274383
fix merge errors
RLKRo Oct 30, 2023
dda1bc0
make import absolute
RLKRo Oct 30, 2023
2d63c03
Apply suggestions by @RLKRo
ruthenian8 Nov 2, 2023
e2d8377
Update actor & group removing context assignment statements
ruthenian8 Nov 2, 2023
5df3c55
test responses for None; use validate_label once
ruthenian8 Nov 20, 2023
34c7d1a
merge dev into feat/async_handlers
ruthenian8 Nov 20, 2023
6378aa5
return context from the 'pipeline' service group
ruthenian8 Nov 20, 2023
84947ec
update tutorial tests for script/core;
ruthenian8 Nov 20, 2023
4878de9
Remove random seed; use seed value of 42
ruthenian8 Nov 27, 2023
95a5459
Deprecate overwrite_current_node
ruthenian8 Nov 27, 2023
8b0db0c
update deprecation details
ruthenian8 Nov 28, 2023
3cb2b77
fix: try to resolve docs bug
ruthenian8 Nov 28, 2023
ed126ab
update docs
ruthenian8 Nov 28, 2023
82a8c32
Update signature typings
ruthenian8 Nov 28, 2023
2a39d80
revert tutorial changes
RLKRo Nov 30, 2023
51a8624
remove overwrite_current_node_in_processing instead of deprecating
RLKRo Nov 30, 2023
63ba0f7
remove most context returns; update docs and typing
RLKRo Nov 30, 2023
7b44375
fix actor test
RLKRo Dec 1, 2023
1afa571
codestyle
RLKRo Dec 1, 2023
58fa9aa
add parallel processing test
RLKRo Dec 4, 2023
cd41155
make global/local/node conditions run sequentially
RLKRo Dec 4, 2023
3c70317
Merge branch 'dev' into feat/async_handlers
RLKRo Dec 5, 2023
74ea26d
replace args, kwargs with update_ctx_misc
RLKRo Dec 7, 2023
223b284
codestyle
RLKRo Dec 7, 2023
ce3a8a2
fix typing
RLKRo Dec 10, 2023
48b6934
line collapse
RLKRo Dec 10, 2023
d8075f0
rename function to fit inside code blocks
RLKRo Dec 10, 2023
6e0307f
fix tutorial function signatures
RLKRo Dec 10, 2023
1079a51
Merge branch 'dev' into feat/async_handlers
RLKRo Dec 11, 2023
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
2 changes: 1 addition & 1 deletion dff/messengers/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-

from .interface import MessengerInterface, PollingMessengerInterface, CallbackMessengerInterface, CLIMessengerInterface
from .types import PipelineRunnerFunction, PollingInterfaceLoopFunction
from .types import PollingInterfaceLoopFunction
51 changes: 20 additions & 31 deletions dff/messengers/common/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
The Message Interfaces module contains several basic classes that define the message interfaces.
These classes provide a way to define the structure of the messengers that are used to communicate with the DFF.
"""
from __future__ import annotations
import abc
import asyncio
import logging
import uuid
from typing import Optional, Any, List, Tuple, TextIO, Hashable
from typing import Optional, Any, List, Tuple, TextIO, Hashable, TYPE_CHECKING

from dff.script import Context, Message
from dff.messengers.common.types import PollingInterfaceLoopFunction

from .types import PipelineRunnerFunction, PollingInterfaceLoopFunction
if TYPE_CHECKING:
from dff.pipeline.types import PipelineRunnerFunction

logger = logging.getLogger(__name__)

Expand All @@ -29,9 +32,8 @@ async def connect(self, pipeline_runner: PipelineRunnerFunction):
Method invoked when message interface is instantiated and connection is established.
May be used for sending an introduction message or displaying general bot information.

:param pipeline_runner: A function that should return pipeline response to user request;
:param pipeline_runner: A function that should process user request and return context;
usually it's a :py:meth:`~dff.pipeline.pipeline.pipeline.Pipeline._run_pipeline` function.
:type pipeline_runner: PipelineRunnerFunction
"""
raise NotImplementedError

Expand Down Expand Up @@ -96,12 +98,10 @@ async def connect(
The looping behavior is determined by `loop` and `timeout`,
for most cases the loop itself shouldn't be overridden.

:param pipeline_runner: A function that should return pipeline response to user request;
:param pipeline_runner: A function that should process user request and return context;
usually it's a :py:meth:`~dff.pipeline.pipeline.pipeline.Pipeline._run_pipeline` function.
:type pipeline_runner: PipelineRunnerFunction
:param loop: a function that determines whether polling should be continued;
called in each cycle, should return `True` to continue polling or `False` to stop.
:type loop: PollingInterfaceLoopFunction
:param timeout: a time interval between polls (in seconds).
"""
while loop():
Expand All @@ -124,33 +124,23 @@ def __init__(self):
async def connect(self, pipeline_runner: PipelineRunnerFunction):
self._pipeline_runner = pipeline_runner

async def on_request_async(self, request: Any, ctx_id: Hashable) -> Context:
async def on_request_async(
self, request: Message, ctx_id: Optional[Hashable] = None, update_ctx_misc: Optional[dict] = None
) -> Context:
"""
Method invoked on user input. This method works just like
:py:meth:`~dff.pipeline.pipeline.pipeline.Pipeline._run_pipeline`,
however callback message interface may contain additional functionality (e.g. for external API accessing).
Return context that represents dialog with the user;
`last_response`, `id` and some dialog info can be extracted from there.

:param request: User input.
:param ctx_id: Any unique id that will be associated with dialog between this user and pipeline.
:return: Context that represents dialog with the user.
Method that should be invoked on user input.
This method has the same signature as :py:class:`~dff.pipeline.types.PipelineRunnerFunction`.
"""
return await self._pipeline_runner(request, ctx_id)
return await self._pipeline_runner(request, ctx_id, update_ctx_misc)

def on_request(self, request: Any, ctx_id: Hashable) -> Context:
def on_request(
self, request: Any, ctx_id: Optional[Hashable] = None, update_ctx_misc: Optional[dict] = None
) -> Context:
"""
Method invoked on user input. This method works just like
:py:meth:`~dff.pipeline.pipeline.pipeline.Pipeline._run_pipeline`,
however callback message interface may contain additional functionality (e.g. for external API accessing).
Return context that represents dialog with the user;
`last_response`, `id` and some dialog info can be extracted from there.

:param request: User input.
:param ctx_id: Any unique id that will be associated with dialog between this user and pipeline.
:return: Context that represents dialog with the user.
Method that should be invoked on user input.
This method has the same signature as :py:class:`~dff.pipeline.types.PipelineRunnerFunction`.
"""
return asyncio.run(self.on_request_async(request, ctx_id))
return asyncio.run(self.on_request_async(request, ctx_id, update_ctx_misc))


class CLIMessengerInterface(PollingMessengerInterface):
Expand Down Expand Up @@ -183,9 +173,8 @@ async def connect(self, pipeline_runner: PipelineRunnerFunction, **kwargs):
"""
The CLIProvider generates new dialog id used to user identification on each `connect` call.

:param pipeline_runner: A function that should return pipeline response to user request;
:param pipeline_runner: A function that should process user request and return context;
usually it's a :py:meth:`~dff.pipeline.pipeline.pipeline.Pipeline._run_pipeline` function.
:type pipeline_runner: PipelineRunnerFunction
:param \\**kwargs: argument, added for compatibility with super class, it shouldn't be used normally.
"""
self._ctx_id = uuid.uuid4()
Expand Down
15 changes: 2 additions & 13 deletions dff/messengers/common/types.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
"""
Types
-----
The Types module contains two special types that are used throughout the `DFF Messengers`.
The first type is used for the messenger interface to client interaction and the second one
to control the polling loop.
The Types module contains special types that are used throughout the `DFF Messengers`.
"""
from typing import Callable, Any, Hashable, Awaitable
from typing import Callable
from typing_extensions import TypeAlias

from dff.script import Context


PipelineRunnerFunction: TypeAlias = Callable[[Any, Hashable], Awaitable[Context]]
"""
A function type for messenger_interface-to-client interaction.
Accepts anything (user input) and hashable value (current context id), returns string (answer from pipeline).
"""


PollingInterfaceLoopFunction: TypeAlias = Callable[[], bool]
"""
Expand Down
3 changes: 2 additions & 1 deletion dff/messengers/telegram/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from telebot import types, logger

from dff.script import Context
from dff.messengers.common import PollingMessengerInterface, PipelineRunnerFunction, CallbackMessengerInterface
from dff.messengers.common import PollingMessengerInterface, CallbackMessengerInterface
from dff.pipeline.types import PipelineRunnerFunction
from .messenger import TelegramMessenger
from .message import TelegramMessage, Message

Expand Down
Loading