From 959558fd82eb2a3c980231acd500b73ba4b698b3 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 13 Jul 2026 23:27:54 -0400 Subject: [PATCH] refac --- backend/open_webui/config.py | 8 ++++++++ .../open_webui/utils/context_compaction.py | 19 +++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 9e74c09b78..edd989857a 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -1988,6 +1988,10 @@ ENABLE_CALENDAR = os.getenv('ENABLE_CALENDAR', 'True').lower() == 'true' ENABLE_AUTOMATIONS = os.getenv('ENABLE_AUTOMATIONS', 'True').lower() == 'true' ENABLE_SUBAGENTS = os.getenv('ENABLE_SUBAGENTS', 'False').lower() == 'true' +SUBAGENTS_BACKGROUND_ENABLED = os.getenv('SUBAGENTS_BACKGROUND_ENABLED', 'False').lower() == 'true' +SUBAGENTS_MAX_CONCURRENT = int(os.getenv('SUBAGENTS_MAX_CONCURRENT', '20')) +SUBAGENTS_MAX_ASYNC = int(os.getenv('SUBAGENTS_MAX_ASYNC', '20')) +SUBAGENTS_SYSTEM_PROMPT = os.getenv('SUBAGENTS_SYSTEM_PROMPT', '') AUTOMATION_MAX_COUNT = os.getenv('AUTOMATION_MAX_COUNT', '') @@ -3022,6 +3026,10 @@ DEFAULT_CONFIG = { 'calendar.enable': ENABLE_CALENDAR, 'automations.enable': ENABLE_AUTOMATIONS, 'subagents.enable': ENABLE_SUBAGENTS, + 'subagents.background_enabled': SUBAGENTS_BACKGROUND_ENABLED, + 'subagents.max_concurrent': SUBAGENTS_MAX_CONCURRENT, + 'subagents.max_async': SUBAGENTS_MAX_ASYNC, + 'subagents.system_prompt': SUBAGENTS_SYSTEM_PROMPT, 'automations.max_count': AUTOMATION_MAX_COUNT, 'automations.min_interval': AUTOMATION_MIN_INTERVAL, 'automations.auth_token_expires_in': AUTOMATION_AUTH_TOKEN_EXPIRES_IN, diff --git a/backend/open_webui/utils/context_compaction.py b/backend/open_webui/utils/context_compaction.py index 2cdc4c3aef..f476a80d82 100644 --- a/backend/open_webui/utils/context_compaction.py +++ b/backend/open_webui/utils/context_compaction.py @@ -58,6 +58,9 @@ async def compact_messages_for_request( return messages, previous_summary, False boundary = _find_compaction_boundary(messages) + if boundary is None: + return messages, previous_summary, False + compacted_messages = messages[:boundary] recent_messages = messages[boundary:] if not compacted_messages or not recent_messages: @@ -240,19 +243,11 @@ def _exceeds_token_threshold(messages: list[dict], system_prompt: str, summary: return estimated > threshold -def _find_compaction_boundary(messages: list[dict]) -> int: +def _find_compaction_boundary(messages: list[dict]) -> int | None: keep_count = max(2, len(messages) * 2 // 5) - split = max(1, len(messages) - keep_count) - - while split < len(messages) - 1: - previous = messages[split - 1] if split > 0 else {} - current = messages[split] - if current.get('role') == 'tool' or previous.get('tool_calls') or previous.get('output'): - split += 1 - continue - break - - return min(split, len(messages) - 2) + target = max(1, len(messages) - keep_count) + boundaries = [idx for idx, message in enumerate(messages) if message.get('role') == 'user'][1:] + return next((idx for idx in reversed(boundaries) if idx <= target), None) async def _generate_summary(