Compare commits

...

30 Commits

Author SHA1 Message Date
142904dff4 feat: 重构工具配置为通用模型,增加使用统计
- ToolConfig 模型:支持多种工具类型(搜索、计算器、代码执行等)
- ToolUsageLog 模型:记录工具调用日志
- 工具使用统计:调用次数、成功率、错误记录
- 后台管理界面:工具列表+统计展示
- API 重构:/api/v2/tools(替代 search-tools)
2026-04-13 16:15:26 +08:00
b9e99da01b fix: 搜索改为勾选即执行,不再依赖关键词检测 2026-04-13 16:00:32 +08:00
34f02ad4d4 fix: 修复数据库缺少tools字段,移动搜索复选框到输入框区域 2026-04-13 13:31:14 +08:00
0c9bfca346 feat: 添加搜索工具功能(Tavily Search)
- 新增 SearchToolConfig 模型:支持搜索工具配置
- Agent 增加 tools 字段:可配置可用工具列表
- 后台管理增加搜索工具配置页面
- Agent 管理增加工具启用开关
- 网页端增加搜索工具禁用复选框
- WebSocket chat 处理增加搜索调用逻辑
- 默认配置 Tavily Search API
2026-04-13 13:26:43 +08:00
85dd206154 fix: 简化新建对话判断条件,只检查无对话ID且无消息 2026-04-13 12:20:56 +08:00
1b9bb1090c fix: 新建对话时检查是否已经是新建状态,避免重复创建空对话 2026-04-13 12:18:03 +08:00
2373040b04 fix: 修复JS语法错误 - 删除重复代码块 2026-04-13 11:26:30 +08:00
f0789d6bbc style: 版本切换控件简化并整合到操作按钮区域 2026-04-13 11:11:06 +08:00
e05233fb4f fix: 修复loading动画不去掉的问题 - isRegenerating标志在user_message时被错误重置 2026-04-13 11:02:32 +08:00
7fa143b5b0 feat: assistant消息支持多版本历史,重新生成保留旧版本+版本切换控件 2026-04-13 10:53:18 +08:00
b573638bf8 fix: 复制按钮使用传统方法确保可用,重新生成不再重复显示用户消息 2026-04-13 10:37:47 +08:00
af997aa5c5 fix: 操作按钮一直显示,不再需要悬停 2026-04-13 10:31:54 +08:00
b1feaee976 feat: 添加浏览器favicon,优化消息操作按钮(复制+重新生成) 2026-04-13 10:29:45 +08:00
87f9f4a7d8 fix: 复制按钮修复 - 使用隐藏input存储原始内容 2026-04-12 22:23:10 +08:00
a1f1032000 fix: 复制按钮功能修复 - 正确存储和读取原始内容 2026-04-12 20:35:37 +08:00
7d6a345a7d fix: 快捷语句添加按钮固定左侧,支持鼠标滚轮横向滚动 2026-04-12 20:24:06 +08:00
c6f157aa97 fix: 快捷语句改为横向扁平布局,支持左右滑动 2026-04-12 20:15:46 +08:00
6e87f59fab feat: 网页端优化 - Markdown渲染、复制按钮、快捷语句右侧布局 2026-04-12 19:11:50 +08:00
066d2fe44d fix: 优化思考内容提取,支持多种思考标记格式 2026-04-12 18:51:30 +08:00
23935a1a28 fix: 简化消息发送流程,直接发送完整回复 2026-04-12 18:41:27 +08:00
08c4f79313 fix: 暂时使用非流式输出确保稳定性 2026-04-12 18:32:35 +08:00
186f69c87a fix: WebSocket断开后正确退出循环 2026-04-12 18:10:28 +08:00
8c90fd5641 fix: 可选链赋值语法兼容性修复 2026-04-12 18:04:29 +08:00
248ac4e471 fix: loadAgents顺序修正 2026-04-12 17:58:47 +08:00
51cc8161f1 fix: 修复DEFAULT_phrases拼写错误导致JS不执行 2026-04-12 17:45:43 +08:00
a62fe929c1 feat: 流式输出支持 - 思考内容流式显示后折叠,回答内容流式输出 2026-04-12 17:16:06 +08:00
6adeb9b371 feat: 切换对话时恢复对应的Agent显示 2026-04-12 17:06:39 +08:00
051fd5c1c8 feat: 切换Agent自动创建新对话 2026-04-12 17:01:05 +08:00
90d31dba69 fix: WebSocket数据库会话问题修复 - 每次消息处理使用新会话 2026-04-12 16:49:39 +08:00
3854d78c9c feat: 网页端Agent切换 + 快捷语句功能 2026-04-12 16:43:39 +08:00
6 changed files with 1814 additions and 1512 deletions

