Files
open-webui/backend/open_webui/utils/agents/discovery.py
T
Classic298 7429093191 fix: coding agent models get ids made only of letters, digits, dashes and underscores (#34171)
Cursor and Pi model names carry brackets, equals signs, commas or slashes, and Open WebUI kept those in the ids of their agent models, so a pinned Cursor or Pi model opened a new chat on "Select a model". Agent models now get ids made only of letters, digits, dashes and underscores, with a few extra characters added when the name had to change, so two models never end up with the same id. The agent itself still runs on its own model name. Existing pins, default model choices and automations that point at an agent model whose name had other characters (dots included, as in most Codex models) stop matching and need the model picked again, and an existing chat with such an agent answers "Agent model is unavailable in the selected terminal" until the model is picked again.

Fixes #34168
2026-11-12 22:00:04 +09:00

79 lines
2.8 KiB
Python

"""Models exposed by the selected Open Terminal environment."""
from urllib.parse import urlencode
from open_webui.utils.agents.session import model_id
from open_webui.utils.terminals import MissingTerminalContext, get_terminal_json
def model_controls(model):
controls = {}
for key, label, values, value_key, label_key, default_key in (
(
'reasoning_effort',
'Reasoning effort',
'supportedReasoningEfforts',
'reasoningEffort',
'reasoningEffort',
'defaultReasoningEffort',
),
('service_tier', 'Service tier', 'serviceTiers', 'id', 'name', 'defaultServiceTier'),
):
options = {
option[value_key]: {
'label': option.get(label_key) or option[value_key],
'params': {key: option[value_key]},
}
for option in model.get(values, [])
}
if options:
controls[key] = {'label': label, 'options': options}
if model.get(default_key) in options:
controls[key]['default'] = model[default_key]
return controls
async def get_models(request, user, metadata, *, provider=None, cwd=None):
terminal_id = metadata.get('terminal_id')
if not terminal_id:
return []
try:
if not cwd:
directory = await get_terminal_json(request, user, metadata, '/files/cwd')
cwd = (directory or {}).get('cwd')
params = {'provider': provider} if provider else {}
if cwd:
params['cwd'] = cwd
path = '/agents' + ('?' + urlencode(params) if params else '')
catalog = await get_terminal_json(request, user, metadata, path)
except MissingTerminalContext:
return []
if not catalog or catalog.get('protocol_version') != 1:
return []
models = [
{
'id': model_id({'terminal_id': terminal_id, 'provider': agent['id'], 'model': model['id']}),
'name': f'{agent["id"]}/{model["id"]}',
'owned_by': 'terminal',
'object': 'model',
'agent': {'terminal_id': terminal_id, 'provider': agent['id'], 'model': model['id']},
'info': {
'params': {'model_controls': model_controls(model)},
'meta': {
'capabilities': {
'vision': True,
'file_upload': True,
'web_search': False,
'image_generation': False,
'code_interpreter': False,
'builtin_tools': False,
}
},
},
}
for agent in catalog.get('agents', [])
if agent.get('available')
for model in agent.get('models', [])
]
return list({model['id']: model for model in models}.values())