Files
open-webui/src/lib/components/chat/MessageInput/TerminalMenu.svelte
T
2026-11-10 21:18:18 +09:00

530 lines
18 KiB
Svelte

<script lang="ts">
export let modelId: string | null = null;
import { getContext, tick } from 'svelte';
import { resolveTerminalTarget, type TerminalTarget } from '$lib/apis/terminal';
import UserTerminalsModal from '../UserTerminalsModal.svelte';
import { updateChatById } from '$lib/apis/chats';
import { toast } from 'svelte-sonner';
import TerminalSystemInfoModal from '../TerminalSystemInfoModal.svelte';
import Activity from '$lib/components/icons/Activity.svelte';
import WorkingDirectoryMenu from './WorkingDirectoryMenu.svelte';
import type { TerminalContext } from '$lib/types';
import {
chatId as activeChatId,
config,
settings,
showSettings,
terminalServers,
selectedTerminalId,
temporaryChatEnabled,
user
} from '$lib/stores';
import { getToolServersData } from '$lib/apis';
import { isTemporaryChatId, isSavedChatId } from '$lib/utils/chatId';
import Dropdown from '$lib/components/common/Dropdown.svelte';
import DropdownMenu from '$lib/components/common/DropdownMenu.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Cloud from '$lib/components/icons/Cloud.svelte';
import Server from '$lib/components/icons/Server.svelte';
import ChevronRight from '$lib/components/icons/ChevronRight.svelte';
const i18n = getContext<any>('i18n');
export let show = false;
export let inMenu = false;
export let disabled = false;
export let chatId: string | null = null;
export let terminalContext: TerminalContext | null = null;
$: systemTerminals = ($terminalServers ?? []).filter(
(t) =>
t.id &&
!(
($temporaryChatEnabled || isTemporaryChatId($activeChatId)) &&
t.scope === 'chat'
)
);
// Tether presences are the user's own paired devices, listed apart from servers.
const isDevice = (terminal: any) => terminal.id.startsWith('tether:');
$: systemSections = [
{ devices: false, managed: true, terminals: systemTerminals.filter((t) => !isDevice(t)) },
{ devices: true, managed: false, terminals: systemTerminals.filter(isDevice) }
].filter((section) => !section.devices || section.terminals.length > 0);
$: directTerminals = (
$config?.features?.enable_direct_integrations &&
($user?.role === 'admin' || ($user?.permissions?.features?.direct_tool_servers ?? true))
? ($settings?.terminalServers ?? [])
: []
).filter((s) => s.url);
const refreshTerminalServersStore = async (servers: typeof directTerminals) => {
// Preserve system terminals (those with an `id`) — only refresh direct ones
const existingSystemTerminals = ($terminalServers ?? []).filter((t) => t.id);
const activeTerminals = servers.filter((s) => s.enabled);
if (activeTerminals.length > 0) {
let data = await getToolServersData(
activeTerminals.map((t) => ({
url: t.url,
auth_type: t.auth_type ?? 'bearer',
key: t.key ?? '',
path: t.path ?? '/openapi.json',
config: { enable: true }
}))
);
data = data.filter((d) => d && !d.error);
terminalServers.set([
...data.map((d, i) => ({
...d,
key: activeTerminals[i]?.key ?? '',
config: activeTerminals[i]?.config ?? d?.config ?? {}
})),
...existingSystemTerminals
]);
} else {
terminalServers.set(existingSystemTerminals);
}
};
const selectDirect = async (terminal: (typeof directTerminals)[0]) => {
const newId = $selectedTerminalId === terminal.url ? null : terminal.url;
if (!(await saveSelection(newId))) return;
selectedTerminalId.set(newId);
// Enable the selected direct terminal, disable all others
const updatedServers = ($settings?.terminalServers ?? []).map((s) => ({
...s,
enabled: newId !== null && s.url === terminal.url
}));
settings.set({
...$settings,
terminalServers: updatedServers
});
show = false;
// Refresh the store so Chat.svelte can inject it as a tool
await refreshTerminalServersStore(updatedServers);
};
const selectSystem = async (terminal: (typeof systemTerminals)[0]) => {
const newId = $selectedTerminalId === terminal.id ? null : terminal.id;
if (!(await saveSelection(newId))) return;
selectedTerminalId.set(newId);
// Disable all direct terminals when switching to a system terminal
if ($settings?.terminalServers?.some((s) => s.enabled)) {
const updatedServers = ($settings.terminalServers ?? []).map((s) => ({
...s,
enabled: false
}));
settings.set({
...$settings,
terminalServers: updatedServers
});
await refreshTerminalServersStore(updatedServers);
}
show = false;
};
$: selectedSystemTerminal = systemTerminals.find((t) => (!!$selectedTerminalId && t.id === $selectedTerminalId));
$: selectedDirectTerminal = directTerminals.find((t) => t.url === $selectedTerminalId);
$: folderTarget = resolveTerminalTarget(
$selectedTerminalId,
systemTerminals,
directTerminals,
'', { chatId: $activeChatId || null, modelId: modelId });
$: selectedDevice = !!selectedSystemTerminal && isDevice(selectedSystemTerminal);
$: selectedLabel =
selectedSystemTerminal?.name ||
selectedSystemTerminal?.id ||
selectedDirectTerminal?.name ||
selectedDirectTerminal?.url?.replace(/^https?:\/\//, '') ||
($selectedTerminalId ? $i18n.t('Unavailable terminal') : $i18n.t('Terminal'));
$: if (disabled && show) show = false;
let showTerminals = false;
async function saveSelection(id: string | null) {
if (isSavedChatId(chatId)) {
try {
await updateChatById(localStorage.token, chatId!, {
terminal_id: id,
working_directory: ''
});
} catch (e) {
toast.error(String(e));
return false;
}
}
return true;
}
async function selectTerminal(id: string) {
if (await saveSelection(id)) selectedTerminalId.set(id);
}
let info: { target: TerminalTarget; label: string } | null = null;
const openInfo = async (selector: string, label: string) => {
if (
systemTerminals.find((t) => (!!selector && t.id === selector))?.scope === 'chat' &&
!isSavedChatId($activeChatId)
)
return;
const target = resolveTerminalTarget(
selector,
systemTerminals,
directTerminals,
localStorage.token ?? '', { chatId: $activeChatId || null, modelId: modelId });
if (!target) return;
show = false;
await tick();
info = { target, label };
};
$: if (
!$config?.features?.enable_tool_servers ||
(info &&
(info.target.context.chatId !== $activeChatId ||
(info.target.system
? !systemTerminals.some((t) => t.id === info?.target.selector)
: !directTerminals.some((t) => t.url === info?.target.selector))))
)
info = null;
$: triggerClass = inMenu
? `flex w-full min-w-0 items-center gap-2 h-[1.6875rem] px-2 text-[0.8125rem] rounded-xl text-left ${disabled ? 'cursor-not-allowed opacity-40' : 'cursor-pointer hover:bg-gray-50/40 dark:hover:bg-gray-800/40'}`
: `flex items-center gap-1.5 translate-y-[1px] text-[0.8125rem] transition rounded-lg ${
disabled
? 'p-1 cursor-not-allowed opacity-40 text-gray-400 dark:text-gray-600'
: `text-gray-600 hover:text-gray-700 dark:text-gray-300 dark:hover:text-gray-200 cursor-pointer ${
$selectedTerminalId && selectedLabel ? ' p-1 ' : ' p-1 opacity-50'
}`
}`;
</script>
<div class={inMenu ? 'w-full' : 'flex items-center translate-x-0.5'}>
{#if disabled}
<Tooltip content={selectedLabel} placement="top">
<button
type="button"
disabled
aria-disabled="true"
aria-label={selectedLabel}
class={triggerClass}
>
{#if selectedDevice}
<Server className="size-3.5" strokeWidth="2" />
{:else}
<Cloud className="size-3.5" strokeWidth="2" />
{/if}
{#if inMenu || ($selectedTerminalId && selectedLabel)}
<span class="truncate text-[0.8125rem] {inMenu ? 'flex-1' : 'max-w-[9.375rem]'}"
>{selectedLabel}</span
>
{/if}
{#if inMenu}<ChevronRight />{/if}
</button>
</Tooltip>
{:else}
<Dropdown bind:show align="start">
<Tooltip content={selectedLabel} placement="top">
<button type="button" aria-label={selectedLabel} class={triggerClass}>
{#if selectedDevice}
<Server className="size-3.5" strokeWidth="2" />
{:else}
<Cloud className="size-3.5" strokeWidth="2" />
{/if}
{#if inMenu || ($selectedTerminalId && selectedLabel)}
<span class="truncate text-[0.8125rem] {inMenu ? 'flex-1' : 'max-w-[9.375rem]'}"
>{selectedLabel}</span
>
{/if}
{#if inMenu}<ChevronRight />{/if}
</button>
</Tooltip>
<div slot="content">
<DropdownMenu
className="{inMenu
? 'w-70'
: 'w-60'} max-w-[calc(100vw-2rem)] max-h-72 overflow-y-auto overflow-x-hidden scrollbar-thin"
>
<!-- Direct terminals (gated by permission) -->
{#if directTerminals.length > 0}
<div class="flex items-center justify-between pl-2 pr-1 py-1">
<span
class="text-[0.625rem] font-normal text-gray-400 dark:text-gray-500 uppercase tracking-wider"
>
{$i18n.t('Direct')}
</span>
<Tooltip content={$i18n.t('Add Terminal')} placement="top">
<button
type="button"
aria-label={$i18n.t('Add Terminal')}
class="p-0.5 rounded-md text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 transition"
on:click|stopPropagation={() => {
show = false;
showSettings.set('tools');
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-3.5"
>
<path
d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
/>
</svg>
</button>
</Tooltip>
</div>
{#each directTerminals as terminal}
<div
class="group/terminal relative rounded-xl hover:bg-gray-50/40 dark:hover:bg-gray-800/40"
>
<button
type="button"
role="menuitemradio"
aria-checked={$selectedTerminalId === terminal.url}
class="flex w-full min-w-0 gap-2 items-center h-[1.6875rem] pl-2 {$selectedTerminalId ===
terminal.url
? 'pr-14'
: 'pr-8'} text-[0.8125rem] font-normal cursor-pointer rounded-xl {$selectedTerminalId ===
terminal.url
? 'bg-gray-50/40 dark:bg-gray-800/40'
: 'hover:bg-gray-50/40 dark:hover:bg-gray-800/40'}"
on:click={() => selectDirect(terminal)}
>
<div class="flex flex-1 gap-2 items-center truncate">
<Cloud className="size-3.5 shrink-0" strokeWidth="2" />
<span class="truncate"
>{terminal.name || terminal.url.replace(/^https?:\/\//, '')}</span
>
</div>
{#if $selectedTerminalId === terminal.url}
<div class="absolute right-2 text-emerald-600 dark:text-emerald-400">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-3.5"
>
<path
fill-rule="evenodd"
d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
clip-rule="evenodd"
/>
</svg>
</div>
{/if}
</button>
<div
class="absolute inset-y-0 {$selectedTerminalId === terminal.url
? 'right-6'
: 'right-1'} flex items-center opacity-0 pointer-events-none transition-opacity group-hover/terminal:opacity-100 group-hover/terminal:pointer-events-auto group-focus-within/terminal:opacity-100 group-focus-within/terminal:pointer-events-auto"
>
<Tooltip content={$i18n.t('System info')} placement="top">
<button
type="button"
aria-label={$i18n.t('System info for {{name}}', {
name: terminal.name || terminal.url.replace(/^https?:\/\//, '')
})}
class="flex size-6 shrink-0 items-center justify-center rounded-lg text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 focus-visible:outline focus-visible:outline-2"
on:click|stopPropagation={() =>
openInfo(
terminal.url,
terminal.name || terminal.url.replace(/^https?:\/\//, '')
)}><Activity className="size-3.5" /></button
>
</Tooltip>
</div>
</div>
{/each}
{/if}
<!-- System terminals, then the user's own devices -->
{#each systemSections as section}
<div class="flex items-center justify-between pl-2 pr-1 py-1">
<span
class="text-[0.625rem] font-normal text-gray-400 dark:text-gray-500 uppercase tracking-wider"
>
{section.devices
? $i18n.t('Your devices')
: section.managed
? $i18n.t('Terminals')
: $i18n.t('System')}
</span>
{#if section.managed}
<Tooltip content={$i18n.t('Manage terminals')} placement="top">
<button
type="button"
aria-label={$i18n.t('Manage terminals')}
class="p-0.5 rounded-md text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 transition"
on:click|stopPropagation={() => {
show = false;
showTerminals = true;
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-3.5"
>
<path
d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
/>
</svg>
</button>
</Tooltip>
{:else if !section.devices && $user?.role === 'admin'}
<Tooltip content={$i18n.t('Add Terminal')} placement="top">
<button
type="button"
aria-label={$i18n.t('Add Terminal')}
class="p-0.5 rounded-md text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 transition"
on:click|stopPropagation={() => {
show = false;
showSettings.set('admin:integrations');
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-3.5"
>
<path
d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
/>
</svg>
</button>
</Tooltip>
{/if}
</div>
{#each section.terminals as terminal}
<div
class="group/terminal relative rounded-xl hover:bg-gray-50/40 dark:hover:bg-gray-800/40"
>
<button
type="button"
role="menuitemradio"
aria-checked={$selectedTerminalId === terminal.id}
class="flex w-full min-w-0 gap-2 items-center h-[1.6875rem] pl-2 {$selectedTerminalId ===
terminal.id
? 'pr-14'
: 'pr-8'} text-[0.8125rem] font-normal cursor-pointer rounded-xl {$selectedTerminalId ===
terminal.id
? 'bg-gray-50/40 dark:bg-gray-800/40'
: 'hover:bg-gray-50/40 dark:hover:bg-gray-800/40'}"
on:click={() => selectSystem(terminal)}
>
<div class="flex flex-1 gap-2 items-center truncate">
{#if section.devices}
<Server className="size-3.5 shrink-0" strokeWidth="2" />
{:else}
<Cloud className="size-3.5 shrink-0" strokeWidth="2" />
{/if}
<span class="truncate">{terminal.name || $i18n.t('Terminal')}</span>
</div>
{#if $selectedTerminalId === terminal.id}
<div class="absolute right-2 text-emerald-600 dark:text-emerald-400">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="size-3.5"
>
<path
fill-rule="evenodd"
d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
clip-rule="evenodd"
/>
</svg>
</div>
{/if}
</button>
{#if terminal.scope !== 'chat' || isSavedChatId($activeChatId)}
<div
class="absolute inset-y-0 {$selectedTerminalId === terminal.id
? 'right-6'
: 'right-1'} flex items-center opacity-0 pointer-events-none transition-opacity group-hover/terminal:opacity-100 group-hover/terminal:pointer-events-auto group-focus-within/terminal:opacity-100 group-focus-within/terminal:pointer-events-auto"
>
<Tooltip content={$i18n.t('System info')} placement="top">
<button
type="button"
aria-label={$i18n.t('System info for {{name}}', {
name: terminal.name || terminal.id
})}
class="flex size-6 shrink-0 items-center justify-center rounded-lg text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 focus-visible:outline focus-visible:outline-2"
on:click|stopPropagation={() =>
openInfo(terminal.id, terminal.name || terminal.id)}
><Activity className="size-3.5" /></button
>
</Tooltip>
</div>
{/if}
</div>
{:else}
<button
type="button"
class="flex w-full items-center h-[1.6875rem] px-2 text-[0.8125rem] text-gray-500 rounded-xl hover:bg-gray-50/40 dark:hover:bg-gray-800/40"
on:click={() => {
show = false;
showTerminals = true;
}}>{$i18n.t('New terminal')}</button
>
{/each}
{/each}
</DropdownMenu>
</div>
</Dropdown>
{/if}
</div>
{#if info}
{#key info}
<TerminalSystemInfoModal
target={info.target}
label={info.label}
onClose={() => (info = null)}
/>
{/key}
{/if}
{#if folderTarget && (selectedSystemTerminal?.scope !== 'chat' || isSavedChatId(chatId))}
{#key `${$user?.id}:${folderTarget.selector}:${chatId ?? ''}`}
<WorkingDirectoryMenu
{inMenu}
target={folderTarget}
{chatId}
userId={$user?.id ?? ''}
bind:terminalContext
onSelect={async (path) => {
if (isSavedChatId(chatId)) {
await updateChatById(localStorage.token, chatId!, { working_directory: path });
}
}}
{disabled}
/>
{/key}
{/if}
{#if showTerminals}
<UserTerminalsModal {modelId}
bind:show={showTerminals}
{chatId}
selected={$selectedTerminalId}
onSelect={selectTerminal}
/>
{/if}