~ 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
323 lines · 16.4 KB Raw History
// The gallery on the web: /workspace from web/dist/workspace.html, as
// look.mjs serves it, in front of a stand-in for HUD's appview whose one
// canvas, "Gallery", holds every tile of target/gallery/gallery.json in
// order (`just gallery-data`): each tile's header from its `header`, its
// call answered with its `result`. No PDS, no HUD server. What a tile
// writes (a like, a message sent) is answered and nothing is written;
// a call the gallery has no result for answers that it has none.
//
//   node harness/gallery.mjs           `just gallery`: kept running, the browser opened on it
//   node harness/gallery.mjs --shot    `just gallery --shot`: the strip, a screen at a time, as pictures in
//                                      target/gallery/<desktop|phone>-<light|dark>-NN.png, and each whole strip
//                                      as one sheet, target/gallery/<desktop|phone>-<light|dark>.png; then the
//                                      tiles that drew an error or nothing, said
//
// Every answer the stand-in gives is held to its method's lexicon
// (schemas/), as look.mjs holds them; one that is not is said.

import { execFile } from 'node:child_process';
import { createServer } from 'node:http';
import { createRequire } from 'node:module';
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { views } from './gallery/common.mjs';

const here = dirname(fileURLToPath(import.meta.url));
const root = resolve(here, '..');
const out = join(root, 'target/gallery');
const say = (line) => console.log(`==> ${line}`);
const fail = (why) => {
	say(why);
	process.exit(2);
};

const argv = process.argv.slice(2);
const shot = argv.includes('--shot');
for (const a of argv) if (a !== '--shot') fail(`${a}: no such option (only --shot)`);

const html = join(root, 'web/dist/workspace.html');
if (!existsSync(html)) fail('no web/dist/workspace.html: `just web`');
const file = join(out, 'gallery.json');
if (!existsSync(file)) fail('no target/gallery/gallery.json: `just gallery-data`');
const gallery = JSON.parse(readFileSync(file, 'utf8'));
const { profiles } = JSON.parse(readFileSync(join(here, 'gallery/answers.json'), 'utf8'));
const me = gallery.me;

// The lexicons, to hold every answer to its method's.
const { Lexicons } = createRequire(import.meta.url)('@atproto/lexicon');
const lexicons = new Lexicons(
	readdirSync(join(root, 'schemas'))
		.filter((f) => f.endsWith('.json'))
		.map((f) => JSON.parse(readFileSync(join(root, 'schemas', f), 'utf8')))
		.filter((d) => d.defs?.main?.type !== 'space')
);
const invalid = new Set();

/** Nothing given: null, undefined, "", [] or {}. */
const empty = (v) => v === null || v === undefined || v === '' || (Array.isArray(v) ? v.length === 0 : typeof v === 'object' && Object.keys(v).length === 0);
/** A call as the workspace's callKey reads it: keys in order, empties dropped. */
const canonical = (v) =>
	Array.isArray(v)
		? `[${v.map(canonical).join(',')}]`
		: typeof v === 'object' && v !== null
			? `{${Object.keys(v)
					.filter((k) => !empty(v[k]))
					.sort()
					.map((k) => `${JSON.stringify(k)}:${canonical(v[k])}`)
					.join(',')}}`
			: JSON.stringify(v);
const callKey = (name, args) => `${name} ${canonical(args ?? {})}`;

// The canvas: a tile per gallery tile, in order, keyed by its place.
const CANVAS = 'ca11e7000001';
const at = '2026-11-16T23:30:00.000Z';
const keyOf = new Map(gallery.tiles.map((t, i) => [t.id, (i + 1).toString(16).padStart(12, '0')]));
const idOf = new Map([...keyOf].map(([id, key]) => [key, id]));
let tiles = gallery.tiles.map((t, i) => ({
	key: keyOf.get(t.id),
	author: me.did,
	record: {
		$type: 'place.hud.tile',
		tool: t.call.name,
		arguments: t.call.args,
		title: t.header.title,
		order: String(i + 1),
		by: t.header.agent ? 'agent' : 'person',
		createdAt: at,
		...(t.header.from ? { from: keyOf.get(t.header.from) } : {})
	},
	...(t.header.icon ? { icon: t.header.icon } : {})
}));
const answers = new Map(gallery.tiles.map((t) => [callKey(t.call.name, t.call.args), t.result]));
const canvas = () => ({
	key: CANVAS,
	owner: me.did,
	record: { $type: 'place.hud.canvas', name: 'Gallery', createdAt: at, updatedAt: at },
	members: [{ did: me.did, handle: me.handle, role: 'owner' }],
	tileCount: tiles.length,
	storage: { kind: 'file' },
	viewer: { role: 'owner' }
});
const tab = () => ({ canvas: canvas() });
const ordered = () => [...tiles].sort((a, b) => Number(a.record.order) - Number(b.record.order));
const tileOf = (key) => tiles.find((t) => t.key === key);

