Accept Messages
This commit is contained in:
44
ums/management/process.py
Normal file
44
ums/management/process.py
Normal file
@ -0,0 +1,44 @@
|
||||
# Agenten Plattform
|
||||
#
|
||||
# (c) 2024 Magnus Bender
|
||||
# Institute of Humanities-Centered Artificial Intelligence (CHAI)
|
||||
# Universitaet Hamburg
|
||||
# https://www.chai.uni-hamburg.de/~bender
|
||||
#
|
||||
# source code released under the terms of GNU Public License Version 3
|
||||
# https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
|
||||
from fastapi import BackgroundTasks
|
||||
|
||||
from ums.management.db import DB
|
||||
|
||||
from ums.utils import AgentMessage, AgentResponse
|
||||
|
||||
class MessageProcessor():
|
||||
|
||||
def __init__(self, db:DB):
|
||||
self.db = db
|
||||
|
||||
def new_message(self,
|
||||
sender:str, receiver:str, message:AgentMessage,
|
||||
background_tasks: BackgroundTasks
|
||||
) -> AgentResponse:
|
||||
|
||||
try:
|
||||
db_count = self.db.add_message(sender, receiver, message)
|
||||
background_tasks.add_task(self._process_message, db_count)
|
||||
|
||||
return AgentResponse(
|
||||
count=db_count,
|
||||
msg="Added message to queue"
|
||||
)
|
||||
except Exception as e:
|
||||
return AgentResponse(
|
||||
count=-1,
|
||||
error=True,
|
||||
error_msg=str(e)
|
||||
)
|
||||
|
||||
def _process_message(self, count:int):
|
||||
# TODO !!!
|
||||
self.db.set_processed(count=count, processed=True)
|
Reference in New Issue
Block a user