diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index e20b20ffe3..6466818932 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -2113,6 +2113,16 @@ 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' +try: + subagents_models = JSONCodec.loads(os.getenv('SUBAGENTS_MODELS', '[]')) + if not isinstance(subagents_models, list) or not all(isinstance(entry, dict) for entry in subagents_models): + raise ValueError('SUBAGENTS_MODELS must be a JSON list of objects') +except Exception as e: + log.exception('Error loading SUBAGENTS_MODELS: %s', e) + subagents_models = [] + +SUBAGENTS_MODELS = subagents_models + 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')) @@ -3248,6 +3258,7 @@ DEFAULT_CONFIG = { 'calendar.enable': ENABLE_CALENDAR, 'automations.enable': ENABLE_AUTOMATIONS, 'subagents.enable': ENABLE_SUBAGENTS, + 'subagents.models': SUBAGENTS_MODELS, 'subagents.background_enabled': SUBAGENTS_BACKGROUND_ENABLED, 'subagents.max_concurrent': SUBAGENTS_MAX_CONCURRENT, 'subagents.max_async': SUBAGENTS_MAX_ASYNC, diff --git a/backend/open_webui/routers/configs.py b/backend/open_webui/routers/configs.py index 1a8585dd6e..6af1f43db5 100644 --- a/backend/open_webui/routers/configs.py +++ b/backend/open_webui/routers/configs.py @@ -68,6 +68,7 @@ MODELS_CONFIG_KEYS = { } SUBAGENTS_CONFIG_KEYS = { 'ENABLE_SUBAGENTS': 'subagents.enable', + 'SUBAGENTS_MODELS': 'subagents.models', 'SUBAGENTS_BACKGROUND_ENABLED': 'subagents.background_enabled', 'SUBAGENTS_MAX_CONCURRENT': 'subagents.max_concurrent', 'SUBAGENTS_MAX_ASYNC': 'subagents.max_async', @@ -810,6 +811,7 @@ async def set_models_config(request: Request, form_data: ModelsConfigForm, user= class SubagentsConfigForm(BaseModel): ENABLE_SUBAGENTS: bool + SUBAGENTS_MODELS: list[dict] SUBAGENTS_BACKGROUND_ENABLED: bool SUBAGENTS_MAX_CONCURRENT: int SUBAGENTS_MAX_ASYNC: int diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 9e43578208..0433de6689 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -1794,6 +1794,7 @@ async def delegate_task( context: str = '', file_ids: list[str] | None = None, background: bool = False, + model_id: str | None = None, __request__: Request = None, __user__: dict = None, __metadata__: dict = None, @@ -1808,6 +1809,7 @@ async def delegate_task( :param file_ids: Attached file IDs the sub-agent needs. Use this for images or files; do not put file IDs only in context. :param background: Return immediately and continue this chat when the sub-agent finishes + :param model_id: ID of one of the listed sub-agent models. Omit to use the current model :return: Foreground result text, or a JSON dispatch handle for background work """ if __request__ is None: @@ -1822,6 +1824,7 @@ async def delegate_task( context, background, file_ids=file_ids, + model_id=model_id, request=__request__, user_data=__user__ or {}, metadata=__metadata__ or {}, diff --git a/backend/open_webui/utils/subagents.py b/backend/open_webui/utils/subagents.py index 3389f4729a..720c2be07f 100644 --- a/backend/open_webui/utils/subagents.py +++ b/backend/open_webui/utils/subagents.py @@ -18,6 +18,7 @@ from open_webui.tasks import create_task, has_active_tasks from open_webui.utils.auth import VERIFIED_USER_ROLES, create_token from open_webui.utils.json_codec import JSONCodec from open_webui.utils.misc import add_or_update_system_message, get_message_list, is_raster_image_content_type +from open_webui.utils.models import get_all_models, get_filtered_models from open_webui.utils.tool_search import strip_deferred_tools_manifest from starlette.datastructures import Headers @@ -73,6 +74,21 @@ async def _build_request(source: Request, user_id: str, *, internal: bool) -> Re return request +async def get_subagent_models(request: Request, user: UserModel) -> list[dict]: + configured_models = await Config.get('subagents.models') + if not configured_models: + return [] + models = {model['id']: model for model in await get_filtered_models(await get_all_models(request, user=user), user)} + subagent_models = [] + for entry in configured_models: + model = models.get(entry.get('id')) + if not model: + continue + description = entry.get('description') or model.get('info', {}).get('meta', {}).get('description') or '' + subagent_models.append({'id': model['id'], 'name': model.get('name', model['id']), 'description': description}) + return subagent_models + + async def process_pending_internal_messages( source_request: Request, parent_chat_id: str, @@ -282,6 +298,7 @@ async def delegate( background: bool, *, file_ids: list[str] | None = None, + model_id: str | None = None, request: Request, user_data: dict, metadata: dict, @@ -343,6 +360,16 @@ async def delegate( return 'Error: model context is required.' if run.get('direct'): return 'Error: sub-agents are unavailable for direct connections.' + if model_id: + subagent_models = await get_subagent_models(request, UserModel(**user_data)) + available_model_ids = [model['id'] for model in subagent_models] + if model_id not in available_model_ids: + return ( + f'Error: model {model_id} is not available for sub-agents. ' + f'Available models: {", ".join(available_model_ids) or "none"}. ' + 'Omit model_id to use the current model.' + ) + model_id = model_id or run['model_id'] delegated_files = [] if file_ids: requested_file_ids = {str(file_id) for file_id in file_ids if file_id} @@ -405,7 +432,7 @@ async def delegate( 'role': 'user', 'content': prompt, 'timestamp': int(time.time()), - 'models': [run['model_id']], + 'models': [model_id], **({'files': prompt_files} if prompt_files else {}), } chat = await Chats.insert_new_chat( @@ -415,7 +442,7 @@ async def delegate( chat={ 'id': chat_id, 'title': f'Sub-agent: {task[:60]}', - 'models': [run['model_id']], + 'models': [model_id], 'history': { 'currentId': assistant_message_id, 'messages': { @@ -427,7 +454,7 @@ async def delegate( 'role': 'assistant', 'content': '', 'done': False, - 'model': run['model_id'], + 'model': model_id, 'timestamp': int(time.time()), }, }, @@ -472,7 +499,7 @@ async def delegate( str(config.get('subagents.system_prompt') or '').strip() or DEFAULT_SUBAGENT_SYSTEM_PROMPT ) form_data = { - 'model': run['model_id'], + 'model': model_id, 'params': chat_context.get('params') or {}, 'messages': add_or_update_system_message( subagent_system_prompt, diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index 64b76fc756..c7445dda18 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -938,11 +938,22 @@ async def get_builtin_tools( ) spec = get_builtin_tool_spec(func) - if func.__name__ == 'delegate_task' and not config.get('subagents.background_enabled'): + if func.__name__ == 'delegate_task': + from open_webui.utils.subagents import get_subagent_models + parameters = spec.get('parameters', {}) - parameters.get('properties', {}).pop('background', None) - if isinstance(parameters.get('required'), list): - parameters['required'] = [name for name in parameters['required'] if name != 'background'] + properties = parameters.get('properties', {}) + if not config.get('subagents.background_enabled'): + properties.pop('background', None) + if isinstance(parameters.get('required'), list): + parameters['required'] = [name for name in parameters['required'] if name != 'background'] + + subagent_models = await get_subagent_models(request, UserModel(**user)) if user else [] + if subagent_models: + properties['model_id']['enum'] = [subagent_model['id'] for subagent_model in subagent_models] + spec['description'] += f' Available models: {JSONCodec.dumps(subagent_models)}' + else: + properties.pop('model_id', None) tools_dict[func.__name__] = { 'tool_id': f'builtin:{func.__name__}', diff --git a/src/lib/components/admin/Settings/Subagents.svelte b/src/lib/components/admin/Settings/Subagents.svelte index a1a490c4f2..90e9adfd18 100644 --- a/src/lib/components/admin/Settings/Subagents.svelte +++ b/src/lib/components/admin/Settings/Subagents.svelte @@ -4,16 +4,22 @@ import { toast } from 'svelte-sonner'; import { config as appConfig } from '$lib/stores'; - import { getBackendConfig } from '$lib/apis'; + import { getBackendConfig, getModels } from '$lib/apis'; import { getSubagentsConfig, setSubagentsConfig } from '$lib/apis/configs'; + import SettingsSelect from '$lib/components/common/SettingsSelect.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; import Switch from '$lib/components/common/Switch.svelte'; + import Tooltip from '$lib/components/common/Tooltip.svelte'; + import Plus from '$lib/components/icons/Plus.svelte'; + import XMark from '$lib/components/icons/XMark.svelte'; const i18n = getContext('i18n'); let loading = true; let saving = false; let enabled = false; + let subagentModels: { id: string; description: string }[] = []; + let models: any[] = []; let backgroundEnabled = false; let maxConcurrent = 20; let maxAsync = 20; @@ -25,12 +31,14 @@ try { const config = await getSubagentsConfig(localStorage.token); enabled = config?.ENABLE_SUBAGENTS ?? false; + subagentModels = config?.SUBAGENTS_MODELS ?? []; backgroundEnabled = config?.SUBAGENTS_BACKGROUND_ENABLED ?? false; maxConcurrent = Number(config?.SUBAGENTS_MAX_CONCURRENT) || 20; maxAsync = Number(config?.SUBAGENTS_MAX_ASYNC) || 20; maxIterations = Number(config?.SUBAGENTS_MAX_ITERATIONS) || 30; maxOutput = Number(config?.SUBAGENTS_MAX_OUTPUT) || 30000; systemPrompt = config?.SUBAGENTS_SYSTEM_PROMPT ?? ''; + models = await getModels(localStorage.token, null, false); } catch (error) { toast.error(`${error}`); } finally { @@ -43,6 +51,7 @@ try { await setSubagentsConfig(localStorage.token, { ENABLE_SUBAGENTS: enabled, + SUBAGENTS_MODELS: subagentModels, SUBAGENTS_BACKGROUND_ENABLED: backgroundEnabled, SUBAGENTS_MAX_CONCURRENT: maxConcurrent, SUBAGENTS_MAX_ASYNC: maxAsync, @@ -83,6 +92,74 @@
{#if enabled} +