View File

@@ -18,11 +18,11 @@ import os
from models_v2 import (
init_db, get_db, SessionLocal,
User, Conversation, Message, SystemConfig,
LLMProvider, Agent, Channel, ChannelAgentMapping, MatrixRoomMapping,
LLMProvider, Agent, Channel, ChannelAgentMapping, MatrixRoomMapping, ToolConfig, ToolUsageLog,
init_default_data
)
from services.llm_service import llm_service
from services.agent_service import AgentService, LLMProviderService, ChannelService
from services.agent_service import AgentService, LLMProviderService, ChannelService, ToolService
from services.conversation_service import ConversationService
# 配置日志
@@ -60,8 +60,13 @@ class ConnectionManager:
for connection in self.active_connections[user_id]:
try:
await connection.send_json(message)
except:
pass
logger.info(f"发送消息到用户 {user_id}: {message.get('type', 'unknown')}")
except Exception as e:
logger.error(f"发送消息失败: {e}")
async def ping(self, user_id: str):
"""发送心跳ping"""
await self.send_to_user(user_id, {"type": "ping"})
manager = ConnectionManager()
@@ -199,6 +204,7 @@ async def get_agents(db: Session = Depends(get_db)):
"thinking_prompt": a.thinking_prompt,
"thinking_prefix": a.thinking_prefix,
"thinking_suffix": a.thinking_suffix,
"tools": a.tools or [], # 工具列表
"max_history": a.max_history,
"temperature_override": a.temperature_override,
"is_default": a.is_default,
@@ -413,6 +419,162 @@ async def unbind_agent(mapping_id: int, db: Session = Depends(get_db)):
return {"success": success}
# ==================== 工具配置 API ====================
@app.get("/api/v2/tools")
async def get_tools(tool_type: str = None, db: Session = Depends(get_db)):
"""获取所有工具配置"""
service = ToolService(db)
if tool_type:
tools = service.get_tools_by_type(tool_type)
else:
tools = service.get_all_tools()
return {
"tools": [
{
"id": t.id,
"name": t.name,
"tool_type": t.tool_type,
"provider": t.provider,
"config": t.config,
"is_active": t.is_active,
"is_default": t.is_default,
"total_calls": t.total_calls,
"success_calls": t.success_calls,
"failed_calls": t.failed_calls
}
for t in tools
]
}
@app.post("/api/v2/tools")
async def create_tool(data: dict, db: Session = Depends(get_db)):
"""创建工具配置"""
service = ToolService(db)
tool = service.create_tool(data)
return {"success": True, "tool": {"id": tool.id, "name": tool.name, "tool_type": tool.tool_type}}
@app.put("/api/v2/tools/{tool_id}")
async def update_tool(tool_id: int, data: dict, db: Session = Depends(get_db)):
"""更新工具配置"""
service = ToolService(db)
if data.get('is_default'):
service.set_default_tool(tool_id)
tool = service.update_tool(tool_id, data)
if not tool:
return {"success": False, "message": "工具不存在"}
return {"success": True, "tool": {"id": tool.id, "name": tool.name}}
@app.delete("/api/v2/tools/{tool_id}")
async def delete_tool(tool_id: int, db: Session = Depends(get_db)):
"""删除工具配置"""
service = ToolService(db)
success = service.delete_tool(tool_id)
return {"success": success}
@app.post("/api/v2/tools/{tool_id}/default")
async def set_tool_default(tool_id: int, db: Session = Depends(get_db)):
"""设置默认工具"""
service = ToolService(db)
success = service.set_default_tool(tool_id)
return {"success": success}
@app.get("/api/v2/tools/stats")
async def get_tool_stats(days: int = 7, db: Session = Depends(get_db)):
"""获取工具使用统计"""
service = ToolService(db)
stats = service.get_usage_stats(days=days)
return stats
@app.post("/api/v2/tools/search")
async def perform_search(data: dict, db: Session = Depends(get_db)):
"""执行搜索供前端或Agent调用"""
import httpx
import time
query = data.get('query')
if not query:
return {"success": False, "message": "缺少搜索关键词"}
service = ToolService(db)
tool_id = data.get('tool_id')
if tool_id:
tool = service.get_tool(tool_id)
else:
tool = service.get_default_tool('search')
if not tool or not tool.config.get('api_key'):
return {"success": False, "message": "未配置搜索工具"}
# Tavily Search API
if tool.provider == 'tavily' or tool.tool_type == 'search':
start_time = time.time()
try:
tavily_url = "https://api.tavily.com/search"
config = tool.config
payload = {
"api_key": config.get('api_key'),
"query": query,
"max_results": config.get('max_results', 5),
"include_raw_content": config.get('include_raw_content', False),
"search_depth": config.get('search_depth', 'basic')
}
async with httpx.AsyncClient(timeout=30) as client:
response = await client.post(tavily_url, json=payload)
result = response.json()
duration_ms = int((time.time() - start_time) * 1000)
# 更新统计和日志
service.increment_stats(tool.id, True)
service.log_usage({
'tool_id': tool.id,
'tool_type': 'search',
'query': query,
'success': True,
'result_summary': f'{len(result.get("results", []))} results',
'conversation_id': data.get('conversation_id'),
'agent_id': data.get('agent_id'),
'duration_ms': duration_ms
})
return {
"success": True,
"results": result.get("results", []),
"query": query
}
except Exception as e:
duration_ms = int((time.time() - start_time) * 1000)
service.increment_stats(tool.id, False)
service.log_usage({
'tool_id': tool.id,
'tool_type': 'search',
'query': query,
'success': False,
'error_message': str(e),
'conversation_id': data.get('conversation_id'),
'duration_ms': duration_ms
})
return {"success": False, "message": str(e)}
return {"success": False, "message": "不支持的搜索提供商"}
# ==================== 对话 API保留原有 ====================
@app.get("/api/conversations")
@@ -511,174 +673,289 @@ async def delete_conversation(conversation_id: str, db: Session = Depends(get_db
# ==================== WebSocket路由 ====================
@app.websocket("/ws/{user_id}")
async def websocket_endpoint(websocket: WebSocket, user_id: str, db: Session = Depends(get_db)):
async def websocket_endpoint(websocket: WebSocket, user_id: str):
"""WebSocket连接 - 实时对话"""
actual_user_id = MAIN_USER_ID
await manager.connect(websocket, actual_user_id)
conv_service = ConversationService(db)
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
# 获取默认Agent配置
agent_service = AgentService(db)
default_agent = agent_service.get_default_agent()
# 初始化时获取默认Agent ID
db = SessionLocal()
try:
agent_service = AgentService(db)
default_agent = agent_service.get_default_agent()
default_agent_id = default_agent.id if default_agent else None
finally:
db.close()
current_conversation_id = None
current_agent_id = default_agent.id if default_agent else None
current_agent_id = default_agent_id
try:
while True:
data = await websocket.receive_json()
action = data.get("action")
if action == "select_conversation":
current_conversation_id = data.get("conversation_id")
conversation = conv_service.get_conversation(current_conversation_id)
if conversation:
messages = conv_service.get_messages(conversation.id)
await websocket.send_json({
"type": "history",
"conversation_id": current_conversation_id,
"messages": [
{
"role": m.role,
"content": m.content,
"thinking_content": m.thinking_content,
"source": m.source,
"created_at": m.created_at.isoformat()
}
for m in messages
]
})
elif action == "switch_agent":
# 切换Agent
new_agent_id = data.get("agent_id")
agent = agent_service.get_agent(new_agent_id)
if agent and agent.is_active:
current_agent_id = new_agent_id
await websocket.send_json({
"type": "agent_switched",
"agent_id": current_agent_id,
"agent_name": agent.display_name or agent.name
})
elif action == "chat":
message = data.get("message", "")
conversation_id = data.get("conversation_id")
enable_thinking = data.get("enable_thinking", True) # 可临时关闭思考
if not message.strip():
continue
# 获取或创建会话
if conversation_id:
conversation = conv_service.get_conversation(conversation_id)
else:
conversation = conv_service.create_conversation(user.id)
conversation_id = conversation.conversation_id
await websocket.send_json({
"type": "conversation_created",
"conversation_id": conversation_id
})
# 保存用户消息
user_msg = conv_service.add_message(
conversation_id=conversation.id,
role='user',
content=message,
source='web'
)
# 广播用户消息
await manager.send_to_user(MAIN_USER_ID, {
"type": "user_message",
"conversation_id": conversation_id,
"message": {
"id": user_msg.id,
"role": "user",
"content": message,
"source": "web",
"created_at": user_msg.created_at.isoformat()
}
})
# 获取Agent配置
agent_config = agent_service.get_agent_config(current_agent_id)
if not agent_config or not agent_config.get('provider'):
await websocket.send_json({
"type": "error",
"message": "Agent配置不完整"
})
continue
# 获取对话历史
history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20))
# 调用LLM
try:
data = await websocket.receive_json()
except Exception as json_err:
logger.error(f"JSON解析错误: {json_err}")
# 如果连接已断开,退出循环
if "disconnect" in str(json_err).lower() or "closed" in str(json_err).lower():
logger.info("WebSocket已断开退出循环")
break
try:
# 发送"正在思考"状态
if agent_config['agent'].get('enable_thinking') and enable_thinking:
text_data = await websocket.receive_text()
if text_data.strip():
data = json.loads(text_data)
else:
continue
except Exception as text_err:
logger.error(f"文本消息解析错误: {text_err}")
if "disconnect" in str(text_err).lower() or "closed" in str(text_err).lower():
logger.info("WebSocket已断开退出循环")
break
continue
action = data.get("action")
logger.info(f"WebSocket收到消息: action={action}")
# 每次消息处理时创建新的数据库会话,处理完后关闭
try:
db = SessionLocal()
conv_service = ConversationService(db)
agent_service = AgentService(db)
user = conv_service.get_or_create_user(MAIN_USER_ID, display_name="主用户", user_type='web')
if action == "select_conversation":
current_conversation_id = data.get("conversation_id")
conversation = conv_service.get_conversation(current_conversation_id)
if conversation:
messages = conv_service.get_messages(conversation.id)
# 获取对话使用的Agent ID
conv_agent_id = conversation.current_agent_id
await websocket.send_json({
"type": "thinking_start",
"type": "history",
"conversation_id": current_conversation_id,
"agent_id": conv_agent_id, # 返回对话的Agent ID
"messages": [
{
"role": m.role,
"content": m.content,
"thinking_content": m.thinking_content,
"agent_id": m.agent_id, # 每条消息的Agent ID
"source": m.source,
"created_at": m.created_at.isoformat()
}
for m in messages
]
})
elif action == "switch_agent":
# 切换Agent
new_agent_id = data.get("agent_id")
agent = agent_service.get_agent(new_agent_id)
if agent and agent.is_active:
current_agent_id = new_agent_id
await websocket.send_json({
"type": "agent_switched",
"agent_id": current_agent_id,
"agent_name": agent.display_name or agent.name
})
elif action == "chat":
message = data.get("message", "")
conversation_id = data.get("conversation_id")
enable_thinking = data.get("enable_thinking", True)
agent_id_override = data.get("agent_id")
disabled_tools = data.get("disabled_tools", []) # 禁用的工具列表
if agent_id_override:
agent = agent_service.get_agent(agent_id_override)
if agent and agent.is_active:
current_agent_id = agent_id_override
if not message.strip():
continue
# 获取Agent配置
agent_config = agent_service.get_agent_config(current_agent_id)
agent_tools = agent_config.get('agent', {}).get('tools', [])
# 检查是否需要执行搜索
search_context = None
if 'search' in agent_tools and 'search' not in disabled_tools:
# 只要启用了搜索工具且未禁用,就执行搜索(不再依赖关键词检测)
should_search = True
if should_search:
# 执行搜索
tool_service = ToolService(db)
search_tool = tool_service.get_default_tool('search')
if search_tool and search_tool.config.get('api_key'):
import httpx
import time
start_time = time.time()
try:
logger.info(f"执行搜索: query={message}")
tavily_url = "https://api.tavily.com/search"
config = search_tool.config
payload = {
"api_key": config.get('api_key'),
"query": message,
"max_results": config.get('max_results', 5),
"search_depth": config.get('search_depth', 'basic')
}
# 同步调用
with httpx.Client(timeout=30) as client:
resp = client.post(tavily_url, json=payload)
search_result = resp.json()
duration_ms = int((time.time() - start_time) * 1000)
if search_result.get("results"):
# 构建搜索上下文
search_context = "\n\n【搜索结果】\n"
for i, r in enumerate(search_result["results"][:5], 1):
search_context += f"{i}. {r.get('title', 'N/A')}\n {r.get('content', r.get('snippet', 'N/A'))[:200]}\n 来源: {r.get('url', 'N/A')}\n"
logger.info(f"搜索完成: {len(search_result['results'])} 条结果")
# 更新统计和日志
tool_service.increment_stats(search_tool.id, True)
tool_service.log_usage({
'tool_id': search_tool.id,
'tool_type': 'search',
'query': message,
'success': True,
'result_summary': f'{len(search_result["results"])} results',
'conversation_id': conversation_id,
'agent_id': current_agent_id,
'duration_ms': duration_ms
})
except Exception as e:
duration_ms = int((time.time() - start_time) * 1000)
logger.error(f"搜索失败: {e}")
tool_service.increment_stats(search_tool.id, False)
tool_service.log_usage({
'tool_id': search_tool.id,
'tool_type': 'search',
'query': message,
'success': False,
'error_message': str(e),
'conversation_id': conversation_id,
'duration_ms': duration_ms
})
# 获取或创建会话
if conversation_id:
conversation = conv_service.get_conversation(conversation_id)
else:
conversation = conv_service.create_conversation(user.id)
conversation_id = conversation.conversation_id
await websocket.send_json({
"type": "conversation_created",
"conversation_id": conversation_id
})
response, thinking_content = await llm_service.chat(
messages=history,
provider_config=agent_config['provider'],
agent_config=agent_config['agent'],
enable_thinking=enable_thinking
)
# 发送思考内容
if thinking_content:
await websocket.send_json({
"type": "thinking_content",
"conversation_id": conversation_id,
"content": thinking_content
})
# 发送思考结束
await websocket.send_json({
"type": "thinking_end",
"conversation_id": conversation_id
})
# 保存AI回复
assistant_msg = conv_service.add_message(
# 保存用户消息
user_msg = conv_service.add_message(
conversation_id=conversation.id,
role='assistant',
content=response,
source='web',
thinking_content=thinking_content,
agent_id=current_agent_id,
model_used=agent_config['provider'].get('default_model')
role='user',
content=message,
source='web'
)
# 广播AI回复
# 广播用户消息
await manager.send_to_user(MAIN_USER_ID, {
"type": "assistant_message",
"type": "user_message",
"conversation_id": conversation_id,
"message": {
"id": assistant_msg.id,
"role": "assistant",
"content": response,
"thinking_content": thinking_content,
"id": user_msg.id,
"role": "user",
"content": message,
"source": "web",
"agent_id": current_agent_id,
"agent_name": agent_config['agent'].get('display_name'),
"created_at": assistant_msg.created_at.isoformat()
"created_at": user_msg.created_at.isoformat()
}
})
except Exception as e:
logger.error(f"LLM调用失败: {e}")
await websocket.send_json({
"type": "error",
"message": f"AI服务暂时不可用: {str(e)}"
})
# 获取Agent配置
agent_config = agent_service.get_agent_config(current_agent_id)
if not agent_config or not agent_config.get('provider'):
await websocket.send_json({
"type": "error",
"message": "Agent配置不完整"
})
continue
# 获取对话历史
history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20))
# 如果有搜索结果,添加到消息中
if search_context:
# 在系统提示中添加搜索结果说明
modified_system_prompt = agent_config['agent'].get('system_prompt', '') + "\n\n如果提供了搜索结果,请基于搜索结果回答用户问题,并注明信息来源。"
agent_config['agent']['system_prompt'] = modified_system_prompt
# 将搜索结果作为系统消息添加到历史
history.append({"role": "system", "content": f"以下是搜索到的相关信息,请参考这些内容回答用户问题:{search_context}"})
# 使用非流式调用LLM简化版本确保稳定
try:
# 调用LLM非流式
response, thinking_content = await llm_service.chat(
messages=history,
provider_config=agent_config['provider'],
agent_config=agent_config['agent'],
enable_thinking=enable_thinking
)
logger.info(f"LLM响应: response长度={len(response)}, thinking长度={len(thinking_content) if thinking_content else 0}")
# 保存AI回复
assistant_msg = conv_service.add_message(
conversation_id=conversation.id,
role='assistant',
content=response,
source='web',
thinking_content=thinking_content if thinking_content else None,
agent_id=current_agent_id,
model_used=agent_config['provider'].get('default_model')
)
# 发送完整回复(包含思考内容)
await websocket.send_json({
"type": "assistant_message",
"conversation_id": conversation_id,
"message": {
"id": assistant_msg.id,
"role": "assistant",
"content": response,
"thinking_content": thinking_content if thinking_content else None,
"source": "web",
"agent_id": current_agent_id,
"agent_name": agent_config['agent'].get('display_name'),
"created_at": assistant_msg.created_at.isoformat()
}
})
logger.info(f"AI回复已发送: conversation_id={conversation_id}")
# 启用发送按钮
await websocket.send_json({
"type": "stream_end",
"conversation_id": conversation_id
})
except Exception as e:
logger.error(f"LLM调用失败: {e}")
await websocket.send_json({
"type": "error",
"message": f"AI服务暂时不可用: {str(e)}"
})
finally:
db.close()
except WebSocketDisconnect:
manager.disconnect(websocket, user_id)

