This commit is contained in:
Timothy Jaeryang Baek committed 2026-11-10 23:39:04 +09:00
1 parent f1768b41a1
commit 4af7407dc5
4 files changed
+33 -5

No files matched your search

+3 -3
View File
@@ -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
+3 -2
View File
@@ -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<string, string> = {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 @@
<div class="flex justify-end"><button type="button" class={button} onclick={() => editing = null}>Cancel</button><button class={button} disabled={busy}>Save</button></div>
</form>{/if}
<section><h2 class="mb-3 font-medium">Templates</h2><div class="space-y-2">{#each templates as template (template.id)}<div class="flex items-center justify-between rounded-xl border border-gray-200 p-3 dark:border-gray-800"><div><p class="text-sm">{template.data.name ?? template.id}</p><p class="text-xs text-gray-500">{template.data.scope ?? 'None'} · {template.data.image ?? 'Server default'}</p></div><div><button class={button} disabled={busy} onclick={() => edit(template)}>Configure</button><button class={button} disabled={busy} onclick={() => remove(template)}>Delete terminal</button></div></div>{:else}<p class="text-sm text-gray-500">No templates yet.</p>{/each}</div></section>
<section><h2 class="mb-3 font-medium">Environments</h2><div class="space-y-2">{#each environments as row (row.id)}<div class="flex flex-wrap items-center justify-between gap-2 rounded-xl border border-gray-200 p-3 dark:border-gray-800"><div><p class="text-sm">{templates.find(t => t.id === row.template_id)?.data.name ?? row.template_id}</p><p class="text-xs text-gray-500">{row.context_id} · {row.status}</p></div><div><button class={button} disabled={busy} onclick={() => action(row, 'restart')}>Restart</button><button class={button} disabled={busy} onclick={() => action(row, 'reset')}>Reset</button><button class={button} disabled={busy} onclick={() => action(row, 'delete')}>Delete environment</button></div></div>{:else}<p class="text-sm text-gray-500">No environments created.</p>{/each}</div></section>
<section><h2 class="mb-3 font-medium">Environments</h2><div class="space-y-2">{#each environments as row (row.id)}<div class="flex flex-wrap items-center justify-between gap-2 rounded-xl border border-gray-200 p-3 dark:border-gray-800"><div><p class="text-sm">{templates.find(t => t.id === row.template_id)?.data.name ?? row.template_id}</p><p class="text-xs text-gray-500">{row.context_id} · {row.status}</p></div><div>{#if row.status === 'running'}<button class={button} disabled={busy} onclick={() => action(row, 'restart')}>Restart</button><button class={button} disabled={busy} onclick={() => action(row, 'stop')}>Stop</button>{:else}<button class={button} disabled={busy} onclick={() => action(row, 'start')}>Start</button>{/if}<button class={button} disabled={busy} onclick={() => action(row, 'reset')}>Reset</button><button class={button} disabled={busy} onclick={() => action(row, 'delete')}>Delete environment</button></div></div>{:else}<p class="text-sm text-gray-500">No environments created.</p>{/each}</div></section>
</main>
+18
View File
@@ -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)
+9
View File
@@ -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)