~ 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
81 lines · 3.6 KB Raw History
// What the sign-in screen (SignIn.svelte) reads of its app file's
// `signIn`: who matches what is typed of a handle, asked of the
// typeahead's own host straight from the browser, as no one (a public
// XRPC query, as typeahead.waow.tech's, which answers any origin), each
// account's DID, handle, name and picture read by the typeahead's dotted
// paths; and its lines' links, Markdown's `[label](href)`.

import type { Typeahead } from './app';

/** An account the typeahead matched: its handle as that service says, unverified; its picture and the one drawn where that fails to load, https alone. */
export type Found = { did: string; handle: string; name: string | null; avatar: string | null; fallback: string | null };

/** What is asked for what is typed: trimmed, without a leading @; null under two letters, which match too many to say. */
export function typed(text: string): string | null {
	const q = text.trim().replace(/^@/, '');
	return q.length >= 2 ? q.slice(0, 256) : null;
}

/** The typeahead's URL for `q`: its method at its host, `q` its `param`, with its other `params`. */
export function typeaheadUrl(typeahead: Typeahead, q: string): string {
	const url = new URL(`/xrpc/${typeahead.method}`, typeahead.url);
	for (const [key, value] of Object.entries(typeahead.params ?? {})) url.searchParams.set(key, String(value));
	url.searchParams.set(typeahead.param, q);
	return url.href;
}

/** What is at a dotted path of `value`. */
function pathIn(value: unknown, path: string): unknown {
	let found = value;
	for (const key of path.split('.')) found = typeof found === 'object' && found !== null ? (found as Record<string, unknown>)[key] : undefined;
	return found;
}

/** The accounts an answer holds, by the typeahead's paths: those with a DID and a handle (not `handle.invalid`). */
export function foundIn(typeahead: Typeahead, answer: unknown): Found[] {
	const items = pathIn(answer, typeahead.items);
	if (!Array.isArray(items)) return [];
	const text = (item: unknown, path: string | undefined) => {
		const found = path ? pathIn(item, path) : undefined;
		return typeof found === 'string' && found !== '' ? found : null;
	};
	const https = (item: unknown, path: string | undefined) => {
		const found = text(item, path);
		return found?.startsWith('https://') ? found : null;
	};
	return items.flatMap((item): Found[] => {
		const did = text(item, typeahead.did);
		const handle = text(item, typeahead.handle);
		if (!did?.startsWith('did:') || !handle || handle === 'handle.invalid') return [];
		return [{ did, handle, name: text(item, typeahead.name), avatar: https(item, typeahead.avatar), fallback: https(item, typeahead.avatarFallback) }];
	});
}

/** Who matches what is typed, best first: none for under two letters, or where the typeahead cannot say. */
export async function findActors(typeahead: Typeahead, text: string, signal?: AbortSignal): Promise<Found[]> {
	const q = typed(text);
	if (!q) return [];
	try {
		const res = await fetch(typeaheadUrl(typeahead, q), { signal });
		return res.ok ? foundIn(typeahead, await res.json()) : [];
	} catch {
		return [];
	}
}

/** A line's words and links: Markdown's `[label](href)` as a link, all else as it is. */
export type Part = { text: string; href?: string };

/** A line split into its words and its links. */
export function partsOf(line: string): Part[] {
	const parts: Part[] = [];
	const link = /\[([^\]]+)\]\(([^)\s]+)\)/g;
	let at = 0;
	for (const found of line.matchAll(link)) {
		if (found.index > at) parts.push({ text: line.slice(at, found.index) });
		parts.push({ text: found[1], href: found[2] });
		at = found.index + found[0].length;
	}
	if (at < line.length) parts.push({ text: line.slice(at) });
	return parts;
}