diff --git a/astrbot/core/core_lifecycle.py b/astrbot/core/core_lifecycle.py index 17fd52138..cd3f2f27b 100644 --- a/astrbot/core/core_lifecycle.py +++ b/astrbot/core/core_lifecycle.py @@ -16,13 +16,12 @@ import time import traceback from asyncio import Queue -from astrbot.core import LogBroker, logger, sp +from astrbot.api import logger, sp +from astrbot.core import LogBroker from astrbot.core.astrbot_config_mgr import AstrBotConfigManager from astrbot.core.config.default import VERSION from astrbot.core.conversation_mgr import ConversationManager from astrbot.core.db import BaseDatabase -from astrbot.core.db.migration.migra_45_to_46 import migrate_45_to_46 -from astrbot.core.db.migration.migra_webchat_session import migrate_webchat_session from astrbot.core.knowledge_base.kb_mgr import KnowledgeBaseManager from astrbot.core.persona_mgr import PersonaManager from astrbot.core.pipeline.scheduler import PipelineContext, PipelineScheduler @@ -34,6 +33,7 @@ from astrbot.core.star.context import Context from astrbot.core.star.star_handler import EventType, star_handlers_registry, star_map from astrbot.core.umop_config_router import UmopConfigRouter from astrbot.core.updator import AstrBotUpdator +from astrbot.core.utils.migra_helper import migra from . import astrbot_config, html_renderer from .event_bus import EventBus @@ -97,19 +97,8 @@ class AstrBotCoreLifecycle: sp=sp, ) - # 4.5 to 4.6 migration for umop_config_router - try: - await migrate_45_to_46(self.astrbot_config_mgr, self.umop_config_router) - except Exception as e: - logger.error(f"Migration from version 4.5 to 4.6 failed: {e!s}") - logger.error(traceback.format_exc()) - - # migration for webchat session - try: - await migrate_webchat_session(self.db) - except Exception as e: - logger.error(f"Migration for webchat session failed: {e!s}") - logger.error(traceback.format_exc()) + # apply migration + await migra(self.db, self.astrbot_config_mgr, self.umop_config_router) # 初始化事件队列 self.event_queue = Queue() diff --git a/astrbot/core/utils/migra_helper.py b/astrbot/core/utils/migra_helper.py new file mode 100644 index 000000000..27358201a --- /dev/null +++ b/astrbot/core/utils/migra_helper.py @@ -0,0 +1,61 @@ +import traceback + +from astrbot.core import astrbot_config, logger +from astrbot.core.db.migration.migra_45_to_46 import migrate_45_to_46 +from astrbot.core.db.migration.migra_webchat_session import migrate_webchat_session + + +async def migra(db, astrbot_config_mgr, umop_config_router) -> None: + """ + Stores the migration logic here. + btw, i really don't like migration :( + """ + # 4.5 to 4.6 migration for umop_config_router + try: + await migrate_45_to_46(astrbot_config_mgr, umop_config_router) + except Exception as e: + logger.error(f"Migration from version 4.5 to 4.6 failed: {e!s}") + logger.error(traceback.format_exc()) + + # migration for webchat session + try: + await migrate_webchat_session(db) + except Exception as e: + logger.error(f"Migration for webchat session failed: {e!s}") + logger.error(traceback.format_exc()) + + # migra third party agent runner configs + try: + _c = False + providers = astrbot_config["provider"] + ids_map = {} + for prov in providers: + type_ = prov.get("type") + if type_ in ["dify", "coze", "dashscope"]: + prov["provider_type"] = "agent_runner" + ids_map[prov["id"]] = { + "type": type_, + "id": prov["id"], + } + _c = True + default_prov_id = astrbot_config["provider_settings"]["default_provider_id"] + if default_prov_id in ids_map: + astrbot_config["provider_settings"]["default_provider_id"] = "" + p = ids_map[default_prov_id] + if p["type"] == "dify": + astrbot_config["provider_settings"]["dify_agent_runner_provider_id"] = ( + p["id"] + ) + elif p["type"] == "coze": + astrbot_config["provider_settings"]["coze_agent_runner_provider_id"] = ( + p["id"] + ) + elif p["type"] == "dashscope": + astrbot_config["provider_settings"][ + "dashscope_agent_runner_provider_id" + ] = p["id"] + if _c: + astrbot_config.save_config() + except Exception as e: + logger.error(f"Migration for third party agent runner configs failed: {e!s}") + logger.error(traceback.format_exc())