AI 路由开关

This commit is contained in:
zihanjian
2025-08-21 15:58:10 +08:00
parent 6b48e22bae
commit a1b3799c0c
3 changed files with 40 additions and 1 deletions

View File

@@ -172,6 +172,34 @@ class AIRouter:
self.logger.error(f"AI路由器处理异常 - {e}")
return False, None
def _check_permission(self, ctx: MessageContext) -> bool:
"""
检查是否有权限使用AI路由功能
:param ctx: 消息上下文
:return: 是否有权限
"""
# 检查是否启用AI路由
ai_router_config = getattr(ctx.config, 'AI_ROUTER', {})
if not ai_router_config.get('enable', True):
self.logger.info("AI路由功能已禁用")
return False
# 私聊始终允许
if not ctx.is_group:
return True
# 群聊需要检查白名单
allowed_groups = ai_router_config.get('allowed_groups', [])
current_group = ctx.get_receiver()
if current_group in allowed_groups:
self.logger.info(f"群聊 {current_group} 在AI路由白名单中允许使用")
return True
else:
self.logger.info(f"群聊 {current_group} 不在AI路由白名单中禁止使用")
return False
def dispatch(self, ctx: MessageContext) -> bool:
"""
执行AI路由分发
@@ -180,6 +208,11 @@ class AIRouter:
"""
print(f"[AI路由器] dispatch被调用消息内容: {ctx.text}")
# 检查权限
if not self._check_permission(ctx):
print("[AI路由器] 权限检查失败返回False")
return False
# 获取AI路由决策
success, decision = self.route(ctx)
print(f"[AI路由器] route返回 - success: {success}, decision: {decision}")

View File

@@ -142,4 +142,8 @@ perplexity: # -----perplexity配置这行不填-----
trigger_keyword: ask # 触发Perplexity服务的前置词
allow_all: false # 是否允许所有群聊和用户使用Perplexity设为true时忽略下面的白名单配置
allowed_groups: [] # 允许使用Perplexity的群聊ID列表例如["123456789@chatroom", "123456789@chatroom"]
allowed_users: [] # 允许使用Perplexity的用户ID列表例如["wxid_123456789", "filehelper"]
allowed_users: [] # 允许使用Perplexity的用户ID列表例如["wxid_123456789", "filehelper"]
ai_router: # -----AI路由器配置-----
enable: true # 是否启用AI路由功能
allowed_groups: [] # 允许使用AI路由的群聊ID列表例如["123456789@chatroom", "123456789@chatroom"]

View File

@@ -42,4 +42,6 @@ class Config(object):
self.ALIYUN_IMAGE = yconfig.get("aliyun_image", {})
self.GEMINI_IMAGE = yconfig.get("gemini_image", {})
self.GEMINI = yconfig.get("gemini", {})
self.AI_ROUTER = yconfig.get("ai_router", {"enable": True, "allowed_groups": []})
self.MAX_HISTORY = yconfig.get("MAX_HISTORY", 300)
self.SEND_RATE_LIMIT = yconfig.get("send_rate_limit", 0)