Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289<script lang="ts"> import { tick } from 'svelte'; import { listJobs, stopJob, type Job } from '../api'; import { Tail, headline, outcome, readable } from '../jobs.svelte';
// One job's output, following it while it runs: the body of the terminal // window on the canvas and of the terminal that opens inside a chat on a // phone. It only reads; the one thing it does to the job is stop it. let { sessionId, jobId, onjob, }: { sessionId: string; jobId: string; onjob?: (job: Job) => void } = $props();
// svelte-ignore state_referenced_locally const tail = new Tail(sessionId, jobId); const shown = $derived(readable(tail.text)); let now = $state(Date.now()); let showCode = $state(false); let childJobs = $state<Job[]>([]); let scroller = $state<HTMLElement>(); let pinned = true; let stopping = $state<'asking' | 'stopping' | null>(null); let problem = $state('');
const hasSource = $derived(tail.job?.kind === 'cell' && !!tail.source); const autoCode = $derived(hasSource && !shown && !tail.problem && !tail.done); const showingCode = $derived(hasSource && (autoCode || showCode)); const activeChildren = $derived( childJobs.filter((j) => j.parent_id === jobId && j.state === 'running'), ); const statusText = $derived(tail.job ? outcome(tail.job, now) : '');
$effect(() => { tail.start(); return () => tail.stop(); });
$effect(() => { if (tail.job) onjob?.(tail.job); });
// Update the minute-level running label only at minute boundaries while running. $effect(() => { const job = tail.job; if ( !job || job.state !== 'running' || !job.started_at || job.started_at <= 0 ) return; let timer: ReturnType<typeof setTimeout>; const schedule = () => { const elapsed = Math.max(0, Date.now() - job.started_at); const wait = Math.max(1000, 60_000 - (elapsed % 60_000) + 50); timer = setTimeout(() => { now = Date.now(); schedule(); }, wait); }; schedule(); const sync = () => { if (!document.hidden) now = Date.now(); }; document.addEventListener('visibilitychange', sync); return () => { clearTimeout(timer); document.removeEventListener('visibilitychange', sync); }; });
$effect(() => { if (tail.job?.kind !== 'cell' || tail.job.state !== 'running') { childJobs = []; return; } let timer: ReturnType<typeof setTimeout>; const abort = new AbortController(); const refresh = async () => { if (!document.hidden) { const jobs = await listJobs(sessionId, abort.signal).catch(() => null); if (abort.signal.aborted) return; if (jobs) childJobs = jobs; } timer = setTimeout(refresh, 5000); }; refresh(); return () => { clearTimeout(timer); abort.abort(); }; });
// Output arriving moves the view only for a reader who is at the end of it. $effect(() => { shown; if (pinned && !showingCode) tick().then( () => scroller && (scroller.scrollTop = scroller.scrollHeight), ); });
function scrolled() { if (scroller && !showingCode) pinned = scroller.scrollHeight - scroller.scrollTop - scroller.clientHeight < 24; }
async function stop() { if (stopping !== 'asking') { stopping = 'asking'; setTimeout(() => stopping === 'asking' && (stopping = null), 3000); return; } stopping = 'stopping'; problem = ''; try { await stopJob(sessionId, jobId); } catch (error) { problem = String((error as Error).message ?? error); } finally { stopping = null; } }</script>
<div class="terminal"> <pre class="output nowheel" bind:this={scroller} onscroll={scrolled}>{#if showingCode}{#if autoCode}<span class="quiet" >(・ω・)ノ waiting for output…</span >{#if activeChildren.length} {#each activeChildren.slice(0, 3) as child (child.id)}<span class="quiet" >↳ {headline(child.command, child.kind)} · {outcome( child, now, )}</span > {/each}{#if activeChildren.length > 3}<span class="quiet" >… +{activeChildren.length - 3} more</span > {/if}{/if}
<span class="faded">{tail.source}</span>{:else}<span class="quiet" >cell source
</span>{tail.source}{/if}{:else}{#if tail.skipped}<span class="quiet" >… {tail.skipped.toLocaleString()} earlier bytes not shown</span>{/if}{shown}{#if !shown && !tail.problem && !tail.done}<span class="quiet">(・ω・)ノ waiting for output…</span >{:else if !shown && tail.done && !tail.problem}<span class="quiet" >(・_・ ) this {tail.job?.kind === 'cell' ? 'cell' : 'job'} wrote nothing</span >{/if}{/if}</pre> <footer> <span class="status" class:failed={tail.job?.state === 'exited' && (tail.job.exit_code !== 0 || tail.job.timed_out)}>{statusText}</span > {#if tail.problem || problem}<span class="problem" title={tail.problem || problem}>{tail.problem || problem}</span >{/if} <div class="actions"> {#if hasSource && !autoCode} <button type="button" class="toggle" aria-pressed={showCode} onclick={() => (showCode = !showCode)} >{showCode ? 'output' : 'code'}</button > {/if} {#if tail.job?.state === 'running'} <button type="button" class="stop" disabled={stopping === 'stopping'} onclick={stop} >{stopping === 'asking' ? 'stop it?' : stopping === 'stopping' ? 'stopping…' : 'stop'}</button > {/if} </div> </footer></div>
<style> .terminal { display: flex; flex: 1; flex-direction: column; min-height: 0; } .output { flex: 1; min-height: 0; margin: 0; padding: 12px 16px; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; color: var(--text); font-family: var(--mono); font-size: calc(var(--type-sm) * var(--canvas-scale)); line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--well); } .quiet { color: var(--muted); } .faded { color: var(--muted); opacity: 0.72; } footer { display: flex; flex: none; align-items: center; gap: 10px; min-height: 40px; padding: 0 12px 0 16px; background: var(--title-bar); font-size: calc(var(--type-sm) * var(--canvas-scale)); } .status { color: var(--muted); font-variant-numeric: tabular-nums; } .status.failed, .problem { color: var(--danger); } .problem { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } .actions { display: flex; align-items: center; gap: 8px; margin-left: auto; } .toggle, .stop { height: 26px; padding: 0 12px; font: inherit; background: none; border-radius: 8px; cursor: pointer; } .toggle { color: var(--muted); border: 1px solid color-mix(in oklab, var(--text) 14%, transparent); } .toggle:hover, .toggle[aria-pressed='true'] { color: var(--text); border-color: color-mix(in oklab, var(--text) 28%, transparent); } .stop { color: var(--danger); border: 1px solid color-mix(in oklab, var(--danger) 45%, transparent); } .stop:hover:not(:disabled) { background: color-mix(in oklab, var(--danger) 10%, transparent); } .stop:disabled { opacity: 0.5; cursor: default; }</style>