Compare commits

...

22 Commits

Author SHA1 Message Date
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
3 changed files with 802 additions and 604 deletions

View File

@@ -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()
@@ -511,181 +516,210 @@ 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) # 可临时关闭思考
agent_id_override = data.get("agent_id") # 前端可以指定agent
# 如果前端指定了agent使用它
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
# 获取或创建会话
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")
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
# 获取或创建会话
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))
# 使用非流式调用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

@@ -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