~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
512 lines · 18.7 KB Raw History
<script lang="ts">
	import { onMount } from 'svelte';
	import { Interactions } from '@hud/shared/interactions';
	import { ACTION_TOOLS, acted, answered, sent, text } from '@hud/shared/result';
	import { setTileContext } from './lib/context';
	import { workspaceEvents } from './lib/live';
	import { clicked, closeMenus, menuOpen, pressed } from './lib/menus';
	import { handleText, memberText, type Canvas } from './lib/model';
	import MiniPlayer from './lib/molecules/MiniPlayer.svelte';
	import SignInAgain from './lib/molecules/SignInAgain.svelte';
	import Toast from './lib/molecules/Toast.svelte';
	import { NowPlaying } from './lib/nowplaying.svelte';
	import ConnectDialog from './lib/organisms/ConnectDialog.svelte';
	import DeleteData from './lib/organisms/DeleteData.svelte';
	import LoginDialog from './lib/organisms/LoginDialog.svelte';
	import Overview from './lib/organisms/Overview.svelte';
	import Palette, { type PaletteItem } from './lib/organisms/Palette.svelte';
	import ShareDialog from './lib/organisms/ShareDialog.svelte';
	import { themeId } from './lib/theme';
	import ThemeBrowser from './lib/organisms/ThemeBrowser.svelte';
	import ThemeEditor from './lib/organisms/ThemeEditor.svelte';
	import Strip from './lib/organisms/Strip.svelte';
	import StripMap from './lib/organisms/StripMap.svelte';
	import TabBar from './lib/organisms/TabBar.svelte';
	import WorkspacePage from './lib/templates/WorkspacePage.svelte';
	import { callTool } from './lib/tools';
	import type { Workspace } from './lib/workspace.svelte';
	import { signOut, switchAccount, upload } from './lib/xrpc';

	// Signed in: the tabs (the canvases open), the strip map of the one on
	// screen, and its strip of columns; each strip, once shown, stays
	// mounted while its tab is out of sight, keeping its scroll and its
	// tiles. One stream hears every open canvas. The keys (none while
	// typing, or while a dialog or menu is open) go round the strip, its
	// columns and the tabs; the palette (K) finds anything. One player, its
	// <audio> here, plays what any tile plays, and the mini player shows it.

	let { ws }: { ws: Workspace } = $props();

	let menu = $state(false);
	let themes = $state(false);
	let renaming = $state<string | null>(null);
	let palette = $state(false);
	let sharing = $state(false);
	let connecting = $state(false);
	/** Signing in to another account beside this one. */
	let addingAccount = $state(false);
	/** Deleting everything HUD keeps for whoever is signed in. */
	let deletingData = $state(false);
	let overview = $state(false);
	let editing = $state(false);
	let browsing = $state(false);
	/** The canvases whose strips are mounted: each shown once. */
	let shown = $state<string[]>([]);

	const current = $derived(ws.current);
	const info = $derived(ws.info(current));
	const ui = $derived(current ? ws.ui(current) : null);
	const strip = $derived(current ? ws.strip(current) : []);

	$effect(() => {
		if (current && !shown.includes(current)) shown = [...shown, current];
	});
	$effect(() => {
		const open = ws.open;
		if (shown.some((id) => !open.includes(id))) shown = shown.filter((id) => open.includes(id));
	});

	/** Whether what an event came from, or what has focus, is a field being typed in. */
	const editable = (target: EventTarget | null) => target instanceof Element && target.closest('input, textarea, select, [contenteditable]') !== null;

	// The focused column takes the keys: a tile's body, to scroll it, or a
	// launcher's search; unless what has focus is in it already, or is a
	// field being typed in elsewhere (a tab's name, as a new canvas opens
	// with its launcher), which keeps it until it is done.
	$effect(() => {
		const key = ui?.focus;
		if (!key || overview || palette || renaming) return;
		requestAnimationFrame(() => {
			const tile = document.querySelector<HTMLElement>(`[data-tile="${CSS.escape(key)}"]`);
			const column = tile ?? document.querySelector<HTMLElement>(`[data-column="${CSS.escape(key)}"]`);
			const active = document.activeElement;
			if (!column || column.contains(active) || editable(active)) return;
			(tile ? tile.querySelector<HTMLElement>('.body') : column.querySelector<HTMLElement>('input'))?.focus({ preventScroll: true });
		});
	});

	const feedback = new Interactions(
		async (feed, interactions) => {
			const result = await callTool('send_interactions', { feed, interactions });
			if (result.isError) throw new Error(text(result) || 'send_interactions failed.');
			return sent(result);
		},
		(message) => console.debug(message)
	);

	const player = new NowPlaying();
	/** The tile what plays came from, while it is open on an open canvas. */
	const source = $derived.by(() => {
		const from = player.source;
		return from && ws.tiles.get(from.canvas)?.some((tile) => tile.id === from.tile) ? from : null;
	});

	function goToSource() {
		if (!source) return;
		overview = false;
		if (source.canvas !== current) void ws.activate(source.canvas);
		ws.show(source.canvas, source.tile);
	}

	setTileContext({
		open: (from, call) => {
			if (current) void ws.openFrom(current, from, call);
		},
		replace: async (tile, call) => (current ? ws.replace(current, tile, call) : false),
		act: async (kind, args) => acted(kind, await callTool(ACTION_TOOLS[kind], $state.snapshot(args) as Record<string, unknown>)),
		tool: async (name, args) => answered(await callTool(name, $state.snapshot(args) as Record<string, unknown>)),
		upload: async (bytes, onprogress) => {
			const answer = (await upload('place.hud.uploadBlob', bytes, onprogress)) as { blob?: unknown } | null;
			if (!answer?.blob) throw new Error('HUD answered the upload without its blob.');
			return answer.blob;
		},
		feedback,
		player
	});

	async function remove(canvas: Canvas) {
		if (!window.confirm(`Delete “${canvas.name}” and its ${canvas.tiles} tile${canvas.tiles === 1 ? '' : 's'}, for everyone it is shared with?`)) return;
		await ws.remove(canvas.id);
	}

	function openOverview() {
		if (!current || strip.length === 0) return;
		closeMenus();
		overview = true;
	}

	function closeOverview(key: string | null) {
		overview = false;
		if (current && (key ?? ui?.focus)) ws.focus(current, (key ?? ui?.focus)!, 'center');
	}

	/** A launcher right of the focused column, or at the end. */
	function launch() {
		if (!current) return;
		overview = false;
		const at = strip.findIndex((column) => column.slots.some((slot) => slot.key === ui?.focus));
		ws.launch(current, at < 0 ? (strip.at(-1)?.key ?? null) : strip[at].key);
	}

	/** Step the focus a column left or right, keeping its place in a stack. */
	function step(by: -1 | 1) {
		if (!current || !ui) return;
		const at = strip.findIndex((column) => column.slots.some((slot) => slot.key === ui.focus));
		const row = at < 0 ? 0 : strip[at].slots.findIndex((slot) => slot.key === ui.focus);
		const next = strip[Math.min(Math.max(at + by, 0), strip.length - 1)];
		if (next) ws.focus(current, next.slots[Math.min(row, next.slots.length - 1)].key, overview ? null : 'center');
	}

	function stepStack(by: -1 | 1) {
		if (!current || !ui) return;
		const column = strip.find((found) => found.slots.some((slot) => slot.key === ui.focus));
		const at = column?.slots.findIndex((slot) => slot.key === ui.focus) ?? -1;
		const next = column?.slots[at + by];
		if (next) ws.focus(current, next.key, 'nearest');
	}

	function switchTab(by: -1 | 1) {
		const at = ws.open.indexOf(current ?? '');
		const next = ws.open[(at + by + ws.open.length) % ws.open.length];
		if (next) void ws.activate(next);
	}

	function keydown(event: KeyboardEvent) {
		pressed(event);
		if (event.defaultPrevented) return;
		const key = event.key;
		const command = event.metaKey || event.ctrlKey;
		if (command && key.toLowerCase() === 'k') {
			event.preventDefault();
			palette = !palette;
			return;
		}
		if (editable(event.target) || document.querySelector('dialog[open]') || menuOpen() || event.altKey || !current) return;
		if (command && key === 'ArrowDown' && ui?.focus) {
			event.preventDefault();
			void ws.stack(current, ui.focus);
			return;
		}
		if (command && key === 'ArrowUp' && ui?.focus) {
			event.preventDefault();
			void ws.unstack(current, ui.focus);
			return;
		}
		if (command) return;
		if (key === '[' || key === ']') return (event.preventDefault(), switchTab(key === '[' ? -1 : 1));
		if (/^[1-9]$/.test(key)) {
			const id = ws.open[Number(key) - 1];
			if (id) {
				event.preventDefault();
				void ws.activate(id);
			}
			return;
		}
		if (key === 'n' || key === 'N') return (event.preventDefault(), (menu = true));
		if (key === 'k' || key === 'K') return (event.preventDefault(), (palette = true));
		if (overview) {
			const by = ({ ArrowLeft: -1, h: -1, ArrowRight: 1, l: 1 } as Record<string, -1 | 1>)[key];
			if (by) return (event.preventDefault(), step(by));
			if (['Escape', 'Enter', 'o', 'O', ' '].includes(key)) return (event.preventDefault(), closeOverview(null));
			return;
		}
		const shift = event.shiftKey;
		const focus = ui?.focus;
		switch (key) {
			case 'ArrowLeft':
			case 'h':
			case 'H':
				event.preventDefault();
				if (shift) void ws.move(current, -1);
				else step(-1);
				break;
			case 'ArrowRight':
			case 'l':
			case 'L':
				event.preventDefault();
				if (shift) void ws.move(current, 1);
				else step(1);
				break;
			case 'ArrowUp':
			case 'ArrowDown':
				if (shift) {
					event.preventDefault();
					stepStack(key === 'ArrowUp' ? -1 : 1);
				}
				break;
			case 'r':
			case 'R':
				if (focus) ws.cycleWidth(current, focus, shift ? -1 : 1);
				break;
			case 'z':
			case 'Z':
				ws.zoom(current);
				break;
			case 'o':
			case 'O':
				openOverview();
				break;
			case 't':
			case 'T':
				event.preventDefault();
				launch();
				break;
			case 'w':
			case 'W':
				if (focus) void ws.close(current, focus);
				break;
			case 'Escape':
				ws.toast = null;
				if (ui?.zoomed) ui.zoomed = null;
				break;
		}
	}

	let pinched = 0;
	let pinchedAt = 0;
	let pinchLock = 0;
	/** A pinch (a wheel with ctrl, as a trackpad sends it) opens the overview, once a gesture; out again closes it. */
	function wheel(event: WheelEvent) {
		if (!event.ctrlKey) return;
		event.preventDefault();
		const now = performance.now();
		if (now < pinchLock) return;
		if (now - pinchedAt > 200) pinched = 0;
		pinchedAt = now;
		pinched += event.deltaY;
		if (!overview && pinched > 24) openOverview();
		else if (overview && pinched < -24) closeOverview(null);
		else return;
		pinchLock = now + 400;
		pinched = 0;
	}

	const items: PaletteItem[] = $derived.by(() => {
		const name = info?.name ?? 'this canvas';
		const actions: PaletteItem[] = [
			{ key: 'new-canvas', section: 'Actions', icon: 'newCanvas', text: 'New canvas', sub: 'In a tab of its own', keys: 'N', run: async () => (renaming = await ws.create()) },
			{ key: 'new-tile', section: 'Actions', icon: 'add', text: 'New tile', sub: 'On this canvas, right of the focused column', keys: 'T', run: launch },
			{ key: 'share', section: 'Actions', icon: 'share', text: `Share “${name}”`, sub: null, run: () => (sharing = true) },
			{ key: 'overview', section: 'Actions', icon: 'overview', text: 'Overview of this canvas', sub: null, keys: 'O', run: openOverview },
			{ key: 'zoom', section: 'Actions', icon: 'zoom', text: 'Zoom the focused column', sub: null, keys: 'Z', run: () => current && ws.zoom(current) },
			{ key: 'connect', section: 'Actions', icon: 'connect', text: 'Connect your AI', sub: null, run: () => (connecting = true) },
			{ key: 'theme', section: 'Actions', icon: 'theme', text: 'Choose a theme', sub: ws.theme?.name ?? 'System', run: () => (themes = true) },
			{ key: 'new-theme', section: 'Actions', icon: 'theme', text: 'New theme', sub: 'Made from another, published for anyone', run: () => (editing = true) },
			{ key: 'browse-themes', section: 'Actions', icon: 'theme', text: 'Browse everyone’s themes', sub: null, run: () => (browsing = true) },
			...ws.accounts.map(
				(other): PaletteItem => ({
					key: `switch:${other.did}`,
					section: 'Actions',
					icon: 'switchAccount',
					text: `Switch to ${handleText(other.did, other.handle)}`,
					sub: 'Signed in on this browser',
					run: () => void switchAccount(other.did).catch((error: unknown) => ws.fail(error))
				})
			),
			{ key: 'add-account', section: 'Actions', icon: 'addAccount', text: 'Add account', sub: 'Sign in to another, to switch between them', run: () => (addingAccount = true) },
			{
				key: 'sign-out',
				section: 'Actions',
				icon: 'signOut',
				text: 'Sign out',
				sub: ws.accounts[0] ? `Then on as ${handleText(ws.accounts[0].did, ws.accounts[0].handle)}` : null,
				run: () => void signOut()
			},
			{ key: 'delete-data', section: 'Actions', icon: 'deleteData', text: 'Delete your HUD data', sub: 'Everything HUD keeps for you, for good', run: () => (deletingData = true) }
		];
		const where = (canvas: Canvas) =>
			canvas.invitedBy !== null
				? `Invitation from ${memberText(canvas, canvas.invitedBy)}`
				: canvas.id === current
					? 'This canvas'
					: ws.open.includes(canvas.id)
						? 'Open tab'
						: canvas.owner === ws.me.did
							? 'Put away'
							: `Shared by ${memberText(canvas, canvas.owner)} · put away`;
		const canvases: PaletteItem[] = ws.canvases.map((canvas) => ({
			key: `canvas:${canvas.id}`,
			section: 'Canvases',
			icon: 'canvas',
			text: canvas.name,
			sub: where(canvas),
			run: () => void ws.activate(canvas.id)
		}));
		const tiles: PaletteItem[] = ws.open.flatMap((id) =>
			(ws.tiles.get(id) ?? []).map((tile) => ({
				key: `tile:${id}:${tile.id}`,
				section: 'Open tiles',
				icon: 'record',
				tile,
				text: ws.title(tile),
				sub: `In ${ws.info(id)?.name ?? 'a canvas'}`,
				run: () => {
					if (id !== current) void ws.activate(id);
					ws.show(id, tile.id);
				}
			}))
		);
		return [...actions, ...canvases, ...tiles];
	});

	onMount(() => {
		const stop = workspaceEvents({
			tile: (event) => ws.heardTile(event),
			presence: (event) => ws.heardPresence(event),
			canvases: () => void ws.load(),
			opened: () => void ws.reload(),
			// Refused: asking again finds a sign-out (the app then shows the card and this stops), else it is tried again.
			refused: async () => {
				await ws.load();
				return true;
			}
		});
		const hidden = () => {
			if (document.visibilityState === 'hidden') void feedback.flush();
		};
		document.addEventListener('visibilitychange', hidden);
		window.addEventListener('wheel', wheel, { passive: false });
		return () => {
			stop();
			document.removeEventListener('visibilitychange', hidden);
			window.removeEventListener('wheel', wheel);
			void feedback.flush();
		};
	});
