~ 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
93 lines · 3.5 KB Raw History
// What HUD's apps tell a feed generator of what is done with its feed, as
// Bluesky's app does (its feed-feedback): the posts seen, the post opened,
// "more like this" and "less like this". Only for a feed a trusted view
// draws (its tree names the feed in `_meta.ui.report`), when signed in. Seen
// posts and opened posts queue and go in batches through the app's
// `send_interactions` call, at most one send every 10 s, and at once when
// the app is hidden, torn down or moves on; a "more/less like this" goes
// at once. A failure drops the batch; a feed that does not take
// interactions (`sent: 0` with a reason) is not told any more.

export const SEEN = 'app.bsky.feed.defs#interactionSeen';
export const CLICKTHROUGH = 'app.bsky.feed.defs#clickthroughItem';

/** The most interactions one send carries, as the lexicon allows. */
const BATCH = 100;
/** The least time between two throttled sends. */
const EVERY_MS = 10_000;

/** One interaction, as `send_interactions` takes it. */
export type Interaction = { item: string; event: string; feedContext?: string; reqId?: string };

/** What a send answers: how many went, or why none did. */
export type Sent = { sent: number; reason: string | null };

export class Interactions {
	/** Waiting to go, by feed. */
	#queue = new Map<string, Interaction[]>();
	/** Feeds that said they take no interactions. */
	#stopped = new Set<string>();
	#last = 0;
	#timer: number | null = null;
	#send: (feed: string, interactions: Interaction[]) => Promise<Sent>;
	#log: (message: string) => void;

	constructor(send: (feed: string, interactions: Interaction[]) => Promise<Sent>, log: (message: string) => void) {
		this.#send = send;
		this.#log = log;
	}

	/** Queue one, to go with the next send: now, if the last was 10 s ago or
	 * more; else when it will have been. */
	add(feed: string, interaction: Interaction) {
		if (this.#stopped.has(feed)) return;
		const queued = this.#queue.get(feed) ?? [];
		queued.push(interaction);
		this.#queue.set(feed, queued);
		if (this.#timer !== null) return;
		const wait = this.#last + EVERY_MS - Date.now();
		if (wait <= 0) void this.flush();
		else this.#timer = window.setTimeout(() => void this.flush(), wait);
	}

	/** Send everything queued, now. */
	async flush() {
		if (this.#timer !== null) window.clearTimeout(this.#timer);
		this.#timer = null;
		if (this.#queue.size === 0) return;
		this.#last = Date.now();
		const queued = [...this.#queue];
		this.#queue.clear();
		await Promise.all(
			queued.flatMap(([feed, interactions]) =>
				Array.from({ length: Math.ceil(interactions.length / BATCH) }, (_, i) => this.#deliver(feed, interactions.slice(i * BATCH, (i + 1) * BATCH)))
			)
		);
	}

	/** Send one now, apart from the queue, as for "more like this": a
	 * failure, or a feed that takes none, throws, for the button to say. */
	async now(feed: string, interaction: Interaction) {
		if (this.#stopped.has(feed)) throw new Error("This feed doesn't take feedback.");
		const { sent, reason } = await this.#send(feed, [interaction]);
		if (sent === 0 && reason) {
			this.#stop(feed);
			throw new Error(reason);
		}
	}

	async #deliver(feed: string, interactions: Interaction[]) {
		if (this.#stopped.has(feed)) return;
		try {
			const { sent, reason } = await this.#send(feed, interactions);
			if (sent === 0 && reason) this.#stop(feed);
		} catch (error) {
			this.#log(`send_interactions for ${feed} failed; ${interactions.length} dropped: ${error instanceof Error ? error.message : String(error)}`);
		}
	}

	#stop(feed: string) {
		this.#stopped.add(feed);
		this.#queue.delete(feed);
	}
}