-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor message reply in CreateMessageReplyUseCase as Chai…
…n of Responsibility
- Loading branch information
1 parent
e8ba594
commit 5f0fe78
Showing
29 changed files
with
440 additions
and
154 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
6 changes: 5 additions & 1 deletion
6
src/app/flask_postgresql/presenters/message_reply_presenter.py
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
110 changes: 0 additions & 110 deletions
110
src/interactor/use_cases/create_text_message_reply_test.py
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
from typing import List | ||
|
||
from linebot.v3.messaging.models.message import Message | ||
|
||
from src.interactor.dtos.event_dto import EventInputDto | ||
from src.interactor.interfaces.repositories.agent_executor_repository import ( | ||
AgentExecutorRepositoryInterface, | ||
) | ||
from src.interactor.use_cases.message.cor.addition_handler import AdditionHandler | ||
from src.interactor.use_cases.message.cor.default_handler import DefaultHandler | ||
from src.interactor.use_cases.message.cor.muting_handler import MutingHandler | ||
|
||
|
||
class ReplyMessagesCOR: | ||
def __init__(self): | ||
self._chain = MutingHandler(AdditionHandler(DefaultHandler())) | ||
|
||
def handle(self, input_dto: EventInputDto, repository: AgentExecutorRepositoryInterface): | ||
response: List[Message] = [] | ||
self._chain.handle(input_dto, repository, response) | ||
return response |
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,24 @@ | ||
from typing import List | ||
|
||
from linebot.v3.messaging.models import TextMessage | ||
from linebot.v3.messaging.models.message import Message | ||
|
||
from src.interactor.dtos.event_dto import EventInputDto | ||
from src.interactor.interfaces.repositories.agent_executor_repository import ( | ||
AgentExecutorRepositoryInterface, | ||
) | ||
from src.interactor.use_cases.message.cor.handler_base import Handler | ||
|
||
|
||
class AdditionHandler(Handler): | ||
def handle( | ||
self, | ||
input_dto: EventInputDto, | ||
repository: AgentExecutorRepositoryInterface, | ||
response: List[Message], | ||
): | ||
response.append(TextMessage(text="test handler")) | ||
if self._successor is not None: | ||
return self._successor.handle(input_dto, repository, response) | ||
else: | ||
response.append(TextMessage(text="靜悄悄的,什麼都沒有發生。")) |
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,29 @@ | ||
from typing import List | ||
|
||
from langchain.agents import AgentExecutor | ||
from linebot.v3.messaging.models import TextMessage | ||
from linebot.v3.messaging.models.message import Message | ||
|
||
from src.interactor.dtos.event_dto import EventInputDto | ||
from src.interactor.interfaces.repositories.agent_executor_repository import ( | ||
AgentExecutorRepositoryInterface, | ||
) | ||
from src.interactor.use_cases.message.cor.handler_base import Handler | ||
|
||
|
||
class DefaultHandler(Handler): | ||
def handle( | ||
self, | ||
input_dto: EventInputDto, | ||
repository: AgentExecutorRepositoryInterface, | ||
response: List[Message], | ||
): | ||
try: | ||
agent_executor = self._get_agent_executor(input_dto, repository) | ||
result = agent_executor.run(input=input_dto.user_input) | ||
response.append(TextMessage(text=result)) | ||
except Exception as e: | ||
print(e) | ||
response.append(TextMessage(text="出現錯誤啦!請稍後再試。")) | ||
finally: | ||
return response |
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,47 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List, Type | ||
|
||
from langchain.agents import AgentExecutor | ||
from linebot.v3.messaging.models.message import Message | ||
|
||
from src.interactor.dtos.event_dto import EventInputDto | ||
from src.interactor.interfaces.repositories.agent_executor_repository import ( | ||
AgentExecutorRepositoryInterface, | ||
) | ||
|
||
|
||
class Handler(ABC): | ||
def __init__(self, successor: Type["Handler"] = None): | ||
self._successor = successor | ||
|
||
def _get_agent_executor( | ||
self, | ||
input_dto: EventInputDto, | ||
repository: AgentExecutorRepositoryInterface, | ||
) -> AgentExecutor: | ||
""" | ||
Retrieves the agent executor associated with the current window. | ||
:param None: This function does not take any parameters. | ||
:return: None | ||
""" | ||
|
||
window_id = input_dto.window.get("window_id") | ||
|
||
agent_executor = repository.get( | ||
window_id=window_id, | ||
) | ||
if agent_executor is None: | ||
agent_executor = repository.create( | ||
window_id=window_id, | ||
) | ||
return agent_executor | ||
|
||
@abstractmethod | ||
def handle( | ||
self, | ||
input_dto: EventInputDto, | ||
repository: AgentExecutorRepositoryInterface, | ||
response: List[Message], | ||
) -> List[Message]: | ||
pass |
Oops, something went wrong.