View File

@@ -58,6 +58,9 @@ class Agent(Base):
name = Column(String(100), unique=True, index=True) # Agent名称
display_name = Column(String(100)) # 显示名称
# 工具配置
tools = Column(JSON, default=list) # 可用工具列表 ["search", "calculator", ...]
# 大模型配置
llm_provider_id = Column(Integer, ForeignKey('llm_providers.id'))
model_override = Column(String(100), nullable=True) # 覆盖Provider默认模型
@@ -224,6 +227,60 @@ class MatrixRoomMapping(Base):
created_at = Column(DateTime, default=datetime.utcnow)
# ==================== 搜索工具配置 ====================
class ToolConfig(Base):
"""工具配置(通用,支持搜索、计算器、代码执行等)"""
__tablename__ = 'tool_configs'
id = Column(Integer, primary_key=True, index=True)
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配置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) # 是否为该类型的默认工具
# 统计
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):
@@ -335,6 +392,18 @@ def init_default_data():
)
db.add(matrix_mapping)
# 5. 创建默认搜索工具配置
search_config = SearchToolConfig(
name="Tavily Search",
provider="tavily",
api_key="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",
max_results=5,
search_depth="basic",
is_active=True,
is_default=True
)
db.add(search_config)
db.commit()
print("默认数据初始化完成")

