From b8eb110cf43b56ef9b7faedcb5d111273117cfd1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 16:59:17 +0000 Subject: [PATCH] feat: add global unified context mode configuration and implementation Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com> --- astrbot/core/astr_main_agent.py | 30 +++++++++++++++++++++ astrbot/core/config/default.py | 4 +++ astrbot/core/pipeline/waking_check/stage.py | 15 +++++++++++ 3 files changed, 49 insertions(+) diff --git a/astrbot/core/astr_main_agent.py b/astrbot/core/astr_main_agent.py index 1ea36ff7a..13ea6ac70 100644 --- a/astrbot/core/astr_main_agent.py +++ b/astrbot/core/astr_main_agent.py @@ -830,6 +830,33 @@ def _get_compress_provider( return provider +def _apply_global_context_info(event: AstrMessageEvent, req: ProviderRequest) -> None: + """Add platform and session information to user prompt when in global unified context mode.""" + from astrbot.core.config.default import GLOBAL_UNIFIED_CONTEXT_UMO + + if event.unified_msg_origin != GLOBAL_UNIFIED_CONTEXT_UMO: + return + + # Get original UMO from extras + original_umo = event.get_extra("original_umo") + if not original_umo: + return + + # Parse the original UMO to extract platform, message type, and session info + try: + parts = original_umo.split(":", 2) + if len(parts) == 3: + platform_id, message_type, session_id = parts + context_info = f"[Context: Platform={platform_id}, Type={message_type}, Session={session_id}]" + # Prepend context info to the user prompt + if req.prompt: + req.prompt = f"{context_info} {req.prompt}" + else: + req.prompt = context_info + except Exception as e: + logger.warning(f"Failed to parse original UMO for global context: {e}") + + async def build_main_agent( *, event: AstrMessageEvent, @@ -888,6 +915,9 @@ async def build_main_agent( if isinstance(req.contexts, str): req.contexts = json.loads(req.contexts) + # Apply global context information if enabled + _apply_global_context_info(event, req) + if config.file_extract_enabled: try: await _apply_file_extract(event, req, config) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 12056a04e..435bf8b7c 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -17,11 +17,15 @@ WEBHOOK_SUPPORTED_PLATFORMS = [ "lark", ] +# Constant UMO for global unified context mode +GLOBAL_UNIFIED_CONTEXT_UMO = "global::global" + # 默认配置 DEFAULT_CONFIG = { "config_version": 2, "platform_settings": { "unique_session": False, + "global_unified_context_mode": False, "rate_limit": { "time": 60, "count": 30, diff --git a/astrbot/core/pipeline/waking_check/stage.py b/astrbot/core/pipeline/waking_check/stage.py index 2dcb840e9..fe2915b1e 100644 --- a/astrbot/core/pipeline/waking_check/stage.py +++ b/astrbot/core/pipeline/waking_check/stage.py @@ -72,11 +72,26 @@ class WakingCheckStage(Stage): ) platform_settings = self.ctx.astrbot_config.get("platform_settings", {}) self.unique_session = platform_settings.get("unique_session", False) + self.global_unified_context_mode = platform_settings.get( + "global_unified_context_mode", False + ) async def process( self, event: AstrMessageEvent, ) -> None | AsyncGenerator[None, None]: + # apply global unified context mode + if self.global_unified_context_mode: + from astrbot.core.config.default import GLOBAL_UNIFIED_CONTEXT_UMO + + original_umo = event.unified_msg_origin + event.unified_msg_origin = GLOBAL_UNIFIED_CONTEXT_UMO + # Store original UMO for reference in later stages + event.set_extra("original_umo", original_umo) + logger.debug( + f"Global unified context mode enabled. Changed UMO from {original_umo} to {GLOBAL_UNIFIED_CONTEXT_UMO}" + ) + # apply unique session if self.unique_session and event.message_obj.type == MessageType.GROUP_MESSAGE: sid = build_unique_session_id(event)