diff --git a/src/lib/components/chat/MessageInput/TerminalMenu.svelte b/src/lib/components/chat/MessageInput/TerminalMenu.svelte index 1d8693fc62..ddfaf663de 100644 --- a/src/lib/components/chat/MessageInput/TerminalMenu.svelte +++ b/src/lib/components/chat/MessageInput/TerminalMenu.svelte @@ -1,6 +1,6 @@ @@ -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)} {/each} - {#if !matches.length} + {#if !filteredTerminals.length} {$i18n.t('No terminals found')} diff --git a/src/lib/utils/terminal.ts b/src/lib/utils/terminal.ts new file mode 100644 index 0000000000..52361989b1 --- /dev/null +++ b/src/lib/utils/terminal.ts @@ -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[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, '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); +}; diff --git a/src/lib/utils/terminalSelection.ts b/src/lib/utils/terminalSelection.ts deleted file mode 100644 index 9289cdca8b..0000000000 --- a/src/lib/utils/terminalSelection.ts +++ /dev/null @@ -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[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, '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; -};