View File

@@ -5,7 +5,7 @@ from sqlalchemy.orm import Session
from typing import List, Optional, Dict
import logging
from models_v2 import Agent, LLMProvider, ChannelAgentMapping, Channel, init_default_data
from models_v2 import Agent, LLMProvider, ChannelAgentMapping, Channel, ToolConfig, ToolUsageLog, init_default_data
logger = logging.getLogger(__name__)
@@ -52,6 +52,7 @@ class AgentService:
thinking_prompt=data.get('thinking_prompt'),
thinking_prefix=data.get('thinking_prefix', ''),
thinking_suffix=data.get('thinking_suffix', ''),
tools=data.get('tools', []), # 工具列表
max_history=data.get('max_history', 20),
temperature_override=data.get('temperature_override'),
is_active=data.get('is_active', True),
@@ -405,4 +406,172 @@ class ChannelService:
'is_active': channel.is_active,
'is_primary': channel.is_primary,
'agent_mappings': self.get_channel_agents(channel_id)
}
}
class ToolService:
"""工具管理服务"""
def __init__(self, db: Session):
self.db = db
def get_all_tools(self) -> List[ToolConfig]:
"""获取所有工具配置"""
return self.db.query(ToolConfig).order_by(ToolConfig.tool_type, ToolConfig.is_default.desc()).all()
def get_tools_by_type(self, tool_type: str) -> List[ToolConfig]:
"""获取指定类型的工具"""
return self.db.query(ToolConfig).filter(ToolConfig.tool_type == tool_type).all()
def get_active_tools(self) -> List[ToolConfig]:
"""获取活跃的工具"""
return self.db.query(ToolConfig).filter(ToolConfig.is_active == True).all()
def get_tool(self, tool_id: int) -> Optional[ToolConfig]:
"""获取单个工具"""
return self.db.query(ToolConfig).filter(ToolConfig.id == tool_id).first()
def get_default_tool(self, tool_type: str) -> Optional[ToolConfig]:
"""获取指定类型的默认工具"""
tool = self.db.query(ToolConfig).filter(
ToolConfig.tool_type == tool_type,
ToolConfig.is_default == True,
ToolConfig.is_active == True
).first()
if not tool:
tool = self.db.query(ToolConfig).filter(
ToolConfig.tool_type == tool_type,
ToolConfig.is_active == True
).first()
return tool
def create_tool(self, data: Dict) -> ToolConfig:
"""创建工具配置"""
tool = ToolConfig(
name=data.get('name'),
tool_type=data.get('tool_type', 'search'),
provider=data.get('provider'),
config=data.get('config', {}),
is_active=data.get('is_active', True),
is_default=data.get('is_default', False)
)
self.db.add(tool)
self.db.commit()
self.db.refresh(tool)
return tool
def update_tool(self, tool_id: int, data: Dict) -> Optional[ToolConfig]:
"""更新工具配置"""
tool = self.get_tool(tool_id)
if not tool:
return None
for key, value in data.items():
if hasattr(tool, key) and value is not None:
setattr(tool, key, value)
self.db.commit()
self.db.refresh(tool)
return tool
def delete_tool(self, tool_id: int) -> bool:
"""删除工具配置"""
tool = self.get_tool(tool_id)
if not tool:
return False
self.db.delete(tool)
self.db.commit()
return True
def set_default_tool(self, tool_id: int) -> bool:
"""设置默认工具"""
tool = self.get_tool(tool_id)
if not tool:
return False
# 清除同类型的其他默认
self.db.query(ToolConfig).filter(
ToolConfig.tool_type == tool.tool_type
).update({ToolConfig.is_default: False})
tool.is_default = True
self.db.commit()
return True
def increment_stats(self, tool_id: int, success: bool):
"""更新工具调用统计"""
tool = self.get_tool(tool_id)
if tool:
tool.total_calls += 1
if success:
tool.success_calls += 1
else:
tool.failed_calls += 1
self.db.commit()
def log_usage(self, data: Dict) -> ToolUsageLog:
"""记录工具使用日志"""
log = ToolUsageLog(
tool_id=data.get('tool_id'),
tool_type=data.get('tool_type'),
query=data.get('query'),
success=data.get('success', True),
error_message=data.get('error_message'),
result_summary=data.get('result_summary'),
conversation_id=data.get('conversation_id'),
agent_id=data.get('agent_id'),
user_id=data.get('user_id'),
duration_ms=data.get('duration_ms')
)
self.db.add(log)
self.db.commit()
self.db.refresh(log)
return log
def get_usage_stats(self, days: int = 7) -> Dict:
"""获取工具使用统计"""
from datetime import timedelta
start_date = datetime.utcnow() - timedelta(days=days)
# 按工具类型统计
logs = self.db.query(ToolUsageLog).filter(
ToolUsageLog.called_at >= start_date
).all()
stats = {
'total_calls': len(logs),
'success_rate': sum(1 for l in logs if l.success) / len(logs) * 100 if logs else 0,
'by_type': {},
'by_tool': {},
'recent_errors': []
}
for log in logs:
# 按类型
if log.tool_type not in stats['by_type']:
stats['by_type'][log.tool_type] = {'total': 0, 'success': 0, 'failed': 0}
stats['by_type'][log.tool_type]['total'] += 1
if log.success:
stats['by_type'][log.tool_type]['success'] += 1
else:
stats['by_type'][log.tool_type]['failed'] += 1
# 按工具
tool = self.get_tool(log.tool_id) if log.tool_id else None
tool_name = tool.name if tool else f'Tool#{log.tool_id}'
if tool_name not in stats['by_tool']:
stats['by_tool'][tool_name] = {'total': 0, 'success': 0}
stats['by_tool'][tool_name]['total'] += 1
if log.success:
stats['by_tool'][tool_name]['success'] += 1
# 最近错误
if not log.success and log.error_message:
stats['recent_errors'].append({
'tool': tool_name,
'error': log.error_message[:100],
'time': log.called_at.isoformat()
})
return stats

