feat: 重构工具配置为通用模型,增加使用统计

- ToolConfig 模型:支持多种工具类型(搜索、计算器、代码执行等)
- ToolUsageLog 模型:记录工具调用日志
- 工具使用统计:调用次数、成功率、错误记录
- 后台管理界面:工具列表+统计展示
- API 重构:/api/v2/tools(替代 search-tools)
This commit is contained in:
2026-04-13 16:15:26 +08:00
parent b9e99da01b
commit 142904dff4
4 changed files with 513 additions and 231 deletions

View File

@@ -229,31 +229,58 @@ class MatrixRoomMapping(Base):
# ==================== 搜索工具配置 ====================
class SearchToolConfig(Base):
"""搜索工具配置(Tavily等)"""
__tablename__ = 'search_tool_config'
class ToolConfig(Base):
"""工具配置(通用,支持搜索、计算器、代码执行等)"""
__tablename__ = 'tool_configs'
id = Column(Integer, primary_key=True, index=True)
name = Column(String(100)) # 工具名称,如 "Tavily Search"
provider = Column(String(50)) # 提供商tavily, google, bing
name = Column(String(100)) # 工具名称,如 "Tavily Search"、"Calculator"
tool_type = Column(String(50), index=True) # 工具类型search, calculator, code_runner, image_gen, etc.
provider = Column(String(50), nullable=True) # 提供商可选tavily, google, wolfram, etc.
# API配置
api_key = Column(String(200)) # API密钥
api_base = Column(String(200), nullable=True) # API地址可选
# 搜索参数
max_results = Column(Integer, default=5) # 最大返回结果数
include_raw_content = Column(Boolean, default=False) # 是否包含原始内容
search_depth = Column(String(20), default='basic') # basic 或 advanced
# API配置JSON不同工具可能有不同配置
config = Column(JSON, default=dict)
# search示例: {"api_key": "xxx", "max_results": 5, "search_depth": "basic"}
# calculator示例: {"api_base": "xxx"}
# 状态
is_active = Column(Boolean, default=True)
is_default = Column(Boolean, default=False) # 是否为默认搜索工具
is_default = Column(Boolean, default=False) # 是否为该类型的默认工具
# 统计
total_calls = Column(Integer, default=0) # 总调用次数
success_calls = Column(Integer, default=0) # 成功次数
failed_calls = Column(Integer, default=0) # 失败次数
created_at = Column(DateTime, default=datetime.utcnow)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class ToolUsageLog(Base):
"""工具使用日志"""
__tablename__ = 'tool_usage_logs'
id = Column(Integer, primary_key=True, index=True)
tool_id = Column(Integer, ForeignKey('tool_configs.id'))
tool_type = Column(String(50), index=True)
# 调用信息
query = Column(Text) # 调用参数/查询内容
success = Column(Boolean, default=True)
error_message = Column(Text, nullable=True)
result_summary = Column(Text, nullable=True) # 结果摘要
# 关联信息
conversation_id = Column(String(100), nullable=True)
agent_id = Column(Integer, nullable=True)
user_id = Column(String(100), nullable=True)
# 性能
duration_ms = Column(Integer, nullable=True) # 调用耗时(毫秒)
called_at = Column(DateTime, default=datetime.utcnow)
# ==================== 系统配置(保留) ====================
class SystemConfig(Base):