diff --git a/backend/open_webui/routers/terminals.py b/backend/open_webui/routers/terminals.py index 8ab4c908ab..b0e2ae7fbd 100644 --- a/backend/open_webui/routers/terminals.py +++ b/backend/open_webui/routers/terminals.py @@ -431,13 +431,22 @@ async def _resolve_terminal_access(ws: WebSocket, server_id: str, token: str): return user, connection +@router.websocket('/{server_id}/files/events') +async def ws_file_events(ws: WebSocket, server_id: str): + await _proxy_terminal_websocket(ws, server_id) + + @router.websocket('/{server_id}/api/terminals/{session_id}') -async def ws_terminal( +async def ws_terminal(ws: WebSocket, server_id: str, session_id: str): + await _proxy_terminal_websocket(ws, server_id, session_id) + + +async def _proxy_terminal_websocket( ws: WebSocket, server_id: str, - session_id: str, + session_id: str | None = None, ): - """Proxy an interactive WebSocket terminal session to a terminal server. + """Proxy terminal sessions and filesystem notifications to a terminal server. Uses first-message auth: the client sends ``{"type": "auth", "token": ""}`` as its first message. The proxy validates the JWT, then connects to the @@ -473,9 +482,11 @@ async def ws_terminal( # Encode session_id as an opaque path segment so it cannot smuggle '?'/'#'/'&' (at any # decode depth) and inject an attacker-chosen user_id ahead of the one appended below. - safe_session_id = urllib.parse.quote(session_id, safe='') - - upstream_url = f'{ws_base}/api/terminals/{safe_session_id}' + upstream_path = ( + f'api/terminals/{urllib.parse.quote(session_id, safe="")}' + if session_id is not None else 'files/events' + ) + upstream_url = f'{ws_base}/{upstream_path}' if upstream_params: upstream_url += f'?{urllib.parse.urlencode(upstream_params)}' @@ -500,15 +511,16 @@ async def ws_terminal( else: await upstream.send_str(_json.dumps({'type': 'auth', 'token': ''})) - await publish_event( - request, - EVENTS.TERMINAL_SESSION_OPENED, - actor=user, - subject_id=session_id, - subject_type='terminal.session', - data={'server_id': server_id}, - ) - opened = True + if session_id is not None: + await publish_event( + request, + EVENTS.TERMINAL_SESSION_OPENED, + actor=user, + subject_id=session_id, + subject_type='terminal.session', + data={'server_id': server_id}, + ) + opened = True async def _client_to_upstream(): """Forward client → upstream.""" @@ -537,6 +549,8 @@ async def ws_terminal( aiohttp.WSMsgType.ERROR, ): break + if session_id is None and upstream.close_code and 4000 <= upstream.close_code < 5000: + await ws.close(code=upstream.close_code) except Exception: pass diff --git a/src/lib/apis/terminal/events.ts b/src/lib/apis/terminal/events.ts new file mode 100644 index 0000000000..85eab09149 --- /dev/null +++ b/src/lib/apis/terminal/events.ts @@ -0,0 +1,52 @@ +import type { TerminalConnection } from './index'; + +export function watchTerminalFiles( + connection: TerminalConnection, + chatId: string | null, + directory: string, + onChange: () => void, + onError: (message: string) => void +) { + let stopped = false; + let socket: WebSocket; + let retry: ReturnType; + let retryDelay = 1000; + const connect = () => { + const url = new URL(`${connection.baseUrl.replace(/\/$/, '')}/files/events`, location.href); + url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'; + socket = new WebSocket(url); + socket.onopen = () => { + socket.send( + JSON.stringify({ type: 'auth', token: connection.key.trim(), chat_id: chatId ?? '' }) + ); + socket.send(JSON.stringify({ type: 'subscribe', path: directory })); + }; + socket.onmessage = (event) => { + if (stopped) return; + try { + const message = JSON.parse(event.data); + if (message.type === 'ready') { + retryDelay = 1000; + onError(''); + onChange(); + } else if (message.type === 'change') onChange(); + } catch { + /* Ignore frames that are not filesystem notifications. */ + } + }; + socket.onclose = (event) => { + if (stopped) return; + onError('Live updates unavailable'); + if (event.code >= 4000 && event.code < 5000) return; + retry = setTimeout(connect, retryDelay); + retryDelay = Math.min(retryDelay * 2, 30000); + }; + }; + connect(); + return () => { + stopped = true; + clearTimeout(retry); + socket.onopen = socket.onmessage = socket.onclose = null; + socket.close(); + }; +} diff --git a/src/lib/components/chat/ChatTabs/GitBar.svelte b/src/lib/components/chat/ChatTabs/GitBar.svelte index eeb49fbc24..4798b6e63b 100644 --- a/src/lib/components/chat/ChatTabs/GitBar.svelte +++ b/src/lib/components/chat/ChatTabs/GitBar.svelte @@ -1,6 +1,7 @@ {#if status?.is_repo}
{#if expanded} @@ -338,7 +353,7 @@ aria-orientation="horizontal" aria-valuenow={height} tabindex="0" - class="absolute inset-x-0 -top-[0.1875rem] h-[0.375rem] z-20 cursor-row-resize touch-none hover:bg-gray-200/50 dark:hover:bg-white/8" + class="absolute inset-x-0 -top-px h-px z-20 cursor-row-resize touch-none border-t border-transparent hover:border-gray-200 dark:hover:border-gray-800 transition" on:pointerdown={(event) => { event.preventDefault(); (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); @@ -364,7 +379,9 @@ ); } }} - > + > + + {/if}
@@ -573,7 +590,9 @@ {#if status.behind}↓{status.behind}{/if} {/if} - +