2025-03-27 01:20:00 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
import sqlalchemy
|
|
|
|
|
|
|
|
|
|
from ....core import app
|
|
|
|
|
from ....entity.persistence import bot as persistence_bot
|
2025-04-16 13:40:59 +08:00
|
|
|
from ....entity.persistence import pipeline as persistence_pipeline
|
2025-03-27 01:20:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class BotService:
|
|
|
|
|
"""机器人服务"""
|
|
|
|
|
|
|
|
|
|
ap: app.Application
|
|
|
|
|
|
|
|
|
|
def __init__(self, ap: app.Application) -> None:
|
|
|
|
|
self.ap = ap
|
|
|
|
|
|
|
|
|
|
async def get_bots(self) -> list[dict]:
|
|
|
|
|
"""获取所有机器人"""
|
|
|
|
|
result = await self.ap.persistence_mgr.execute_async(
|
|
|
|
|
sqlalchemy.select(persistence_bot.Bot)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
bots = result.all()
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
self.ap.persistence_mgr.serialize_model(persistence_bot.Bot, bot)
|
|
|
|
|
for bot in bots
|
|
|
|
|
]
|
2025-04-29 17:24:07 +08:00
|
|
|
|
2025-03-27 01:20:00 +08:00
|
|
|
async def get_bot(self, bot_uuid: str) -> dict | None:
|
|
|
|
|
"""获取机器人"""
|
|
|
|
|
result = await self.ap.persistence_mgr.execute_async(
|
2025-04-29 17:24:07 +08:00
|
|
|
sqlalchemy.select(persistence_bot.Bot).where(
|
|
|
|
|
persistence_bot.Bot.uuid == bot_uuid
|
|
|
|
|
)
|
2025-03-27 01:20:00 +08:00
|
|
|
)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
2025-03-27 01:20:00 +08:00
|
|
|
bot = result.first()
|
|
|
|
|
|
|
|
|
|
if bot is None:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
return self.ap.persistence_mgr.serialize_model(persistence_bot.Bot, bot)
|
|
|
|
|
|
|
|
|
|
async def create_bot(self, bot_data: dict) -> str:
|
|
|
|
|
"""创建机器人"""
|
2025-03-27 23:50:02 +08:00
|
|
|
# TODO: 检查配置信息格式
|
2025-03-27 01:20:00 +08:00
|
|
|
bot_data['uuid'] = str(uuid.uuid4())
|
2025-04-16 13:40:59 +08:00
|
|
|
|
|
|
|
|
# checkout the default pipeline
|
|
|
|
|
result = await self.ap.persistence_mgr.execute_async(
|
2025-04-29 17:24:07 +08:00
|
|
|
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(
|
|
|
|
|
persistence_pipeline.LegacyPipeline.is_default == True
|
|
|
|
|
)
|
2025-04-16 13:40:59 +08:00
|
|
|
)
|
|
|
|
|
pipeline = result.first()
|
|
|
|
|
if pipeline is not None:
|
|
|
|
|
bot_data['use_pipeline_uuid'] = pipeline.uuid
|
|
|
|
|
bot_data['use_pipeline_name'] = pipeline.name
|
|
|
|
|
|
2025-03-27 01:20:00 +08:00
|
|
|
await self.ap.persistence_mgr.execute_async(
|
|
|
|
|
sqlalchemy.insert(persistence_bot.Bot).values(bot_data)
|
|
|
|
|
)
|
2025-03-27 23:50:02 +08:00
|
|
|
|
|
|
|
|
bot = await self.get_bot(bot_data['uuid'])
|
|
|
|
|
|
|
|
|
|
await self.ap.platform_mgr.load_bot(bot)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
2025-03-27 01:20:00 +08:00
|
|
|
return bot_data['uuid']
|
|
|
|
|
|
|
|
|
|
async def update_bot(self, bot_uuid: str, bot_data: dict) -> None:
|
|
|
|
|
"""更新机器人"""
|
|
|
|
|
if 'uuid' in bot_data:
|
|
|
|
|
del bot_data['uuid']
|
2025-04-16 13:40:59 +08:00
|
|
|
|
|
|
|
|
# set use_pipeline_name
|
|
|
|
|
if 'use_pipeline_uuid' in bot_data:
|
|
|
|
|
result = await self.ap.persistence_mgr.execute_async(
|
2025-04-29 17:24:07 +08:00
|
|
|
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(
|
|
|
|
|
persistence_pipeline.LegacyPipeline.uuid
|
|
|
|
|
== bot_data['use_pipeline_uuid']
|
|
|
|
|
)
|
2025-04-16 13:40:59 +08:00
|
|
|
)
|
|
|
|
|
pipeline = result.first()
|
|
|
|
|
if pipeline is not None:
|
|
|
|
|
bot_data['use_pipeline_name'] = pipeline.name
|
|
|
|
|
else:
|
2025-04-29 17:24:07 +08:00
|
|
|
raise Exception('Pipeline not found')
|
2025-04-16 13:40:59 +08:00
|
|
|
|
2025-03-27 01:20:00 +08:00
|
|
|
await self.ap.persistence_mgr.execute_async(
|
2025-04-29 17:24:07 +08:00
|
|
|
sqlalchemy.update(persistence_bot.Bot)
|
|
|
|
|
.values(bot_data)
|
|
|
|
|
.where(persistence_bot.Bot.uuid == bot_uuid)
|
2025-03-27 01:20:00 +08:00
|
|
|
)
|
2025-03-27 23:50:02 +08:00
|
|
|
await self.ap.platform_mgr.remove_bot(bot_uuid)
|
2025-04-29 17:24:07 +08:00
|
|
|
|
2025-03-27 23:50:02 +08:00
|
|
|
# select from db
|
|
|
|
|
bot = await self.get_bot(bot_uuid)
|
|
|
|
|
|
|
|
|
|
runtime_bot = await self.ap.platform_mgr.load_bot(bot)
|
|
|
|
|
|
|
|
|
|
if runtime_bot.enable:
|
|
|
|
|
await runtime_bot.run()
|
2025-03-27 01:20:00 +08:00
|
|
|
|
|
|
|
|
async def delete_bot(self, bot_uuid: str) -> None:
|
|
|
|
|
"""删除机器人"""
|
2025-03-27 23:50:02 +08:00
|
|
|
await self.ap.platform_mgr.remove_bot(bot_uuid)
|
2025-03-27 01:20:00 +08:00
|
|
|
await self.ap.persistence_mgr.execute_async(
|
2025-04-29 17:24:07 +08:00
|
|
|
sqlalchemy.delete(persistence_bot.Bot).where(
|
|
|
|
|
persistence_bot.Bot.uuid == bot_uuid
|
|
|
|
|
)
|
2025-03-27 01:20:00 +08:00
|
|
|
)
|