feat(migration): enhance migration process with error handling and agent runner config updates

This commit is contained in:
Soulter
2025-11-24 14:37:25 +08:00
parent 70db8d264b
commit e2a7a028bd
2 changed files with 57 additions and 36 deletions

View File

@@ -98,7 +98,16 @@ class AstrBotCoreLifecycle:
)
# apply migration
await migra(self.db, self.astrbot_config_mgr, self.umop_config_router)
try:
await migra(
self.db,
self.astrbot_config_mgr,
self.umop_config_router,
self.astrbot_config_mgr,
)
except Exception as e:
logger.error(f"AstrBot migration failed: {e!s}")
logger.error(traceback.format_exc())
# 初始化事件队列
self.event_queue = Queue()

View File

@@ -1,11 +1,40 @@
import traceback
from astrbot.core import astrbot_config, logger
from astrbot.core.astrbot_config_mgr import AstrBotConfig, AstrBotConfigManager
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:
def _migra_agent_runner_configs(conf: AstrBotConfig, ids_map: dict) -> None:
"""
Migra agent runner configs from provider configs.
"""
try:
default_prov_id = conf["provider_settings"]["default_provider_id"]
if default_prov_id in ids_map:
conf["provider_settings"]["default_provider_id"] = ""
p = ids_map[default_prov_id]
if p["type"] == "dify":
conf["provider_settings"]["dify_agent_runner_provider_id"] = p["id"]
conf["provider_settings"]["agent_runner_type"] = "dify"
elif p["type"] == "coze":
conf["provider_settings"]["coze_agent_runner_provider_id"] = p["id"]
conf["provider_settings"]["agent_runner_type"] = "coze"
elif p["type"] == "dashscope":
conf["provider_settings"]["dashscope_agent_runner_provider_id"] = p[
"id"
]
conf["provider_settings"]["agent_runner_type"] = "dashscope"
conf.save_config()
except Exception as e:
logger.error(f"Migration for third party agent runner configs failed: {e!s}")
logger.error(traceback.format_exc())
async def migra(
db, astrbot_config_mgr, umop_config_router, acm: AstrBotConfigManager
) -> None:
"""
Stores the migration logic here.
btw, i really don't like migration :(
@@ -25,37 +54,20 @@ async def migra(db, astrbot_config_mgr, umop_config_router) -> None:
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())
_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
if _c:
astrbot_config.save_config()
for conf in acm.confs.values():
_migra_agent_runner_configs(conf, ids_map)