diff --git a/routes/bot_manager.py b/routes/bot_manager.py index 5fb3266..dcf8fda 100644 --- a/routes/bot_manager.py +++ b/routes/bot_manager.py @@ -1041,6 +1041,39 @@ async def migrate_add_marketplace_fields(): logger.info("Marketplace fields migration completed") +async def migrate_single_agent_mode(): + """ + 添加单智能体模式相关字段到 agent_user 表 + """ + pool = get_db_pool_manager().pool + + async with pool.connection() as conn: + async with conn.cursor() as cursor: + # 检查 single_agent_bot_id 字段是否存在 + await cursor.execute(""" + SELECT column_name + FROM information_schema.columns + WHERE table_name = 'agent_user' AND column_name = 'single_agent_bot_id' + """) + has_single_agent_bot_id = await cursor.fetchone() + + if not has_single_agent_bot_id: + logger.info("Adding single_agent_bot_id column to agent_user table") + await cursor.execute(""" + ALTER TABLE agent_user + ADD COLUMN single_agent_bot_id UUID + """) + await cursor.execute(""" + CREATE INDEX IF NOT EXISTS idx_agent_user_single_agent_bot + ON agent_user(single_agent_bot_id) WHERE single_agent_bot_id IS NOT NULL + """) + logger.info("Single agent mode migration completed") + else: + logger.info("single_agent_bot_id column already exists") + + await conn.commit() + + async def init_bot_manager_tables(): """ 初始化 Bot Manager 相关的所有数据库表 @@ -1054,6 +1087,8 @@ async def init_bot_manager_tables(): await migrate_bot_owner_and_shares() # 3. Marketplace 字段迁移 await migrate_add_marketplace_fields() + # 4. Single Agent Mode 字段迁移 + await migrate_single_agent_mode() # SQL 表创建语句 tables_sql = [