View File

@@ -128,6 +128,8 @@ class LLMService:
# 处理思考功能
if enable_thinking and agent_config.get('enable_thinking', True):
thinking_prompt = agent_config.get('thinking_prompt')
thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '')
if supports_thinking and thinking_model:
# 使用专门的思考模型
@@ -139,22 +141,15 @@ class LLMService:
thinking_result = await self._call_api(
api_base, api_key, thinking_model, thinking_messages,
max_tokens=min(max_tokens, 1000),
temperature=0.3 # 思考时降低温度
temperature=0.3
)
thinking_content = thinking_result
except Exception as e:
logger.warning(f"思考模型调用失败: {e}")
elif supports_thinking:
# Provider支持思考但无单独模型尝试在回复中获取思考部分
pass # 在回复解析时处理
elif thinking_prompt:
# Provider不支持思考Agent配置了思考提示词
# 将思考提示词添加到系统提示
enhanced_system = f"{system_prompt}\n\n在回答之前,请先思考问题。思考过程请用{agent_config.get('thinking_prefix', '')}{agent_config.get('thinking_suffix', '')}包裹,然后再给出正式回答。"
if thinking_prompt:
enhanced_system += f"\n思考指导:{thinking_prompt}"
# Agent配置了思考提示词,添加到系统提示中
enhanced_system = f"{system_prompt}\n\n{thinking_prompt}"
final_messages[0] = {"role": "system", "content": enhanced_system}
# 调用主模型
@@ -165,19 +160,37 @@ class LLMService:
temperature=temperature
)
# 尝试从回复中提取思考内容
if enable_thinking and not supports_thinking:
# 尝试从回复中提取思考内容支持DeepSeek R1、GLM等模型的思考模式
if enable_thinking and agent_config.get('enable_thinking', True):
thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '')
if thinking_prefix and thinking_suffix:
# 提取思考部分
pattern = f"{re.escape(thinking_prefix)}(.*?)?{re.escape(thinking_suffix)}"
match = re.search(pattern, response, re.DOTALL)
if match:
thinking_content = match.group(1).strip()
# 移除思考部分,只保留回复
response = re.sub(pattern, '', response, flags=re.DOTALL).strip()
# 如果没有配置前缀后缀,使用常见的思考标记
if not thinking_prefix:
# 尝试常见的思考标记
common_thinking_markers = [
('<think>', '</think>'),
('【思考】', '【回答】'),
('Thought:', 'Answer:'),
('思考:', '回答:'),
]
for prefix, suffix in common_thinking_markers:
if prefix in response and suffix in response:
thinking_prefix = prefix
thinking_suffix = suffix
break
# 提取思考部分
if thinking_prefix and thinking_suffix and thinking_prefix in response:
try:
start_idx = response.find(thinking_prefix)
end_idx = response.find(thinking_suffix, start_idx)
if end_idx > start_idx:
thinking_content = response[start_idx + len(thinking_prefix):end_idx].strip()
# 移除思考部分,只保留回复
response = response[end_idx + len(thinking_suffix):].strip()
except Exception as e:
logger.warning(f"提取思考内容失败: {e}")
return response, thinking_content
@@ -268,8 +281,7 @@ class LLMService:
thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '')
in_thinking = False
thinking_buffer = ""
buffer = "" # 用于累积和检测思考部分
async with httpx.AsyncClient(timeout=60.0) as client:
async with client.stream("POST", url, headers=headers, json=payload) as response:
@@ -284,35 +296,46 @@ class LLMService:
delta = data['choices'][0].get('delta', {})
if 'content' in delta:
text = delta['content']
buffer += text
# 检测思考部分
if thinking_prefix and thinking_suffix:
for char in text:
if in_thinking:
thinking_buffer += char
# 检查是否结束思考
if thinking_buffer.endswith(thinking_suffix):
thinking_content = thinking_buffer[:-len(thinking_suffix)]
yield {"type": "thinking", "text": thinking_content}
in_thinking = False
thinking_buffer = ""
# 检测思考部分(简化逻辑)
if thinking_prefix and thinking_suffix and thinking_prefix in buffer:
# 尝试解析思考部分
try:
start_idx = buffer.find(thinking_prefix)
if start_idx >= 0:
# 找到思考开始,继续找结束
end_idx = buffer.find(thinking_suffix, start_idx)
if end_idx > start_idx:
# 思考部分完整,发送思考然后发送内容
thinking = buffer[start_idx + len(thinking_prefix):end_idx]
yield {"type": "thinking", "text": thinking}
# 发送思考后的内容
remaining = buffer[end_idx + len(thinking_suffix):]
if remaining:
yield {"type": "content", "text": remaining}
buffer = ""
else:
# 检查是否接近结束
suffix_len = len(thinking_suffix)
if len(thinking_buffer) >= suffix_len:
yield {"type": "thinking", "text": thinking_buffer[-suffix_len:]}
# 思考部分还没结束,先发送之前的内容
if start_idx > 0:
yield {"type": "content", "text": buffer[:start_idx]}
# 等待更多数据来完成思考部分
buffer = buffer[start_idx:]
else:
if char == thinking_prefix[0]:
# 可能开始思考
thinking_buffer = char
if len(thinking_prefix) == 1:
in_thinking = True
else:
yield {"type": "content", "text": char}
# 没有思考标记,直接发送内容
yield {"type": "content", "text": text}
buffer = ""
except:
yield {"type": "content", "text": text}
else:
# 没有思考标记配置,直接发送内容
yield {"type": "content", "text": text}
except json.JSONDecodeError:
continue
# 处理剩余buffer
if buffer:
yield {"type": "content", "text": buffer}
# 全局实例

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff