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,
|
||||
|
||||
69
migrations/versions/add_bot_model.py
Normal file
69
migrations/versions/add_bot_model.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""add bot model
|
||||
|
||||
Revision ID: add_bot_model
|
||||
Revises: initial
|
||||
Create Date: 2026-03-15 10:22:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = 'add_bot_model'
|
||||
down_revision = 'initial'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# 创建 bots 表
|
||||
op.create_table('bots',
|
||||
sa.Column('id', sa.String(36), primary_key=True),
|
||||
sa.Column('name', sa.String(80), unique=True, nullable=False),
|
||||
sa.Column('display_name', sa.String(80), nullable=True),
|
||||
sa.Column('avatar', sa.String(256), nullable=True),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('owner_id', sa.String(36), sa.ForeignKey('users.id'), nullable=False),
|
||||
sa.Column('agent_id', sa.String(36), sa.ForeignKey('agents.id'), nullable=True),
|
||||
sa.Column('token_hash', sa.String(256), nullable=True),
|
||||
sa.Column('is_system', sa.Boolean(), default=False),
|
||||
sa.Column('status', sa.String(20), default='offline'),
|
||||
sa.Column('capabilities', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('config', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||
sa.Column('last_active_at', sa.DateTime(), nullable=True),
|
||||
)
|
||||
|
||||
# 为 sessions 表添加 bot_id 字段
|
||||
op.add_column('sessions', sa.Column('bot_id', sa.String(36), sa.ForeignKey('bots.id'), nullable=True))
|
||||
|
||||
# 为 messages 表添加新字段
|
||||
op.add_column('messages', sa.Column('sender_name', sa.String(80), nullable=True))
|
||||
op.add_column('messages', sa.Column('bot_id', sa.String(36), sa.ForeignKey('bots.id'), nullable=True))
|
||||
|
||||
# 创建索引
|
||||
op.create_index('ix_bots_name', 'bots', ['name'], unique=True)
|
||||
op.create_index('ix_bots_owner_id', 'bots', ['owner_id'])
|
||||
op.create_index('ix_bots_agent_id', 'bots', ['agent_id'])
|
||||
op.create_index('ix_sessions_bot_id', 'sessions', ['bot_id'])
|
||||
op.create_index('ix_messages_bot_id', 'messages', ['bot_id'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
# 删除索引
|
||||
op.drop_index('ix_messages_bot_id', 'messages')
|
||||
op.drop_index('ix_sessions_bot_id', 'sessions')
|
||||
op.drop_index('ix_bots_agent_id', 'bots')
|
||||
op.drop_index('ix_bots_owner_id', 'bots')
|
||||
op.drop_index('ix_bots_name', 'bots')
|
||||
|
||||
# 删除 messages 表的新字段
|
||||
op.drop_column('messages', 'bot_id')
|
||||
op.drop_column('messages', 'sender_name')
|
||||
|
||||
# 删除 sessions 表的 bot_id 字段
|
||||
op.drop_column('sessions', 'bot_id')
|
||||
|
||||
# 删除 bots 表
|
||||
op.drop_table('bots')
|
||||
Reference in New Issue
Block a user