const said = (text, structuredContent = {}) => ({ content: [{ type: 'text', text }], structuredContent, isError: false });
/** What a tool answers here: the gallery's result for its call; a write, answered with nothing written. */
function tool(name, args) {
	if (name === 'query' && args?.method === 'app.bsky.actor.getProfiles') {
		const asked = new Set(args.params?.actors ?? []);
		return { ...said('Profiles.', { method: args.method, response: { profiles: profiles.filter((p) => asked.has(p.did) || asked.has(p.handle)) } }), _meta: {} };
	}
	if (name === 'create_record') return said('Done; the gallery writes nothing.', { uri: `at://${me.did}/${args?.collection ?? 'com.example.record'}/3n2gallery000`, cid: 'bafyreigallerygallerygallerygallerygallerygallerygallerygall' });
	if (name === 'delete_record') return said('Done; the gallery deletes nothing.');
	if (name === 'call_procedure') return said('Done; the gallery sends nothing.', { output: {} });
	if (name === 'send_interactions') return said('Told nothing; this is the gallery.', { sent: args?.interactions?.length ?? 0 });
	return answers.get(callKey(name, args)) ?? { isError: true, content: [{ type: 'text', text: `The gallery has no result for ${name} ${JSON.stringify(args ?? {})}.` }] };
}

/** One launcher an app, from the trusted views that name their app (the bundled ones and the operator's), so each tile's
 * header names its app as a signed-in person's workspace would (its launchers name the apps). Opened, each lists its app's records. */
const launchers = (() => {
	const seen = new Map();
	for (const [, view] of [...views].sort(([a], [b]) => (a < b ? -1 : 1))) {
		const nsid = view.method ?? view.collection;
		if (typeof view.app !== 'string' || typeof nsid !== 'string') continue;
		const [tld, name] = nsid.split('.');
		const domain = `${name}.${tld}`;
		if (seen.has(domain) || !view.method || !view.service) continue;
		seen.set(domain, { app: view.app, domain, title: view.app, method: view.method, service: view.service, params: {} });
	}
	return [...seen.values()];
})();

/** Each method the page asks, as the appview answers it here. */
const methods = {
	getWorkspace: () => ({ me, open: [tab()], current: CANVAS, launchers }),
	listCanvases: () => ({ canvases: [canvas()] }),
	getCanvas: (q) => (q.canvas === CANVAS ? { canvas: canvas(), tiles: ordered() } : null),
	openCanvas: () => ({ canvas: canvas(), open: [tab()], current: CANVAS }),
	setTabOrder: () => ({ open: [tab()], current: CANVAS }),
	removeTile: ({ tile }) => ((tiles = tiles.filter((t) => t.key !== tile)), {}),
	setTileWidth: ({ tile, width }) => {
		const t = tileOf(tile);
		t.record.width = width;
		return { tile: t };
	},
	placeTile: ({ tile }) => ({ tile: tileOf(tile) }),
	updateTile: ({ tile, title }) => {
		const t = tileOf(tile);
		if (title !== undefined) t.record.title = title;
		return { tile: t };
	},
	callTool: ({ name, arguments: args }) => ({ result: tool(name, args) })
};

/** A minute of silence as a WAV (8 kHz, 8-bit, mono), for a player's blob. */
const silence = (() => {
	const n = 8000 * 60;
	const wav = Buffer.alloc(44 + n, 128);
	wav.write('RIFF', 0);
	wav.writeUInt32LE(36 + n, 4);
	wav.write('WAVEfmt ', 8);
	wav.writeUInt32LE(16, 16);
	wav.writeUInt16LE(1, 20);
	wav.writeUInt16LE(1, 22);
	wav.writeUInt32LE(8000, 24);
	wav.writeUInt32LE(8000, 28);
	wav.writeUInt16LE(1, 32);
	wav.writeUInt16LE(8, 34);
	wav.write('data', 36);
	wav.writeUInt32LE(n, 40);
	return wav;
})();

