diff --git a/backend/open_webui/routers/templates.py b/backend/open_webui/routers/templates.py index ca83fb8831..007106fdfe 100644 --- a/backend/open_webui/routers/templates.py +++ b/backend/open_webui/routers/templates.py @@ -181,6 +181,16 @@ async def restart(terminal_id: str, body: InstanceForm, user=Depends(get_verifie return await instance_action(terminal_id, 'restart', body, user) +@router.post('/{terminal_id}/stop') +async def stop(terminal_id: str, body: InstanceForm, user=Depends(get_verified_user)): + return await instance_action(terminal_id, 'stop', body, user) + + +@router.post('/{terminal_id}/start') +async def start(terminal_id: str, body: InstanceForm, user=Depends(get_verified_user)): + return await instance_action(terminal_id, 'start', body, user) + + @router.post('/{terminal_id}/reset') async def reset(terminal_id: str, body: InstanceForm, user=Depends(get_verified_user)): return await instance_action(terminal_id, 'reset', body, user) diff --git a/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte b/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte index 7e3498d9b4..e3f77effb5 100644 --- a/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte +++ b/src/lib/components/chat/MessageInput/TerminalItemMenu.svelte @@ -4,19 +4,39 @@ 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 Spinner from '$lib/components/common/Spinner.svelte'; import EllipsisHorizontal from '$lib/components/icons/EllipsisHorizontal.svelte'; import ArrowPath from '$lib/components/icons/ArrowPath.svelte'; import Activity from '$lib/components/icons/Activity.svelte'; import Cog6 from '$lib/components/icons/Cog6.svelte'; + import Folder from '$lib/components/icons/Folder.svelte'; + import Stop from '$lib/components/icons/Stop.svelte'; + import Play from '$lib/components/icons/Play.svelte'; const i18n = getContext('i18n'); export let label = ''; + /** Reads this chat's machine status when the menu opens; enables Restart, Stop, and Start. */ + export let loadStatus: (() => Promise) | null = null; export let onRestart: (() => void) | null = null; + export let onStop: (() => void) | null = null; + export let onStart: (() => void) | null = null; export let onInfo: (() => void) | null = null; + export let onFiles: (() => void) | null = null; export let onSettings: (() => void) | null = null; let show = false; + let status: string | null = null; + let checking = false; + + $: if (show && loadStatus) check(); + $: running = !loadStatus || status === 'running'; + + async function check() { + checking = true; + status = await loadStatus!().catch(() => null); + checking = false; + } const item = 'select-none flex h-[1.6875rem] w-full items-center gap-2 rounded-xl px-2 text-[0.8125rem] hover:bg-gray-50/40 dark:hover:bg-gray-800/40 transition'; @@ -28,7 +48,7 @@ }; -{#if onRestart || onInfo || onSettings} +{#if loadStatus || onInfo || onFiles || onSettings} + {#if loadStatus && checking} +
+ {:else if loadStatus} + {#if status === 'running'} + {#if onRestart} + + {/if} + {#if onStop} + + {/if} + {:else if onStart} + + {/if} {/if} - {#if onInfo} - + {#if !checking && running} + {#if onInfo} + + {/if} + {#if onFiles} + + {/if} {/if} {#if onSettings} - {#if onRestart || onInfo} -
- {/if} +
diff --git a/src/lib/components/chat/MessageInput/TerminalMenu.svelte b/src/lib/components/chat/MessageInput/TerminalMenu.svelte index 9da47e41b2..de51a014c8 100644 --- a/src/lib/components/chat/MessageInput/TerminalMenu.svelte +++ b/src/lib/components/chat/MessageInput/TerminalMenu.svelte @@ -6,6 +6,7 @@ import { updateChatById } from '$lib/apis/chats'; import { toast } from 'svelte-sonner'; import TerminalSystemInfoModal from '../TerminalSystemInfoModal.svelte'; + import TerminalFilesModal from '../TerminalFilesModal.svelte'; import WorkingDirectoryMenu from './WorkingDirectoryMenu.svelte'; import type { TerminalContext } from '$lib/types'; @@ -61,19 +62,50 @@ 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)); - } + // Lifecycle actions act on this chat's machine of a terminal the user manages. + const machineContext = () => ({ + chat_id: isSavedChatId($activeChatId) ? $activeChatId : null, + model_id: modelId + }); + const machineStatus = async (id: string) => { + const query = new URLSearchParams(); + const context = machineContext(); + if (context.chat_id) query.set('chat_id', context.chat_id); + if (context.model_id) query.set('model_id', context.model_id); + return (await templatesApi(`/${encodeURIComponent(id)}/instance?${query}`))?.status ?? null; + }; + const lifecycleCopy = { + restart: { + title: 'Restart {{name}}?', + message: 'Running work stops and the machine starts again. Files are kept.', + label: 'Restart', + pending: 'Restarting {{name}}…', + done: '{{name}} restarted' + }, + stop: { + title: 'Stop {{name}}?', + message: + 'Running work stops. Files are kept, and the machine starts again when it is next used.', + label: 'Stop', + pending: 'Stopping {{name}}…', + done: '{{name}} stopped' + }, + start: { pending: 'Starting {{name}}…', done: '{{name}} started' } + }; + let pending: { id: string; name: string; action: 'restart' | 'stop' } | null = null; + let showConfirm = false; + const runLifecycle = (id: string, name: string, action: 'restart' | 'stop' | 'start') => { + const copy = lifecycleCopy[action]; + toast.promise(templatesApi(`/${encodeURIComponent(id)}/${action}`, 'POST', machineContext()), { + loading: $i18n.t(copy.pending, { name }), + success: () => $i18n.t(copy.done, { name }), + error: (e) => (e instanceof Error ? e.message : String(e)) + }); + }; + const confirmLifecycle = (id: string, name: string, action: 'restart' | 'stop') => { + pending = { id, name, action }; + show = false; + showConfirm = true; }; $: systemTerminals = ($terminalServers ?? []).filter( @@ -211,6 +243,20 @@ if (await saveSelection(id)) selectedTerminalId.set(id); } let info: { target: TerminalTarget; label: string } | null = null; + let files: { target: TerminalTarget; label: string } | null = null; + const openFiles = async (selector: string, label: string) => { + const target = resolveTerminalTarget( + selector, + systemTerminals, + directTerminals, + localStorage.token ?? '', + { chatId: $activeChatId || null, modelId: modelId } + ); + if (!target) return; + show = false; + await tick(); + files = { target, label }; + }; const openInfo = async (selector: string, label: string) => { if ( systemTerminals.find((t) => !!selector && t.id === selector)?.scope === 'chat' && @@ -350,7 +396,11 @@ ? 'right-6' : 'right-1'} {moreClass}" > - openInfo(terminal.url, label)} /> + openInfo(terminal.url, label)} + onFiles={() => openFiles(terminal.url, label)} + /> {/each} @@ -399,6 +449,8 @@ {#each section.terminals as terminal} {@const label = terminal.name || $i18n.t('Terminal')} + {@const owned = canManage(terminal, manageable)} + {@const reachable = terminal.scope !== 'chat' || isSavedChatId($activeChatId)}
@@ -491,10 +537,16 @@ /> {/if} +{#if files} + {#key files} + (files = null)} /> + {/key} +{/if} + pending && runLifecycle(pending.id, pending.name, pending.action)} /> diff --git a/src/lib/components/chat/TerminalFilesModal.svelte b/src/lib/components/chat/TerminalFilesModal.svelte new file mode 100644 index 0000000000..a6c83f7f15 --- /dev/null +++ b/src/lib/components/chat/TerminalFilesModal.svelte @@ -0,0 +1,31 @@ + + + +
+
+

{label}

+ +
+
+ +
+
+
diff --git a/src/lib/components/chat/UserTerminalsModal.svelte b/src/lib/components/chat/UserTerminalsModal.svelte index b41a98fb85..883d22441e 100644 --- a/src/lib/components/chat/UserTerminalsModal.svelte +++ b/src/lib/components/chat/UserTerminalsModal.svelte @@ -29,6 +29,10 @@ import { terminalServers, user } from '$lib/stores'; import TerminalSystemInfoModal from './TerminalSystemInfoModal.svelte'; import Activity from '$lib/components/icons/Activity.svelte'; + import Folder from '$lib/components/icons/Folder.svelte'; + import Stop from '$lib/components/icons/Stop.svelte'; + import Play from '$lib/components/icons/Play.svelte'; + import TerminalFilesModal from './TerminalFilesModal.svelte'; /** Edits one terminal, or creates one when `terminalId` is null. */ export let show = true; @@ -263,8 +267,9 @@ } let info: { target: TerminalTarget; label: string } | null = null; + let files: { target: TerminalTarget; label: string } | null = null; // Requests carry the viewer's identity, so another person's machine can't be reached this way. - // A stopped machine would start on request, so only running machines offer System info. + // Only running machines offer System info and files: Terminals won't start one for System info. function machineTarget(machine: any) { if (!terminalId || machine.status !== 'running') return null; const context = machine.context ?? {}; @@ -275,28 +280,42 @@ }); } - function machineAction(machine: any, action: 'restart' | 'delete') { - const title = machineName(machine); - const [heading, message, button] = - action === 'restart' - ? [ - $i18n.t('Restart {{name}}?', { name: title }), - $i18n.t('Running work stops. Files are kept.'), - $i18n.t('Restart') - ] - : [ - $i18n.t('Delete {{name}}?', { name: title }), - $i18n.t('The machine and its files are deleted.'), - $i18n.t('Delete') - ]; - confirm(heading, message, button, async () => { - await templatesApi( - `${path(terminalId!)}/${action === 'delete' ? 'instance' : action}`, - action === 'delete' ? 'DELETE' : 'POST', - machine.context ?? {} - ); - await loadMachines(); - }); + const machineCopy = { + restart: [ + 'Restart {{name}}?', + 'Running work stops and the machine starts again. Files are kept.', + 'Restart' + ], + stop: [ + 'Stop {{name}}?', + 'Running work stops. Files are kept, and the machine starts again when it is next used.', + 'Stop' + ], + delete: ['Delete {{name}}?', 'The machine and its files are deleted.', 'Delete'] + }; + + const sendMachineAction = async ( + machine: any, + action: 'restart' | 'stop' | 'start' | 'delete' + ) => { + await templatesApi( + `${path(terminalId!)}/${action === 'delete' ? 'instance' : action}`, + action === 'delete' ? 'DELETE' : 'POST', + machine.context ?? {} + ); + await loadMachines(); + }; + + function machineAction(machine: any, action: 'restart' | 'stop' | 'start' | 'delete') { + // Starting changes nothing that is running, so it needs no confirmation. + if (action === 'start') return run(() => sendMachineAction(machine, action)); + const [heading, message, button] = machineCopy[action]; + confirm( + $i18n.t(heading, { name: machineName(machine) }), + $i18n.t(message), + $i18n.t(button), + () => sendMachineAction(machine, action) + ); } function deleteTerminal() { @@ -630,20 +649,42 @@
{@const target = machineTarget(machine)} - {#if target} + {#if machine.status === 'running'} + {#if target} + + + {/if} machineAction(machine, 'restart')} + >{$i18n.t('Restart')} + + {:else} + {/if} - +