Files
open-webui/src/lib/components/workspace/Workspace.svelte
T
2026-11-13 22:40:53 +09:00

291 lines
10 KiB
Svelte

<script lang="ts">
import type { WorkspaceContext } from '$lib/types';
import { getPageContext } from '$lib/utils/page';
const { goto, url: pageUrl, tab: pageTab } = getPageContext();
import { onMount, getContext, setContext } from 'svelte';
import type { Writable } from 'svelte/store';
import type { i18n as i18nType } from 'i18next';
import {
WEBUI_NAME,
config,
showSidebar,
user,
mobile,
type WorkspaceAction,
type WorkspaceSection
} from '$lib/stores';
import { getModelItems } from '$lib/apis/models';
import { searchKnowledgeBases } from '$lib/apis/knowledge';
import { getPromptItems } from '$lib/apis/prompts';
import { getSkillItems } from '$lib/apis/skills';
import { getToolList } from '$lib/apis/tools';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Sidebar from '$lib/components/icons/Sidebar.svelte';
import SplitCreateButton from '$lib/components/common/SplitCreateButton.svelte';
import { formatNumber } from '$lib/utils';
const i18n = getContext<Writable<i18nType>>('i18n');
import { writable } from 'svelte/store';
const workspaceActions = writable<WorkspaceAction[]>([]);
const workspaceCounts = writable<Record<WorkspaceSection, number | null>>({
models: null,
knowledge: null,
prompts: null,
skills: null,
tools: null
});
setContext<WorkspaceContext>('workspace', { workspaceActions, workspaceCounts });
let loaded = false;
let lastPath = '';
let activeWorkspaceSection = '';
let visibleActions = [];
$: if ($pageUrl.pathname !== lastPath) {
lastPath = $pageUrl.pathname;
workspaceActions.set([]);
}
$: if (loaded && $pageUrl.pathname.startsWith('/workspace')) {
loadWorkspaceCounts();
}
$: activeWorkspaceSection = $pageUrl.pathname.split('/')[2] ?? '';
$: visibleActions = $workspaceActions.filter((action) => action.visible ?? true);
const getCount = (res: any) => res?.total ?? (Array.isArray(res) ? res.length : null);
const formatCount = (count: number | null) => formatNumber(count ?? 0);
const loadWorkspaceCounts = async () => {
const canViewModels = $user?.role === 'admin' || $user?.permissions?.workspace?.models;
const canViewKnowledge = $user?.role === 'admin' || $user?.permissions?.workspace?.knowledge;
const canViewPrompts = $user?.role === 'admin' || $user?.permissions?.workspace?.prompts;
const canViewSkills = $user?.role === 'admin' || $user?.permissions?.workspace?.skills;
const canViewTools =
$config?.features?.enable_tools &&
($user?.role === 'admin' || $user?.permissions?.workspace?.tools);
const [modelRes, knowledgeRes, promptRes, skillRes, toolRes] = await Promise.all([
canViewModels
? getModelItems(localStorage.token, null, null, null, null, null, 1).catch(() => null)
: null,
canViewKnowledge
? searchKnowledgeBases(localStorage.token, null, null, 1, null).catch(() => null)
: null,
canViewPrompts
? getPromptItems(localStorage.token, null, null, null, null, null, 1).catch(() => null)
: null,
canViewSkills ? getSkillItems(localStorage.token, null, null, 1).catch(() => null) : null,
canViewTools ? getToolList(localStorage.token).catch(() => null) : null
]);
workspaceCounts.set({
models: getCount(modelRes),
knowledge: getCount(knowledgeRes),
prompts: getCount(promptRes),
skills: getCount(skillRes),
tools: getCount(toolRes)
});
};
onMount(async () => {
if ($user?.role !== 'admin') {
if ($pageUrl.pathname.includes('/models') && !$user?.permissions?.workspace?.models) {
goto('/', { replaceState: true });
} else if (
$pageUrl.pathname.includes('/knowledge') &&
!$user?.permissions?.workspace?.knowledge
) {
goto('/', { replaceState: true });
} else if (
$pageUrl.pathname.includes('/prompts') &&
!$user?.permissions?.workspace?.prompts
) {
goto('/', { replaceState: true });
} else if (
$pageUrl.pathname.includes('/tools') &&
(!$config?.features?.enable_tools || !$user?.permissions?.workspace?.tools)
) {
goto('/', { replaceState: true });
} else if ($pageUrl.pathname.includes('/skills') && !$user?.permissions?.workspace?.skills) {
goto('/', { replaceState: true });
}
}
loaded = true;
});
import PageTitle from '$lib/components/common/PageTitle.svelte';
</script>
<!-- LICENSE covers this Open WebUI browser-title identifier.
Do not alter, remove, obscure, or replace it except as LICENSE permits:
https://docs.openwebui.com/license. -->
<PageTitle title={$i18n.t('Workspace') + ' / ' + $WEBUI_NAME} />
{#if loaded}
<div
id="workspace-layout"
class="flex flex-1 min-w-0 w-full h-full max-h-full transition-width duration-200 ease-in-out {$showSidebar &&
!pageTab
? 'md:max-w-[calc(100%-var(--sidebar-width))]'
: ''} max-w-full"
>
<div class="flex h-full min-w-0 flex-1 flex-col">
<nav class="px-1.5 md:px-2.5 drag-region select-none">
<div class="flex items-center gap-0.5 md:gap-1">
{#if $mobile && !pageTab}
<div class="{$showSidebar ? 'md:hidden' : ''} self-center flex flex-none items-center">
<Tooltip
content={$showSidebar ? $i18n.t('Close Sidebar') : $i18n.t('Open Sidebar')}
interactive={true}
>
<button
id="sidebar-toggle-button"
class=" cursor-pointer flex rounded-lg hover:bg-gray-100 dark:hover:bg-gray-850 transition cursor-"
aria-label={$showSidebar ? $i18n.t('Close Sidebar') : $i18n.t('Open Sidebar')}
on:click={() => {
showSidebar.set(!$showSidebar);
}}
>
<div class=" self-center p-1.5">
<Sidebar className="size-4" />
</div>
</button>
</Tooltip>
</div>
{/if}
<div class="flex w-full items-center">
<div
class="flex min-w-0 mr-1.5 items-center gap-0.5 md:gap-1 scrollbar-none overflow-x-auto w-fit text-center text-sm font-normal bg-transparent py-1 touch-auto pointer-events-auto"
on:wheel={(e) => {
if (e.deltaY !== 0) {
e.preventDefault();
e.currentTarget.scrollLeft += e.deltaY;
}
}}
>
{#if $user?.role === 'admin' || $user?.permissions?.workspace?.models}
<a
draggable="false"
data-sveltekit-keepfocus
aria-current={activeWorkspaceSection === 'models' ? 'page' : null}
class="min-w-fit px-1 text-sm inline-flex items-center gap-1 {activeWorkspaceSection ===
'models'
? 'text-gray-900 dark:text-gray-100'
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition select-none"
href="/workspace/models"
>
<span>{$i18n.t('Models')}</span>
<span class="text-sm opacity-60">
{formatCount($workspaceCounts.models)}
</span>
</a>
{/if}
{#if $user?.role === 'admin' || $user?.permissions?.workspace?.knowledge}
<a
draggable="false"
data-sveltekit-keepfocus
aria-current={activeWorkspaceSection === 'knowledge' ? 'page' : null}
class="min-w-fit px-1 text-sm inline-flex items-center gap-1 {activeWorkspaceSection ===
'knowledge'
? 'text-gray-900 dark:text-gray-100'
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition select-none"
href="/workspace/knowledge"
>
<span>{$i18n.t('Knowledge')}</span>
<span class="text-sm opacity-60">
{formatCount($workspaceCounts.knowledge)}
</span>
</a>
{/if}
{#if $user?.role === 'admin' || $user?.permissions?.workspace?.prompts}
<a
draggable="false"
data-sveltekit-keepfocus
aria-current={activeWorkspaceSection === 'prompts' ? 'page' : null}
class="min-w-fit px-1 text-sm inline-flex items-center gap-1 {activeWorkspaceSection ===
'prompts'
? 'text-gray-900 dark:text-gray-100'
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition select-none"
href="/workspace/prompts"
>
<span>{$i18n.t('Prompts')}</span>
<span class="text-sm opacity-60">
{formatCount($workspaceCounts.prompts)}
</span>
</a>
{/if}
{#if $user?.role === 'admin' || $user?.permissions?.workspace?.skills}
<a
draggable="false"
data-sveltekit-keepfocus
aria-current={activeWorkspaceSection === 'skills' ? 'page' : null}
class="min-w-fit px-1 text-sm inline-flex items-center gap-1 {activeWorkspaceSection ===
'skills'
? 'text-gray-900 dark:text-gray-100'
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition select-none"
href="/workspace/skills"
>
<span>{$i18n.t('Skills')}</span>
<span class="text-sm opacity-60">
{formatCount($workspaceCounts.skills)}
</span>
</a>
{/if}
{#if $config?.features?.enable_tools && ($user?.role === 'admin' || $user?.permissions?.workspace?.tools)}
<a
draggable="false"
data-sveltekit-keepfocus
aria-current={activeWorkspaceSection === 'tools' ? 'page' : null}
class="min-w-fit px-1 text-sm inline-flex items-center gap-1 {activeWorkspaceSection ===
'tools'
? 'text-gray-900 dark:text-gray-100'
: 'text-gray-300 dark:text-gray-600 hover:text-gray-700 dark:hover:text-white'} transition select-none"
href="/workspace/tools"
>
<span>{$i18n.t('Tools')}</span>
<span class="text-sm opacity-60">
{formatCount($workspaceCounts.tools)}
</span>
</a>
{/if}
</div>
<div class="ml-auto flex shrink-0 items-center gap-1">
<SplitCreateButton actions={visibleActions} />
</div>
</div>
<!-- <div class="flex items-center text-xl font-normal">{$i18n.t('Workspace')}</div> -->
</div>
</nav>
<div
class="pb-1 flex-1 min-w-0 max-h-full overflow-y-auto overflow-x-hidden"
class:px-3={!['/workspace/models/create', '/workspace/models/edit'].includes(
$pageUrl.pathname
)}
id="workspace-container"
>
<slot />
</div>
</div>
</div>
{/if}
<style>
:global(html.high-contrast) a[aria-current='page'] {
text-decoration-line: underline;
text-decoration-thickness: 2px;
text-underline-offset: 3px;
}
</style>