From f4cf2ca0c79a2effc3db7486b7d63aec8dadf6e3 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 10 Nov 2026 22:56:33 +0900 Subject: [PATCH] refac --- backend/open_webui/routers/templates.py | 29 +- .../chat/MessageInput/TerminalItemMenu.svelte | 69 ++ .../chat/MessageInput/TerminalMenu.svelte | 279 +++-- .../components/chat/UserTerminalsModal.svelte | 1000 ++++++++++------- 4 files changed, 791 insertions(+), 586 deletions(-) create mode 100644 src/lib/components/chat/MessageInput/TerminalItemMenu.svelte diff --git a/backend/open_webui/routers/templates.py b/backend/open_webui/routers/templates.py index a0eb108e6f..d8338c40bb 100644 --- a/backend/open_webui/routers/templates.py +++ b/backend/open_webui/routers/templates.py @@ -59,6 +59,14 @@ async def management_connections(user=Depends(get_verified_user)): return result +@router.get('/connections/{connection_id}/status') +async def connection_status(connection_id: str, user=Depends(get_verified_user)): + connection = await get_connection(user, connection_id) + if not await has_connection_write_access(user, connection): + raise HTTPException(403, 'Creation is not allowed') + return await server_request(connection, 'api/v1/terminals/status') + + @router.post('', status_code=201) async def create_template(body: CreateForm, user=Depends(get_verified_user)): connection = await get_connection(user, body.connection_id) @@ -136,8 +144,27 @@ async def current_instance(terminal_id: str, request: Request, chat_id: str | No @router.get('/{terminal_id}/instances') async def list_instances(terminal_id: str, user=Depends(get_verified_user)): + from open_webui.models.chats import Chats + from open_webui.models.models import Models + from open_webui.models.users import Users + connection, template = await template_connection(user, terminal_id, 'write') - return await server_request(connection, path(template['id'], '/instances')) + rows = await server_request(connection, path(template['id'], '/instances')) + for row in rows: + kind, _, value = (row.get('context_id') or '').partition(':') + row['context'] = {f'{kind}_id': value} if value else {} + if kind == 'chat': + chat = await Chats.get_chat_by_id(value) + owner = await Users.get_user_by_id(chat.user_id) if chat else None + row['label'] = chat.title if chat else None + row['user_name'] = owner.name if owner else None + elif kind == 'user': + owner = await Users.get_user_by_id(value) + row['label'] = owner.name if owner else None + elif kind == 'model': + model = await Models.get_model_by_id(value) + row['label'] = model.name if model else value + return rows async def instance_action(terminal_id, action, body, user): diff --git a/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte b/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte new file mode 100644 index 0000000000..7e3498d9b4 --- /dev/null +++ b/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte @@ -0,0 +1,69 @@ + + +{#if onRestart || onInfo || onSettings} + + + + + +
+ + {#if onRestart} + + {/if} + {#if onInfo} + + {/if} + {#if onSettings} + {#if onRestart || onInfo} +
+ {/if} + + {/if} +
+
+
+{/if} diff --git a/src/lib/components/chat/MessageInput/TerminalMenu.svelte b/src/lib/components/chat/MessageInput/TerminalMenu.svelte index 0b95198d43..9da47e41b2 100644 --- a/src/lib/components/chat/MessageInput/TerminalMenu.svelte +++ b/src/lib/components/chat/MessageInput/TerminalMenu.svelte @@ -6,7 +6,6 @@ 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'; @@ -31,17 +30,51 @@ import Cog6 from '$lib/components/icons/Cog6.svelte'; import Server from '$lib/components/icons/Server.svelte'; import ChevronRight from '$lib/components/icons/ChevronRight.svelte'; + import TerminalItemMenu from './TerminalItemMenu.svelte'; + import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; + import Check from '$lib/components/icons/Check.svelte'; const i18n = getContext('i18n'); + const rowClass = + 'flex w-full min-w-0 gap-2 items-center h-[1.6875rem] pl-2 text-[0.8125rem] font-normal cursor-pointer rounded-xl hover:bg-gray-50/40 dark:hover:bg-gray-800/40'; + const headerButtonClass = + 'p-0.5 rounded-md text-gray-400 hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300 transition'; + const moreClass = + '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'; + export let show = false; export let inMenu = false; export let disabled = false; export let chatId: string | null = null; export let terminalContext: TerminalContext | null = null; - let connections = Promise.resolve([]); - $: if (show) connections = templatesApi('/connections').catch(() => []); + // Connections the user may create terminals on; a terminal on one of them is theirs to manage. + let writable: { id: string; name: string }[] = []; + $: if (show) + templatesApi('/connections') + .then((rows) => (writable = rows)) + .catch(() => (writable = [])); + $: manageable = new Set(writable.map((c) => c.id)); + const connectionOf = (id: string) => + id.startsWith('template:') ? id.slice('template:'.length, id.lastIndexOf(':')) : ''; + const canManage = (terminal: any, ids: Set) => + !!terminal.template_id && ids.has(connectionOf(terminal.id)); + + let restartTarget: { id: string; name: string } | null = null; + let showRestart = false; + const restart = async () => { + if (!restartTarget) return; + try { + await templatesApi(`/${encodeURIComponent(restartTarget.id)}/restart`, 'POST', { + chat_id: isSavedChatId($activeChatId) ? $activeChatId : null, + model_id: modelId + }); + toast.success($i18n.t('Restarted')); + } catch (e) { + toast.error(e instanceof Error ? e.message : String(e)); + } + }; $: systemTerminals = ($terminalServers ?? []).filter( (t) => @@ -155,6 +188,11 @@ let showTerminals = false; let managedTerminalId: string | null = null; + const openTerminalDialog = (id: string | null) => { + managedTerminalId = id; + show = false; + showTerminals = true; + }; async function saveSelection(id: string | null) { if (isSavedChatId(chatId)) { try { @@ -194,7 +232,8 @@ $: if ( !$config?.features?.enable_tool_servers || (info && - (info.target.context.chatId !== $activeChatId || + // A new chat has no ID: the store holds '' and the target holds null. + ((info.target.context.chatId ?? '') !== ($activeChatId || '') || (info.target.system ? !systemTerminals.some((t) => t.id === info?.target.selector) : !directTerminals.some((t) => t.url === info?.target.selector)))) @@ -273,91 +312,51 @@ {#each directTerminals as terminal} -
+ {@const label = terminal.name || terminal.url.replace(/^https?:\/\//, '')} +
- - - + openInfo(terminal.url, label)} />
{/each} {/if} - + {#each systemSections as section}
{section.devices ? $i18n.t('Your devices') : $i18n.t('Terminals')} - {#if !section.devices && $user?.role === 'admin'} - - - + {#if !section.devices} +
+ {#if writable.length} + + + + {/if} + {#if $user?.role === 'admin'} + + + + {/if} +
{/if}
{#each section.terminals as terminal} -
+ {@const label = terminal.name || $i18n.t('Terminal')} +
- {#if terminal.template_id || terminal.scope !== 'chat' || isSavedChatId($activeChatId)} -
- {#if terminal.template_id} - - - - {/if} - {#if terminal.scope !== 'chat' || isSavedChatId($activeChatId)} - - - - {/if} -
- {/if} +
+ { + restartTarget = { id: terminal.id, name: label }; + show = false; + showRestart = true; + } + : null} + onInfo={terminal.scope !== 'chat' || isSavedChatId($activeChatId) + ? () => openInfo(terminal.id, label) + : null} + onSettings={canManage(terminal, manageable) + ? () => openTerminalDialog(terminal.id) + : null} + /> +
{/each} - {#if !section.devices} - {#await connections then allowed} - {#if allowed.length} - - {/if} - {/await} - {/if} {/each}
@@ -520,10 +484,17 @@ {#if showTerminals} {/if} + + diff --git a/src/lib/components/chat/UserTerminalsModal.svelte b/src/lib/components/chat/UserTerminalsModal.svelte index e6cc4dd7c3..b420073a2d 100644 --- a/src/lib/components/chat/UserTerminalsModal.svelte +++ b/src/lib/components/chat/UserTerminalsModal.svelte @@ -1,58 +1,140 @@
-
-

- {editing - ? $i18n.t(editing.id ? 'Configure terminal' : 'New terminal') - : sharing - ? `${$i18n.t('Share')} ${sharing.name}` - : $i18n.t('Terminals')} +
+

+ {terminalId ? name || $i18n.t('Terminal settings') : $i18n.t('New terminal')}

- +
+ {#if terminalId && !loading} + (showAccess = true)} + /> + {/if} + +
-