feat: v2.0.0 架构重构 - 大模型池、Agent管理、渠道独立绑定、思考功能
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
"""
|
||||
会话管理服务
|
||||
会话管理服务 - v2.0 兼容版
|
||||
支持思考内容、Agent追踪等新字段
|
||||
"""
|
||||
from typing import List, Optional
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
import uuid
|
||||
|
||||
from models import User, Conversation, Message
|
||||
# 兼容v1和v2模型
|
||||
try:
|
||||
from models_v2 import User, Conversation, Message, Agent
|
||||
USE_V2 = True
|
||||
except ImportError:
|
||||
from models import User, Conversation, Message
|
||||
USE_V2 = False
|
||||
|
||||
|
||||
class ConversationService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.use_v2 = USE_V2
|
||||
|
||||
def get_or_create_user(self, user_id: str, display_name: str = None, user_type: str = 'web', matrix_user_id: str = None) -> User:
|
||||
"""获取或创建用户"""
|
||||
@@ -31,14 +39,24 @@ class ConversationService:
|
||||
self.db.commit()
|
||||
return user
|
||||
|
||||
def create_conversation(self, user_id: int, title: str = None) -> Conversation:
|
||||
def create_conversation(self, user_id: int, title: str = None, channel_id: int = None) -> Conversation:
|
||||
"""创建新会话"""
|
||||
conversation_id = f"conv_{uuid.uuid4().hex[:12]}"
|
||||
conversation = Conversation(
|
||||
conversation_id=conversation_id,
|
||||
user_id=user_id,
|
||||
title=title
|
||||
)
|
||||
|
||||
if self.use_v2:
|
||||
conversation = Conversation(
|
||||
conversation_id=conversation_id,
|
||||
user_id=user_id,
|
||||
title=title,
|
||||
channel_id=channel_id
|
||||
)
|
||||
else:
|
||||
conversation = Conversation(
|
||||
conversation_id=conversation_id,
|
||||
user_id=user_id,
|
||||
title=title
|
||||
)
|
||||
|
||||
self.db.add(conversation)
|
||||
self.db.commit()
|
||||
self.db.refresh(conversation)
|
||||
@@ -57,15 +75,39 @@ class ConversationService:
|
||||
Conversation.is_active == True
|
||||
).order_by(Conversation.updated_at.desc()).all()
|
||||
|
||||
def add_message(self, conversation_id: int, role: str, content: str, source: str = 'web', extra_data: dict = None) -> Message:
|
||||
def add_message(
|
||||
self,
|
||||
conversation_id: int,
|
||||
role: str,
|
||||
content: str,
|
||||
source: str = 'web',
|
||||
extra_data: dict = None,
|
||||
thinking_content: str = None, # v2新增
|
||||
agent_id: int = None, # v2新增
|
||||
model_used: str = None # v2新增
|
||||
) -> Message:
|
||||
"""添加消息"""
|
||||
message = Message(
|
||||
conversation_id=conversation_id,
|
||||
role=role,
|
||||
content=content,
|
||||
source=source,
|
||||
extra_data=extra_data
|
||||
)
|
||||
|
||||
if self.use_v2:
|
||||
message = Message(
|
||||
conversation_id=conversation_id,
|
||||
role=role,
|
||||
content=content,
|
||||
source=source,
|
||||
extra_data=extra_data,
|
||||
thinking_content=thinking_content,
|
||||
agent_id=agent_id,
|
||||
model_used=model_used
|
||||
)
|
||||
else:
|
||||
message = Message(
|
||||
conversation_id=conversation_id,
|
||||
role=role,
|
||||
content=content,
|
||||
source=source,
|
||||
extra_data=extra_data
|
||||
)
|
||||
|
||||
self.db.add(message)
|
||||
|
||||
# 更新会话时间
|
||||
@@ -75,6 +117,10 @@ class ConversationService:
|
||||
# 如果没有标题,用第一条用户消息作为标题
|
||||
if not conversation.title and role == 'user':
|
||||
conversation.title = content[:50] + ('...' if len(content) > 50 else '')
|
||||
|
||||
# v2: 更新当前Agent
|
||||
if self.use_v2 and agent_id:
|
||||
conversation.current_agent_id = agent_id
|
||||
|
||||
self.db.commit()
|
||||
self.db.refresh(message)
|
||||
@@ -93,7 +139,8 @@ class ConversationService:
|
||||
return []
|
||||
|
||||
messages = self.db.query(Message).filter(
|
||||
Message.conversation_id == conversation.id
|
||||
Message.conversation_id == conversation.id,
|
||||
Message.role.in_(['user', 'assistant', 'system']) # 排除thinking
|
||||
).order_by(Message.created_at.desc()).limit(limit).all()
|
||||
|
||||
# 反转顺序,最早的在前
|
||||
@@ -108,4 +155,16 @@ class ConversationService:
|
||||
conversation.is_active = False
|
||||
self.db.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
def set_conversation_agent(self, conversation_id: str, agent_id: int):
|
||||
"""设置会话使用的Agent(v2)"""
|
||||
if not self.use_v2:
|
||||
return False
|
||||
|
||||
conversation = self.get_conversation(conversation_id)
|
||||
if conversation:
|
||||
conversation.current_agent_id = agent_id
|
||||
self.db.commit()
|
||||
return True
|
||||
return False
|
||||
Reference in New Issue
Block a user