~ 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
182 lines · 7.0 KB Raw History
// A Bluesky feed's answer as app.bsky.feed.getFeed gives it, made up, the
// same each time (a seeded random): posts with pictures of real shapes, link
// cards, quotes (some with media of their own), videos with posters, reposts,
// long and short texts with links and mentions. Pictures are on Bluesky's CDN
// as the AppView names them; the runner (../scroll.mjs) answers those URLs
// with local pictures, `r<k>` in each URL naming the shape (`SHAPES[k]`).

/** The shapes pictures come in, width by height, as people post them. */
export const SHAPES: [number, number][] = [
	[1000, 1000],
	[1000, 750],
	[750, 1000],
	[1000, 563],
	[563, 1000],
	[1000, 667],
	[667, 1000],
	[1000, 524],
	[800, 1000],
	[1000, 400]
];

const WORDS =
	'the a of to and in that is for it on with as was at by this from be have or not are but all an they which one you were her his there can more said what about out when up so if into time some could them see other than then now look only come its over think also back after use two how our work first well way even new want because any these give day most us people feed post photo today city night light music game news open source code design river morning coffee'.split(
		' '
	);

/** A random number generator from a seed (mulberry32). */
function random(seed: number) {
	let a = seed >>> 0;
	return () => {
		a = (a + 0x6d2b79f5) >>> 0;
		let t = a;
		t = Math.imul(t ^ (t >>> 15), t | 1);
		t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
		return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
	};
}

const encoder = new TextEncoder();
const bytes = (s: string) => encoder.encode(s).length;

