chadtmiller.com/hud
A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts
place.hud.hudkit 0.3.1 Latest 5 packages
0
0
// A tile's mini form, for the overview: a line of what it shows and a
// picture from it, where it has one, read from the result its tile drew.
// What it shows comes first: a list's first item, a record's value, an
// account's profile. In it, the words nearest the top (a post's text before
// its link card's title, a document's title before its description), and a
// picture of what it holds (a post's image or link card, a cover, a banner)
// before anyone's avatar.
import type { Source } from '@hud/renderer';
import { accountOf, type View } from '@hud/shared/result';
export type Preview = { text: string | null; image: Source | null };
const isObject = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value);
/** The keys what it says is found under, the first more telling; then, failing those, the keys a name is (an author's, say). */
const WORDS = ['text', 'title', 'description'];
const NAMES = ['displayName', 'name'];
/** The keys a picture of what it holds is found under, then a face's. */
const PICTURES = ['thumb', 'fullsize', 'image', 'cover', 'coverImage', 'banner'];
const FACES = ['avatar'];
/** How much of a result is looked through, at most. */
const LOOKED = 600;
const isUrl = (value: unknown): value is string => typeof value === 'string' && /^https?:\/\//.test(value);
/** A blob HUD made fetchable, drawn small: its `thumb`, else its `url`, with its `proxy` to fall back on. */
function blobSource(blob: Record<string, unknown>): Source {
const src = typeof blob.thumb === 'string' ? blob.thumb : (blob.url as string);
return { src, fallback: typeof blob.proxy === 'string' && blob.proxy !== src ? blob.proxy : null };
}
/** The best of each, nearest the top: words before a name, by the order of WORDS among those as near; a picture of what it holds before a face. */
function find(data: unknown): Preview {
let text: { tier: number; depth: number; rank: number; value: string } | null = null;
let picture: { tier: number; value: Source } | null = null;
let queue: unknown[] = [data];
let looked = 0;
for (let depth = 0; queue.length > 0 && looked < LOOKED; depth++) {
const next: unknown[] = [];
for (const node of queue) {
if (++looked > LOOKED) break;
const entries = Array.isArray(node) ? node.map((value) => [null, value] as const) : isObject(node) ? Object.entries(node) : [];
for (const [key, value] of entries) {
if (key !== null) {
const said = WORDS.includes(key) ? 0 : NAMES.includes(key) ? 1 : -1;
const rank = said === 0 ? WORDS.indexOf(key) : NAMES.indexOf(key);
if (said >= 0 && typeof value === 'string' && value.trim() && (!text || said < text.tier || (said === text.tier && depth === text.depth && rank < text.rank)))
text = { tier: said, depth, rank, value: value.trim() };
// A picture: a URL as a view gives it, or a blob's that HUD made fetchable, its thumbnail (the overview draws it small); of a list of them, the first.
const tier = PICTURES.includes(key) ? 0 : FACES.includes(key) ? 1 : -1;
const first = Array.isArray(value) ? value[0] : value;
const found = tier < 0 ? null : isUrl(first) ? { src: first, fallback: null } : isObject(first) && isUrl(first.url) ? blobSource(first) : null;
if (found && (!picture || tier < picture.tier)) picture = { tier, value: found };
}
if (typeof value === 'object' && value !== null) next.push(value);
}
}
queue = next;
}
return { text: text?.value ?? null, image: picture?.value ?? null };
}
/** What a tile's result shows, in short. */
export function preview(view: View): Preview {
switch (view.renderer) {
case 'tree': {
// An account's page: its profile.
const account = accountOf(view);
if (account) return find(account.profile?.value ?? null);
// The page's first record's value; of a query, the answer's first item (the first of its first list, a feed's `feed`).
const data = isObject(view.data) ? view.data : null;
if (data && Array.isArray(data.records)) return find(isObject(data.records[0]) ? (data.records[0].value ?? null) : null);
const response = data?.response;
const items = isObject(response) ? Object.values(response).find(Array.isArray) : undefined;
return find(items?.[0] ?? response ?? null);
}
case 'error':
return { text: view.message, image: null };
}
}