mirror of
https://github.com/open-webui/open-webui.git
synced 2026-12-02 09:47:47 -05:00
refac
This commit is contained in:
1 parent
983e850754
commit
d42988f0c5
5 files changed
+160
-242
No files matched your search
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
export let modelId: string | null = null;
|
||||
import { getContext, onDestroy, tick } from 'svelte';
|
||||
import { getContext, tick } from 'svelte';
|
||||
import type { Writable } from 'svelte/store';
|
||||
import { templatesApi, resolveTerminalTarget, type TerminalTarget } from '$lib/apis/terminal';
|
||||
import UserTerminalsModal from '../UserTerminalsModal.svelte';
|
||||
@@ -20,7 +20,7 @@
|
||||
temporaryChatEnabled,
|
||||
user
|
||||
} from '$lib/stores';
|
||||
import { selectChatTerminal } from '$lib/utils/terminalSelection';
|
||||
import { selectTerminal } from '$lib/utils/terminal';
|
||||
import { isTemporaryChatId, isSavedChatId } from '$lib/utils/chatId';
|
||||
|
||||
import Dropdown from '$lib/components/common/Dropdown.svelte';
|
||||
@@ -50,8 +50,6 @@
|
||||
export let disabled = false;
|
||||
export let chatId: string | null = null;
|
||||
export let terminalContext: TerminalContext | null = null;
|
||||
let disposed = false;
|
||||
onDestroy(() => (disposed = true));
|
||||
|
||||
// Connections the user may create terminals on; a terminal on one of them is theirs to manage.
|
||||
let writable: { id: string; name: string }[] = [];
|
||||
@@ -128,20 +126,10 @@
|
||||
: []
|
||||
).filter((s) => s.url);
|
||||
|
||||
const choose = async (id: string) => {
|
||||
const ownerChatId = chatId;
|
||||
const ownerActiveChatId = $activeChatId;
|
||||
const next = $selectedTerminalId === id ? null : id;
|
||||
const toggleTerminal = (id: string) => {
|
||||
show = false;
|
||||
await selectChatTerminal(
|
||||
next,
|
||||
ownerChatId,
|
||||
selectedTerminalId,
|
||||
() => !disposed && chatId === ownerChatId && $activeChatId === ownerActiveChatId
|
||||
);
|
||||
selectTerminal(selectedTerminalId, $selectedTerminalId === id ? null : id, chatId);
|
||||
};
|
||||
const selectDirect = (terminal: (typeof directTerminals)[0]) => choose(terminal.url);
|
||||
const selectSystem = (terminal: (typeof systemTerminals)[0]) => choose(terminal.id);
|
||||
|
||||
$: selectedSystemTerminal = systemTerminals.find(
|
||||
(t) => !!$selectedTerminalId && t.id === $selectedTerminalId
|
||||
@@ -171,16 +159,6 @@
|
||||
show = false;
|
||||
showTerminals = true;
|
||||
};
|
||||
const selectTerminal = async (id: string) => {
|
||||
const ownerChatId = chatId;
|
||||
const ownerActiveChatId = $activeChatId;
|
||||
await selectChatTerminal(
|
||||
id,
|
||||
ownerChatId,
|
||||
selectedTerminalId,
|
||||
() => !disposed && chatId === ownerChatId && $activeChatId === ownerActiveChatId
|
||||
);
|
||||
};
|
||||
let info: { target: TerminalTarget; label: string } | null = null;
|
||||
let files: { target: TerminalTarget; label: string } | null = null;
|
||||
const openFiles = async (selector: string, label: string) => {
|
||||
@@ -318,7 +296,7 @@
|
||||
class="{rowClass} {$selectedTerminalId === terminal.url
|
||||
? 'pr-14 bg-gray-50/40 dark:bg-gray-800/40'
|
||||
: 'pr-8'}"
|
||||
on:click={() => selectDirect(terminal)}
|
||||
on:click={() => toggleTerminal(terminal.url)}
|
||||
>
|
||||
<div class="flex flex-1 gap-2 items-center truncate">
|
||||
<Cloud className="size-3.5 shrink-0" strokeWidth="2" />
|
||||
@@ -398,7 +376,7 @@
|
||||
class="{rowClass} {$selectedTerminalId === terminal.id
|
||||
? 'pr-14 bg-gray-50/40 dark:bg-gray-800/40'
|
||||
: 'pr-8'}"
|
||||
on:click={() => selectSystem(terminal)}
|
||||
on:click={() => toggleTerminal(terminal.id)}
|
||||
>
|
||||
<div class="flex flex-1 gap-2 items-center truncate">
|
||||
{#if section.devices}
|
||||
@@ -472,7 +450,7 @@
|
||||
bind:show={showTerminals}
|
||||
terminalId={managedTerminalId}
|
||||
connections={writable}
|
||||
onCreate={selectTerminal}
|
||||
onCreate={async (id) => selectTerminal(selectedTerminalId, id, chatId)}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
|
||||
@@ -99,8 +99,8 @@
|
||||
import { fileIconName } from './FileNav/fileIcon';
|
||||
import UntitledFile from './Tabs/UntitledFile.svelte';
|
||||
import QuickOpen from './Tabs/QuickOpen.svelte';
|
||||
import TerminalPicker, { type PickableTerminal } from './Tabs/TerminalPicker.svelte';
|
||||
import { selectChatTerminal } from '$lib/utils/terminalSelection';
|
||||
import TerminalPicker, { type TerminalOption } from './Tabs/TerminalPicker.svelte';
|
||||
import { selectTerminal } from '$lib/utils/terminal';
|
||||
import VoiceNote from './Tabs/VoiceNote.svelte';
|
||||
import Browser from './Tabs/Browser.svelte';
|
||||
import Desktop from './Tabs/Desktop.svelte';
|
||||
@@ -398,11 +398,8 @@
|
||||
voiceNote?.finishRecording();
|
||||
return;
|
||||
}
|
||||
if (!$user) return;
|
||||
withTerminal(startVoiceNote);
|
||||
};
|
||||
const startVoiceNote = () => {
|
||||
if (!canVoiceNote || !selectedTarget || !$user) return;
|
||||
if (!selectedTarget) return askForTerminal(openVoiceNote);
|
||||
if (!canVoiceNote || !$user) return;
|
||||
voiceNoteTarget = {
|
||||
target: selectedTarget,
|
||||
chatId,
|
||||
@@ -417,7 +414,6 @@
|
||||
let desktopEnabled = false;
|
||||
let browserEnabled = false;
|
||||
let featureVersion = 0;
|
||||
let featureCheck: Promise<void> = Promise.resolve();
|
||||
let releasePanel: (target: HTMLElement) => void = () => {};
|
||||
const preservePanel = (target: HTMLElement) => ({ destroy: () => releasePanel(target) });
|
||||
|
||||
@@ -605,6 +601,8 @@
|
||||
newTab = false,
|
||||
sourceModelId = modelId
|
||||
) => {
|
||||
if (!selector && !showFilesTab)
|
||||
return askForTerminal(() => openFiles($selectedTerminalId, directory, newTab, sourceModelId));
|
||||
if (selector ? !terminalAccessible(selector) : !showFilesTab) return;
|
||||
const target = resolveTerminalTarget(
|
||||
selector,
|
||||
@@ -685,7 +683,7 @@
|
||||
if (page != null) fileViews[id]?.revealPage?.(page);
|
||||
};
|
||||
const newFile = () => {
|
||||
if (!terminalFilesAvailable || !selectedTarget) return;
|
||||
if (!selectedTarget) return askForTerminal(newFile);
|
||||
let number = 1;
|
||||
while (tabs.tabs.some((tab) => tab.untitled && tab.label === `Untitled-${number}`)) number++;
|
||||
openPanel({
|
||||
@@ -903,7 +901,7 @@
|
||||
dragged = false;
|
||||
};
|
||||
const newTerminal = () => {
|
||||
if (!terminalEnabled || !selectedTarget) return;
|
||||
if (!selectedTarget) return askForTerminal(newTerminal);
|
||||
let terminalNumber = 1;
|
||||
while (
|
||||
tabs.tabs.some((tab) => tab.kind === 'terminal' && tab.terminalNumber === terminalNumber)
|
||||
@@ -922,7 +920,7 @@
|
||||
if (viewEntry && viewReady) startSession(viewEntry, tabs.tabs.find((tab) => tab.id === id)!);
|
||||
};
|
||||
const openDesktop = () => {
|
||||
if (!desktopEnabled || !selectedTarget) return;
|
||||
if (!selectedTarget) return askForTerminal(openDesktop);
|
||||
openPanel({
|
||||
id: JSON.stringify(['desktop', selectedTarget.baseUrl, selectedTarget.context.modelId]),
|
||||
kind: 'desktop',
|
||||
@@ -937,8 +935,9 @@
|
||||
baseUrl?: string,
|
||||
initialUrl?: string
|
||||
) => {
|
||||
if (!selector || !terminalAccessible(selector)) return;
|
||||
if (selector === $selectedTerminalId && !browserEnabled) return;
|
||||
if (!selector)
|
||||
return askForTerminal(() => openBrowser(port, $selectedTerminalId, baseUrl, initialUrl));
|
||||
if (!terminalAccessible(selector)) return;
|
||||
const url = initialUrl ?? (port ? `http://localhost:${port}/` : undefined);
|
||||
const existing =
|
||||
url &&
|
||||
@@ -1247,11 +1246,8 @@
|
||||
$: newTabViews = availableViews.filter(
|
||||
(view) => view.kind !== 'files' && view.kind !== 'voice' && view.available
|
||||
);
|
||||
$: canOpenNewTab = showFilesTab || terminalFilesAvailable || anyTerminal || newTabViews.length > 0;
|
||||
const menuFiles = () => {
|
||||
if (showFilesTab) openFiles($selectedTerminalId, undefined, true);
|
||||
else withTerminal(() => openFiles($selectedTerminalId, undefined, true));
|
||||
};
|
||||
$: canOpenNewTab =
|
||||
showFilesTab || terminalFilesAvailable || canSelectTerminal || newTabViews.length > 0;
|
||||
|
||||
const quickSplit = () => {
|
||||
if (tabs.tabs.length < 2) {
|
||||
@@ -1283,23 +1279,19 @@
|
||||
let action: (() => void) | undefined;
|
||||
switch (shortcut) {
|
||||
case Shortcut.VOICE_NOTE:
|
||||
if (voiceNoteTarget || canVoiceNote || (anyTerminal && !selectedTarget))
|
||||
action = openVoiceNote;
|
||||
if (voiceNoteTarget || canVoiceNote || canSelectTerminal) action = openVoiceNote;
|
||||
break;
|
||||
case Shortcut.QUICK_OPEN:
|
||||
if (selectedTarget || anyTerminal)
|
||||
action = () =>
|
||||
withTerminal(() => {
|
||||
showQuickOpen = true;
|
||||
});
|
||||
if (terminalFilesAvailable && selectedTarget)
|
||||
action = () => {
|
||||
showQuickOpen = true;
|
||||
};
|
||||
break;
|
||||
case Shortcut.OPEN_FILES:
|
||||
if (showFilesTab) action = () => openFiles();
|
||||
else if (anyTerminal) action = () => withTerminal(() => openFiles());
|
||||
if (showFilesTab || canSelectTerminal) action = () => openFiles();
|
||||
break;
|
||||
case Shortcut.OPEN_DESKTOP:
|
||||
if (desktopEnabled || (!selectedTarget && anyTerminal))
|
||||
action = () => withTerminal(openDesktop, 'desktop');
|
||||
if ((desktopEnabled && selectedTarget) || canSelectTerminal) action = openDesktop;
|
||||
break;
|
||||
case Shortcut.NEW_CHAT_TAB:
|
||||
if (canSideChat) action = () => openFeature('side');
|
||||
@@ -1308,15 +1300,13 @@
|
||||
if (showCommentsTab) action = () => openFeature('comments');
|
||||
break;
|
||||
case Shortcut.NEW_FILE:
|
||||
if (selectedTarget || anyTerminal) action = () => withTerminal(newFile);
|
||||
if ((terminalFilesAvailable && selectedTarget) || canSelectTerminal) action = newFile;
|
||||
break;
|
||||
case Shortcut.NEW_TERMINAL:
|
||||
if (terminalEnabled || (!selectedTarget && anyTerminal))
|
||||
action = () => withTerminal(newTerminal, 'terminal');
|
||||
if ((terminalEnabled && selectedTarget) || canSelectTerminal) action = newTerminal;
|
||||
break;
|
||||
case Shortcut.NEW_BROWSER:
|
||||
if (browserEnabled || (!selectedTarget && anyTerminal))
|
||||
action = () => withTerminal(() => openBrowser(), 'browser');
|
||||
if ((browserEnabled && selectedTarget) || canSelectTerminal) action = () => openBrowser();
|
||||
break;
|
||||
case Shortcut.CLOSE_TAB:
|
||||
if (panelVisible && tabs.activeId)
|
||||
@@ -1349,7 +1339,8 @@
|
||||
};
|
||||
|
||||
$: directTerminals =
|
||||
($settings as { terminalServers?: { url: string; key?: string }[] })?.terminalServers ?? [];
|
||||
($settings as { terminalServers?: { url: string; key?: string; name?: string }[] })
|
||||
?.terminalServers ?? [];
|
||||
const terminalAccessible = (selector: string) => {
|
||||
if (!$config?.features?.enable_tool_servers) return false;
|
||||
const system = ($terminalServers ?? []).find((terminal) => (!!selector && terminal.id === selector));
|
||||
@@ -1373,77 +1364,43 @@
|
||||
{ chatId: chatId, modelId: modelId ?? null }
|
||||
)
|
||||
: null;
|
||||
// Every terminal this chat could use, so terminal tabs stay offered before one is selected.
|
||||
const listTerminals = (..._deps: unknown[]): PickableTerminal[] => {
|
||||
if (!$config?.features?.enable_tool_servers) return [];
|
||||
const direct = directTerminals
|
||||
.filter((terminal) => terminal.url && terminalAccessible(terminal.url))
|
||||
.map((terminal) => ({
|
||||
id: terminal.url,
|
||||
label: (terminal as { name?: string }).name || terminal.url.replace(/^https?:\/\//, ''),
|
||||
device: false
|
||||
}));
|
||||
const system = ($terminalServers ?? [])
|
||||
.filter((terminal) => terminal.id && terminalAccessible(terminal.id))
|
||||
.map((terminal) => ({
|
||||
id: terminal.id,
|
||||
label: terminal.name || $i18n.t('Terminal'),
|
||||
device: terminal.id.startsWith('tether:')
|
||||
}));
|
||||
return [...direct, ...system];
|
||||
// Terminals this chat can use, offered when terminal tabs are opened before one is selected.
|
||||
let availableTerminals: TerminalOption[] = [];
|
||||
$: availableTerminals = $config?.features?.enable_tool_servers
|
||||
? [
|
||||
...($config?.features?.enable_direct_integrations &&
|
||||
($user?.role === 'admin' || ($user?.permissions?.features?.direct_tool_servers ?? true))
|
||||
? directTerminals
|
||||
.filter((terminal) => terminal.url)
|
||||
.map((terminal) => ({
|
||||
id: terminal.url,
|
||||
label: terminal.name || terminal.url.replace(/^https?:\/\//, ''),
|
||||
device: false
|
||||
}))
|
||||
: []),
|
||||
...($terminalServers ?? [])
|
||||
.filter(
|
||||
(terminal) =>
|
||||
terminal.id && !(chatContextNeedsSavedChat(terminal) && !isSavedChatId(chatId))
|
||||
)
|
||||
.map((terminal) => ({
|
||||
id: terminal.id,
|
||||
label: terminal.name || $i18n.t('Terminal'),
|
||||
device: terminal.id.startsWith('tether:')
|
||||
}))
|
||||
]
|
||||
: [];
|
||||
$: canSelectTerminal = !selectedTarget && availableTerminals.length > 0;
|
||||
let pendingTerminalAction: (() => void) | null = null;
|
||||
const askForTerminal = (action: () => void) => {
|
||||
if (availableTerminals.length) pendingTerminalAction = action;
|
||||
};
|
||||
$: pickableTerminals = listTerminals(
|
||||
$config,
|
||||
$user,
|
||||
$terminalServers,
|
||||
directTerminals,
|
||||
chatId,
|
||||
$i18n
|
||||
);
|
||||
$: anyTerminal = pickableTerminals.length > 0;
|
||||
|
||||
type TerminalFeature = 'terminal' | 'browser' | 'desktop';
|
||||
const featureEnabled = (feature?: TerminalFeature) =>
|
||||
feature === 'terminal'
|
||||
? terminalEnabled
|
||||
: feature === 'browser'
|
||||
? browserEnabled
|
||||
: feature === 'desktop'
|
||||
? desktopEnabled
|
||||
: true;
|
||||
const featureLabels: Record<TerminalFeature, string> = {
|
||||
terminal: 'Terminal',
|
||||
browser: 'Browser',
|
||||
desktop: 'Desktop'
|
||||
};
|
||||
let terminalPick: { action: () => void; feature?: TerminalFeature } | null = null;
|
||||
// Run a terminal action, first asking which terminal to use when none is selected.
|
||||
const withTerminal = (action: () => void, feature?: TerminalFeature) => {
|
||||
if (selectedTarget) {
|
||||
if (featureEnabled(feature)) action();
|
||||
return;
|
||||
}
|
||||
if (anyTerminal) terminalPick = { action, feature };
|
||||
};
|
||||
const pickTerminal = async (id: string) => {
|
||||
const pick = terminalPick;
|
||||
terminalPick = null;
|
||||
if (!pick) return;
|
||||
const owner = chatId;
|
||||
const current = () => mounted && chatId === owner;
|
||||
if (!(await selectChatTerminal(id, owner, selectedTerminalId, current))) return;
|
||||
const runWithTerminal = async (id: string) => {
|
||||
const action = pendingTerminalAction;
|
||||
pendingTerminalAction = null;
|
||||
selectTerminal(selectedTerminalId, id, chatId);
|
||||
await tick();
|
||||
await featureCheck;
|
||||
if (!current() || $selectedTerminalId !== id || !selectedTarget) return;
|
||||
if (!featureEnabled(pick.feature)) {
|
||||
toast.error(
|
||||
$i18n.t('{{feature}} is not available on this terminal', {
|
||||
feature: $i18n.t(featureLabels[pick.feature!])
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
pick.action();
|
||||
if (selectedTarget) action?.();
|
||||
};
|
||||
const checkTerminal = async () => {
|
||||
const version = ++featureVersion;
|
||||
@@ -1467,7 +1424,7 @@
|
||||
$: {
|
||||
selectedTarget;
|
||||
chatId;
|
||||
featureCheck = checkTerminal();
|
||||
void checkTerminal();
|
||||
}
|
||||
|
||||
$: if (mounted && !chatLoading && $user?.id && chatId !== tabsChatId) {
|
||||
@@ -1757,13 +1714,13 @@
|
||||
{/key}
|
||||
{/if}
|
||||
|
||||
{#if terminalPick}
|
||||
{#if pendingTerminalAction}
|
||||
<TerminalPicker
|
||||
terminals={pickableTerminals}
|
||||
terminals={availableTerminals}
|
||||
onclose={() => {
|
||||
terminalPick = null;
|
||||
pendingTerminalAction = null;
|
||||
}}
|
||||
onselect={pickTerminal}
|
||||
onselect={runWithTerminal}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
@@ -2303,37 +2260,34 @@
|
||||
>
|
||||
<div slot="content">
|
||||
<DropdownMenu className="min-w-56">
|
||||
{#if showFilesTab || anyTerminal}
|
||||
<button on:click={menuFiles}
|
||||
{#if showFilesTab || canSelectTerminal}
|
||||
<button on:click={() => openFiles($selectedTerminalId, undefined, true)}
|
||||
><Icon name="folder" size={14} /><span class="flex-1 text-start"
|
||||
>{$i18n.t('Files')}</span
|
||||
><HotkeyHint name="openFiles" className="ms-auto" /></button
|
||||
>
|
||||
{/if}
|
||||
{#if selectedTarget || anyTerminal}<button on:click={() => withTerminal(newFile)}
|
||||
{#if terminalFilesAvailable || canSelectTerminal}<button on:click={newFile}
|
||||
><Icon name="page" size={14} /><span class="flex-1 text-start"
|
||||
>{$i18n.t('New File')}</span
|
||||
><HotkeyHint name="newFile" className="ms-auto" /></button
|
||||
>{/if}
|
||||
{#if terminalEnabled || (!selectedTarget && anyTerminal)}<button
|
||||
on:click={() => withTerminal(newTerminal, 'terminal')}
|
||||
{#if terminalEnabled || canSelectTerminal}<button on:click={newTerminal}
|
||||
><Icon name="terminal" size={14} /><span class="flex-1 text-start"
|
||||
>{$i18n.t('New Terminal')}</span
|
||||
><HotkeyHint name="newTerminal" className="ms-auto" /></button
|
||||
>{/if}
|
||||
{#if browserEnabled || (!selectedTarget && anyTerminal)}<button
|
||||
on:click={() => withTerminal(() => openBrowser(), 'browser')}
|
||||
{#if browserEnabled || canSelectTerminal}<button on:click={() => openBrowser()}
|
||||
><Icon name="browser" size={14} /><span class="flex-1 text-start"
|
||||
>{$i18n.t('Browser')}</span
|
||||
><HotkeyHint name="newBrowser" className="ms-auto" /></button
|
||||
>{/if}
|
||||
{#if desktopEnabled || (!selectedTarget && anyTerminal)}<button
|
||||
on:click={() => withTerminal(openDesktop, 'desktop')}
|
||||
{#if desktopEnabled || canSelectTerminal}<button on:click={openDesktop}
|
||||
><Icon name="monitor" size={14} /><span class="flex-1 text-start"
|
||||
>{$i18n.t('Desktop')}</span
|
||||
><HotkeyHint name="openDesktop" className="ms-auto" /></button
|
||||
>{/if}
|
||||
{#if canVoiceNote || (!selectedTarget && anyTerminal && $user)}
|
||||
{#if canVoiceNote || canSelectTerminal}
|
||||
<button on:click={openVoiceNote}>
|
||||
<Icon name="microphone" size={14} />
|
||||
<span class="flex-1 text-start">{$i18n.t('Voice Note')}</span>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts" context="module">
|
||||
export type PickableTerminal = { id: string; label: string; device: boolean };
|
||||
export type TerminalOption = { id: string; label: string; device: boolean };
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -10,19 +10,20 @@
|
||||
import Server from '$lib/components/icons/Server.svelte';
|
||||
|
||||
const i18n = getContext<any>('i18n');
|
||||
export let terminals: PickableTerminal[];
|
||||
export let terminals: TerminalOption[];
|
||||
export let onclose: () => void;
|
||||
export let onselect: (id: string) => void;
|
||||
|
||||
let query = '';
|
||||
let selectedIndex = 0;
|
||||
|
||||
$: matches = terminals.filter((terminal) =>
|
||||
$: filteredTerminals = terminals.filter((terminal) =>
|
||||
terminal.label.toLowerCase().includes(query.trim().toLowerCase())
|
||||
);
|
||||
$: if (selectedIndex >= matches.length) selectedIndex = Math.max(0, matches.length - 1);
|
||||
$: if (selectedIndex >= filteredTerminals.length)
|
||||
selectedIndex = Math.max(0, filteredTerminals.length - 1);
|
||||
|
||||
const select = (terminal: PickableTerminal) => {
|
||||
const select = (terminal: TerminalOption) => {
|
||||
onselect(terminal.id);
|
||||
onclose();
|
||||
};
|
||||
@@ -34,15 +35,15 @@
|
||||
event.stopPropagation();
|
||||
selectedIndex = Math.max(
|
||||
0,
|
||||
Math.min(matches.length - 1, selectedIndex + (event.key === 'ArrowDown' ? 1 : -1))
|
||||
Math.min(filteredTerminals.length - 1, selectedIndex + (event.key === 'ArrowDown' ? 1 : -1))
|
||||
);
|
||||
document
|
||||
.getElementById(`terminal-picker-option-${selectedIndex}`)
|
||||
?.scrollIntoView({ block: 'nearest' });
|
||||
} else if (event.key === 'Enter' && matches[selectedIndex]) {
|
||||
} else if (event.key === 'Enter' && filteredTerminals[selectedIndex]) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
select(matches[selectedIndex]);
|
||||
select(filteredTerminals[selectedIndex]);
|
||||
}
|
||||
};
|
||||
</script>
|
||||
@@ -59,9 +60,11 @@
|
||||
role="combobox"
|
||||
aria-label={$i18n.t('Select a terminal')}
|
||||
aria-autocomplete="list"
|
||||
aria-expanded={matches.length > 0}
|
||||
aria-expanded={filteredTerminals.length > 0}
|
||||
aria-controls="terminal-picker-options"
|
||||
aria-activedescendant={matches.length ? `terminal-picker-option-${selectedIndex}` : undefined}
|
||||
aria-activedescendant={filteredTerminals.length
|
||||
? `terminal-picker-option-${selectedIndex}`
|
||||
: undefined}
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
placeholder={$i18n.t('Select a terminal')}
|
||||
@@ -78,7 +81,7 @@
|
||||
aria-label={$i18n.t('Terminals')}
|
||||
class="max-h-[min(20rem,60dvh)] overflow-y-auto px-1 pb-1"
|
||||
>
|
||||
{#each matches as terminal, index (terminal.id)}
|
||||
{#each filteredTerminals as terminal, index (terminal.id)}
|
||||
<button
|
||||
type="button"
|
||||
role="option"
|
||||
@@ -103,7 +106,7 @@
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
{#if !matches.length}
|
||||
{#if !filteredTerminals.length}
|
||||
<div role="status" class="px-3 pb-3 pt-1 text-[0.6875rem] text-gray-400 dark:text-gray-500">
|
||||
{$i18n.t('No terminals found')}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { get, type Writable } from 'svelte/store';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { settings, terminalServers } from '$lib/stores';
|
||||
import { getToolServersData } from '$lib/apis';
|
||||
import { updateChatById } from '$lib/apis/chats';
|
||||
import { isSavedChatId } from '$lib/utils/chatId';
|
||||
|
||||
type Settings = Parameters<typeof settings.set>[0];
|
||||
type DirectTerminal = {
|
||||
url: string;
|
||||
key?: string;
|
||||
auth_type?: string;
|
||||
path?: string;
|
||||
enabled?: boolean;
|
||||
config?: unknown;
|
||||
};
|
||||
|
||||
// Reload direct terminals into the store, keeping system terminals (those with an `id`).
|
||||
const refreshDirectTerminals = async (servers: DirectTerminal[]) => {
|
||||
const systemTerminals = (get(terminalServers) ?? []).filter((terminal) => terminal.id);
|
||||
const enabled = servers.filter((server) => server.enabled);
|
||||
if (!enabled.length) {
|
||||
terminalServers.set(systemTerminals);
|
||||
return;
|
||||
}
|
||||
const data = await getToolServersData(
|
||||
enabled.map((server) => ({
|
||||
url: server.url,
|
||||
auth_type: server.auth_type ?? 'bearer',
|
||||
key: server.key ?? '',
|
||||
path: server.path ?? '/openapi.json',
|
||||
config: { enable: true }
|
||||
}))
|
||||
);
|
||||
terminalServers.set([
|
||||
...data.flatMap((item, i) =>
|
||||
item && !item.error
|
||||
? [{ ...item, key: enabled[i].key ?? '', config: enabled[i].config ?? item.config ?? {} }]
|
||||
: []
|
||||
),
|
||||
...systemTerminals
|
||||
]);
|
||||
};
|
||||
|
||||
// Select a chat's terminal at once, saving it to the chat in the background.
|
||||
export const selectTerminal = (
|
||||
selectedTerminalId: Pick<Writable<string | null>, 'set'>,
|
||||
id: string | null,
|
||||
chatId: string | null
|
||||
) => {
|
||||
selectedTerminalId.set(id);
|
||||
if (isSavedChatId(chatId))
|
||||
updateChatById(localStorage.token, chatId!, { terminal_id: id, working_directory: '' }).catch(
|
||||
(error) => toast.error(String(error))
|
||||
);
|
||||
|
||||
// Direct terminals live in settings, outside the Settings type; only the selected one stays enabled.
|
||||
const current = get(settings) as Settings & { terminalServers?: DirectTerminal[] };
|
||||
const servers = current?.terminalServers ?? [];
|
||||
const isDirect = !!id && servers.some((server) => server.url === id);
|
||||
if (!isDirect && !servers.some((server) => server.enabled)) return;
|
||||
const updated = servers.map((server) => ({ ...server, enabled: isDirect && server.url === id }));
|
||||
settings.set({ ...current, terminalServers: updated } as Settings);
|
||||
void refreshDirectTerminals(updated);
|
||||
};
|
||||
@@ -1,82 +0,0 @@
|
||||
import { get, type Writable } from 'svelte/store';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import { settings, terminalServers } from '$lib/stores';
|
||||
import { getToolServersData } from '$lib/apis';
|
||||
import { updateChatById } from '$lib/apis/chats';
|
||||
import { isSavedChatId } from '$lib/utils/chatId';
|
||||
|
||||
type Settings = Parameters<typeof settings.set>[0];
|
||||
type DirectTerminal = {
|
||||
url: string;
|
||||
key?: string;
|
||||
auth_type?: string;
|
||||
path?: string;
|
||||
enabled?: boolean;
|
||||
config?: unknown;
|
||||
};
|
||||
|
||||
// Rebuild the direct entries of the terminal store; system terminals (with an `id`) are kept.
|
||||
const refreshDirectTerminals = async (servers: DirectTerminal[]) => {
|
||||
const system = (get(terminalServers) ?? []).filter((terminal) => terminal.id);
|
||||
const active = servers.filter((server) => server.enabled);
|
||||
if (!active.length) {
|
||||
terminalServers.set(system);
|
||||
return;
|
||||
}
|
||||
const data = await getToolServersData(
|
||||
active.map((terminal) => ({
|
||||
url: terminal.url,
|
||||
auth_type: terminal.auth_type ?? 'bearer',
|
||||
key: terminal.key ?? '',
|
||||
path: terminal.path ?? '/openapi.json',
|
||||
config: { enable: true }
|
||||
}))
|
||||
);
|
||||
terminalServers.set([
|
||||
...data
|
||||
.map((item, index) => ({ item, server: active[index] }))
|
||||
.filter(({ item }) => item && !item.error)
|
||||
.map(({ item, server }) => ({
|
||||
...item,
|
||||
key: server?.key ?? '',
|
||||
config: server?.config ?? item?.config ?? {}
|
||||
})),
|
||||
...system
|
||||
]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Make `id` the chat's terminal: persist it on a saved chat, then enable only the matching
|
||||
* direct terminal. `isCurrent` lets the caller drop a selection that outlived its chat.
|
||||
*/
|
||||
export const selectChatTerminal = async (
|
||||
id: string | null,
|
||||
chatId: string | null,
|
||||
selectedTerminalId: Pick<Writable<string | null>, 'set'>,
|
||||
isCurrent: () => boolean = () => true
|
||||
) => {
|
||||
if (isSavedChatId(chatId)) {
|
||||
try {
|
||||
await updateChatById(localStorage.token, chatId!, {
|
||||
terminal_id: id,
|
||||
working_directory: ''
|
||||
});
|
||||
} catch (error) {
|
||||
toast.error(String(error));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!isCurrent()) return false;
|
||||
selectedTerminalId.set(id);
|
||||
|
||||
// Direct terminals live in user settings but are not part of the Settings type.
|
||||
const current = get(settings) as Settings & { terminalServers?: DirectTerminal[] };
|
||||
const servers = current?.terminalServers ?? [];
|
||||
const direct = !!id && servers.some((server) => server.url === id);
|
||||
// A system terminal only needs the direct ones switched off when any were on.
|
||||
if (!direct && !servers.some((server) => server.enabled)) return true;
|
||||
const updated = servers.map((server) => ({ ...server, enabled: direct && server.url === id }));
|
||||
settings.set({ ...current, terminalServers: updated } as Settings);
|
||||
await refreshDirectTerminals(updated);
|
||||
return true;
|
||||
};
|
||||
Reference in new issue
Block a user