mirror of
https://gitcode.com/JianFeeeee/TrulyMEM-TrueHumanMEM.git
synced 2026-10-15 12:15:32 +00:00
refactor: 工具调用限制全部移入 config.json,代码零硬编码
- 删除 server.py BackendServer.__init__ 中 _tool_limits 的硬编码字典 - _load_config 从 ~/.trulymem/config.json 读取全部限制值 - 首次启动无配置文件时自动生成带默认值的 config.json - _create_tool_limiter 直接索引 _tool_limits 字典,无硬编码兜底 - 如需修改限制值,编辑 ~/.trulymem/config.json 即可生效(下次重启)
This commit is contained in:
@ -65,13 +65,7 @@ class BackendServer:
|
|||||||
|
|
||||||
self._lock = threading.Lock()
|
self._lock = threading.Lock()
|
||||||
self._config = {"api_key": "", "base_url": "https://api.deepseek.com", "model": "deepseek-v4-flash"}
|
self._config = {"api_key": "", "base_url": "https://api.deepseek.com", "model": "deepseek-v4-flash"}
|
||||||
self._tool_limits = {
|
self._tool_limits: Dict[str, int] = {}
|
||||||
"persona_update_max": 1,
|
|
||||||
"task_update_max": 20,
|
|
||||||
"task_query_max": 30,
|
|
||||||
"memory_query_max": 30,
|
|
||||||
"memory_update_max": 15,
|
|
||||||
}
|
|
||||||
self._message_history: list = []
|
self._message_history: list = []
|
||||||
|
|
||||||
def start(self, api_key: str = "", base_url: str = "https://api.deepseek.com", model: str = "deepseek-v4-flash") -> None:
|
def start(self, api_key: str = "", base_url: str = "https://api.deepseek.com", model: str = "deepseek-v4-flash") -> None:
|
||||||
@ -103,8 +97,19 @@ class BackendServer:
|
|||||||
self._thread = threading.Thread(target=self._run_loop, daemon=True)
|
self._thread = threading.Thread(target=self._run_loop, daemon=True)
|
||||||
self._thread.start()
|
self._thread.start()
|
||||||
|
|
||||||
|
# 工具限制默认值(仅首次启动无 config.json 时使用)
|
||||||
|
# 启动后请直接编辑配置文件修改
|
||||||
|
_DEFAULT_LIMITS = {
|
||||||
|
"persona_update_max": 1,
|
||||||
|
"task_update_max": 20,
|
||||||
|
"task_query_max": 30,
|
||||||
|
"memory_query_max": 30,
|
||||||
|
"memory_update_max": 15,
|
||||||
|
}
|
||||||
|
|
||||||
def _load_config(self) -> None:
|
def _load_config(self) -> None:
|
||||||
"""加载配置。如果指定了用户名,从用户的 config_path 加载。"""
|
"""加载配置。如果指定了用户名,从用户的 config_path 加载。
|
||||||
|
所有工具调用限制值均从配置文件读取,不硬编码在代码中。"""
|
||||||
config_file = self._config_file
|
config_file = self._config_file
|
||||||
|
|
||||||
# 如果指定了用户名,尝试从全局数据库获取用户的配置路径
|
# 如果指定了用户名,尝试从全局数据库获取用户的配置路径
|
||||||
@ -121,16 +126,30 @@ class BackendServer:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# 工具限制字段列表
|
||||||
|
limit_keys = list(self._DEFAULT_LIMITS.keys())
|
||||||
|
|
||||||
if config_file.exists():
|
if config_file.exists():
|
||||||
try:
|
try:
|
||||||
with open(config_file, 'r') as f:
|
with open(config_file, 'r') as f:
|
||||||
saved = json.load(f)
|
saved = json.load(f)
|
||||||
|
# API 配置
|
||||||
self._config.update(saved)
|
self._config.update(saved)
|
||||||
for key in self._tool_limits:
|
# 工具限制:从配置文件读取(覆盖代码默认值)
|
||||||
|
for key in limit_keys:
|
||||||
if key in saved:
|
if key in saved:
|
||||||
self._tool_limits[key] = saved[key]
|
self._tool_limits[key] = int(saved[key])
|
||||||
|
else:
|
||||||
|
# 配置文件中缺失的字段也写入文件(后续 _save_config 会补全)
|
||||||
|
self._tool_limits[key] = self._DEFAULT_LIMITS[key]
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
# 读取失败时使用默认值
|
||||||
|
for key in limit_keys:
|
||||||
|
self._tool_limits[key] = self._DEFAULT_LIMITS[key]
|
||||||
|
else:
|
||||||
|
# 首次启动,用默认值写入配置文件
|
||||||
|
self._tool_limits = dict(self._DEFAULT_LIMITS)
|
||||||
|
self._save_config()
|
||||||
|
|
||||||
def _save_config(self) -> None:
|
def _save_config(self) -> None:
|
||||||
"""保存配置。如果指定了用户名,保存到用户的 config_path。"""
|
"""保存配置。如果指定了用户名,保存到用户的 config_path。"""
|
||||||
@ -157,12 +176,14 @@ class BackendServer:
|
|||||||
|
|
||||||
def _create_tool_limiter(self):
|
def _create_tool_limiter(self):
|
||||||
from .tool_limiter import ToolLimiter, ToolLimits
|
from .tool_limiter import ToolLimiter, ToolLimits
|
||||||
|
# 所有值从 _tool_limits 读取(由 _load_config 从 config.json 加载)
|
||||||
|
# _load_config 已保证所有键存在
|
||||||
limits = ToolLimits(
|
limits = ToolLimits(
|
||||||
persona_update_max=self._tool_limits.get("persona_update_max", 1),
|
persona_update_max=self._tool_limits["persona_update_max"],
|
||||||
task_update_max=self._tool_limits.get("task_update_max", 20),
|
task_update_max=self._tool_limits["task_update_max"],
|
||||||
task_query_max=self._tool_limits.get("task_query_max", 30),
|
task_query_max=self._tool_limits["task_query_max"],
|
||||||
memory_query_max=self._tool_limits.get("memory_query_max", 30),
|
memory_query_max=self._tool_limits["memory_query_max"],
|
||||||
memory_update_max=self._tool_limits.get("memory_update_max", 15),
|
memory_update_max=self._tool_limits["memory_update_max"],
|
||||||
)
|
)
|
||||||
return ToolLimiter(limits)
|
return ToolLimiter(limits)
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,10 @@ from dataclasses import dataclass
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class ToolLimits:
|
class ToolLimits:
|
||||||
"""工具调用限制配置"""
|
"""工具调用限制配置
|
||||||
|
实际值由 server.py 从 config.json 加载后传入,此处默认值仅作安全兜底。
|
||||||
|
如需修改限制,请编辑 ~/.trulymem/config.json。
|
||||||
|
"""
|
||||||
persona_update_max: int = 1
|
persona_update_max: int = 1
|
||||||
task_update_max: int = 20 # 工作记忆链修改(create/set_state/delete/link_info)
|
task_update_max: int = 20 # 工作记忆链修改(create/set_state/delete/link_info)
|
||||||
task_query_max: int = 30 # 工作记忆链查询(memory_recall 查任务相关)
|
task_query_max: int = 30 # 工作记忆链查询(memory_recall 查任务相关)
|
||||||
|
|||||||
Reference in New Issue
Block a user