~ 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
82 lines · 4.1 KB Raw History
// Fetches the lexicons the server bundles into lexicons/, from their
// canonical repositories at their default branches: com.atproto from
// bluesky-social/atproto, app.bsky and chat.bsky from bluesky-social/bsky,
// social.grain and the community.lexicon.location defs Grain refers to from
// grainsocial/grain, xyz.statusphere from
// bluesky-social/statusphere-example-app, and fm.plyr from zzstoatzz/plyr.fm
// (its lexicons/ flat, a file per NSID, and next/backend's appview queries,
// fm.plyr.unspecced.*). Only the documents HUD reads are
// kept: those defining a record or a query (which the query tool calls
// and views draw), those of defs alone, and the procedures a trusted view
// calls (an `Action`'s `call`, a `Messages`' `send`), which call_procedure
// checks input against: a bundled view's (views/*.json) and Grain's, an
// operator's whose views HUD checks against these as it starts
// (harness/apps.mjs, the copy hud-core's tests load), and those Grain's
// app file registers a device for push with (its `push`); no other
// procedure, no subscription or permission set. `just lexicons` runs it;
// what it writes is committed.

import { execFileSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, relative } from 'node:path';
import { appFiles, appViews } from '../harness/apps.mjs';

const SOURCES = [
	{ repo: 'bluesky-social/atproto', dirs: ['lexicons/com/atproto'] },
	{ repo: 'bluesky-social/bsky', dirs: ['lexicons/app/bsky', 'lexicons/chat/bsky'] },
	{ repo: 'grainsocial/grain', dirs: ['lexicons/social/grain', 'lexicons/community/lexicon/location'] },
	{ repo: 'bluesky-social/statusphere-example-app', dirs: ['lexicons/xyz/statusphere'] },
	{ repo: 'zzstoatzz/plyr.fm', dirs: ['lexicons', 'next/backend/lexicons/unspecced'] }
];
const SKIP = new Set(['procedure', 'subscription', 'permission-set']);

/** The procedures the trusted views call: each `{ method, service, input }` under a node's `call` or `send` prop. */
const views = new URL('../views/', import.meta.url).pathname;
const called = new Set();
function calls(node) {
	if (Array.isArray(node)) return node.forEach(calls);
	if (typeof node !== 'object' || node === null) return;
	for (const prop of [node.props?.call, node.props?.send]) if (typeof prop?.method === 'string') called.add(prop.method);
	Object.values(node).forEach(calls);
}
for (const name of readdirSync(views)) if (name.endsWith('.json')) calls(JSON.parse(readFileSync(join(views, name), 'utf8')));
for (const [, doc] of appViews()) calls(doc);
for (const [, app] of appFiles()) for (const k of ['register', 'unregister']) if (typeof app.push?.[k]?.method === 'string') called.add(app.push[k].method);

const out = new URL('../lexicons/', import.meta.url).pathname;
rmSync(out, { recursive: true, force: true });
mkdirSync(out, { recursive: true });

function* files(dir) {
	for (const name of readdirSync(dir)) {
		const path = join(dir, name);
		if (statSync(path).isDirectory()) yield* files(path);
		else if (name.endsWith('.json')) yield path;
	}
}

const tmp = mkdtempSync(join(tmpdir(), 'hud-lexicons-'));
let kept = 0;
try {
	for (const { repo, dirs } of SOURCES) {
		const root = join(tmp, repo.replace('/', '-'));
		mkdirSync(root, { recursive: true });
		const tarball = `https://codeload.github.com/${repo}/tar.gz/HEAD`;
		execFileSync('sh', ['-c', `curl -fsSL ${tarball} | tar -xz -C ${root} --strip-components 1`], { stdio: 'inherit' });
		for (const dir of dirs) {
			for (const path of files(join(root, dir))) {
				const doc = JSON.parse(readFileSync(path, 'utf8'));
				if (doc.lexicon !== 1 || typeof doc.id !== 'string') continue;
				const type = doc.defs?.main?.type;
				if (SKIP.has(type) && !(type === 'procedure' && called.has(doc.id))) continue;
				writeFileSync(join(out, `${doc.id}.json`), JSON.stringify(doc, null, '\t') + '\n');
				kept++;
			}
		}
		console.log(`${repo}: ${dirs.map((d) => relative('lexicons', d) || d).join(', ')}`);
	}
} finally {
	rmSync(tmp, { recursive: true, force: true });
}
console.log(`${kept} lexicons in lexicons/`);