const streams = new Set();
const server = createServer(async (req, res) => {
	const url = new URL(req.url, 'http://gallery');
	if (url.pathname === '/' || url.pathname === '/workspace') return res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' }).end(readFileSync(html));
	// A blob, as the server that recorded the answers proxied it: an image from Bluesky's CDN; one a player asks for by a range, a minute of silence.
	const blob = url.pathname.match(/^\/blob\/(did:[^/]+)\/([a-z0-9]+)$/);
	if (blob && req.headers.range) return res.writeHead(200, { 'content-type': 'audio/wav', 'content-length': silence.length }).end(silence);
	if (blob) return res.writeHead(302, { location: `https://cdn.bsky.app/img/feed_fullsize/plain/${blob[1]}/${blob[2]}@jpeg` }).end();
	const icon = url.pathname.match(/^\/icon\/([a-z0-9.-]+)$/);
	if (icon) {
		const png = join(out, 'icons', `${icon[1]}.png`);
		return existsSync(png) ? res.writeHead(200, { 'content-type': 'image/png' }).end(readFileSync(png)) : res.writeHead(404).end();
	}
	const nsid = url.pathname.match(/^\/xrpc\/place\.hud\.(\w+)$/)?.[1];
	if (nsid === 'getWorkspaceEvents') {
		res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' });
		res.write(`data: ${JSON.stringify({ $type: 'place.hud.defs#presenceEvent', canvas: CANVAS, present: [{ did: me.did, handle: me.handle }] })}\n\n`);
		streams.add(res);
		req.on('close', () => streams.delete(res));
		return;
	}
	const method = nsid && methods[nsid];
	if (!method) return res.writeHead(404, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'MethodNotImplemented', message: `The gallery does not do ${url.pathname}.` }));
	let input = Object.fromEntries(url.searchParams);
	if (req.method === 'POST') {
		let body = '';
		for await (const chunk of req) body += chunk;
		input = JSON.parse(body || '{}');
	}
	const answer = method(input);
	if (!answer) return res.writeHead(404, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'NotFound', message: 'No such canvas.' }));
	// A tile's icon is an absolute URL, this stand-in's; and so is a recorded blob's, and an app's icon in a tree.
	const body = JSON.parse(
		JSON.stringify(answer)
			.replaceAll('"icon":"/icon/', `"icon":"${base}/icon/`)
			.replace(/https?:\/\/(localhost|127\.0\.0\.1)(:\d+)?\/(blob|icon)\//g, (_, _host, _port, path) => `${base}/${path}/`)
	);
	const id = `place.hud.${nsid}`;
	try {
		lexicons.assertValidXrpcOutput(id, body);
	} catch (e) {
		if (!invalid.has(id)) say(`${id}: the answer is not as its lexicon says, so no server could give it: ${e.message}`);
		invalid.add(id);
	}
	res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(body));
});

const port = shot ? 0 : Number(process.env.GALLERY_PORT ?? 8799);
await new Promise((done, failed) => server.once('error', failed).listen(port, '127.0.0.1', done)).catch((e) => fail(`127.0.0.1:${port}: ${e.message} (GALLERY_PORT=<port> for another)`));
const base = `http://127.0.0.1:${server.address().port}`;

