Refactor and improve the code a bit
This commit is contained in:
parent
1ef9439906
commit
112f03a47a
@ -9,6 +9,7 @@ import pickle
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
|
from functools import lru_cache
|
||||||
from typing import Optional, List, Dict, Any, Tuple
|
from typing import Optional, List, Dict, Any, Tuple
|
||||||
|
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ def pp(*args, **kw):
|
|||||||
return pformat(*args, **kw)
|
return pformat(*args, **kw)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=300)
|
||||||
def parse_json(content: str) -> Dict:
|
def parse_json(content: str) -> Dict:
|
||||||
content = content.strip()
|
content = content.strip()
|
||||||
try:
|
try:
|
||||||
@ -49,6 +51,10 @@ def parse_maybe_json(json_string):
|
|||||||
return str(parsed_json)
|
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):
|
class AIMessageBase(object):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
pass
|
pass
|
||||||
@ -215,11 +221,7 @@ class AIResponder(object):
|
|||||||
del self.history[0]
|
del self.history[0]
|
||||||
else:
|
else:
|
||||||
current = self.history[index]
|
current = self.history[index]
|
||||||
|
count = sum(1 for item in self.history if same_channel(item, current))
|
||||||
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))
|
|
||||||
if count > self.config.get('history-per-channel', 3):
|
if count > self.config.get('history-per-channel', 3):
|
||||||
del self.history[index]
|
del self.history[index]
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user