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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207<script lang="ts"> import type { Job } from './api'; import { ago } from './format'; import { headline, outcome, watchJobs } from './jobs.svelte'; import MenuButton from './MenuButton.svelte';
// The session's background jobs: an activity pulse (not a prompt: that is a live terminal), with how many are // running, that lists them (finished ones too, while the kernel still has // their output). Nothing shows until the session has had a job. let { sessionId, onopen, up = false, inline = false, }: { sessionId: string; onopen: (job: string) => void; up?: boolean; inline?: boolean; } = $props(); let jobs = $state<Job[]>([]); let listed = $state(false); let expanded = $state<Record<string, boolean>>({}); // When each job last wrote, as far as this list has seen: the record only // counts bytes, so a count that grew between reads is output just now. const heard = new Map<string, { bytes: number; at: number }>(); const lastOutput = (job: Job) => heard.get(job.id)?.at ?? job.started_at; const sortOrder = (a: Job, b: Job) => Number(b.state === 'running') - Number(a.state === 'running') || lastOutput(b) - lastOutput(a); const groups = $derived.by(() => { const sorted = [...jobs].sort(sortOrder); const ids = new Set(sorted.map((j) => j.id)); return sorted .filter((j) => !j.parent_id || !ids.has(j.parent_id)) .map((root) => { const nested = sorted.filter((j) => j.parent_id === root.id); return { root, runningChildren: nested.filter((j) => j.state === 'running'), exitedChildren: nested.filter((j) => j.state !== 'running'), }; }); }); const running = $derived( groups.filter((g) => g.root.state === 'running').length, );
$effect(() => watchJobs(sessionId, (found) => { const now = Date.now(); for (const job of found) { const seen = heard.get(job.id); if (!seen || seen.bytes !== job.bytes) heard.set(job.id, { bytes: job.bytes, at: seen ? now : (job.ended_at ?? job.started_at), }); } jobs = found; }), );</script>
{#if jobs.length || inline} <MenuButton label="background jobs" count={running} live={running > 0} {up} {inline} bind:open={listed} > {#snippet icon()} <svg viewBox="0 0 16 16" width="15" height="15" aria-hidden="true" ><path d="M1.5 8.5h2.8l1.9-4.5 3.2 8 1.9-4.5h3.2" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" /></svg > {/snippet} {#each groups as { root, runningChildren, exitedChildren } (root.id)} {@render jobRow(root)} {#if runningChildren.length || exitedChildren.length} <li class="tree-branch"> <ul class="children"> {#each runningChildren as child (child.id)} {@render jobRow(child)} {/each} {#if exitedChildren.length} {@const isOpen = !!expanded[root.id]} <li> <button type="button" class="collapse-toggle" aria-expanded={isOpen} onclick={() => (expanded[root.id] = !expanded[root.id])} > <span class="command" >{isOpen ? '▾' : '▸'} {exitedChildren.length} finished command{exitedChildren.length === 1 ? '' : 's'}</span > </button> {#if isOpen} <ul class="children"> {#each exitedChildren as child (child.id)} {@render jobRow(child)} {/each} </ul> {/if} </li> {/if} </ul> </li> {/if} {/each} </MenuButton>{/if}
{#snippet jobRow(job: Job)} <li data-chip-target="terminal:{sessionId}:{job.id}"> <button onclick={() => ((listed = false), onopen(job.id))}> <span class="command" class:cell={job.kind === 'cell'} title={job.command} >{headline(job.command, job.kind)}</span > <span class="outcome" class:running={job.state === 'running'} class:failed={job.state === 'exited' && (job.exit_code !== 0 || job.timed_out)}>{outcome(job)}</span > <span class="when">{ago(new Date(lastOutput(job)).toISOString())}</span> </button> </li>{/snippet}
<style> .command { overflow: hidden; font-family: var(--mono); font-size: calc(var(--type-sm) * var(--ui-scale)); white-space: nowrap; text-overflow: ellipsis; } .outcome { color: var(--muted); font-size: calc(var(--type-sm) * var(--ui-scale)); font-variant-numeric: tabular-nums; white-space: nowrap; } .outcome.running { color: var(--accent); } .outcome.failed { color: var(--danger); } .command.cell { font-family: var(--sans); } .tree-branch { --rail-line: color-mix(in oklab, var(--text) 20%, transparent); } .children { margin: 0; padding: 0 0 0 18px; list-style: none; } .children > li, .children > li > button { position: relative; } .children > li:not(:last-child)::before, .children > li:last-child > button::before, .children > li > button::after { position: absolute; left: -9px; background: var(--rail-line); content: ''; pointer-events: none; } .children > li:not(:last-child)::before { top: 0; bottom: 0; width: 1px; } .children > li:last-child > button::before { top: 0; bottom: 50%; width: 1px; } .children > li > button::after { top: 50%; width: 7px; height: 1px; } .collapse-toggle { color: var(--muted); }</style>