1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00
royalnet/keipack/stars/api_kei.py

91 lines
3.5 KiB
Python
Raw Permalink Normal View History

2019-11-29 20:18:01 +00:00
import random
import datetime
2019-12-09 23:39:33 +00:00
import logging
2019-11-29 20:18:01 +00:00
from typing import *
2019-11-11 12:40:26 +00:00
from starlette.requests import Request
from starlette.responses import *
2019-11-29 20:18:01 +00:00
from royalnet.constellation import *
2019-11-11 12:40:26 +00:00
from royalnet.utils import *
2019-12-09 23:39:33 +00:00
from royalnet.commands import CommandInterface
2019-11-29 23:45:41 +00:00
from ..tables import *
2019-12-09 01:22:29 +00:00
from ..utils import *
2019-11-11 12:40:26 +00:00
2019-12-09 23:39:33 +00:00
log = logging.getLogger(__name__)
2019-11-11 12:40:26 +00:00
class ApiKei(PageStar):
path = "/api/kei"
2019-11-29 23:45:41 +00:00
methods = ["POST"]
2019-12-09 23:39:33 +00:00
def __init__(self, interface: CommandInterface):
super().__init__(interface)
2019-12-09 01:22:29 +00:00
self._conversations: Dict[str, Conversation] = {}
2019-12-09 23:39:33 +00:00
log.debug("Kei initialized.")
2019-11-11 12:40:26 +00:00
async def page(self, request: Request) -> JSONResponse:
async with self.session_acm() as session:
2019-11-29 23:45:41 +00:00
form = await request.form()
2019-12-09 01:22:29 +00:00
kpid = form["kpid"]
convid = form["convid"]
2019-12-12 23:02:49 +00:00
msg = form.get("message")
2019-12-10 01:24:58 +00:00
previous = form.get("previous")
2019-12-09 01:22:29 +00:00
person = await asyncify(session.query(self.alchemy.get(KeiPerson)).filter_by(kpid=kpid).one_or_none)
2019-11-29 23:45:41 +00:00
if person is None:
2019-12-09 01:22:29 +00:00
person = self.alchemy.get(KeiPerson)(kpid=kpid)
2019-11-29 23:45:41 +00:00
session.add(person)
2019-12-13 01:48:16 +00:00
first = True
else:
first = False
2019-12-12 23:02:49 +00:00
message = self.alchemy.get(KeiMessage)(kei_person=person, message=msg, previous=previous)
2019-11-29 23:45:41 +00:00
session.add(message)
await asyncify(session.commit)
2019-12-20 18:41:40 +00:00
unlocks = await asyncify(session.query(self.alchemy.get(KeiUnlocks)).one)
2019-12-09 01:22:29 +00:00
# Find conversation
while True:
if convid not in self._conversations:
# Create a new conversation
2019-12-23 13:27:37 +00:00
# if first:
# self._conversations[convid] = await FirstConversation.create(self.interface)
# else:
# self._conversations[convid] = await StartConversation.create(self.interface)
self._conversations[convid] = await RygmovieConversation.create(self.interface)
2019-12-09 23:39:33 +00:00
log.info(f"[{convid}] SYSTEM: New conversation created - {self._conversations[convid]}")
2019-12-09 01:22:29 +00:00
conv: Conversation = self._conversations[convid]
2019-12-09 23:39:33 +00:00
try:
2019-12-13 01:48:16 +00:00
log.info(f"[{convid}] {person}: '{message.message}'")
2019-12-09 23:39:33 +00:00
except Exception:
pass
2019-12-09 01:22:29 +00:00
try:
2019-12-09 23:39:33 +00:00
result = await conv.next(session=session,
person=person,
2019-12-10 01:24:58 +00:00
message=message,
2019-12-20 18:41:40 +00:00
previous=previous,
unlocks=unlocks)
2019-12-09 01:22:29 +00:00
except StopAsyncIteration:
del self._conversations[convid]
continue
except Exception as e:
2019-12-09 23:39:33 +00:00
log.error(f"[{convid}] ERROR: {e}")
2019-12-13 01:48:16 +00:00
emotion, text = Emotion.QUESTION, "...?"
2019-12-09 23:39:33 +00:00
del self._conversations[convid]
2019-12-09 01:22:29 +00:00
break
2019-12-09 23:39:33 +00:00
else:
if isinstance(result, Conversation):
self._conversations[convid] = result
log.info(f"[{convid}] SYSTEM: Switched conversation - {self._conversations[convid]}")
else:
emotion, text = result
break
log.info(f"[{convid}] Kei ({emotion.value}): '{text}'")
2019-11-11 12:40:26 +00:00
return JSONResponse({
2019-11-29 23:45:41 +00:00
"emotion": str(emotion),
2019-11-11 12:40:26 +00:00
"text": text,
2019-12-09 01:22:29 +00:00
}, headers={
"Access-Control-Allow-Origin": "https://kei.steffo.eu",
2019-11-11 12:40:26 +00:00
})