if (!shot) {
	const page = `${base}/workspace`;
	say(`the gallery, ${gallery.tiles.length} tiles, at ${page} (ctrl-c to stop)`);
	execFile(process.platform === 'darwin' ? 'open' : 'xdg-open', [page], (e) => e && say(`open ${page} yourself: ${e.message}`));
} else {
	const { chromium } = await import('playwright');
	const devices = {
		desktop: { viewport: { width: 1440, height: 900 }, deviceScaleFactor: 2 },
		phone: { viewport: { width: 390, height: 844 }, deviceScaleFactor: 3, isMobile: true, hasTouch: true }
	};
	mkdirSync(out, { recursive: true });
	for (const f of readdirSync(out)) if (/^(desktop|phone)-(light|dark)(-\d+)?\.png$/.test(f)) rmSync(join(out, f));
	/** What each tile drew, by gallery id, as seen in any picture: an error's words, or nothing drawn. */
	const problems = new Map();
	const browser = await chromium.launch();
	try {
		for (const project of ['desktop-light', 'desktop-dark', 'phone-light', 'phone-dark']) {
			const [device, scheme] = project.split('-');
			const context = await browser.newContext({ ...devices[device], colorScheme: scheme, locale: 'en-US', timezoneId: 'America/Los_Angeles', reducedMotion: 'reduce' });
			const page = await context.newPage();
			await page.clock.setFixedTime(new Date('2026-11-17T01:00:00.000Z'));
			await page.goto(`${base}/workspace`, { waitUntil: 'load' });
			const strip = page.locator('.strip.shown');
			await strip.waitFor();
			// A screen at a time, the strip not snapping back between them.
			await page.addStyleTag({ content: '.strip.shown { scroll-snap-type: none !important; scroll-behavior: auto !important; }' });
			const settle = async () => {
				await page.waitForTimeout(700);
				await page.waitForFunction(() => !document.querySelector('.strip.shown [aria-busy="true"]'), null, { timeout: 10000 }).catch(() => {});
				await page.waitForLoadState('networkidle', { timeout: 5000 }).catch(() => {});
				await page.waitForTimeout(300);
			};
			// Each screen starts at a column's left edge: the first one the screen before did not show whole.
			const columns = await strip.evaluate((el) => {
				el.scrollLeft = 0;
				const left = el.getBoundingClientRect().left;
				return { room: el.clientWidth, spans: [...el.querySelectorAll('[data-column]')].map((c) => [c.getBoundingClientRect().left - left, c.getBoundingClientRect().right - left]) };
			});
			const starts = [];
			for (let i = 0; i < columns.spans.length; ) {
				const left = i === 0 ? 0 : (columns.spans[i - 1][1] + columns.spans[i][0]) / 2;
				starts.push(left);
				const next = columns.spans.findIndex(([, right], j) => j > i && right > left + columns.room + 1);
				if (next < 0) break;
				i = next;
			}
			const pages = [];
			const pictured = new Set();
			for (const [i, left] of starts.entries()) {
				await strip.evaluate((el, x) => (el.scrollLeft = x), left);
				await settle();
				const seen = await page.evaluate(() =>
					[...document.querySelectorAll('.strip.shown article.tile')]
						.filter((t) => {
							const r = t.getBoundingClientRect();
							return r.right > 0 && r.left < innerWidth;
						})
						.map((t) => {
							const body = t.querySelector('.body');
							const alert = body?.querySelector('[role="alert"], .error');
							const drawn = (body?.innerText.trim().length ?? 0) > 0 || !!body?.querySelector('img, video, audio, canvas, svg, iframe');
							return { key: t.dataset.tile, alert: alert?.innerText.trim() ?? null, drawn, busy: body?.getAttribute('aria-busy') === 'true' };
						})
				);
				for (const s of seen) {
					pictured.add(s.key);
					const id = idOf.get(s.key) ?? s.key;
					const what = s.alert ? `draws an error: ${s.alert.replace(/\s+/g, ' ').slice(0, 200)}` : s.busy ? 'still loading' : !s.drawn ? 'draws nothing' : null;
					if (what) problems.set(`${id} ${what}`, [...(problems.get(`${id} ${what}`) ?? []), project]);
				}
				const picture = join(out, `${project}-${String(i + 1).padStart(2, '0')}.png`);
				await page.screenshot({ path: picture, animations: 'disabled', caret: 'hide' });
				pages.push(picture);
			}
			await context.close();
			for (const key of keyOf.values()) if (!pictured.has(key)) problems.set(`${idOf.get(key)} is in no picture`, [...(problems.get(`${idOf.get(key)} is in no picture`) ?? []), project]);

			// The whole strip as one sheet: its screens in order, in rows, each at its CSS size.
			const [across, cell] = device === 'desktop' ? [2, 1440] : [8, 390];
			const sheet = await browser.newPage({ viewport: { width: across * cell, height: 900 }, deviceScaleFactor: 1 });
			const images = pages.map((p) => `<img src="data:image/png;base64,${readFileSync(p).toString('base64')}" style="width:${cell}px;display:block">`).join('');
			await sheet.setContent(`<body style="margin:0;background:${scheme === 'dark' ? '#000' : '#fff'}"><div style="display:grid;grid-template-columns:repeat(${across},${cell}px)">${images}</div></body>`);
			await sheet.waitForFunction(() => [...document.images].every((i) => i.complete));
			const whole = join(out, `${project}.png`);
			await sheet.screenshot({ path: whole, fullPage: true });
			await sheet.close();
			console.log(`${whole} (${pages.length} screens, ${project}-01.png to -${String(pages.length).padStart(2, '0')}.png)`);
		}
	} finally {
		await browser.close();
		for (const res of streams) res.end();
		server.close();
	}
	for (const [what, where] of problems) say(`${what} (${where.join(', ')})`);
	say(`${problems.size ? `${problems.size} tiles drew an error or nothing` : 'every tile drew'}; pictures in ${out}`);
	process.exit(invalid.size ? 1 : 0);
}