Implement possibility to answer to a different channel

This commit is contained in:
OK
2023-04-12 17:56:31 +02:00
parent defe598651
commit b1ece64874
6 changed files with 74 additions and 58 deletions
+25 -10
View File
@@ -32,8 +32,11 @@ def parse_response(content: str) -> Dict:
def parse_maybe_json(json_string):
if json_string is None:
return None
json_string = json_string.strip()
if isinstance(json_string, list):
return ' '.join([str(x) for x in json_string])
if isinstance(json_string, dict):
return ' '.join([str(x) for x in json_string.values()])
json_string = str(json_string).strip()
try:
parsed_json = parse_response(json_string)
except Exception:
@@ -70,9 +73,17 @@ class AIMessage(AIMessageBase):
class AIResponse(AIMessageBase):
def __init__(self, answer: Optional[str], answer_needed: bool, staff: Optional[str], picture: Optional[str], hack: bool) -> None:
def __init__(self,
answer: Optional[str],
answer_needed: bool,
channel: Optional[str],
staff: Optional[str],
picture: Optional[str],
hack: bool
) -> None:
self.answer = answer
self.answer_needed = answer_needed
self.channel = channel
self.staff = staff
self.picture = picture
self.hack = hack
@@ -117,7 +128,7 @@ class AIResponder(object):
raise RuntimeError(f"Failed to generate image {repr(description)} after multiple retries")
async def post_process(self, message: AIMessage, response: Dict[str, Any]) -> AIResponse:
for fld in ('answer', 'staff', 'picture', 'hack'):
for fld in ('answer', 'channel', 'staff', 'picture', 'hack'):
if str(response.get(fld)).strip().lower() in \
('none', '', 'null', '"none"', '"null"', "'none'", "'null'"):
response[fld] = None
@@ -134,11 +145,15 @@ class AIResponder(object):
response['answer'] = re.sub(r'\[[^\]]*\]\(([^\)]*)\)', r'\1', response['answer'])
if message.direct or message.user in message.message:
response['answer_needed'] = True
return AIResponse(response['answer'],
response['answer_needed'],
parse_maybe_json(response['staff']),
parse_maybe_json(response['picture']),
response['hack'])
response_message = AIResponse(response['answer'],
response['answer_needed'],
response['channel'],
parse_maybe_json(response['staff']),
parse_maybe_json(response['picture']),
response['hack'])
if response_message.staff is not None and response_message.answer is not None:
response_message.answer_needed = True
return response_message
def short_path(self, message: AIMessage, limit: int) -> bool:
if message.direct or 'short-path' not in self.config:
@@ -224,7 +239,7 @@ class AIResponder(object):
async def send(self, message: AIMessage) -> AIResponse:
limit = self.config["history-limit"]
if self.short_path(message, limit):
return AIResponse(None, False, None, None, False)
return AIResponse(None, False, None, None, None, False)
retries = 3
while retries > 0:
messages = self._message(message, limit)