From 4af7407dc55086f01c1900939a6e811ccfd70207 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Tue, 10 Nov 2026 23:39:04 +0900 Subject: [PATCH] refac --- README.md | 6 +++--- terminals/frontend/src/routes/+page.svelte | 5 +++-- terminals/routers/templates.py | 18 ++++++++++++++++++ terminals/utils/instances.py | 9 +++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f42f3bf..0edb030 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ The examples below use the same `$API_KEY` shell variable. In a new shell, set i The built-in UI at `/` has two sections: - **Templates:** create or edit instance configuration and scheduled resets, or delete a template and its environments. -- **Environments:** inspect tracked environments, including stopped ones; restart, reset, or delete the selected environment. +- **Environments:** inspect tracked environments, including stopped ones; start, stop, restart, reset, or delete the selected environment. Use the Terminals admin API key. Set `TERMINALS_ENABLE_UI=false` for an API-only deployment. @@ -214,7 +214,7 @@ profile changes to existing terminals. ### Apply changes to existing terminals -Edit configuration with `PATCH /api/v1/terminals/templates/{id}`. Changes take effect when an environment is next created. Restart stops the selected environment and preserves its files; its next use loads the current template configuration: +Edit configuration with `PATCH /api/v1/terminals/templates/{id}`. Changes take effect when an environment next starts. Restart stops the selected environment, preserves its files, and starts it again with the current template configuration: ```bash curl -fsS -X POST http://localhost:3000/api/v1/terminals/templates/data-science/restart \ @@ -223,7 +223,7 @@ curl -fsS -X POST http://localhost:3000/api/v1/terminals/templates/data-science/ -d '{"user_id":"demo-user"}' ``` -For chat or model scope, also supply `chat_id` or `model_id`. Restart does not create an unused environment. Scope and storage-mode changes are rejected while affected environments exist. +For chat or model scope, also supply `chat_id` or `model_id`. Restart does not create an unused environment. Use `/stop` to stop it without starting it again, and `/start` to start it, creating it if needed. Scope and storage-mode changes are rejected while affected environments exist. ### Schedule file resets diff --git a/terminals/frontend/src/routes/+page.svelte b/terminals/frontend/src/routes/+page.svelte index d7da66a..c66fcdc 100644 --- a/terminals/frontend/src/routes/+page.svelte +++ b/terminals/frontend/src/routes/+page.svelte @@ -39,7 +39,8 @@ } function context(row: any) {const [scope, ...parts] = row.context_id.split(':'); return {user_id: 'admin', ...(scope === 'user' ? {user_id: parts.join(':')} : scope === 'chat' ? {chat_id: parts.join(':')} : scope === 'model' ? {model_id: parts.join(':')} : {})};} async function action(row: any, action: string) { - if (!confirm(action === 'restart' ? 'Stop running work and retain files?' : 'Remove files and running work for everyone using this environment?')) return; + const prompts: Record = {restart: 'Stop running work and start again? Files are kept.', stop: 'Stop running work? Files are kept.', reset: 'Remove files and running work for everyone using this environment?', delete: 'Remove files and running work for everyone using this environment?'}; + if (action !== 'start' && !confirm(prompts[action])) return; await run(async () => {await api(`/templates/${encodeURIComponent(row.template_id)}/${action === 'delete' ? 'instance' : action}`, action === 'delete' ? 'DELETE' : 'POST', context(row)); await load();}); } async function remove(template: any) { @@ -63,5 +64,5 @@
{/if}

Templates

{#each templates as template (template.id)}

{template.data.name ?? template.id}

{template.data.scope ?? 'None'} · {template.data.image ?? 'Server default'}

{:else}

No templates yet.

{/each}
-

Environments

{#each environments as row (row.id)}

{templates.find(t => t.id === row.template_id)?.data.name ?? row.template_id}

{row.context_id} · {row.status}

{:else}

No environments created.

{/each}
+

Environments

{#each environments as row (row.id)}

{templates.find(t => t.id === row.template_id)?.data.name ?? row.template_id}

{row.context_id} · {row.status}

{#if row.status === 'running'}{:else}{/if}
{:else}

No environments created.

{/each}
diff --git a/terminals/routers/templates.py b/terminals/routers/templates.py index 3c6e4f4..51e23a5 100644 --- a/terminals/routers/templates.py +++ b/terminals/routers/templates.py @@ -282,6 +282,24 @@ async def restart_instance(template_id: str, body: InstanceContext, request: Req return {"ok": True} +@router.post("/{template_id}/stop") +async def stop_environment(template_id: str, body: InstanceContext, request: Request): + context, _ = await context_for(template_id, body) + await change_instance(request.app.state.backend, template_id, context, "stop") + return {"ok": True} + + +@router.post("/{template_id}/start") +async def start_environment(template_id: str, body: InstanceContext, request: Request): + """Start an environment, creating it on first use like any other request would.""" + context, _ = await context_for(template_id, body) + try: + await request.app.state.backend.ensure_terminal(template_id=template_id, context_id=context) + except ProvisioningError as exc: + raise HTTPException(status_code=503, detail=str(exc)) from exc + return {"ok": True} + + @router.post("/{template_id}/reset") async def reset_instance(template_id: str, body: InstanceContext, request: Request): context, _ = await context_for(template_id, body) diff --git a/terminals/utils/instances.py b/terminals/utils/instances.py index 23e55cb..fe5252c 100644 --- a/terminals/utils/instances.py +++ b/terminals/utils/instances.py @@ -189,6 +189,12 @@ async def describe_instance(backend, row): async def change_instance(backend, template_id, context_id, action): + """Stop, restart, reset, or delete one environment. + + Restart stops the environment and starts it again on the current template, so a + new image or setup script takes effect right away. Stop leaves it stopped until + it is next used or started. + """ from terminals.db.session import async_session from terminals.models.template import TemplateLifecycleState from terminals.utils.context import get_terminal_id @@ -209,3 +215,6 @@ async def change_instance(backend, template_id, context_id, action): await remove_home(backend, row, spec) await session.delete(row) await session.commit() + if action == "restart": + # Outside the locks: starting takes the same environment lock. + await backend.ensure_terminal(template_id=template_id, context_id=context_id)