feat: Step 1 - 添加 Bot 数据模型 (v0.9.0)
- 创建 Bot 模型:id/name/display_name/avatar/description - 添加 owner_id/agent_id/token_hash/is_system 字段 - 添加 status/capabilities/config 字段 - 修改 Session 模型:添加 bot_id 关联 - 修改 Message 模型:添加 sender_name 和 bot_id - 创建数据库迁移脚本 技术方案实现: - Bot 一对一绑定 Agent - owner_id 区分所有权 - is_system 支持系统级 Bot - capabilities 存储能力标签
This commit is contained in:
@@ -129,12 +129,57 @@ class Agent(db.Model):
|
||||
}
|
||||
|
||||
|
||||
class Bot(db.Model):
|
||||
"""机器人模型 - Agent 的用户侧展示配置"""
|
||||
__tablename__ = 'bots'
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=generate_uuid)
|
||||
name: Mapped[str] = mapped_column(String(80), unique=True, nullable=False)
|
||||
display_name: Mapped[Optional[str]] = mapped_column(String(80), nullable=True)
|
||||
avatar: Mapped[Optional[str]] = mapped_column(String(256), nullable=True)
|
||||
description: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
owner_id: Mapped[str] = mapped_column(String(36), ForeignKey('users.id'), nullable=False)
|
||||
agent_id: Mapped[Optional[str]] = mapped_column(String(36), ForeignKey('agents.id'), nullable=True)
|
||||
token_hash: Mapped[Optional[str]] = mapped_column(String(256), nullable=True)
|
||||
is_system: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
status: Mapped[str] = mapped_column(String(20), default='offline')
|
||||
capabilities: Mapped[Optional[str]] = mapped_column(JSON, nullable=True)
|
||||
config: Mapped[Optional[str]] = mapped_column(JSON, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
last_active_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
# 关联
|
||||
owner = relationship('User', foreign_keys=[owner_id])
|
||||
agent = relationship('Agent', foreign_keys=[agent_id])
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Bot {self.name}>'
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'display_name': self.display_name,
|
||||
'avatar': self.avatar,
|
||||
'description': self.description,
|
||||
'owner_id': self.owner_id,
|
||||
'agent_id': self.agent_id,
|
||||
'is_system': self.is_system,
|
||||
'status': self.status,
|
||||
'capabilities': self.capabilities,
|
||||
'config': self.config,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
'last_active_at': self.last_active_at.isoformat() if self.last_active_at else None,
|
||||
}
|
||||
|
||||
|
||||
class Session(db.Model):
|
||||
"""会话模型"""
|
||||
__tablename__ = 'sessions'
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=generate_uuid)
|
||||
user_id: Mapped[str] = mapped_column(String(36), ForeignKey('users.id'), nullable=False)
|
||||
bot_id: Mapped[Optional[str]] = mapped_column(String(36), ForeignKey('bots.id'), nullable=True)
|
||||
primary_agent_id: Mapped[Optional[str]] = mapped_column(String(36), ForeignKey('agents.id'), nullable=True)
|
||||
participating_agent_ids: Mapped[Optional[str]] = mapped_column(JSON, nullable=True)
|
||||
user_socket_id: Mapped[Optional[str]] = mapped_column(String(100), nullable=True)
|
||||
@@ -149,6 +194,7 @@ class Session(db.Model):
|
||||
|
||||
# 关联
|
||||
user = relationship('User', back_populates='sessions')
|
||||
bot = relationship('Bot', foreign_keys=[bot_id])
|
||||
agent = relationship('Agent', back_populates='sessions')
|
||||
messages = relationship('Message', back_populates='session', cascade='all, delete-orphan')
|
||||
|
||||
@@ -159,6 +205,7 @@ class Session(db.Model):
|
||||
return {
|
||||
'id': self.id,
|
||||
'user_id': self.user_id,
|
||||
'bot_id': self.bot_id,
|
||||
'primary_agent_id': self.primary_agent_id,
|
||||
'participating_agent_ids': self.participating_agent_ids,
|
||||
'user_socket_id': self.user_socket_id,
|
||||
@@ -181,6 +228,8 @@ class Message(db.Model):
|
||||
session_id: Mapped[str] = mapped_column(String(36), ForeignKey('sessions.id'), nullable=False)
|
||||
sender_type: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
sender_id: Mapped[str] = mapped_column(String(36), nullable=False)
|
||||
sender_name: Mapped[Optional[str]] = mapped_column(String(80), nullable=True)
|
||||
bot_id: Mapped[Optional[str]] = mapped_column(String(36), ForeignKey('bots.id'), nullable=True)
|
||||
message_type: Mapped[str] = mapped_column(String(20), default='text')
|
||||
content: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
content_type: Mapped[str] = mapped_column(String(20), default='markdown')
|
||||
@@ -193,6 +242,7 @@ class Message(db.Model):
|
||||
|
||||
# 关联
|
||||
session = relationship('Session', back_populates='messages')
|
||||
bot = relationship('Bot', foreign_keys=[bot_id])
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Message {self.id}>'
|
||||
@@ -203,6 +253,8 @@ class Message(db.Model):
|
||||
'session_id': self.session_id,
|
||||
'sender_type': self.sender_type,
|
||||
'sender_id': self.sender_id,
|
||||
'sender_name': self.sender_name,
|
||||
'bot_id': self.bot_id,
|
||||
'message_type': self.message_type,
|
||||
'content': self.content,
|
||||
'content_type': self.content_type,
|
||||
|
||||
Reference in New Issue
Block a user