-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slots feature --------- Co-authored-by: Denis Kuznetsov <kuznetsov.den.p@gmail.com> Co-authored-by: ruthenian8 <ruthenian8@gmail.com> Co-authored-by: pseusys <shveitsar215@gmail.com>
- Loading branch information
1 parent
1593559
commit a2fe683
Showing
28 changed files
with
1,586 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
# flake8: noqa: F401 | ||
|
||
from dff.slots.slots import GroupSlot, ValueSlot, RegexpSlot, FunctionSlot | ||
from dff.slots.conditions import slots_extracted | ||
from dff.slots.processing import extract, extract_all, unset, unset_all, fill_template | ||
from dff.slots.response import filled_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Conditions | ||
--------------------------- | ||
Provides slot-related conditions. | ||
""" | ||
|
||
from __future__ import annotations | ||
from typing import TYPE_CHECKING, Literal | ||
|
||
if TYPE_CHECKING: | ||
from dff.script import Context | ||
from dff.slots.slots import SlotName | ||
from dff.pipeline import Pipeline | ||
|
||
|
||
def slots_extracted(*slots: SlotName, mode: Literal["any", "all"] = "all"): | ||
""" | ||
Conditions that checks if slots are extracted. | ||
:param slots: Names for slots that need to be checked. | ||
:param mode: Whether to check if all slots are extracted or any slot is extracted. | ||
""" | ||
|
||
def check_slot_state(ctx: Context, pipeline: Pipeline) -> bool: | ||
manager = ctx.framework_data.slot_manager | ||
if mode == "all": | ||
return all(manager.is_slot_extracted(slot) for slot in slots) | ||
elif mode == "any": | ||
return any(manager.is_slot_extracted(slot) for slot in slots) | ||
raise ValueError(f"{mode!r} not in ['any', 'all'].") | ||
|
||
return check_slot_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
Processing | ||
--------------------------- | ||
This module provides wrappers for :py:class:`~dff.slots.slots.SlotManager`'s API. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import Awaitable, Callable, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from dff.slots.slots import SlotName | ||
from dff.script import Context | ||
from dff.pipeline import Pipeline | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def extract(*slots: SlotName) -> Callable[[Context, Pipeline], Awaitable[None]]: | ||
""" | ||
Extract slots listed slots. | ||
This will override all slots even if they are already extracted. | ||
:param slots: List of slot names to extract. | ||
""" | ||
|
||
async def inner(ctx: Context, pipeline: Pipeline) -> None: | ||
manager = ctx.framework_data.slot_manager | ||
for slot in slots: # todo: maybe gather | ||
await manager.extract_slot(slot, ctx, pipeline) | ||
|
||
return inner | ||
|
||
|
||
def extract_all(): | ||
""" | ||
Extract all slots defined in the pipeline. | ||
""" | ||
|
||
async def inner(ctx: Context, pipeline: Pipeline): | ||
manager = ctx.framework_data.slot_manager | ||
await manager.extract_all(ctx, pipeline) | ||
|
||
return inner | ||
|
||
|
||
def unset(*slots: SlotName) -> Callable[[Context, Pipeline], None]: | ||
""" | ||
Mark specified slots as not extracted and clear extracted values. | ||
:param slots: List of slot names to extract. | ||
""" | ||
|
||
def unset_inner(ctx: Context, pipeline: Pipeline) -> None: | ||
manager = ctx.framework_data.slot_manager | ||
for slot in slots: | ||
manager.unset_slot(slot) | ||
|
||
return unset_inner | ||
|
||
|
||
def unset_all(): | ||
""" | ||
Mark all slots as not extracted and clear all extracted values. | ||
""" | ||
|
||
def inner(ctx: Context, pipeline: Pipeline): | ||
manager = ctx.framework_data.slot_manager | ||
manager.unset_all_slots() | ||
|
||
return inner | ||
|
||
|
||
def fill_template() -> Callable[[Context, Pipeline], None]: | ||
""" | ||
Fill the response template in the current node. | ||
Response message of the current node should be a format-string: e.g. "Your username is {profile.username}". | ||
""" | ||
|
||
def inner(ctx: Context, pipeline: Pipeline) -> None: | ||
manager = ctx.framework_data.slot_manager | ||
# get current node response | ||
response = ctx.current_node.response | ||
|
||
if response is None: | ||
return | ||
|
||
if callable(response): | ||
response = response(ctx, pipeline) | ||
|
||
new_text = manager.fill_template(response.text) | ||
|
||
response.text = new_text | ||
ctx.current_node.response = response | ||
|
||
return inner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
Response | ||
--------------------------- | ||
Slot-related DFF responses. | ||
""" | ||
|
||
from __future__ import annotations | ||
from typing import Callable, TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from dff.script import Context, Message | ||
from dff.pipeline import Pipeline | ||
|
||
|
||
def filled_template(template: Message) -> Callable[[Context, Pipeline], Message]: | ||
""" | ||
Fill template with slot values. | ||
The `text` attribute of the template message should be a format-string: | ||
e.g. "Your username is {profile.username}". | ||
For the example above, if ``profile.username`` slot has value "admin", | ||
it would return a copy of the message with the following text: | ||
"Your username is admin". | ||
:param template: Template message with a format-string text. | ||
""" | ||
|
||
def fill_inner(ctx: Context, pipeline: Pipeline) -> Message: | ||
message = template.model_copy() | ||
new_text = ctx.framework_data.slot_manager.fill_template(template.text) | ||
message.text = new_text | ||
return message | ||
|
||
return fill_inner |
Oops, something went wrong.