mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
* ♻️ refactor(core): 重构 AI 能力与定时任务调度系统 - 【AI 能力与工具】重构 Capability 注册与管理机制,引入 CapabilityManager 统一管理 - 移除全局能力注册表,改用声明式装饰器 `@capability` 进行解耦注册 - 重构工具解析器链,使用统一的 BaseToolResolver 代替原有的多个特定解析器 - 增强工具查询过滤,支持通配符匹配、工具箱过滤和排除标签 - 【定时任务调度】重构定时任务管理器,引入 SchedulerRegistry 统一管理任务元数据 - 引入 JobConfig 聚合定时任务配置,支持用户维度的定时任务调度 - 重构执行分发器,支持并发限制、串行间隔和随机延迟打散 - 【运行上下文】引入 ScheduledDeps 以支持后台和定时任务环境下的依赖注入 - 优化 RunContext,支持从定时任务上下文快速构造,并提供 emit 辅助方法 - 【日志与监控】引入 AILoggerProxy,实现 AI 各模块的专属日志输出 - 将各模块的全局 logger 替换为对应的模块专属日志代理 - 【其他优化】修复 Pydantic V1 兼容层中 model_validator 的装饰器兼容性问题 - 在非交互式环境(如定时任务)中自动隐藏 HITL 交互工具以节省 Token * ♻️ refactor(core): 优化内部导入路径并提升 Pydantic 兼容性 - 【重构】将 `services/ai` 模块内的绝对导入重构为相对导入,优化包结构 - 【重构】移除不必要的 `if TYPE_CHECKING` 保护,通过 `from __future__ import annotations` 直接导入类型 - 【清理】清理 `core/messages/types.py` 中未使用的 `AssistantContentUnion` 等联合类型定义 - 【优化】在 `utils/pydantic_compat.py` 中新增 `model_rebuild` 兼容函数,统一 Pydantic V1/V2 的模型重建逻辑 - 【优化】将部分函数内部的延迟导入提升至模块顶部,规范代码结构 * ♻️ refactor(imports): 优化导入路径为相对导入并清理冗余导入 - 【重构】将 AI 服务相关模块中的绝对导入路径修改为相对导入,提升模块内聚性与可移植性 - 【清理】移除多处函数内部或类方法中未使用的冗余导入,避免循环引用和资源浪费 - 【格式化】微调部分工具装饰器和返回语句的格式与尾随逗号 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
366 lines
10 KiB
Python
366 lines
10 KiB
Python
import fnmatch
|
|
|
|
from zhenxun.services.ai.core.models import (
|
|
ModelCapabilities,
|
|
ModelModality,
|
|
ReasoningMode,
|
|
)
|
|
from zhenxun.utils.pydantic_compat import model_copy
|
|
|
|
CTX_1M = 1_000_000
|
|
CTX_400K = 400_000
|
|
CTX_256K = 256_000
|
|
CTX_200K = 204_800
|
|
CTX_128K = 128_000
|
|
CTX_8K = 8_192
|
|
|
|
CAP_MULTIMODAL_EMBEDDING = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
ModelModality.FILE,
|
|
},
|
|
is_embedding_model=True,
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
STANDARD_TEXT_TOOL_CAPABILITIES = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
)
|
|
CAP_GEMINI_2_5 = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.BUDGET,
|
|
reasoning_visibility="visible",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"google_map",
|
|
"url_context",
|
|
},
|
|
)
|
|
CAP_GEMINI_3_BASE = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.LEVEL,
|
|
reasoning_visibility="visible",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"google_map",
|
|
"url_context",
|
|
},
|
|
features={
|
|
"mixed_tools",
|
|
"server_side_tool_invocations",
|
|
},
|
|
)
|
|
|
|
CAP_GEMINI_3_PRO = model_copy(
|
|
CAP_GEMINI_3_BASE,
|
|
update={
|
|
"reasoning_effort_map": {
|
|
"max": "high",
|
|
"xhigh": "high",
|
|
"minimal": "low",
|
|
"none": "low",
|
|
}
|
|
},
|
|
)
|
|
|
|
CAP_GEMINI_3_FLASH = model_copy(
|
|
CAP_GEMINI_3_BASE,
|
|
update={
|
|
"reasoning_effort_map": {"max": "high", "xhigh": "high", "none": "minimal"}
|
|
},
|
|
)
|
|
CAP_OPENAI_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="hidden",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
reasoning_effort_map={"max": "xhigh", "minimal": "none"},
|
|
)
|
|
CAP_OPENAI_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
reasoning_effort_map={"max": "xhigh", "minimal": "none"},
|
|
)
|
|
CAP_DEEPSEEK_V4 = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
reasoning_effort_map={"minimal": "low"},
|
|
)
|
|
CAP_MINIMAX_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
)
|
|
|
|
CAP_GLM_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.VIDEO,
|
|
ModelModality.FILE,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
)
|
|
|
|
CAP_GLM_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
)
|
|
|
|
CAP_MINIMAX_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE, ModelModality.VIDEO},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
)
|
|
|
|
CAP_MIMO_TEXT = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
supported_native_tools={"web_search"},
|
|
reasoning_effort_map={"max": "high", "xhigh": "high", "minimal": "low"},
|
|
)
|
|
|
|
CAP_MIMO_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT, ModelModality.AUDIO},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
supported_native_tools={"web_search"},
|
|
reasoning_effort_map={"max": "high", "xhigh": "high", "minimal": "low"},
|
|
)
|
|
|
|
|
|
CAP_OPENAI_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="alloy",
|
|
)
|
|
|
|
CAP_GEMINI_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="Aoede",
|
|
)
|
|
|
|
CAP_MINIMAX_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="female-shaonv",
|
|
)
|
|
|
|
CAP_MIMO_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="mimo_default",
|
|
)
|
|
|
|
CAP_TEXT_EMBEDDING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
is_embedding_model=True,
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
CAP_RERANK_ONLY = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
is_rerank_model=True,
|
|
)
|
|
|
|
CAP_OPENAI_IMAGE = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
CAP_GEMINI_IMAGE = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
},
|
|
)
|
|
|
|
DEFAULT_PERMISSIVE_CAPABILITIES = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
},
|
|
supports_tool_calling=True,
|
|
)
|
|
|
|
MODEL_ALIAS_MAPPING: dict[str, str] = {
|
|
"*DeepSeek-V4-Pro*": "deepseek-v4-pro",
|
|
"*DeepSeek-V4-Flash*": "deepseek-v4-flash",
|
|
}
|
|
|
|
|
|
_ROUTING_TABLE: list[tuple[list[str], ModelCapabilities, int]] = [
|
|
(["mimo-*tts*"], CAP_MIMO_TTS, CTX_8K),
|
|
(["gemini-*tts*"], CAP_GEMINI_TTS, CTX_8K),
|
|
(["*minimax-*tts*", "*MiniMax-*tts*"], CAP_MINIMAX_TTS, CTX_8K),
|
|
(["*tts*"], CAP_OPENAI_TTS, CTX_8K),
|
|
(["*gpt*image*"], CAP_OPENAI_IMAGE, CTX_128K),
|
|
(["*gemini*image*", "*nano-banana*"], CAP_GEMINI_IMAGE, CTX_128K),
|
|
(["glm-4.6v*"], CAP_GLM_MULTIMODAL, CTX_128K),
|
|
(["glm-4.7-flash*"], STANDARD_TEXT_TOOL_CAPABILITIES, CTX_128K),
|
|
(["deepseek-v4-pro*", "deepseek-v4-flash*"], CAP_DEEPSEEK_V4, CTX_1M),
|
|
(["glm-4-long*"], STANDARD_TEXT_TOOL_CAPABILITIES, CTX_1M),
|
|
(["*MiniMax-M3*"], CAP_MINIMAX_MULTIMODAL, CTX_1M),
|
|
(["mimo-v2.5-pro*", "mimo-v2-pro*", "mimo-v2-flash*"], CAP_MIMO_TEXT, CTX_1M),
|
|
(["mimo-v2.5", "mimo-v2-omni*"], CAP_MIMO_MULTIMODAL, CTX_1M),
|
|
(["gpt-5.5*", "gpt-5.4*"], CAP_OPENAI_MULTIMODAL, CTX_1M),
|
|
(["gemini-3*pro*"], CAP_GEMINI_3_PRO, CTX_1M),
|
|
(["gemini-3*"], CAP_GEMINI_3_FLASH, CTX_1M),
|
|
(
|
|
["gemini-2.5-pro*", "gemini-2.5-flash*"],
|
|
CAP_GEMINI_2_5,
|
|
CTX_1M,
|
|
),
|
|
(
|
|
["gpt-5*", "gpt-5-mini*", "gpt-5-nano*", "*codex*"],
|
|
CAP_OPENAI_MULTIMODAL,
|
|
CTX_400K,
|
|
),
|
|
(
|
|
["kimi-k2.7*", "kimi-k2.6*", "kimi-k2.5*"],
|
|
DEFAULT_PERMISSIVE_CAPABILITIES,
|
|
CTX_256K,
|
|
),
|
|
(["glm-5v*"], CAP_GLM_MULTIMODAL, CTX_200K),
|
|
(["glm-5*", "glm-4.7*", "glm-4.6*"], CAP_GLM_REASONING, CTX_200K),
|
|
(["*MiniMax-M2*", "*minimax-m2*"], CAP_MINIMAX_REASONING, CTX_200K),
|
|
(["gpt-4*", "gpt-3.5*", "gpt-*"], CAP_OPENAI_MULTIMODAL, CTX_128K),
|
|
(["o1-*", "o3-*"], CAP_OPENAI_REASONING, CTX_128K),
|
|
(["glm-4v*"], CAP_GLM_MULTIMODAL, CTX_128K),
|
|
(
|
|
["glm-4.5*", "glm-4-flashx-*", "glm-4*"],
|
|
STANDARD_TEXT_TOOL_CAPABILITIES,
|
|
CTX_128K,
|
|
),
|
|
(
|
|
["gemini-embedding-2*", "jina-embeddings-v5-omni*"],
|
|
CAP_MULTIMODAL_EMBEDDING,
|
|
CTX_8K,
|
|
),
|
|
(
|
|
["*embedding*", "*Embedding*", "jina-embeddings-*", "bge-m3*", "*bge-large*"],
|
|
CAP_TEXT_EMBEDDING,
|
|
CTX_8K,
|
|
),
|
|
(["*reranker*", "*rerank*", "jina-colbert-*"], CAP_RERANK_ONLY, CTX_8K),
|
|
]
|
|
|
|
|
|
def _build_registry() -> dict[str, ModelCapabilities]:
|
|
"""构建模型能力注册表 (基于声明式路由表)"""
|
|
registry: dict[str, ModelCapabilities] = {}
|
|
|
|
for patterns, cap_template, ctx_limit in _ROUTING_TABLE:
|
|
cap_instance = model_copy(cap_template, update={"max_input_tokens": ctx_limit})
|
|
for pattern in patterns:
|
|
registry[pattern] = cap_instance
|
|
|
|
return registry
|
|
|
|
|
|
MODEL_CAPABILITIES_REGISTRY = _build_registry()
|
|
|
|
|
|
def get_model_capabilities(model_name: str) -> ModelCapabilities:
|
|
"""
|
|
从注册表获取模型能力,支持别名映射和通配符匹配。
|
|
"""
|
|
canonical_name = model_name
|
|
for alias_pattern, c_name in MODEL_ALIAS_MAPPING.items():
|
|
if fnmatch.fnmatch(model_name, alias_pattern):
|
|
canonical_name = c_name
|
|
break
|
|
|
|
parts = canonical_name.split("/")
|
|
names_to_check = ["/".join(parts[i:]) for i in range(len(parts))]
|
|
|
|
for name in names_to_check:
|
|
if name in MODEL_CAPABILITIES_REGISTRY:
|
|
return MODEL_CAPABILITIES_REGISTRY[name]
|
|
|
|
for pattern, capabilities in MODEL_CAPABILITIES_REGISTRY.items():
|
|
if "*" in pattern and fnmatch.fnmatch(name, pattern):
|
|
return capabilities
|
|
|
|
return DEFAULT_PERMISSIVE_CAPABILITIES
|