2024-09-25 23:48:47 +08:00
|
|
|
|
from .util import *
|
2024-10-01 22:12:21 +08:00
|
|
|
|
|
2024-11-17 00:56:50 +08:00
|
|
|
|
|
2024-09-25 23:48:47 +08:00
|
|
|
|
class MarshoContext:
|
2024-10-01 20:56:37 +08:00
|
|
|
|
"""
|
|
|
|
|
Marsho 的上下文类
|
|
|
|
|
"""
|
2024-11-17 00:56:50 +08:00
|
|
|
|
|
2024-09-25 23:48:47 +08:00
|
|
|
|
def __init__(self):
|
2024-10-01 22:12:21 +08:00
|
|
|
|
self.contents = {
|
|
|
|
|
"private": {},
|
|
|
|
|
"non-private": {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def _get_target_dict(self, is_private):
|
|
|
|
|
return self.contents["private"] if is_private else self.contents["non-private"]
|
2024-09-25 23:48:47 +08:00
|
|
|
|
|
2024-10-03 15:16:32 +08:00
|
|
|
|
def append(self, content, target_id: str, is_private: bool):
|
2024-10-01 20:56:37 +08:00
|
|
|
|
"""
|
|
|
|
|
往上下文中添加消息
|
|
|
|
|
"""
|
2024-10-01 22:12:21 +08:00
|
|
|
|
target_dict = self._get_target_dict(is_private)
|
|
|
|
|
if target_id not in target_dict:
|
|
|
|
|
target_dict[target_id] = []
|
|
|
|
|
target_dict[target_id].append(content)
|
2024-09-25 23:48:47 +08:00
|
|
|
|
|
2024-10-03 15:16:32 +08:00
|
|
|
|
def set_context(self, contexts, target_id: str, is_private: bool):
|
|
|
|
|
"""
|
|
|
|
|
设置上下文
|
|
|
|
|
"""
|
|
|
|
|
target_dict = self._get_target_dict(is_private)
|
|
|
|
|
target_dict[target_id] = contexts
|
|
|
|
|
|
|
|
|
|
def reset(self, target_id: str, is_private: bool):
|
2024-10-01 20:56:37 +08:00
|
|
|
|
"""
|
|
|
|
|
重置上下文
|
|
|
|
|
"""
|
2024-10-01 22:12:21 +08:00
|
|
|
|
target_dict = self._get_target_dict(is_private)
|
2024-11-17 02:48:17 +08:00
|
|
|
|
if target_id in target_dict:
|
|
|
|
|
target_dict[target_id].clear()
|
2024-10-01 00:04:32 +08:00
|
|
|
|
|
2024-10-03 15:16:32 +08:00
|
|
|
|
def build(self, target_id: str, is_private: bool) -> list:
|
2024-10-01 20:56:37 +08:00
|
|
|
|
"""
|
2024-11-17 00:56:50 +08:00
|
|
|
|
构建返回的上下文,不包括系统消息
|
2024-10-01 20:56:37 +08:00
|
|
|
|
"""
|
2024-10-01 22:12:21 +08:00
|
|
|
|
target_dict = self._get_target_dict(is_private)
|
|
|
|
|
if target_id not in target_dict:
|
|
|
|
|
target_dict[target_id] = []
|
2024-11-17 00:56:50 +08:00
|
|
|
|
return target_dict[target_id]
|