Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406<script lang="ts"> import { File, type SelectedLineRange } from '@pierre/diffs'; import { untrack } from 'svelte'; import { diffKey, diffs } from '../anchors'; import { copyText, readContentBatch } from '../api'; import { oneLine, type Trace } from '../burst'; import Markdown from '../chat/Markdown.svelte'; import { parseDisplay, type DisplayMode } from '../display'; import { prefs } from '../prefs.svelte'; import DiffView from './DiffView.svelte';
// Generic display viewer: shows one `display()` activity from a cell's trace // as rendered Markdown, syntax-highlighted code, or a unified diff. On the // canvas its controls sit in `WindowFrame`'s title bar (`toolbar = false`); // in the phone dock or zen desk (`toolbar = true`) it draws a compact bar. let { sessionId, entryId, index = 0, title = '', detail: initialDetail, description = '', windowId = `display:${sessionId}:${entryId}:${index}`, chosen = $bindable<'rendered' | 'raw' | null>(null), toolbar = false, ontitle, onstate, oncomment, }: { sessionId: string; entryId: string; index?: number; title?: string; detail?: string; description?: string; windowId?: string; chosen?: 'rendered' | 'raw' | null; toolbar?: boolean; ontitle?: (title: string) => void; onstate?: (state: { natural: DisplayMode; mode: DisplayMode; text: string; }) => void; oncomment?: ( path: string, change: number, range: SelectedLineRange, ) => void; } = $props();
let loadedTitle = $state(''); let loadedDetail = $state<string | null>(null); let loading = $state(false); let problem = $state(''); let copied = $state(false); let container = $state<HTMLElement>(); let view = $state.raw<File>();
const target = $derived(title || loadedTitle || 'display'); const detail = $derived( initialDetail !== undefined ? initialDetail : (loadedDetail ?? ''), ); const parsed = $derived(parseDisplay(target, detail, description)); const mode = $derived<DisplayMode>( chosen === 'raw' ? 'code' : chosen === 'rendered' ? parsed.natural === 'code' ? 'markdown' : parsed.natural : parsed.natural, ); const rawText = $derived(parsed.body);
$effect(() => { const natural = parsed.natural; const current = mode; const text = rawText || detail; untrack(() => onstate?.({ natural, mode: current, text })); });
$effect(() => { const sid = sessionId; const eid = entryId; const at = index; const given = initialDetail; if (given !== undefined) { loadedDetail = null; problem = ''; loading = false; return; } const abort = new AbortController(); loading = true; problem = ''; loadedDetail = null; readContentBatch(sid, [eid], abort.signal) .then((fields) => { if (abort.signal.aborted) return; const raw = fields.get(eid)?.trace; if (!raw) { problem = 'this display entry is no longer in the transcript'; return; } const trace = JSON.parse(raw) as Trace; const displays = (trace.activities ?? []).filter( (activity) => activity.kind === 'display', ); const found = displays[at]; if (!found) { problem = 'this display block was not found in the trace'; return; } loadedTitle = oneLine(found.target) || 'display'; loadedDetail = found.detail ?? ''; if (!title) untrack(() => ontitle?.(loadedTitle)); }) .catch((failure) => { if (!abort.signal.aborted) problem = String((failure as Error)?.message ?? failure); }) .finally(() => { if (!abort.signal.aborted) loading = false; }); return () => abort.abort(); });
// Register code mode as a context-only diff so line notes on the canvas can quote lines. $effect(() => { if (mode !== 'code' || !rawText) return; const lines = rawText.replace(/\n$/, '').split('\n'); const path = parsed.fileName; const codeKey = diffKey(windowId, path, -1); diffs.set(codeKey, { path, change: -1, diff: `--- a/${path}\n+++ b/${path}\n@@ -1,${lines.length} +1,${lines.length} @@\n${lines.map((line) => ` ${line}`).join('\n')}`, }); return () => diffs.delete(codeKey); });
$effect(() => { if (!container || mode !== 'code' || !rawText) return; const wrap = prefs.wrapCode; const path = parsed.fileName; const shown = new File({ theme: { dark: 'pierre-dark', light: 'pierre-light' }, themeType: untrack(() => prefs.resolved), overflow: wrap ? 'wrap' : 'scroll', disableFileHeader: true, enableGutterUtility: !!oncomment, onGutterUtilityClick: (range) => { oncomment?.(path, -1, range); setTimeout(() => shown.setSelectedLines(null)); }, unsafeCSS: `:host { --diffs-light-bg: var(--well) !important; --diffs-dark-bg: var(--well) !important; --diffs-selection-color-override: var(--accent); --diffs-font-family: var(--mono); --diffs-font-size: calc(12px * var(--canvas-scale)); --diffs-line-height: 19px; }`, }); shown.render({ file: { name: path.split('/').pop() ?? path, contents: rawText.replace(/\n$/, ''), }, containerWrapper: container, }); view = shown; return () => { shown.cleanUp(); view = undefined; }; });
$effect(() => { view?.setThemeType(prefs.resolved); });
async function copy() { await copyText(rawText || detail); copied = true; setTimeout(() => (copied = false), 1400); }</script>
<div class="display-view"> {#if toolbar} <div class="toolbar"> <div class="seg" role="tablist" aria-label="display format"> <button role="tab" aria-selected={mode !== 'code'} class:on={mode !== 'code'} onclick={() => (chosen = 'rendered')} > {parsed.natural === 'diff' ? 'diff' : 'rendered'} </button> <button role="tab" aria-selected={mode === 'code'} class:on={mode === 'code'} onclick={() => (chosen = 'raw')} > {parsed.natural === 'code' ? 'code' : 'raw'} </button> </div> <span class="grow"></span> {#if mode === 'code' || mode === 'diff'} <button class="tool-btn" aria-pressed={prefs.wrapCode} title="wrap long lines" onclick={() => (prefs.wrapCode = !prefs.wrapCode)}>wrap</button > {/if} <button class="tool-btn" onclick={copy} title={copied ? 'copied' : 'copy content'} >{copied ? 'copied' : 'copy'}</button > </div> {/if}
<div class="stream nowheel"> {#if problem} <p class="quiet">(。•︿•。) couldn't load display: {problem}</p> {:else if loading} <p class="quiet">(・ω・)ノ loading display…</p> {:else if !detail} <p class="quiet">nothing displayed</p> {:else if mode === 'diff'} {#if parsed.header} <div class="header"><Markdown text={parsed.header} /></div> {/if} {#each parsed.files as file, partIndex (`${file.path}:${partIndex}`)} <section class="diff-card"> {#if parsed.files.length > 1} <header class="file-head"> <span class="fname" title={file.path} >{file.path.split('/').pop()}</span > <span class="fdir" >{file.path.split('/').slice(0, -1).join('/')}</span > <span class="grow"></span> <span class="counts" ><span class="added">+{file.added}</span> <span class="removed">−{file.removed}</span></span > </header> {/if} <DiffView key={diffKey(windowId, file.path, partIndex)} path={file.path} change={partIndex} diff={file.diff} oncomment={oncomment && ((range) => oncomment(file.path, partIndex, range))} /> </section> {/each} {:else if mode === 'code'} {#if parsed.header && chosen !== 'raw'} <div class="header"><Markdown text={parsed.header} /></div> {/if} <div class="code" data-diff={diffKey(windowId, parsed.fileName, -1)} bind:this={container} ></div> {:else} {#if parsed.header} <p class="description">{parsed.header}</p> {/if} <div class="document"><Markdown text={parsed.body} /></div> {/if} </div></div>
<style> .display-view { display: flex; flex: 1; flex-direction: column; min-width: 0; min-height: 0; color: var(--text); font-size: calc(13px * var(--canvas-scale)); } .toolbar { display: flex; flex: none; align-items: center; gap: 8px; padding: 6px 12px; background: var(--title-bar); } .grow { flex: 1; } .seg { display: inline-flex; gap: 2px; padding: 2px; background: var(--well); border-radius: 8px; } .seg button { padding: 2px 9px; color: var(--muted); font: inherit; font-size: calc(12px * var(--canvas-scale)); background: none; border: 0; border-radius: 6px; cursor: pointer; } .seg button.on { color: var(--text); background: var(--surface); } .tool-btn { padding: 2px 8px; color: var(--muted); font: inherit; font-size: calc(12px * var(--canvas-scale)); background: var(--well); border: 0; border-radius: 6px; cursor: pointer; } .tool-btn:hover, .tool-btn[aria-pressed='true'] { color: var(--text); background: var(--selected); } .stream { flex: 1; min-height: 0; padding: 12px 16px 20px; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; } .header, .description { margin: 0 0 10px; color: var(--muted); font-size: calc(13px * var(--canvas-scale)); line-height: 1.55; } .diff-card + .diff-card { margin-top: 12px; } .file-head { display: flex; align-items: center; gap: 8px; padding: 4px 2px 6px; font-size: calc(12.5px * var(--canvas-scale)); } .fname { font-weight: 620; } .fdir { min-width: 0; overflow: hidden; color: var(--faint); font-size: calc(12px * var(--canvas-scale)); white-space: nowrap; text-overflow: ellipsis; } .counts { flex: none; font-variant-numeric: tabular-nums; } .added { color: var(--added); } .removed { color: var(--danger); } .code { overflow: hidden; border-radius: 10px; } .document { max-width: 74ch; padding: 2px 4px 16px; font-size: calc(14px * var(--canvas-scale)); line-height: 1.65; } .quiet { margin: 24px 0; color: var(--muted); font-size: calc(12.5px * var(--canvas-scale)); text-align: center; }</style>