From 6da3e7a5d60caa516dd7a34d64648e7fcc7a4322 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 6 Nov 2026 14:37:15 +0900 Subject: [PATCH] refac --- backend/open_webui/routers/audio/realtime.py | 34 ++++++++++++++++---- src/lib/components/chat/Chat.svelte | 1 + src/lib/utils/realtime.ts | 21 +++++++++--- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/backend/open_webui/routers/audio/realtime.py b/backend/open_webui/routers/audio/realtime.py index 5243cabad5..769443ff5e 100644 --- a/backend/open_webui/routers/audio/realtime.py +++ b/backend/open_webui/routers/audio/realtime.py @@ -8,6 +8,7 @@ from urllib.parse import urlencode, urlsplit, urlunsplit import aiohttp from fastapi import APIRouter, WebSocket, WebSocketDisconnect +from fastapi.security import HTTPAuthorizationCredentials from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL, DEFAULT_REALTIME_CALL_PROMPT_TEMPLATE from open_webui.env import AIOHTTP_CLIENT_SESSION_SSL, BYPASS_MODEL_ACCESS_CONTROL, ENABLE_FORWARD_USER_INFO_HEADERS from open_webui.models.chats import Chats @@ -402,13 +403,34 @@ async def realtime_call(ws: WebSocket): model_id = auth.get('model_id') if not isinstance(model_id, str): raise ValueError('Select a chat model') - if not ws.app.state.MODELS: - await get_all_models(ws, user=user) - model = ws.app.state.MODELS.get(model_id) - if not model or model.get('direct'): - raise ValueError('Bridge requires a server-configured chat model') + agent_model = model_id.startswith('agent:') + if agent_model: + from open_webui.utils.agents.discovery import get_models as get_agent_models + + terminal_id = auth.get('terminal_id') + if not isinstance(terminal_id, str) or not terminal_id: + raise ValueError('Select a terminal to call an agent') + parts = model_id.split(':', 3) + if len(parts) != 4: + raise ValueError('Invalid agent model') + # Match HTTP chat authentication for terminal connections using session auth. + ws.state.token = HTTPAuthorizationCredentials(scheme='Bearer', credentials=token) + candidates = await get_agent_models(ws, user, terminal_id, chat_id, provider=parts[2]) + model = next((item for item in candidates if item['id'] == model_id), None) + if model is None: + raise ValueError('Agent model is unavailable in the selected terminal') + else: + if not ws.app.state.MODELS: + await get_all_models(ws, user=user) + model = ws.app.state.MODELS.get(model_id) + if not model or model.get('direct'): + raise ValueError('Bridge requires a server-configured chat model') model_info = await Models.get_model_by_id(model_id) - if not BYPASS_MODEL_ACCESS_CONTROL and (user.role != 'admin' or not BYPASS_ADMIN_ACCESS_CONTROL): + if ( + not agent_model + and not BYPASS_MODEL_ACCESS_CONTROL + and (user.role != 'admin' or not BYPASS_ADMIN_ACCESS_CONTROL) + ): try: await check_model_access(user, model, model_info=model_info) except Exception: diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 014eec325b..0997de867b 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -2469,6 +2469,7 @@ return { chatId: isTemporaryChatId($chatId) ? undefined : $chatId || undefined, modelId: model && !('direct' in model && model.direct) ? model.id : '', + terminalId: $selectedTerminalId || undefined, voiceModel: $config?.audio?.realtime?.model, voice: model?.info?.meta?.voice?.voice || $config?.audio?.realtime?.voice, messages: createMessagesList(history, history.currentId).map((message) => { diff --git a/src/lib/utils/realtime.ts b/src/lib/utils/realtime.ts index 4f130839ea..f8ab71004c 100644 --- a/src/lib/utils/realtime.ts +++ b/src/lib/utils/realtime.ts @@ -12,6 +12,7 @@ export type BridgeSubmission = { type CallContext = { chatId?: string; modelId: string; + terminalId?: string; voiceModel?: string; voice?: string; messages: { role: string; content: string }[]; @@ -127,8 +128,13 @@ export class RealtimeCall { try { const context = this.options.context(); this.token = token; - this.configuration = JSON.stringify([context.modelId, context.voiceModel, context.voice]); - if (!context.modelId) throw new Error('Select one server-configured chat model.'); + this.configuration = JSON.stringify([ + context.modelId, + context.terminalId, + context.voiceModel, + context.voice + ]); + if (!context.modelId) throw new Error('Select one chat model or agent.'); this.context = new AudioContext({ sampleRate: 24000 }); if (this.context.sampleRate !== 24000 || !this.context.audioWorklet) { throw new Error( @@ -160,7 +166,13 @@ export class RealtimeCall { const started = Date.now(); this.lastPong = started; ws.onopen = () => - this.send({ type: 'auth', token, chat_id: context.chatId, model_id: context.modelId }); + this.send({ + type: 'auth', + token, + chat_id: context.chatId, + model_id: context.modelId, + terminal_id: context.terminalId + }); ws.onmessage = ({ data }) => { if (session !== this.session) return; try { @@ -302,7 +314,8 @@ export class RealtimeCall { return; const context = this.options.context(); if ( - this.configuration !== JSON.stringify([context.modelId, context.voiceModel, context.voice]) + this.configuration !== + JSON.stringify([context.modelId, context.terminalId, context.voiceModel, context.voice]) ) { const token = this.token; this.end();