修改了助手实例的缓存机制,现在缓存key的生成会同时考虑 mcp_settings 和 prompt 参数。
This commit is contained in:
parent
069925dc63
commit
02a6d3e0d7
@ -639,11 +639,11 @@ async def get_cached_projects():
|
||||
async def remove_project_cache(unique_id: str):
|
||||
"""移除特定项目的缓存"""
|
||||
try:
|
||||
success = agent_manager.remove_cache_by_unique_id(unique_id)
|
||||
if success:
|
||||
return {"message": f"项目缓存移除成功: {unique_id}"}
|
||||
removed_count = agent_manager.remove_cache_by_unique_id(unique_id)
|
||||
if removed_count > 0:
|
||||
return {"message": f"项目缓存移除成功: {unique_id}", "removed_count": removed_count}
|
||||
else:
|
||||
return {"message": f"未找到项目缓存: {unique_id}", "removed": False}
|
||||
return {"message": f"未找到项目缓存: {unique_id}", "removed_count": 0}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"移除项目缓存失败: {str(e)}")
|
||||
|
||||
|
||||
@ -148,7 +148,7 @@ def read_mcp_settings():
|
||||
return mcp_settings_json
|
||||
|
||||
|
||||
def update_agent_llm(agent, model_name: str, api_key: str = None, model_server: str = None, generate_cfg: Dict = None, system_prompt: str = None, mcp_settings: List[Dict] = None):
|
||||
def update_agent_llm(agent, model_name: str, api_key: str = None, model_server: str = None, generate_cfg: Dict = None):
|
||||
"""动态更新助手实例的LLM和配置,支持从接口传入参数"""
|
||||
# 获取基础配置
|
||||
llm_config = {
|
||||
@ -163,15 +163,6 @@ def update_agent_llm(agent, model_name: str, api_key: str = None, model_server:
|
||||
|
||||
# 动态设置LLM
|
||||
agent.llm = llm_instance
|
||||
|
||||
# 更新系统消息(如果提供)
|
||||
if system_prompt:
|
||||
agent.system_message = system_prompt
|
||||
|
||||
# 更新MCP设置(如果提供)
|
||||
if mcp_settings:
|
||||
agent.function_list = mcp_settings
|
||||
|
||||
return agent
|
||||
|
||||
|
||||
|
||||
@ -39,9 +39,27 @@ class FileLoadedAgentManager:
|
||||
self.creation_times: Dict[str, float] = {} # 创建时间记录
|
||||
self.max_cached_agents = max_cached_agents
|
||||
|
||||
def _get_cache_key(self, unique_id: str) -> str:
|
||||
"""获取 unique_id 的哈希值作为缓存键"""
|
||||
return hashlib.md5(unique_id.encode('utf-8')).hexdigest()[:16]
|
||||
def _get_cache_key(self, unique_id: str, system_prompt: str = None, mcp_settings: List[Dict] = None) -> str:
|
||||
"""获取包含 unique_id、system_prompt 和 mcp_settings 的哈希值作为缓存键
|
||||
|
||||
Args:
|
||||
unique_id: 项目的唯一标识符
|
||||
system_prompt: 系统提示词
|
||||
mcp_settings: MCP设置列表
|
||||
|
||||
Returns:
|
||||
str: 缓存键的哈希值
|
||||
"""
|
||||
# 构建包含所有相关参数的字符串
|
||||
cache_data = {
|
||||
'unique_id': unique_id,
|
||||
'system_prompt': system_prompt or '',
|
||||
'mcp_settings': json.dumps(mcp_settings or [], sort_keys=True)
|
||||
}
|
||||
|
||||
# 将字典转换为JSON字符串并计算哈希值
|
||||
cache_str = json.dumps(cache_data, sort_keys=True)
|
||||
return hashlib.md5(cache_str.encode('utf-8')).hexdigest()[:16]
|
||||
|
||||
def _update_access_time(self, cache_key: str):
|
||||
"""更新访问时间(LRU 管理)"""
|
||||
@ -106,7 +124,7 @@ class FileLoadedAgentManager:
|
||||
final_system_prompt = load_system_prompt(project_dir, language, system_prompt, robot_type, unique_id)
|
||||
final_mcp_settings = load_mcp_settings(project_dir, mcp_settings, unique_id)
|
||||
|
||||
cache_key = self._get_cache_key(unique_id)
|
||||
cache_key = self._get_cache_key(unique_id, final_system_prompt, final_mcp_settings)
|
||||
|
||||
# 检查是否已存在该助手实例
|
||||
if cache_key in self.agents:
|
||||
@ -114,7 +132,7 @@ class FileLoadedAgentManager:
|
||||
agent = self.agents[cache_key]
|
||||
|
||||
# 动态更新 LLM 配置和系统设置(如果参数有变化)
|
||||
update_agent_llm(agent, model_name, api_key, model_server, generate_cfg, final_system_prompt, mcp_settings)
|
||||
update_agent_llm(agent, model_name, api_key, model_server, generate_cfg)
|
||||
|
||||
logger.info(f"复用现有的助手实例缓存: {cache_key} (unique_id: {unique_id}")
|
||||
return agent
|
||||
@ -180,27 +198,44 @@ class FileLoadedAgentManager:
|
||||
logger.info(f"已清空所有助手实例缓存,共清理 {cache_count} 个实例")
|
||||
return cache_count
|
||||
|
||||
def remove_cache_by_unique_id(self, unique_id: str) -> bool:
|
||||
"""根据 unique_id 移除特定的缓存
|
||||
def remove_cache_by_unique_id(self, unique_id: str) -> int:
|
||||
"""根据 unique_id 移除所有相关的缓存
|
||||
|
||||
由于缓存key现在包含 system_prompt 和 mcp_settings,
|
||||
一个 unique_id 可能对应多个缓存实例。
|
||||
|
||||
Args:
|
||||
unique_id: 项目的唯一标识符
|
||||
|
||||
Returns:
|
||||
bool: 是否成功移除
|
||||
int: 成功移除的实例数量
|
||||
"""
|
||||
cache_key = self._get_cache_key(unique_id)
|
||||
keys_to_remove = []
|
||||
|
||||
if cache_key in self.agents:
|
||||
# 找到所有匹配的 unique_id 的缓存键
|
||||
for cache_key, stored_unique_id in self.unique_ids.items():
|
||||
if stored_unique_id == unique_id:
|
||||
keys_to_remove.append(cache_key)
|
||||
|
||||
# 移除找到的缓存
|
||||
removed_count = 0
|
||||
for cache_key in keys_to_remove:
|
||||
try:
|
||||
del self.agents[cache_key]
|
||||
del self.unique_ids[cache_key]
|
||||
del self.access_times[cache_key]
|
||||
del self.creation_times[cache_key]
|
||||
removed_count += 1
|
||||
logger.info(f"已移除助手实例缓存: {cache_key} (unique_id: {unique_id})")
|
||||
except KeyError:
|
||||
continue
|
||||
|
||||
logger.info(f"已移除特定 unique_id 的助手实例缓存: {unique_id}")
|
||||
return True
|
||||
if removed_count > 0:
|
||||
logger.info(f"已移除 unique_id={unique_id} 的 {removed_count} 个助手实例缓存")
|
||||
else:
|
||||
logger.warning(f"未找到 unique_id={unique_id} 的缓存实例")
|
||||
|
||||
return False
|
||||
return removed_count
|
||||
|
||||
|
||||
# 全局文件预加载助手管理器实例
|
||||
|
||||
Loading…
Reference in New Issue
Block a user