diff --git a/keipack/stars/api_kei.py b/keipack/stars/api_kei.py index 42809ec4..6bb19d25 100644 --- a/keipack/stars/api_kei.py +++ b/keipack/stars/api_kei.py @@ -32,12 +32,14 @@ class ApiKei(PageStar): convid = form["convid"] msg = form.get("message") previous = form.get("previous") - first = form.get("first", False) person = await asyncify(session.query(self.alchemy.get(KeiPerson)).filter_by(kpid=kpid).one_or_none) if person is None: person = self.alchemy.get(KeiPerson)(kpid=kpid) session.add(person) + first = True + else: + first = False message = self.alchemy.get(KeiMessage)(kei_person=person, message=msg, previous=previous) session.add(message) await asyncify(session.commit) @@ -45,12 +47,15 @@ class ApiKei(PageStar): while True: if convid not in self._conversations: # Create a new conversation - self._conversations[convid] = await ExampleConversation.create(self.interface) + if first: + self._conversations[convid] = await FirstConversation.create(self.interface) + else: + self._conversations[convid] = await MainConversation.create(self.interface) log.info(f"[{convid}] SYSTEM: New conversation created - {self._conversations[convid]}") conv: Conversation = self._conversations[convid] try: - log.info(f"[{convid}] {person}: '{message}'") + log.info(f"[{convid}] {person}: '{message.message}'") except Exception: pass try: @@ -63,7 +68,7 @@ class ApiKei(PageStar): continue except Exception as e: log.error(f"[{convid}] ERROR: {e}") - emotion, text = Emotion.NEUTRAL, "...?" + emotion, text = Emotion.QUESTION, "...?" del self._conversations[convid] break else: diff --git a/keipack/tables/keimessages.py b/keipack/tables/keimessages.py index 587f5581..3719d52e 100644 --- a/keipack/tables/keimessages.py +++ b/keipack/tables/keimessages.py @@ -1,6 +1,6 @@ from typing import * from sqlalchemy import * -from sqlalchemy.orm import relationship +from sqlalchemy.orm import relationship, backref from sqlalchemy.ext.declarative import declared_attr @@ -21,7 +21,8 @@ class KeiMessage: @declared_attr def kei_person(self) -> "KeiPerson": - return relationship("KeiPerson", foreign_keys=self.kei_person_id, backref="kei_messages") + return relationship("KeiPerson", foreign_keys=self.kei_person_id, backref=backref("kei_messages", + cascade="all, delete-orphan")) @declared_attr def previous(self) -> Optional[str]: diff --git a/keipack/tables/keipeople.py b/keipack/tables/keipeople.py index a4f8965f..1a1135b2 100644 --- a/keipack/tables/keipeople.py +++ b/keipack/tables/keipeople.py @@ -19,7 +19,7 @@ class KeiPerson: return Column(Integer, ForeignKey("users.uid")) @declared_attr - def user(self) -> Optional[User]: + def user(self) -> Optional["User"]: return relationship("User", foreign_keys=self.user_id, backref="kei_people") @declared_attr diff --git a/keipack/utils/__init__.py b/keipack/utils/__init__.py index ec6aa1ce..737bc592 100644 --- a/keipack/utils/__init__.py +++ b/keipack/utils/__init__.py @@ -1,10 +1,12 @@ from .emotion import Emotion -from .conversation import Conversation, ExampleConversation +from .conversation import Conversation, ExampleConversation, FirstConversation, MainConversation from .anyinstring import any_in_string __all__ = [ "Emotion", "Conversation", "ExampleConversation", + "FirstConversation", + "MainConversation", "any_in_string", ] diff --git a/keipack/utils/anyinstring.py b/keipack/utils/anyinstring.py index 51c2400c..03695816 100644 --- a/keipack/utils/anyinstring.py +++ b/keipack/utils/anyinstring.py @@ -2,7 +2,7 @@ from typing import * import re -def any_in_string(patterns: List[str], string: str) -> bool: +def any_in_string(patterns: Collection[str], string: str) -> bool: for pattern in patterns: if re.search(pattern, string): return True diff --git a/keipack/utils/conversation.py b/keipack/utils/conversation.py index deb9ad58..571aff24 100644 --- a/keipack/utils/conversation.py +++ b/keipack/utils/conversation.py @@ -1,10 +1,11 @@ import re +import random from typing import * from royalnet.commands import CommandInterface from royalnet.utils import * from .emotion import Emotion from ..tables import KeiPerson, KeiMessage -from ..utils import any_in_string +from ..utils.anyinstring import any_in_string class Conversation: @@ -53,10 +54,10 @@ class ExampleConversation(Conversation): class FirstConversation(Conversation): async def _generator(self): yield - yield Emotion.NEUTRAL, "Ciao!" - yield Emotion.QUESTION, "Come sei arrivato qui...?" - yield Emotion.HAPPY, "Capisco... Ad ogni modo, sono Kei! Tu come ti chiami?" - yield NameConversation.create(self.interface) + yield Emotion.HAPPY, "Ciao!" + yield Emotion.HAPPY, "Come hai trovato questo posto?" + yield Emotion.HAPPY, "Capisco... Ad ogni modo, io sono Kei! Tu come ti chiami?" + yield await NameConversation.create(self.interface) class NameConversation(Conversation): @@ -77,35 +78,94 @@ class NameConversation(Conversation): continue self._person.name = name - await asyncify(self._session.commit()) + await asyncify(self._session.commit) break yield Emotion.GRIN, f"O-kei! {self._person.name}!" yield Emotion.HAPPY, "Saro' sempre a tua disposizione quando mi vorrai dire qualcosa!" - yield Emotion.QUESTION, "Pero' prima ti vorrei chiedere un favore..." + yield Emotion.HAPPY, "Pero' prima ti vorrei chiedere un favore..." yield Emotion.NEUTRAL, "Qualcuno ha criptato con delle password tutti i miei file...\n" \ "Se ne trovi qualcuna in giro, potresti dirmela?\n" while True: if self._message.message == "no": - yield Emotion.CRY, "Non farmi questo... Per piacere, accetta!" + yield Emotion.CRY, "Non puoi farmi questo... Per piacere, accetta!" else: break - yield Emotion.HAPPY, "Grazie! Prometto che quando riavro' tutto ti ricompensero' adeguatamente!" + yield Emotion.HAPPY, "Grazie! Ti prometto che quando riavro' tutti i miei file ti ricompensero' adeguatamente!" + + +class StartConversation(Conversation): + async def _generator(self): + yield + + yield Emotion.HAPPY, "Di cosa vuoi parlare?" + yield MainConversation.create(self.interface) class MainConversation(Conversation): async def _generator(self): yield - yield Emotion.HAPPY, "Di cosa vuoi parlare?" - while True: msg = self._message.message - if any_in_string([r"gatt[oiae]", "ny[ae]+", "mi+a+o+", "me+o+w+", "felin[oi]", "mici[ao]"], msg): - yield Emotion.CAT, "Nyan!" + def anym(*args) -> bool: + return any_in_string(args, msg) + + if anym(r"passwords?"): + yield PasswordConversation.create(self.interface) + + elif anym(r"[aeou]w[aeou]"): + yield Emotion.CAT, random.sample([ + "OwO", + "UwU", + ":3", + "owo", + "uwu", + "ewe", + "awa", + ], 1)[0] + + elif anym(r"gatt[oiae]", "ny[ae]+", "mi+a+o+", "me+o+w+", "felin[oi]", "mici[ao]", "ma+o+"): + yield Emotion.CAT, random.sample([ + "Nyan!", + "Miao!", + "Meow!", + "Nyaaaa...", + "Nya?", + "Mao!", + "*purr*", + ], 1)[0] + + elif anym(r"can[ei]", + r"dog(?:g(?:hi|os?)|s)?", + r"corgis?", + r"cagnolin[oiae]", + r"wo+f+", + r"b[ao]+r+k+", + r"ba+u+"): + yield Emotion.CAT, random.sample([ + "Woof!", + "Bark!", + "Bork!", + "*arf* *arf*", + ]) else: - yield Emotion.QUESTION, "...?" + yield Emotion.WORRIED, "Scusa... Non conosco molte cose... Ho bisogno di piĆ¹ password!" + + +class PasswordConversation(Conversation): + async def _generator(self): + yield + + yield Emotion.SURPRISED, "Hai trovato una password? O-kei, dimmi!" + + if False: + ... + + else: + yield Emotion.NEUTRAL, "No, non ha funzionato." + yield MainConversation.create(self.interface) diff --git a/keipack/version.py b/keipack/version.py deleted file mode 100644 index 74ca7e8f..00000000 --- a/keipack/version.py +++ /dev/null @@ -1 +0,0 @@ -semantic = "0.3" diff --git a/pyproject.toml b/pyproject.toml index caafdcf9..345fcffd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ [tool.poetry] name = "keipack" - version = "0.3" + version = "0.3.7" description = "A mysterious AI assistant" authors = ["Stefano Pigozzi "] license = "AGPL-3.0+" diff --git a/send.bat b/send.bat new file mode 100644 index 00000000..f0332759 --- /dev/null +++ b/send.bat @@ -0,0 +1,2 @@ +scp -i "D:\Chiavi e robe\Terza.pem" "dist/*" "root@ryg.steffo.eu:" +ssh -i "D:\Chiavi e robe\Terza.pem" "root@ryg.steffo.eu"