feat: websocket

This commit is contained in:
Timothy J. Baek
2024-06-03 23:39:52 -07:00
parent 0495f01acb
commit 85484392b2
7 changed files with 153 additions and 4 deletions

View File

@@ -2,8 +2,9 @@ import { browser, dev } from '$app/environment';
// import { version } from '../../package.json';
export const APP_NAME = 'Open WebUI';
export const WEBUI_BASE_URL = browser ? (dev ? `http://${location.hostname}:8080` : ``) : ``;
export const WEBUI_HOSTNAME = browser ? (dev ? `${location.hostname}:8080` : ``) : '';
export const WEBUI_BASE_URL = browser ? (dev ? `http://${WEBUI_HOSTNAME}` : ``) : ``;
export const WEBUI_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1`;
export const OLLAMA_API_BASE_URL = `${WEBUI_BASE_URL}/ollama`;

View File

@@ -2,6 +2,7 @@ import { APP_NAME } from '$lib/constants';
import { type Writable, writable } from 'svelte/store';
import type { GlobalModelConfig, ModelConfig } from '$lib/apis';
import type { Banner } from '$lib/types';
import type { Socket } from 'socket.io-client';
// Backend
export const WEBUI_NAME = writable(APP_NAME);
@@ -13,6 +14,8 @@ export const MODEL_DOWNLOAD_POOL = writable({});
export const mobile = writable(false);
export const socket: Writable<null | Socket> = writable(null);
export const theme = writable('system');
export const chatId = writable('');

View File

@@ -1,6 +1,8 @@
<script>
import { io } from 'socket.io-client';
import { onMount, tick, setContext } from 'svelte';
import { config, user, theme, WEBUI_NAME, mobile } from '$lib/stores';
import { config, user, theme, WEBUI_NAME, mobile, socket } from '$lib/stores';
import { goto } from '$app/navigation';
import { Toaster, toast } from 'svelte-sonner';
@@ -12,7 +14,7 @@
import 'tippy.js/dist/tippy.css';
import { WEBUI_BASE_URL } from '$lib/constants';
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
import i18n, { initI18n, getLanguages } from '$lib/i18n';
setContext('i18n', i18n);
@@ -55,10 +57,20 @@
if (backendConfig) {
// Save Backend Status to Store
await config.set(backendConfig);
await WEBUI_NAME.set(backendConfig.name);
if ($config) {
const _socket = io(`${WEBUI_BASE_URL}`, {
path: '/ws/socket.io',
auth: { token: localStorage.token }
});
_socket.on('connect', () => {
console.log('connected');
});
socket.set(_socket);
if (localStorage.token) {
// Get Session User Info
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {