This commit is contained in:
Timothy Jaeryang Baek committed 2026-11-10 23:38:17 +09:00
1 parent 473a0fb24f
commit c647f3f42e
6 files changed
+274 -76

No files matched your search

+10
View File
@@ -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)
@@ -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<any>('i18n');
export let label = '';
/** Reads this chat's machine status when the menu opens; enables Restart, Stop, and Start. */
export let loadStatus: (() => Promise<string | null>) | 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 @@
};
</script>
{#if onRestart || onInfo || onSettings}
{#if loadStatus || onInfo || onFiles || onSettings}
<Dropdown bind:show align="start" sideOffset={-2}>
<Tooltip content={$i18n.t('More')}>
<button
@@ -45,20 +65,40 @@
<div slot="content">
<DropdownMenu className="min-w-44">
{#if onRestart}
<button type="button" class={item} on:click={(e) => run(e, onRestart)}
><ArrowPath className="size-3.5" />{$i18n.t('Restart')}</button
>
{#if loadStatus && checking}
<div class="flex h-[1.6875rem] items-center px-2"><Spinner className="size-3.5" /></div>
{:else if loadStatus}
{#if status === 'running'}
{#if onRestart}
<button type="button" class={item} on:click={(e) => run(e, onRestart)}
><ArrowPath className="size-3.5" />{$i18n.t('Restart')}</button
>
{/if}
{#if onStop}
<button type="button" class={item} on:click={(e) => run(e, onStop)}
><Stop className="size-3.5" />{$i18n.t('Stop')}</button
>
{/if}
{:else if onStart}
<button type="button" class={item} on:click={(e) => run(e, onStart)}
><Play className="size-3.5" />{$i18n.t('Start')}</button
>
{/if}
{/if}
{#if onInfo}
<button type="button" class={item} on:click={(e) => run(e, onInfo)}
><Activity className="size-3.5" />{$i18n.t('System info')}</button
>
{#if !checking && running}
{#if onInfo}
<button type="button" class={item} on:click={(e) => run(e, onInfo)}
><Activity className="size-3.5" />{$i18n.t('System info')}</button
>
{/if}
{#if onFiles}
<button type="button" class={item} on:click={(e) => run(e, onFiles)}
><Folder className="size-3.5" />{$i18n.t('View files')}</button
>
{/if}
{/if}
{#if onSettings}
{#if onRestart || onInfo}
<hr class="border-gray-50 dark:border-gray-800/30 mx-1 my-0.5" />
{/if}
<hr class="border-gray-50 dark:border-gray-800/30 mx-1 my-0.5" />
<button type="button" class={item} on:click={(e) => run(e, onSettings)}
><Cog6 className="size-3.5" />{$i18n.t('Terminal settings')}</button
>
@@ -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<string>) =>
!!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}"
>
<TerminalItemMenu {label} onInfo={() => openInfo(terminal.url, label)} />
<TerminalItemMenu
{label}
onInfo={() => openInfo(terminal.url, label)}
onFiles={() => openFiles(terminal.url, label)}
/>
</div>
</div>
{/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)}
<div class="group/terminal relative rounded-xl">
<button
type="button"
@@ -430,19 +482,13 @@
>
<TerminalItemMenu
{label}
onRestart={canManage(terminal, manageable)
? () => {
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}
loadStatus={owned && reachable ? () => machineStatus(terminal.id) : null}
onRestart={() => confirmLifecycle(terminal.id, label, 'restart')}
onStop={() => confirmLifecycle(terminal.id, label, 'stop')}
onStart={() => runLifecycle(terminal.id, label, 'start')}
onInfo={reachable ? () => openInfo(terminal.id, label) : null}
onFiles={reachable ? () => openFiles(terminal.id, label) : null}
onSettings={owned ? () => openTerminalDialog(terminal.id) : null}
/>
</div>
</div>
@@ -491,10 +537,16 @@
/>
{/if}
{#if files}
{#key files}
<TerminalFilesModal target={files.target} label={files.label} onClose={() => (files = null)} />
{/key}
{/if}
<ConfirmDialog
bind:show={showRestart}
title={$i18n.t('Restart {{name}}?', { name: restartTarget?.name ?? '' })}
message={$i18n.t('Running work stops. Files are kept.')}
confirmLabel={$i18n.t('Restart')}
onConfirm={restart}
bind:show={showConfirm}
title={pending ? $i18n.t(lifecycleCopy[pending.action].title, { name: pending.name }) : ''}
message={pending ? $i18n.t(lifecycleCopy[pending.action].message) : ''}
confirmLabel={pending ? $i18n.t(lifecycleCopy[pending.action].label) : undefined}
onConfirm={() => pending && runLifecycle(pending.id, pending.name, pending.action)}
/>
@@ -0,0 +1,31 @@
<script lang="ts">
import { getContext } from 'svelte';
import Modal from '$lib/components/common/Modal.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
import FileNav from '$lib/components/chat/FileNav.svelte';
import type { TerminalTarget } from '$lib/apis/terminal';
const i18n = getContext<any>('i18n');
/** Browses one machine's files: the terminal, plus the chat or model it belongs to. */
export let target: TerminalTarget;
export let label: string;
export let onClose: () => void;
</script>
<Modal size="lg" ariaLabel={$i18n.t('Files on {{name}}', { name: label })} onRequestClose={onClose}>
<div class="p-4 text-gray-900 dark:text-gray-100">
<div class="mb-3 flex items-center justify-between gap-2">
<h2 class="min-w-0 truncate text-sm font-medium" title={label}>{label}</h2>
<button
type="button"
aria-label={$i18n.t('Close')}
class="shrink-0 text-gray-500 hover:text-gray-900 dark:hover:text-gray-100"
on:click={onClose}><XMark className="size-4" /></button
>
</div>
<div class="h-[70dvh] min-h-0">
<FileNav {target} manageFileRequests={false} showTerminalDock={false} />
</div>
</div>
</Modal>
@@ -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 @@
<div slot="content">
<DropdownMenu className="min-w-40">
{@const target = machineTarget(machine)}
{#if target}
{#if machine.status === 'running'}
{#if target}
<button
type="button"
class={item}
on:click={() => (info = { target, label: title })}
><Activity className="size-3.5" />{$i18n.t('System info')}</button
>
<button
type="button"
class={item}
on:click={() => (files = { target, label: title })}
><Folder className="size-3.5" />{$i18n.t('View files')}</button
>
{/if}
<button
type="button"
class={item}
on:click={() => (info = { target, label: title })}
><Activity className="size-3.5" />{$i18n.t('System info')}</button
on:click={() => machineAction(machine, 'restart')}
><ArrowPath className="size-3.5" />{$i18n.t('Restart')}</button
>
<button
type="button"
class={item}
on:click={() => machineAction(machine, 'stop')}
><Stop className="size-3.5" />{$i18n.t('Stop')}</button
>
{:else}
<button
type="button"
class={item}
on:click={() => machineAction(machine, 'start')}
><Play className="size-3.5" />{$i18n.t('Start')}</button
>
{/if}
<button
type="button"
class={item}
on:click={() => machineAction(machine, 'restart')}
><ArrowPath className="size-3.5" />{$i18n.t('Restart')}</button
>
<hr class="border-gray-50 dark:border-gray-800/30 mx-1 my-0.5" />
<button
type="button"
class={item}
@@ -732,6 +773,12 @@
{/key}
{/if}
{#if files}
{#key files}
<TerminalFilesModal target={files.target} label={files.label} onClose={() => (files = null)} />
{/key}
{/if}
<ConfirmDialog
bind:show={showConfirm}
title={confirmation?.title ?? ''}
+18
View File
@@ -0,0 +1,18 @@
<script lang="ts">
export let className = 'size-4';
export let strokeWidth = '1.5';
</script>
<svg
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width={strokeWidth}
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
class={className}
>
<rect x="6" y="6" width="12" height="12" rx="1.5" />
</svg>