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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547<script lang="ts"> import { untrack } from 'svelte'; import type { Session } from '../api'; import { stateOf, watchFamily } from '../agents'; import { BAR, CARD, fitView, hoverLineage, layoutTree, shapeOf, withoutInactive, type Placed } from '../agentTree'; import { boards } from '../boards.svelte'; import { busy } from '../live.svelte'; import LiveFace from '../LiveFace.svelte'; import { pixels, zoomFactor } from '../wheel';
// One family as a tree: active branches growing right, closed folds and // finished branches balanced left, with closed leaf sets folded per parent // into a `▸ N closed agents` deck that unfolds in place without shrinking // the active view. Hovering any card highlights its ancestor chain and // steps down gently across descendant depths. let { sessionId, onopen, onfit }: { sessionId: string; onopen: (agent: Session) => void; onfit?: (content: { width: number; height: number }) => void } = $props();
/** The room around the tree when it is fitted. */ const PAD = 48; /** Inside a card, either side. */ const CARD_PAD = 12; /** How far a view may zoom, how far fitting may enlarge it, and the size the window is made for it at. */ const SCALE = { min: 0.25, max: 2.5, fit: 1.35, home: 1.15 };
let family = $state.raw<Session[]>([]); let loaded = $state(false); let failed = $state(false); let surface = $state<HTMLElement>(); let box = $state({ width: 0, height: 0 }); let view = $state({ x: 0, y: 0, k: 1 }); let fitted = true; let drag = $state<{ x: number; y: number; at: { x: number; y: number } } | null>(null); let expanded = $state.raw<Set<string>>(new Set()); let hoveredId = $state<string | null>(null);
$effect(() => watchFamily(sessionId, (found) => { if (found) family = found; loaded = !!found; failed = !found; }), );
// How wide a card wants to be: as wide as its name and short model or activity // snippet (up to a limit), measured in the fonts the card is drawn in. let fonts = $state(0); let ruler: CanvasRenderingContext2D | null = null; $effect(() => { document.fonts?.ready.then(() => fonts++); });
function shortModel(model: string | null | undefined): string { if (!model) return 'no model yet'; return model.replace(/^claude-/, ''); }
function snippetOf(session: Session, hideModel = false): string { const progress = session.current_progress?.findLast((item) => item.title?.trim()); if (progress?.title) return progress.title.trim(); const line = session.activity?.lines.findLast((item) => item.text.trim()); if (line?.text) return line.text.trim(); if (session.role?.trim()) return session.role.trim(); if (hideModel) return 'starting…'; return shortModel(session.model); }
function makeWanted() { fonts; ruler ??= document.createElement('canvas').getContext('2d'); if (!surface || !ruler) return () => CARD.minWidth; const style = getComputedStyle(surface); const sans = style.fontFamily; const mono = style.getPropertyValue('--mono'); const measure = (text: string, font: string) => { ruler!.font = font; return ruler!.measureText(text).width; }; return (session: Session) => { const name = measure(session.name || 'untitled session', `600 12px ${sans}`); if (session.id.startsWith('fold:')) { const sub = measure(shortModel(session.model), `10px ${mono}`); return Math.max(name, sub) + CARD_PAD * 2 + 16; } const tag = measure(shortModel(session.model), `10px ${mono}`); const top = name + (session.closed ? 16 : Math.max(48, tag + 14)); const sub = session.closed ? 0 : Math.min(220, measure(snippetOf(session), `11px ${sans}`)); return Math.max(top, sub) + CARD_PAD * 2 + 8; }; }
const HIDE_KEY = 'albedo.agents.hideInactive'; const remembered = () => { try { return localStorage.getItem(HIDE_KEY) === '1'; } catch { return false; } }; let hideInactive = $state(remembered()); function toggleInactive() { hideInactive = !hideInactive; try { localStorage.setItem(HIDE_KEY, hideInactive ? '1' : '0'); } catch { // Not remembered, still applied. } } const active = $derived(withoutInactive(family, sessionId, (session) => stateOf(session) === 'working')); const hasInactive = $derived(active.length < family.length); const shownFamily = $derived(hideInactive ? active : family); const hidden = $derived(family.length - shownFamily.length);
const tree = $derived(layoutTree(shownFamily, sessionId, makeWanted(), expanded)); const lineage = $derived(hoverLineage(tree.nodes, hoveredId)); const working = $derived(new Set(family.filter(busy).map((session) => session.id))); // The agents that have a window on this board. const shown = $derived(new Set(boards.board?.list.flatMap((node) => (node.type === 'session' ? [node.data.sessionId] : [])) ?? []));
// The window sizes itself to hold the active/core tree (`fitBox`, ignoring unfolded closed pills) with its room around it. $effect(() => { if (tree.nodes.length) { onfit?.({ width: tree.fitBox.width * SCALE.home + PAD * 2, height: tree.fitBox.height * SCALE.home + PAD * 2 + BAR, }); } });
const unscale = () => (surface && surface.clientWidth ? surface.getBoundingClientRect().width / surface.clientWidth : 1);
function fit() { if (!tree.nodes.length || !box.width || !box.height) return; view = fitView(box, tree.fitBox, PAD, BAR, SCALE.min, SCALE.fit); fitted = true; }
$effect(() => { tree; box.width; box.height; if (fitted) fit(); });
const AUTO_KEY = 'albedo.agents.autoFit'; const rememberedAuto = () => { try { return localStorage.getItem(AUTO_KEY) !== '0'; } catch { return true; } }; let auto = $state(rememberedAuto()); function toggleAuto() { auto = !auto; try { localStorage.setItem(AUTO_KEY, auto ? '1' : '0'); } catch { // Not remembered, still applied. } if (auto) fit(); } let shape = ''; $effect(() => { const now = shapeOf(tree.nodes); if (now !== shape && shape !== '' && auto) untrack(fit); shape = now; });
function toggleFold(node: Placed) { if (!node.fold) return; const prevX = node.x; const prevY = node.y; const parentId = node.fold.parentId; const next = new Set(expanded); if (next.has(parentId)) next.delete(parentId); else next.add(parentId); expanded = next; fitted = false; queueMicrotask(() => { const after = tree.nodes.find((candidate) => candidate.id === node.id); if (after) { view = { ...view, x: view.x + (prevX - after.x) * view.k, y: view.y + (prevY - after.y) * view.k, }; } }); }
function zoomAt(factor: number, point: { x: number; y: number }) { const k = Math.min(SCALE.max, Math.max(SCALE.min, view.k * factor)); const ratio = k / view.k; view = { k, x: point.x - (point.x - view.x) * ratio, y: point.y - (point.y - view.y) * ratio }; fitted = false; }
const zoomBy = (factor: number) => zoomAt(factor, { x: box.width / 2, y: box.height / 2 });
$effect(() => { if (!surface) return; const element = surface; const wheel = (event: WheelEvent) => { if (event.defaultPrevented) return; event.preventDefault(); const scale = unscale(); if (event.ctrlKey || event.metaKey) { const rect = element.getBoundingClientRect(); zoomAt(zoomFactor(event), { x: (event.clientX - rect.left) / scale, y: (event.clientY - rect.top) / scale }); } else { view = { ...view, x: view.x - pixels(event, event.deltaX) / scale, y: view.y - pixels(event, event.deltaY) / scale }; fitted = false; } }; element.addEventListener('wheel', wheel, { passive: false }); return () => element.removeEventListener('wheel', wheel); });
function grab(event: PointerEvent) { if (event.button !== 0 || (event.target as Element).closest('button')) return; // Pointer capture alone still lets the browser extend a selection into other windows. event.preventDefault(); drag = { x: event.clientX, y: event.clientY, at: { x: view.x, y: view.y } }; (event.currentTarget as Element).setPointerCapture(event.pointerId); }
function move(event: PointerEvent) { if (!drag) return; const scale = unscale(); view = { ...view, x: drag.at.x + (event.clientX - drag.x) / scale, y: drag.at.y + (event.clientY - drag.y) / scale }; fitted = false; }</script>
<div class="tree nowheel holds-wheel zooms-itself" class:moving={drag !== null} bind:this={surface} bind:clientWidth={box.width} bind:clientHeight={box.height} role="presentation" onpointerdown={grab} onpointermove={move} onpointerup={() => (drag = null)} onpointercancel={() => (drag = null)}> {#if tree.nodes.length} <div class="stage" class:has-hover={hoveredId !== null} style:width="{tree.width}px" style:height="{tree.height}px" style:transform="translate({view.x}px, {view.y}px) scale({view.k})" > <svg width={tree.width} height={tree.height} aria-hidden="true"> {#each tree.links as link (link.id)} {@const hop = lineage.downEdgeHop.get(link.id)} <path d={link.d} class:hop-up={lineage.upEdges.has(link.id)} class:hop-down-1={hop === 1} class:hop-down-2={hop === 2} class:hop-down-3={hop === 3} /> {/each} </svg> {#each tree.nodes as node (node.id)} {@const { session, x, y, width, height, compact, fold, isLead } = node} {@const nodeHop = lineage.downNodeHop.get(node.id)} <button class="agent" class:working={!fold && working.has(session.id)} class:closed={!fold && session.closed} class:compact class:fold-card={!!fold} class:open-hub={!!fold?.expanded} class:shown={!fold && shown.has(session.id)} class:node-hovered={hoveredId === node.id} class:node-ancestor={lineage.ancestors.has(node.id)} class:node-down-1={nodeHop === 1} class:node-down-2={nodeHop === 2} class:node-down-3={nodeHop === 3} style:left="{x}px" style:top="{y}px" style:width="{width}px" style:height="{height}px" title={fold ? `${fold.count} closed agents` : `${session.name || 'untitled session'} · ${session.model ?? 'no model yet'}`} onmouseenter={() => (hoveredId = node.id)} onmouseleave={() => { if (hoveredId === node.id) hoveredId = null; }} onclick={() => (fold ? toggleFold(node) : onopen(session))} > {#if fold} <span class="name fold-name">{session.name}</span> <span class="fold-sub">{shortModel(fold.model)}</span> {:else if compact} <span class="name">{session.name || 'untitled session'}</span> {#if !node.parentId?.startsWith('fold:')}<span class="meta-tag">{stateOf(session)}</span>{/if} {:else} {@const topShowsModel = !isLead && working.has(session.id)} <span class="top-row"> <span class="name">{session.name || 'untitled session'}</span> {#if isLead && working.has(session.id)} <span class="face-inline"><LiveFace {session} /></span> {:else} <span class="meta-tag">{topShowsModel ? shortModel(session.model) : stateOf(session)}</span> {/if} </span> <span class="activity-snip">{snippetOf(session, topShowsModel)}</span> {/if} </button> {/each} </div> <div class="controls"> {#if hasInactive || hideInactive} <button class="toggle" aria-pressed={hideInactive} title={hideInactive ? 'show every agent again' : 'hide the agents that are not working'} onclick={toggleInactive}> {hideInactive ? `show inactive${hidden ? ` (${hidden})` : ''}` : 'hide inactive'} </button> {/if} <button aria-label="Zoom out" title="zoom out" onclick={() => zoomBy(1 / 1.25)}>−</button> <button aria-label="Zoom in" title="zoom in" onclick={() => zoomBy(1.25)}>+</button> <button class="toggle" aria-pressed={auto} title={auto ? 'stop fitting the tree again as agents come and go' : 'fit the tree again whenever agents come or go'} onclick={toggleAuto}>auto-fit</button> <button class="fit" aria-label="Fit the tree" title="fit the tree" onclick={fit}>fit</button> </div> {:else} <p class="quiet" class:error={failed}>{loaded ? 'no agents here (・_・ )' : failed ? "(。•︿•。) couldn't read the agents" : 'reading the agents… (・ω・)ノ'}</p> {/if}</div>
<style> .tree { position: relative; flex: 1; min-height: 0; overflow: hidden; background: var(--well); cursor: grab; touch-action: none; user-select: none; } .tree.moving { cursor: grabbing; } .stage { position: absolute; top: 0; left: 0; transform-origin: 0 0; } svg { position: absolute; inset: 0; pointer-events: none; overflow: visible; } path { fill: none; stroke: var(--muted); stroke-width: 1.3; stroke-linecap: round; stroke-linejoin: round; opacity: 0.22; transition: opacity 150ms ease, stroke 150ms ease, stroke-width 150ms ease; } .stage.has-hover path { opacity: 0.14; } .stage.has-hover path.hop-up { stroke: var(--accent); stroke-width: 1.75; opacity: 0.9; } .stage.has-hover path.hop-down-1 { stroke: var(--accent); stroke-width: 1.55; opacity: 0.62; } .stage.has-hover path.hop-down-2 { stroke: var(--accent); stroke-width: 1.4; opacity: 0.42; } .stage.has-hover path.hop-down-3 { stroke: var(--accent); stroke-width: 1.35; opacity: 0.3; } .agent { position: absolute; display: grid; align-content: center; gap: 2px; padding: 0 12px; color: var(--text); font: inherit; text-align: left; background: var(--surface); border: 0; border-radius: 11px; cursor: pointer; transition: opacity 150ms ease, background-color 140ms ease, box-shadow 140ms ease; } .agent:hover { background-image: linear-gradient(var(--hover), var(--hover)); } .stage.has-hover .agent { opacity: 0.45; } .stage.has-hover .agent.node-ancestor { opacity: 1; } .stage.has-hover .agent.node-down-1 { opacity: 0.95; } .stage.has-hover .agent.node-down-2 { opacity: 0.85; } .stage.has-hover .agent.node-down-3 { opacity: 0.76; } .stage.has-hover .agent.node-hovered { opacity: 1; background-image: linear-gradient(var(--hover), var(--hover)); box-shadow: 0 6px 20px rgb(0 0 0 / 0.28), 0 0 0 1.5px color-mix(in oklab, var(--accent) 55%, transparent); } .agent.shown { box-shadow: 0 0 0 2px var(--focus-ring); } /* Opaque dimmed surface for closed pills so lines never bleed through. */ .agent.closed { background: color-mix(in oklab, var(--surface) 58%, var(--well)); color: color-mix(in oklab, var(--text) 68%, var(--muted)); } .agent.compact { display: flex; align-items: center; justify-content: space-between; gap: 8px; border-radius: 9px; padding: 0 10px; } .agent.fold-card { background: color-mix(in oklab, var(--surface) 78%, var(--well)); box-shadow: 3px 3px 0 color-mix(in oklab, var(--surface) 52%, var(--well)), 6px 6px 0 color-mix(in oklab, var(--surface) 30%, var(--well)); align-content: center; } .agent.fold-card.open-hub { background: var(--surface); box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--accent) 45%, transparent); } .top-row { display: flex; align-items: baseline; justify-content: space-between; gap: 6px; min-width: 0; } /* The card lines stay at their base size: cards are measured and laid out in px (agentTree.ts), and the tree's own zoom is what makes them bigger. */ .name { overflow: hidden; font-size: 12px; font-weight: 600; white-space: nowrap; text-overflow: ellipsis; } .fold-name { color: var(--accent); } .fold-sub { overflow: hidden; color: var(--muted); font-family: var(--mono); font-size: 10px; white-space: nowrap; text-overflow: ellipsis; } .face-inline { flex-shrink: 0; color: var(--accent); font-size: 10.5px; white-space: nowrap; } .meta-tag { flex-shrink: 0; color: var(--faint); font-family: var(--mono); font-size: 10px; white-space: nowrap; } .activity-snip { overflow: hidden; color: var(--muted); font-size: 11px; white-space: nowrap; text-overflow: ellipsis; } .node-hovered .activity-snip { color: var(--text); } .controls { position: absolute; right: 12px; bottom: 12px; display: flex; gap: 2px; padding: 4px; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); } .controls button { min-width: 28px; height: 28px; padding: 0 8px; color: var(--muted); font: inherit; font-size: calc(13px * var(--canvas-scale)); background: none; border: 0; border-radius: 8px; cursor: pointer; transition: background-color 120ms ease, color 120ms ease; } .controls button:hover { color: var(--text); background: var(--hover); } .controls .fit, .controls .toggle { font-size: calc(12px * var(--canvas-scale)); } .controls .toggle[aria-pressed='true'] { color: var(--accent); } .quiet { margin: 20px; color: var(--muted); } .quiet.error { color: var(--danger); }</style>