Refactor and improve the code a bit

This commit is contained in:
OK 2023-04-13 18:57:37 +02:00
parent 1ef9439906
commit 112f03a47a

View File

@ -9,6 +9,7 @@ import pickle
from pathlib import Path
from io import BytesIO
from pprint import pformat
from functools import lru_cache
from typing import Optional, List, Dict, Any, Tuple
@ -18,6 +19,7 @@ def pp(*args, **kw):
return pformat(*args, **kw)
@lru_cache(maxsize=300)
def parse_json(content: str) -> Dict:
content = content.strip()
try:
@ -49,6 +51,10 @@ def parse_maybe_json(json_string):
return str(parsed_json)
def same_channel(item1: Dict[str, Any], item2: Dict[str, Any]) -> bool:
return parse_json(item1['content']).get('channel') == parse_json(item2['content']).get('channel')
class AIMessageBase(object):
def __init__(self) -> None:
pass
@ -215,11 +221,7 @@ class AIResponder(object):
del self.history[0]
else:
current = self.history[index]
def same_channel(item: Dict[str, Any]) -> bool:
return parse_json(item['content']).get('channel') == parse_json(current['content']).get('channel')
count = sum(1 for item in self.history if same_channel(item))
count = sum(1 for item in self.history if same_channel(item, current))
if count > self.config.get('history-per-channel', 3):
del self.history[index]
else: