~ 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
186 lines · 5.2 KB Raw History
<script lang="ts">
	import CodeEditor from '../atoms/CodeEditor.svelte';
	import ProblemList from '../molecules/ProblemList.svelte';
	import { argumentsLint, catalogCompletions, locate, pathOf, viewLint, writable, type Catalog, type Problem } from '../playground/editor';
	import { TOOLS, type Tool } from '../playground/examples';

	/**
	 * The playground's editors: the call (the tool that draws the view, and
	 * its arguments) over the view's document, and under them what is wrong
	 * with either, each pressed to go to it. The catalog completes what is
	 * typed in the view; the server's problems are marked where each is.
	 */
	let {
		tool,
		args = $bindable(),
		view = $bindable(),
		problems,
		catalog,
		edited,
		onswitch,
		onreset,
		resetTitle
	}: {
		tool: Tool;
		args: string;
		view: string;
		problems: Problem[];
		catalog: Catalog | null;
		/** Whether the work differs from how it began: then it can be reset. */
		edited: boolean;
		onswitch: (tool: Tool) => void;
		/** Back to how it began, the call and the view, or null where it began as nothing to go back to. */
		onreset: (() => void) | null;
		/** What resetting goes back to, said where Reset is hovered. */
		resetTitle: string;
	} = $props();

	let argsEditor: CodeEditor | undefined = $state();
	let viewEditor: CodeEditor | undefined = $state();

	const argsExtensions = writable({ lint: argumentsLint });
	const viewExtensions = writable({ lint: viewLint(() => problems), complete: catalogCompletions(() => catalog) });

	$effect(() => {
		void problems;
		viewEditor?.relint();
	});

	type Listed = { at: string | null; message: string; editor: 'args' | 'view'; path: (string | number)[] | null; position: number | null };

	/** Why `text` is not JSON, and where, or null when it is. */
	function syntax(text: string): { message: string; position: number | null } | null {
		try {
			JSON.parse(text);
			return null;
		} catch (error) {
			const message = error instanceof Error ? error.message : String(error);
			const at = /position (\d+)/.exec(message);
			return { message, position: at ? Number(at[1]) : null };
		}
	}

	const listed: Listed[] = $derived.by(() => {
		const found: Listed[] = [];
		const badArgs = syntax(args);
		if (badArgs) found.push({ at: null, message: `The arguments are not JSON: ${badArgs.message}`, editor: 'args', path: null, position: badArgs.position });
		const badView = syntax(view);
		if (badView) found.push({ at: null, message: `The view is not JSON: ${badView.message}`, editor: 'view', path: null, position: badView.position });
		for (const problem of problems) found.push({ at: problem.at, message: problem.message.charAt(0).toUpperCase() + problem.message.slice(1), editor: 'view', path: pathOf(problem.at), position: null });
		return found;
	});

	function reveal(index: number) {
		const item = listed[index];
		if (!item) return;
		const editor = item.editor === 'args' ? argsEditor : viewEditor;
		editor?.reveal((state) => {
			if (item.path) return locate(state, item.path);
			const at = Math.min(item.position ?? 0, state.doc.length);
			return { from: at, to: Math.min(at + 1, state.doc.length) };
		});
	}
</script>

<section class="editors" aria-label="Editor">
	<div class="call">
		<header class="head">
			<h2 id="call-title">Call</h2>
			<select class="input tool" aria-label="Tool" value={tool} onchange={(event) => onswitch(event.currentTarget.value as Tool)}>
				{#each TOOLS as name (name)}<option value={name}>{name}</option>{/each}
			</select>
			<span class="mute hint">The tool and its arguments, without the view</span>
		</header>
		<div class="args">
			<CodeEditor bind:this={argsEditor} bind:value={args} extensions={argsExtensions} label="Arguments" />
		</div>
	</div>
	<div class="view">
		<header class="head">
			<h2>View</h2>
			<span class="mute hint">A place.hud.view document</span>
			{#if onreset && edited}
				<button type="button" class="btn sm reset" title={resetTitle} onclick={onreset}>Reset</button>
			{/if}
		</header>
		<div class="doc">
			<CodeEditor bind:this={viewEditor} bind:value={view} extensions={viewExtensions} label="View" />
		</div>
		{#if listed.length}
			<div class="listed"><ProblemList problems={listed} onreveal={reveal} /></div>
		{/if}
	</div>
</section>

<style>
	.editors {
		display: grid;
		grid-template-rows: auto minmax(0, 1fr);
		height: 100%;
		min-height: 0;
		background: var(--bg);
	}

	.call {
		border-bottom: var(--border);
	}

	.view {
		display: flex;
		flex-direction: column;
		min-height: 0;
	}

	.head {
		display: flex;
		flex: none;
		align-items: center;
		gap: var(--space-2);
		min-height: var(--strip);
		padding: var(--space-1-5) var(--space-3);
		border-bottom: var(--border);
		background: var(--panel);
	}

	h2 {
		font-size: var(--text-sm);
	}

	.hint {
		overflow: hidden;
		font-size: var(--text-xs);
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.tool {
		height: var(--control-sm);
		font: var(--text-xs) var(--font-mono);
	}

	.reset {
		margin-left: auto;
	}

	/* The arguments are a few lines: at most a third of the pane, scrolling past that. */
	.args {
		height: var(--args-height);
		resize: vertical;
		overflow: hidden;
	}

	.doc {
		flex: 1;
		min-height: 0;
	}

	.listed {
		display: flex;
		flex: none;
		max-height: var(--problems-height);
	}

	.listed > :global(*) {
		flex: 1;
	}
</style>