</script>

<svelte:window onkeydown={keydown} onclick={clicked} />

<audio preload="none" {@attach player.attach}></audio>

{#snippet desktopPlaying()}{@render nowPlaying(false)}{/snippet}
{#snippet phonePlaying()}{@render nowPlaying(true)}{/snippet}
{#snippet nowPlaying(bar: boolean)}
	{@const track = player.track}
	{#if track}
		<MiniPlayer
			title={track.title || 'Untitled'}
			artist={track.subtitle}
			art={track.art}
			paused={player.paused}
			time={player.time}
			duration={player.duration}
			error={player.error}
			{bar}
			ontoggle={() => player.toggle()}
			onnext={() => player.next()}
			ongoto={source ? goToSource : null}
			onstop={() => {
				player.clear();
				// Its buttons gone, focus goes to search beside where it was, not to the page.
				document.querySelector<HTMLElement>('[aria-label="Search everything"]')?.focus();
			}}
		/>
	{/if}
{/snippet}

<WorkspacePage playing={player.track ? phonePlaying : undefined}>
	{#snippet tabs()}
		<TabBar
			{ws}
			bind:menu
			bind:themes
			bind:renaming
			onpalette={() => (palette = true)}
			ondelete={(canvas) => void remove(canvas)}
			onnewtheme={() => (editing = true)}
			onbrowsethemes={() => (browsing = true)}
			onaddaccount={() => (addingAccount = true)}
			ondeletedata={() => (deletingData = true)}
			playing={player.track ? desktopPlaying : undefined}
		/>
	{/snippet}
	{#snippet notice()}
		{#if ws.signInAgain}<SignInAgain who={ws.me.handle ?? ws.me.did} />{/if}
	{/snippet}
	{#snippet map()}
		{#if current && ui && info}
			<StripMap
				columns={strip}
				title={(tile) => ws.title(tile)}
				focus={ui.focus}
				zoomed={ui.zoomed}
				arrived={ui.arrived}
				colours={ui.colours}
				view={ui.view}
				members={info.members}
				me={ws.me.did}
				readonly={ws.readonly(current)}
				onjump={(column) => {
					if (overview) closeOverview(column.slots[0].key);
					else ws.focus(current, column.slots[0].key, 'center');
				}}
				onlaunch={launch}
				onshare={() => (sharing = true)}
			/>
		{/if}
	{/snippet}
	{#snippet stage()}
		{#each shown as id (id)}
			<Strip {ws} canvas={id} me={ws.me} shown={id === current && !overview} onconnect={() => (connecting = true)} />
		{/each}
		{#if overview && current && ui}
			<Overview name={info?.name ?? 'this canvas'} columns={strip} title={(tile) => ws.title(tile)} focus={ui.focus} arrived={ui.arrived} colours={ui.colours} previews={ws.previews} onjump={closeOverview} />
		{/if}
	{/snippet}
	{#snippet toast()}
		{#if ws.toast}
			{#key ws.toast.id}
				<Toast message={ws.toast.message} agent={ws.toast.tone === 'agent'} action={ws.toast.action} onclose={() => (ws.toast = null)} />
			{/key}
		{/if}
	{/snippet}
	{#snippet overlays()}
		{#if palette}
			<Palette me={ws.me} {items} onpick={(row) => current && void ws.openNew(current, row.call)} onclose={() => (palette = false)} />
		{/if}
		{#if sharing && info}
			<ShareDialog
				me={ws.me.did}
				canvas={info}
				here={new Set((ws.present.get(info.id) ?? []).map((actor) => actor.did))}
				members={{
					invite: (actor, role) => ws.invite(info.id, actor, role),
					role: (did, role) => ws.setRole(info.id, did, role),
					remove: (did) => ws.removeMember(info.id, did),
					leave: () => ws.leave(info.id)
				}}
				onclose={() => (sharing = false)}
			/>
		{/if}
		{#if connecting}
			<ConnectDialog onclose={() => (connecting = false)} />
		{/if}
		{#if addingAccount}
			<LoginDialog onclose={() => (addingAccount = false)} />
		{/if}
		{#if deletingData}
			<DeleteData me={ws.me} onclose={() => (deletingData = false)} />
		{/if}
		{#if editing}
			{@const starts = [...(ws.theme?.palette && !ws.theme.key ? [ws.theme] : []), ...ws.themes, ...(ws.mine ?? [])].filter((theme, at, all) => theme.palette && all.findIndex((other) => themeId(other) === themeId(theme)) === at)}
			<ThemeEditor
				{starts}
				from={starts.find((theme) => ws.theme && themeId(theme) === themeId(ws.theme)) ?? starts[0]}
				check={(palette) => ws.checkPalette(palette)}
				onsave={(name, scheme, palette) => ws.makeTheme(name, scheme, palette)}
				onclose={() => (editing = false)}
			/>
		{/if}
		{#if browsing}
			<ThemeBrowser
				me={ws.me.did}
				chosen={ws.theme}
				list={(actor, cursor) => ws.listThemes(actor, cursor)}
				onuse={(theme) => void ws.chooseTheme(theme)}
				ondelete={(theme) => ws.deleteTheme(theme)}
				onclose={() => (browsing = false)}
			/>
		{/if}
	{/snippet}
</WorkspacePage>