mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-16 09:41:23 +00:00
refac
This commit is contained in:
parent
cd2db536c9
commit
6da3e7a5d6
3 changed files with 46 additions and 10 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue