Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418<script lang="ts" context="module"> // SessionRuntimeControls — compact model + mode controls for the active session runtime // (saga vr7kgx.6 §4). // // Presentational only. It consumes the selected session's `RuntimeControlState` (the // connection's authoritative capabilities/mode/catalogs) plus the runtime projection, and // dispatches durable `sessionId` + the user's choice — NEVER the private `agent_id` route // handle. The connection resolves the route internally and fences every response. // // Discovery is capability-gated: options come EXCLUSIVELY from the authoritative // `list_models`/`list_modes` catalogs (no hard-coded `plan`/`build`, no free-form custom // mode field). A runtime without `model-catalog` shows "Model catalog unavailable"; a // runtime without `mode-catalog` shows the current `sync.mode` read-only. Stopped/ // unattached sessions are read-only (every affordance is disabled and discovery never // fires from here — the connection rejects control commands unless the tab is attached). // A STOPPED/FAILED runtime is inert — its model/mode selectors are unavailable — so the // row leads with a "Relaunch runtime" action that resumes the runtime by activating the // session, without forcing the user to open the runtime sidebar (saga vr7kgx.2 §5). // // Accepted model changes take effect on the NEXT provider call (never implied to interrupt // an in-flight stream). Mode changes use the catalog only; the runtime does final race // validation. Errors are scoped inline (aria-live) with explicit retry — they never reach // the global connection banner.</script>
<script lang="ts"> import type { SessionRuntimeInfo } from "@/contracts/session-runtime"; import type { OperationPending, WebConnectionState } from "../connection"; import type { RuntimeControlState } from "../runtime-controls"; import { CAPABILITY, hasCapability, isKnownCapability } from "../runtime-controls"; import { towerLabel } from "../severity"; import { createEventDispatcher, onDestroy } from "svelte";
export let sessionId: string; export let control: RuntimeControlState | undefined = undefined; export let runtime: SessionRuntimeInfo | null = null; export let attached = false; export let phase: WebConnectionState["phase"] = "connected"; /** Connection-level operation-pending map (e.g. `session.activate:<id>` while a resume is * in flight). Powers the relaunch button's "Relaunching…" state — mirrors the runtime * panel's pending affordance (saga vr7kgx.2 §5). */ export let operationPending: OperationPending = {};
const dispatcher = createEventDispatcher<{ refreshModels: { sessionId: string }; refreshModes: { sessionId: string }; setModel: { sessionId: string; modelId: string }; setMode: { sessionId: string; mode: string }; refreshReasoning: { sessionId: string }; setReasoning: { sessionId: string; effort: string }; /** Relaunch a stopped/failed runtime by activating the session (saga vr7kgx.2 §5). * Carries the durable `sessionId` + `cwd` only — never the private route handle. */ resume: { sessionId: string; cwd: string }; }>();
let open = false; let trigger: HTMLButtonElement | undefined = undefined; let panelEl: HTMLElement | undefined = undefined;
// Accepted-success announcement (saga vr7kgx.6): when a `set_model` mutation resolves // without error, surface a short live message that the change applies on the next provider // call. Cleared on a timer; reset whenever a new mutation starts or an error lands. let modelSuccess = false; let successTimer: ReturnType<typeof setTimeout> | undefined = undefined; let successSession = sessionId; let seenModelSuccessRevision = control?.modelSuccessRevision ?? 0; let reasoningSuccess = false; let seenReasoningSuccessRevision = control?.reasoningSuccessRevision ?? 0;
$: connected = phase === "connected"; $: usable = connected && attached; // The disclosure leads with what the AGENT is doing (idle/streaming/…), not the runtime // lifecycle. A "running" process can host an idle agent — the activity label resolves that. $: rcStopped = runtime?.runtime_state === "stopped"; $: rcFailed = runtime?.runtime_state === "failed"; // A stopped/failed runtime can be relaunched (resumed) by activating the session. When the // runtime is inactive the model/mode selectors are inert (no catalog, every affordance // disabled) — so lead with a "Relaunch runtime" action right here instead of forcing the // user to open the runtime sidebar to restart the runtime (saga vr7kgx.2 §5). $: canResume = rcStopped || rcFailed; $: activatePending = operationPending[`session.activate:${sessionId}`] === true; $: resumeDisabled = !connected || activatePending || runtime === null; $: activityLabel = rcStopped ? "history available" : rcFailed ? "runtime failed" : towerLabel(runtime?.status); $: caps = control?.capabilities ?? []; $: modelCatalogCap = hasCapability(control, CAPABILITY.modelCatalog); $: modelSwitchCap = hasCapability(control, CAPABILITY.modelSwitch); $: modeCatalogCap = hasCapability(control, CAPABILITY.modeCatalog); $: modeSwitchCap = hasCapability(control, CAPABILITY.modeSwitch); $: reasoningCatalogCap = hasCapability(control, CAPABILITY.reasoningCatalog); $: reasoningSwitchCap = hasCapability(control, CAPABILITY.reasoningSwitch); $: models = control?.models; $: modes = control?.modes; $: activeModelId = models?.active.id ?? ""; $: activeMode = control?.mode ?? ""; $: loadingModels = control?.loadingModels ?? false; $: loadingModes = control?.loadingModes ?? false; $: pendingModel = control?.pendingModel ?? false; $: pendingMode = control?.pendingMode ?? false; $: modelError = control?.modelError; $: modeError = control?.modeError; $: reasoning = control?.reasoning; $: reasoningError = control?.reasoningError; $: loadingReasoning = control?.loadingReasoning ?? false; $: pendingReasoning = control?.pendingReasoning ?? false; $: reasoningSelectDisabled = !usable || !reasoningSwitchCap || reasoning === undefined || loadingReasoning || pendingReasoning || reasoningError !== undefined;
// Disable selects unless attached + capable + not loading/pending + no blocking error. // Mode also requires a successfully loaded authoritative catalog (saga vr7kgx.6 — no // catalog/loading/error means no command, mirrored here so the control is not editable). $: modelSelectDisabled = !usable || !modelSwitchCap || loadingModels || pendingModel || modelError !== undefined; $: modeSelectDisabled = !usable || !modeCatalogCap || !modeSwitchCap || modes === undefined || loadingModes || pendingMode || modeError !== undefined;
/** Distinct providers in catalog order (optgroup grouping) — no invented credentials. */ $: providers = models ? Array.from(new Set(models.models.map((m) => m.provider))) : [];
function onModelChange(event: Event) { const target = event.currentTarget as HTMLSelectElement; const modelId = target.value; target.value = activeModelId; // revert visual until the correlated result lands if (modelId.length === 0 || modelId === activeModelId) return; dispatcher("setModel", { sessionId, modelId }); } function onModeChange(event: Event) { const target = event.currentTarget as HTMLSelectElement; const mode = target.value; target.value = activeMode; // revert until the correlated result lands if (mode.length === 0 || mode === activeMode) return; dispatcher("setMode", { sessionId, mode }); } function onReasoningChange(event: Event) { const target = event.currentTarget as HTMLSelectElement; const effort = target.value; target.value = reasoning?.active ?? ""; if (effort.length > 0 && effort !== reasoning?.active) dispatcher("setReasoning", { sessionId, effort }); } function refreshModels() { if (usable && modelCatalogCap) dispatcher("refreshModels", { sessionId }); } function refreshModes() { if (usable && modeCatalogCap) dispatcher("refreshModes", { sessionId }); } function refreshReasoning() { if (usable && reasoningCatalogCap) dispatcher("refreshReasoning", { sessionId }); } /** Relaunch a stopped/failed runtime. Dispatches the durable `sessionId` + `cwd` only. */ function relaunch() { if (!canResume || resumeDisabled) return; const cwd = runtime?.cwd; if (cwd === undefined) return; dispatcher("resume", { sessionId, cwd }); }
function toggle() { if (!open) { open = true; } else { close(); } } function close() { if (!open) return; open = false; // Deterministic focus restoration to the disclosure trigger. trigger?.focus(); }
function onKeydown(event: KeyboardEvent) { if (event.key !== "Escape") return; if (open) { event.stopPropagation(); close(); } } function onDocumentPointerdown(event: MouseEvent) { if (!open) return; const target = event.target as Node | null; if (panelEl?.contains(target) || trigger === event.target) return; close(); }
function shortId(id: string): string { return id.length > 16 ? `${id.slice(0, 13)}…` : id; } function ctxLabel(ctx: number | undefined): string { if (ctx === undefined) return ""; if (ctx >= 1_000_000) return `${(ctx / 1_000_000).toFixed(1)}M ctx`; if (ctx >= 1_000) return `${(ctx / 1_000).toFixed(0)}k ctx`; return `${ctx} ctx`; }
function clearSuccessTimer() { if (successTimer !== undefined) { clearTimeout(successTimer); successTimer = undefined; } } // Announce only an explicitly accepted LOCAL correlated set_model response. Broadcasts can // also clear `pendingModel`, so inferring success from pending→false would falsely claim a // local acceptance when another client's conflicting change won. $: { const revision = control?.modelSuccessRevision ?? 0; if (sessionId !== successSession) { successSession = sessionId; seenModelSuccessRevision = revision; modelSuccess = false; clearSuccessTimer(); } else if (revision > seenModelSuccessRevision) { seenModelSuccessRevision = revision; modelSuccess = true; clearSuccessTimer(); successTimer = setTimeout(() => { modelSuccess = false; successTimer = undefined; }, 5000); } } $: if (pendingModel || modelError !== undefined) { modelSuccess = false; clearSuccessTimer(); } $: { const revision = control?.reasoningSuccessRevision ?? 0; if (revision > seenReasoningSuccessRevision) { seenReasoningSuccessRevision = revision; reasoningSuccess = true; setTimeout(() => { reasoningSuccess = false; }, 5000); } } $: if (pendingReasoning || reasoningError !== undefined) reasoningSuccess = false;
onDestroy(() => { document.removeEventListener("keydown", onKeydown, true); document.removeEventListener("pointerdown", onDocumentPointerdown, true); clearSuccessTimer(); }); $: if (open) { document.addEventListener("keydown", onKeydown, true); document.addEventListener("pointerdown", onDocumentPointerdown, true); } else { document.removeEventListener("keydown", onKeydown, true); document.removeEventListener("pointerdown", onDocumentPointerdown, true); } // Reset disclosure when the active session changes. let lastSession = sessionId; $: if (sessionId !== lastSession) { lastSession = sessionId; open = false; }</script>
<div class="runtime-controls" data-session-id={sessionId}> <div class="ctl-row" class:relaunch={canResume}> {#if canResume} <button class="btn btn--primary relaunch-btn" type="button" data-action="relaunch" on:click={relaunch} disabled={resumeDisabled} title="Restart the runtime so model and mode become selectable"> {activatePending ? "Relaunching…" : "Relaunch runtime"} </button> {:else} {#if modelCatalogCap} <label class="ctl" data-ctl="model"> <span class="ctl-label">Model</span> <select class="ctl-select mono" value={activeModelId} disabled={modelSelectDisabled} on:change={onModelChange} aria-label="Active model"> {#if loadingModels && models === undefined}<option value={activeModelId}>Loading…</option>{/if} {#if models} {#each providers as provider (provider)} <optgroup label={provider}> {#each models.models.filter((m) => m.provider === provider) as m (m.id)} <option value={m.id}>{shortId(m.id)} · {m.model}{#if m.context_window !== undefined} · {ctxLabel(m.context_window)}{/if}</option> {/each} </optgroup> {/each} {:else} <option value={activeModelId}>{activeModelId ? shortId(activeModelId) : "—"}</option> {/if} </select> </label> {:else} <span class="ctl-unavailable" data-ctl="model" title="This runtime does not expose a model catalog">Model catalog unavailable</span> {/if}
{#if modeCatalogCap} <label class="ctl" data-ctl="mode"> <span class="ctl-label">Mode</span> <select class="ctl-select mono" value={activeMode} disabled={modeSelectDisabled} on:change={onModeChange} aria-label="Active mode"> {#if loadingModes && modes === undefined}<option value={activeMode}>{activeMode || "Loading…"}</option>{/if} {#if modes} {#each modes.modes as m (m.name)} <option value={m.name} title={m.description}>{m.name}</option> {/each} {:else} <option value={activeMode}>{activeMode || "—"}</option> {/if} </select> </label> {:else} <span class="ctl-unavailable" data-ctl="mode" title="This runtime does not expose a mode catalog"> Mode {activeMode ? activeMode : "—"}<span class="unavailable-note"> · discovery unavailable</span></span> {/if}
{#if reasoningCatalogCap} <label class="ctl" data-ctl="reasoning"> <span class="ctl-label">Reasoning</span> <select class="ctl-select mono" value={reasoning?.active ?? ""} disabled={reasoningSelectDisabled} on:change={onReasoningChange} aria-label="Reasoning level"> {#if loadingReasoning && reasoning === undefined}<option value="">Loading…</option>{/if} {#if reasoning} {#each reasoning.levels as level (level.id)} <option value={level.id} title={level.description}>{level.label}</option> {/each} {:else}<option value="">—</option>{/if} </select> </label> {/if} {/if}
<button class="btn btn--ghost disclose" type="button" bind:this={trigger} on:click={toggle} aria-expanded={open} aria-controls={`rc-panel-${sessionId}`} aria-label="Runtime details"> {open ? "✕" : "ℹ"} </button> </div>
{#if modelError || modeError || reasoningError} <div class="ctl-errors" aria-live="polite" role="status"> {#if modelError} <span class="ctl-err" data-err="model"> <span class="err-msg mono">{modelError}</span> <button class="btn btn--ghost retry" type="button" on:click={refreshModels} disabled={!usable || !modelCatalogCap}>Retry</button> </span> {/if} {#if modeError} <span class="ctl-err" data-err="mode"> <span class="err-msg mono">{modeError}</span> <button class="btn btn--ghost retry" type="button" on:click={refreshModes} disabled={!usable || !modeCatalogCap}>Retry</button> </span> {/if} {#if reasoningError} <span class="ctl-err" data-err="reasoning"> <span class="err-msg mono">{reasoningError}</span> <button class="btn btn--ghost retry" type="button" on:click={refreshReasoning} disabled={!usable || !reasoningCatalogCap}>Retry</button> </span> {/if} </div> {/if}
{#if modelSuccess} <p class="ctl-success mono" aria-live="polite" role="status" data-success="model">Model updated — applies on the next provider call.</p> {/if} {#if reasoningSuccess} <p class="ctl-success mono" aria-live="polite" role="status" data-success="reasoning">Reasoning updated — applies on the next provider call.</p> {/if}
{#if pendingModel || pendingMode || pendingReasoning} <p class="ctl-pending mono" aria-live="polite"> {#if pendingModel}Switching model… applies on the next provider call.{/if} {#if pendingMode}Switching mode…{/if} {#if pendingReasoning}Updating reasoning… applies on the next provider call.{/if} </p> {/if}
{#if open} <div class="rc-panel" id={`rc-panel-${sessionId}`} bind:this={panelEl} role="group" aria-label="Runtime details"> <dl class="rc-fields"> <div class="rc-field"><dt>Activity</dt><dd class="mono">{activityLabel}</dd></div> <div class="rc-field"><dt>Runtime</dt><dd class="mono">{runtime?.runtime_state ?? "—"}</dd></div> <div class="rc-field"><dt>Mode</dt><dd class="mono">{activeMode || "—"}</dd></div> <div class="rc-field"><dt>Model</dt><dd class="mono">{models ? models.active.model : "—"}</dd></div> <div class="rc-field"><dt>Provider</dt><dd class="mono">{models ? models.active.provider : "—"}</dd></div> <div class="rc-field"><dt>Reasoning</dt><dd class="mono">{reasoning?.levels.find((level) => level.id === reasoning?.active)?.label ?? reasoning?.active ?? "—"}</dd></div> <div class="rc-field"><dt>Selection ID</dt><dd class="mono">{models ? models.active.id : "—"}</dd></div> </dl> {#if caps.length > 0} <div class="rc-caps" aria-label="Runtime capabilities"> <span class="rc-caps-label">Capabilities</span> <ul class="cap-list"> {#each caps as cap (cap)} <li class="cap" class:unknown={!isKnownCapability(cap)} title={isKnownCapability(cap) ? cap : "Unknown capability — no behavior granted"}>{cap}</li> {/each} </ul> </div> {/if} </div> {/if}</div>
<style> .runtime-controls { display: flex; flex-direction: column; gap: 2px; min-width: 0; } /* Controls have their own stable tracks. A long model label is clipped inside its select, never allowed to resize or displace the mode selector/disclosure. */ .ctl-row { display: grid; grid-template-columns: minmax(150px, 2fr) minmax(100px, 1fr) minmax(120px, 1fr) auto; align-items: center; gap: 8px; min-width: 0; } @media (max-width: 980px) { .ctl-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; } .ctl[data-ctl="reasoning"] { grid-column: 1 / 3; } } @media (max-width: 620px) { .ctl-row { grid-template-columns: minmax(0, 1fr) auto; } .ctl[data-ctl="model"], .ctl[data-ctl="mode"], .ctl[data-ctl="reasoning"] { grid-column: 1; } .disclose { grid-column: 2; grid-row: 1; } } /* When the runtime is stopped/failed the model/mode selectors are inert, so the row leads with a single "Relaunch runtime" action that spans the selector tracks; the disclosure keeps its anchor. Two classes beat every `.ctl-row` rule (base + media queries). */ .ctl-row.relaunch { grid-template-columns: minmax(0, 1fr) auto; } .relaunch-btn { font-size: 11.5px; padding: 3px 10px; justify-content: center; } .ctl { display: flex; align-items: center; gap: 4px; min-width: 0; } .ctl[data-ctl="model"], .ctl[data-ctl="mode"] { min-width: 0; } .ctl-label { font-size: 10px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.04em; flex: none; } .ctl-select { font-size: 11.5px; padding: 2px 6px; border: 1px solid var(--hairline); border-radius: var(--r-xs); background: var(--canvas); color: var(--ink); flex: 1 1 auto; min-width: 0; } .ctl-select:disabled { opacity: 0.6; cursor: not-allowed; } .ctl-select:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--r-xs); } .ctl-unavailable { font-size: 11.5px; color: var(--ink-3); padding: 2px 0; } .ctl-unavailable .unavailable-note { color: var(--ink-mute); }
.disclose { padding: 2px 7px; font-size: 12px; line-height: 1; flex: none; }
.ctl-errors { display: flex; flex-direction: column; gap: 2px; } .ctl-err { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--sev-error-ink); } .ctl-err .err-msg { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .ctl-err .retry { padding: 0 6px; font-size: 11px; }
.ctl-pending { margin: 0; font-size: 11px; color: var(--ink-3); } .ctl-success { margin: 0; font-size: 11px; color: var(--ink-2); }
.rc-panel { position: absolute; z-index: 6; right: 14px; top: 100%; margin-top: 4px; min-width: 220px; max-width: min(360px, 92vw); padding: 10px 12px; border: 1px solid var(--hairline); border-radius: var(--r-md); background: var(--surface); box-shadow: var(--shadow-veil); } .rc-fields { display: flex; flex-direction: column; gap: 4px; margin: 0; } .rc-field { display: grid; grid-template-columns: minmax(64px, 36%) 1fr; gap: 8px; } .rc-field dt { font-size: 10.5px; color: var(--ink-3); } .rc-field dd { margin: 0; font-size: 11.5px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .rc-caps { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--hairline-faint); } .rc-caps-label { font-size: 10px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.04em; } .cap-list { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0 0; padding: 0; list-style: none; } .cap { font-size: 10.5px; font-family: var(--font-mono); padding: 1px 6px; border-radius: var(--r-xs); background: var(--hairline-faint); color: var(--ink-2); } .cap.unknown { background: transparent; border: 1px dashed var(--hairline); color: var(--ink-3); }</style>