/** `count` feed items, numbered from `from` (a later page's). */
export function feed(count: number, from = 0): Record<string, unknown>[] {
	const rand = random(7 + from);
	const pick = <T>(list: readonly T[]): T => list[Math.floor(rand() * list.length)];
	const int = (lo: number, hi: number) => lo + Math.floor(rand() * (hi - lo + 1));
	const authors = Array.from({ length: 60 }, (_, i) => {
		const did = `did:plc:author${String(i).padStart(4, '0')}xyzxyzxyz`;
		const handle = `${pick(WORDS)}${pick(WORDS)}${i}.bsky.social`;
		return {
			did,
			handle,
			displayName: i % 7 === 0 ? undefined : `${pick(WORDS)} ${pick(WORDS)}`.replace(/\b\w/g, (c) => c.toUpperCase()),
			avatar: `https://cdn.bsky.app/img/avatar/plain/${did}/bafkreiavatar${i}r0@jpeg`,
			viewer: { muted: false, blockedBy: false },
			labels: [],
			createdAt: '2024-01-01T00:00:00.000Z'
		};
	});
	const text = (words: number) => {
		const facets: Record<string, unknown>[] = [];
		let s = '';
		for (let w = 0; w < words; w++) {
			const word = pick(WORDS);
			const before = s ? ' ' : '';
			if (rand() < 0.03) {
				const mention = pick(authors);
				const start = bytes(s + before);
				s += `${before}@${mention.handle}`;
				facets.push({ index: { byteStart: start, byteEnd: bytes(s) }, features: [{ $type: 'app.bsky.richtext.facet#mention', did: mention.did }] });
			} else if (rand() < 0.02) {
				const start = bytes(s + before);
				const url = `https://example.com/${word}/${int(1, 9999)}`;
				s += `${before}example.com/${word}…`;
				facets.push({ index: { byteStart: start, byteEnd: bytes(s) }, features: [{ $type: 'app.bsky.richtext.facet#link', uri: url }] });
			} else s += before + word;
			if (rand() < 0.08) s += pick(['.', ',', '!', '?']);
			if (rand() < 0.03) s += '\n\n';
		}
		return { text: s, facets };
	};
	const images = (did: string, n: number, key: string) => ({
		$type: 'app.bsky.embed.images#view',
		images: Array.from({ length: n }, (_, j) => {
			const k = int(0, SHAPES.length - 1);
			const [width, height] = SHAPES[k];
			const cid = `bafkrei${key}i${j}r${k}`;
			return {
				thumb: `https://cdn.bsky.app/img/feed_thumbnail/plain/${did}/${cid}@jpeg`,
				fullsize: `https://cdn.bsky.app/img/feed_fullsize/plain/${did}/${cid}@jpeg`,
				alt: rand() < 0.5 ? text(int(4, 20)).text : '',
				aspectRatio: { width, height }
			};
		})
	});
	const external = (did: string, key: string) => ({
		$type: 'app.bsky.embed.external#view',
		external: {
			uri: `https://news.example.org/${pick(WORDS)}/${key}`,
			title: text(int(4, 12)).text,
			description: text(int(10, 40)).text,
			thumb: rand() < 0.8 ? `https://cdn.bsky.app/img/feed_thumbnail/plain/${did}/bafkrei${key}extr7@jpeg` : undefined
		}
	});
	const video = (did: string, key: string) => {
		const k = pick([3, 4, 0, 3, 8]);
		const [width, height] = SHAPES[k];
		const cid = `bafkrei${key}vidr${k}`;
		return {
			$type: 'app.bsky.embed.video#view',
			cid,
			playlist: `https://video.bsky.app/watch/${encodeURIComponent(did)}/${cid}/playlist.m3u8`,
			thumbnail: `https://video.bsky.app/watch/${encodeURIComponent(did)}/${cid}/thumbnail.jpg`,
			url: `https://video.cdn.bsky.app/hls/${did}/${cid}/video.mp4`,
			aspectRatio: { width, height }
		};
	};
	const media = (did: string, key: string) => {
		const r = rand();
		if (r < 0.7) return images(did, pick([1, 1, 1, 2, 3, 4]), key);
		if (r < 0.85) return video(did, key);
		return external(did, key);
	};
	const quote = (key: string) => {
		const author = pick(authors);
		const t = text(int(5, 50));
		return {
			$type: 'app.bsky.embed.record#viewRecord',
			uri: `at://${author.did}/app.bsky.feed.post/q${key}`,
			cid: `bafyreiq${key}`,
			author,
			value: { $type: 'app.bsky.feed.post', text: t.text, facets: t.facets, createdAt: '2026-11-14T10:00:00.000Z', langs: ['en'] },
			labels: [],
			likeCount: int(0, 900),
			replyCount: int(0, 90),
			repostCount: int(0, 90),
			quoteCount: int(0, 9),
			indexedAt: '2026-11-14T10:00:00.000Z',
			embeds: rand() < 0.4 ? [media(author.did, `q${key}`)] : undefined
		};
	};
	return Array.from({ length: count }, (_, n) => {
		const i = from + n;
		const key = String(i).padStart(5, '0');
		const author = pick(authors);
		const t = text(rand() < 0.3 ? int(30, 70) : int(3, 28));
		const r = rand();
		const embed =
			r < 0.4
				? images(author.did, pick([1, 1, 1, 1, 2, 2, 3, 4]), key)
				: r < 0.55
					? external(author.did, key)
					: r < 0.63
						? video(author.did, key)
						: r < 0.73
							? { $type: 'app.bsky.embed.record#view', record: quote(key) }
							: r < 0.78
								? { $type: 'app.bsky.embed.recordWithMedia#view', media: media(author.did, key), record: { record: quote(key) } }
								: undefined;
		const createdAt = new Date(Date.UTC(2026, 10, 15, 12) - i * 97_000).toISOString();
		const reposter = rand() < 0.1 ? pick(authors) : null;
		return {
			post: {
				uri: `at://${author.did}/app.bsky.feed.post/p${key}`,
				cid: `bafyreip${key}`,
				author,
				record: { $type: 'app.bsky.feed.post', text: t.text, facets: t.facets.length ? t.facets : undefined, createdAt, langs: ['en'] },
				embed,
				bookmarkCount: int(0, 50),
				replyCount: int(0, 400),
				repostCount: int(0, 3000),
				likeCount: int(0, 20000),
				quoteCount: int(0, 200),
				indexedAt: createdAt,
				viewer: { bookmarked: false, threadMuted: false, embeddingDisabled: false },
				labels: []
			},
			reason: reposter ? { $type: 'app.bsky.feed.defs#reasonRepost', by: reposter, indexedAt: createdAt } : undefined,
			feedContext: `ctx${key}`
		};
	});
}