feat: set the chat read limit from the admin settings

The limit on how much of an earlier chat a model can read could only be set with an environment variable. It is now a Chat Read Limit field in Admin Settings under General, next to the skill limits. The environment variable still works as the starting value, and the setting saved in the panel takes precedence.
This commit is contained in:
Classic298 2026-11-03 19:43:34 +01:00
parent f81d0f6a20
commit 4e21ddc8d8
6 changed files with 30 additions and 5 deletions

View file

@ -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'
@ -2929,6 +2933,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,

View file

@ -1176,8 +1176,6 @@ KNOWLEDGE_GREP_MAX_MATCHES = _int_env('KNOWLEDGE_GREP_MAX_MATCHES', 50)
# Characters returned by view_file / view_knowledge_file.
VIEW_FILE_MAX_CHARS = _int_env('VIEW_FILE_MAX_CHARS', 100_000)
VIEW_FILE_DEFAULT_MAX_CHARS = _int_env('VIEW_FILE_DEFAULT_MAX_CHARS', 10_000)
# Characters returned by view_chat; unset returns the full chat.
VIEW_CHAT_MAX_CHARS = int(os.getenv('VIEW_CHAT_MAX_CHARS')) if os.getenv('VIEW_CHAT_MAX_CHARS') else None
# Tools/functions pip options

View file

@ -140,6 +140,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',
@ -1182,6 +1183,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
@ -1240,6 +1242,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 ''

View file

@ -16,7 +16,6 @@ from fastapi import HTTPException, Request
from open_webui.env import (
KNOWLEDGE_GREP_MAX_MATCHES,
VIEW_CHAT_MAX_CHARS,
VIEW_FILE_DEFAULT_MAX_CHARS,
VIEW_FILE_MAX_CHARS,
)
@ -1752,8 +1751,9 @@ async def view_chat(
current_id = msg.get('parentId') if msg else None
truncation = {}
if VIEW_CHAT_MAX_CHARS and sum(len(message['content']) for message in messages) > VIEW_CHAT_MAX_CHARS:
remaining_chars = VIEW_CHAT_MAX_CHARS
max_chars = await Config.get('chats.view_max_chars')
if max_chars and sum(len(message['content']) for message in messages) > max_chars:
remaining_chars = max_chars
kept_messages = []
for message in messages:
if remaining_chars <= 0:

View file

@ -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}
/>
</AdminSettingField>
<AdminSettingField
forId="view-chat-max-chars"
label={$i18n.t('settings.admin.general.viewChatMaxChars.label')}
description={$i18n.t('settings.admin.general.viewChatMaxChars.description')}
>
<input
id="view-chat-max-chars"
class={inputClass}
type="number"
min="1"
step="1"
placeholder={$i18n.t('No limit')}
bind:value={adminConfig.VIEW_CHAT_MAX_CHARS}
/>
</AdminSettingField>
<AdminSettingRow
label={$i18n.t('settings.admin.general.notes.label')}
description={$i18n.t('settings.admin.general.notes.description')}

View file

@ -3170,6 +3170,8 @@
"settings.admin.general.userWebhooks.description": "Allow users to configure webhooks from their account.",
"settings.admin.general.userWebhooks.label": "User Webhooks",
"settings.admin.general.version.label": "Version",
"settings.admin.general.viewChatMaxChars.description": "Maximum characters returned when a model reads a previous chat. The newest messages are kept. Leave empty for no limit.",
"settings.admin.general.viewChatMaxChars.label": "Chat Read Limit",
"settings.admin.general.webuiUrl.description": "Enter the public URL of your WebUI. This URL will be used to generate links in the notifications.",
"settings.admin.general.webuiUrl.label": "WebUI URL",
"settings.admin.images.apiAuthString.description": "Provide the --api-auth username and password when required.",