diff --git a/main_v2.py b/main_v2.py index ca2fcf0..6afb527 100644 --- a/main_v2.py +++ b/main_v2.py @@ -659,58 +659,45 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str): # 获取对话历史 history = conv_service.get_conversation_history(conversation_id, limit=agent_config['agent'].get('max_history', 20)) - # 使用流式调用LLM + # 使用非流式调用LLM(简化版本,确保稳定) try: - thinking_buffer = "" - content_buffer = "" - in_thinking = False + # 先发送思考开始 + if agent_config['agent'].get('enable_thinking') and enable_thinking: + await websocket.send_json({ + "type": "thinking_start", + "conversation_id": conversation_id + }) - # 流式生成 - async for chunk in llm_service.chat_stream( + # 调用LLM(非流式) + response, thinking_content = await llm_service.chat( messages=history, provider_config=agent_config['provider'], agent_config=agent_config['agent'], enable_thinking=enable_thinking - ): - chunk_type = chunk.get('type', 'content') - chunk_text = chunk.get('text', '') - - if chunk_type == 'thinking': - if not in_thinking: - in_thinking = True - await websocket.send_json({ - "type": "thinking_start", - "conversation_id": conversation_id - }) - thinking_buffer += chunk_text - await websocket.send_json({ - "type": "thinking_stream", - "conversation_id": conversation_id, - "text": chunk_text - }) - elif chunk_type == 'content': - if in_thinking: - in_thinking = False - await websocket.send_json({ - "type": "thinking_end", - "conversation_id": conversation_id, - "content": thinking_buffer # 发送完整思考内容用于折叠 - }) - content_buffer += chunk_text - await websocket.send_json({ - "type": "content_stream", - "conversation_id": conversation_id, - "text": chunk_text - }) + ) - # 如果思考没结束就结束了,发送思考结束 - if in_thinking: + # 发送思考内容 + if thinking_content: await websocket.send_json({ - "type": "thinking_end", + "type": "thinking_content", "conversation_id": conversation_id, - "content": thinking_buffer + "content": thinking_content }) + # 发送思考结束 + await websocket.send_json({ + "type": "thinking_end", + "conversation_id": conversation_id, + "content": thinking_content + }) + + # 直接发送完整回复 + await websocket.send_json({ + "type": "content_stream", + "conversation_id": conversation_id, + "text": response + }) + # 发送完成通知 await websocket.send_json({ "type": "stream_end", @@ -721,9 +708,9 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str): assistant_msg = conv_service.add_message( conversation_id=conversation.id, role='assistant', - content=content_buffer, + content=response, source='web', - thinking_content=thinking_buffer if thinking_buffer else None, + thinking_content=thinking_content if thinking_content else None, agent_id=current_agent_id, model_used=agent_config['provider'].get('default_model') ) @@ -734,8 +721,8 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str): "message": { "id": assistant_msg.id, "role": "assistant", - "content": content_buffer, - "thinking_content": thinking_buffer if thinking_buffer else None, + "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'),