fix(runtime): preserve explicit replies and report bot configuration errors

This commit is contained in:
RockChinQ
2026-09-10 17:21:22 +08:00
parent b7a04f7a24
commit 8903a40c41
41 changed files with 1108 additions and 155 deletions
+14 -10
View File
@@ -11,6 +11,7 @@ from ....entity.persistence import agent as persistence_agent
from ....entity.persistence import bot as persistence_bot
from ....entity.persistence import pipeline as persistence_pipeline
from ....workspace.errors import WorkspaceNotFoundError
from .bot_errors import BotApplyError, bot_error_message
from .tenant import TenantContext, require_workspace_uuid, scope_statement
from ....utils import httpclient
from ....platform.sources import http_bot_signing
@@ -664,7 +665,10 @@ class BotService:
bot = await self.get_bot(context, bot_data['uuid'], include_secret=True)
await self.ap.platform_mgr.load_bot(context, bot)
try:
await self.ap.platform_mgr.load_bot(context, bot)
except Exception as exc:
raise BotApplyError(bot_error_message(exc, bot), bot['uuid']) from exc
return bot_data['uuid']
@@ -694,17 +698,17 @@ class BotService:
runtime_bot.bot_entity.description = update_data['description']
return
await self.ap.platform_mgr.remove_bot(context, bot_uuid)
# select from db
# Persisted configuration is distinct from applying it to the running adapter.
bot = await self.get_bot(context, bot_uuid, include_secret=True)
try:
await self.ap.platform_mgr.remove_bot(context, bot_uuid)
runtime_bot = await self.ap.platform_mgr.load_bot(context, bot)
if runtime_bot.enable:
await runtime_bot.run()
except Exception as exc:
raise BotApplyError(bot_error_message(exc, bot), bot['uuid']) from exc
runtime_bot = await self.ap.platform_mgr.load_bot(context, bot)
if runtime_bot.enable:
await runtime_bot.run()
# update all conversation that use this bot
# Reset conversations using this bot after its configuration is applied.
for session in self.ap.sess_mgr.session_list:
if (
session.using_conversation is not None
@@ -0,0 +1,43 @@
"""User-facing bot configuration errors without credentials or validation inputs."""
import json
import pydantic
from .secrets import redact_secrets
class BotApplyError(Exception):
"""Configuration was persisted, but the runtime could not apply it."""
def __init__(self, message: str, bot_uuid: str | None = None):
super().__init__(message)
self.bot_uuid = bot_uuid
def bot_error_message(error: Exception, configuration: dict) -> str:
if isinstance(error, pydantic.ValidationError):
text = '; '.join(
f'{".".join(map(str, item["loc"]))}: {item["msg"]}'
for item in error.errors(include_input=False, include_context=False, include_url=False)
)
else:
text = str(error).strip() or type(error).__name__
replacements = []
def collect(original, masked):
if isinstance(original, dict) and isinstance(masked, dict):
for key, value in original.items():
collect(value, masked.get(key))
elif isinstance(original, (list, tuple)) and isinstance(masked, (list, tuple)):
for value, replacement in zip(original, masked):
collect(value, replacement)
elif isinstance(original, str) and original and original != masked:
for representation in {original, repr(original)[1:-1], json.dumps(original, ensure_ascii=False)[1:-1]}:
replacements.append((representation, str(masked)))
collect(configuration, redact_secrets(configuration))
for original, masked in sorted(replacements, key=lambda item: len(item[0]), reverse=True):
text = text.replace(original, masked)
return text[:2000]