diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index 89476c4934..38a28e4858 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -2067,6 +2067,10 @@ SKILL_PREVIEW_MAX_CHARS = int(os.environ['SKILL_PREVIEW_MAX_CHARS']) if os.geten if SKILL_PREVIEW_MAX_CHARS is not None and SKILL_PREVIEW_MAX_CHARS <= 0: raise ValueError('SKILL_PREVIEW_MAX_CHARS must be a positive integer or unset') +VIEW_CHAT_MAX_CHARS = int(os.environ['VIEW_CHAT_MAX_CHARS']) if os.getenv('VIEW_CHAT_MAX_CHARS') else None +if VIEW_CHAT_MAX_CHARS is not None and VIEW_CHAT_MAX_CHARS <= 0: + raise ValueError('VIEW_CHAT_MAX_CHARS must be a positive integer or unset') + ENABLE_CHANNELS = os.getenv('ENABLE_CHANNELS', 'False').lower() == 'true' ENABLE_ENCRYPTED_CHANNELS = os.getenv('ENABLE_ENCRYPTED_CHANNELS', 'False').lower() == 'true' @@ -2930,6 +2934,7 @@ DEFAULT_CONFIG = { 'skills.lazy_loading.enable': ENABLE_SKILL_LAZY_LOADING, 'skills.content_max_chars': SKILL_CONTENT_MAX_CHARS, 'skills.preview_max_chars': SKILL_PREVIEW_MAX_CHARS, + 'chats.view_max_chars': VIEW_CHAT_MAX_CHARS, 'direct.enable': ENABLE_DIRECT_CONNECTIONS, 'direct.integrations.enable': ENABLE_DIRECT_INTEGRATIONS, 'ollama.enable': ENABLE_OLLAMA_API, diff --git a/backend/open_webui/routers/auths.py b/backend/open_webui/routers/auths.py index 0565e57e9b..9b8a68b85f 100644 --- a/backend/open_webui/routers/auths.py +++ b/backend/open_webui/routers/auths.py @@ -143,6 +143,7 @@ ADMIN_CONFIG_KEYS = { 'ENABLE_SKILL_LAZY_LOADING': 'skills.lazy_loading.enable', 'SKILL_CONTENT_MAX_CHARS': 'skills.content_max_chars', 'SKILL_PREVIEW_MAX_CHARS': 'skills.preview_max_chars', + 'VIEW_CHAT_MAX_CHARS': 'chats.view_max_chars', 'ENABLE_NOTES': 'notes.enable', 'ENABLE_USER_WEBHOOKS': 'ui.enable_user_webhooks', 'ENABLE_USER_STATUS': 'users.enable_status', @@ -1188,6 +1189,7 @@ class AdminConfig(MfaConfigForm): ENABLE_SKILL_LAZY_LOADING: bool = False SKILL_CONTENT_MAX_CHARS: int | None = Field(default=None, gt=0, strict=True) SKILL_PREVIEW_MAX_CHARS: int | None = Field(default=None, gt=0, strict=True) + VIEW_CHAT_MAX_CHARS: int | None = Field(default=None, gt=0, strict=True) ENABLE_NOTES: bool ENABLE_USER_WEBHOOKS: bool ENABLE_USER_STATUS: bool @@ -1246,6 +1248,8 @@ async def update_admin_config(request: Request, form_data: AdminConfig, user=Dep updates.pop('skills.content_max_chars', None) if 'SKILL_PREVIEW_MAX_CHARS' not in form_data.model_fields_set: updates.pop('skills.preview_max_chars', None) + if 'VIEW_CHAT_MAX_CHARS' not in form_data.model_fields_set: + updates.pop('chats.view_max_chars', None) updates['ui.default_interface_settings'] = form_data.DEFAULT_INTERFACE_SETTINGS or {} updates['folders.max_file_count'] = int(form_data.FOLDER_MAX_FILE_COUNT) if form_data.FOLDER_MAX_FILE_COUNT else '' updates['automations.max_count'] = int(form_data.AUTOMATION_MAX_COUNT) if form_data.AUTOMATION_MAX_COUNT else '' diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index b439ff5093..1bfe6f47f1 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -1750,6 +1750,24 @@ async def view_chat( ) current_id = msg.get('parentId') if msg else None + truncation = {} + max_chars = await Config.get('chats.view_max_chars') + if max_chars and sum(len(str(message['content'] or '')) for message in messages) > max_chars: + remaining_chars = max_chars + kept_messages = [] + for message in messages: + if remaining_chars <= 0: + break + content = str(message['content'] or '')[:remaining_chars] + kept_messages.append({**message, 'content': content}) + remaining_chars -= len(content) + truncation = { + 'truncated': True, + 'total_messages': len(messages), + 'returned_messages': len(kept_messages), + } + messages = kept_messages + # Reverse to get chronological order messages.reverse() @@ -1760,6 +1778,7 @@ async def view_chat( 'messages': messages, 'updated_at': chat.updated_at, 'created_at': chat.created_at, + **truncation, }, ensure_ascii=False, ) diff --git a/src/lib/components/admin/Settings/General.svelte b/src/lib/components/admin/Settings/General.svelte index 2ed26d6ebc..12dd20988e 100644 --- a/src/lib/components/admin/Settings/General.svelte +++ b/src/lib/components/admin/Settings/General.svelte @@ -86,6 +86,7 @@ ...adminConfig, SKILL_CONTENT_MAX_CHARS: adminConfig.SKILL_CONTENT_MAX_CHARS ?? null, SKILL_PREVIEW_MAX_CHARS: adminConfig.SKILL_PREVIEW_MAX_CHARS ?? null, + VIEW_CHAT_MAX_CHARS: adminConfig.VIEW_CHAT_MAX_CHARS ?? null, DEFAULT_INTERFACE_SETTINGS: defaultInterfaceSettings, I18N: cleaned }); @@ -378,6 +379,21 @@ bind:value={adminConfig.SKILL_PREVIEW_MAX_CHARS} /> + + +