Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

3.5 KB
Raw Blame
/**
 * The glance card as an Adaptive Card.
 *
 * The Windows 11 widgets board renders Adaptive Cards, not HTML: the PWA
 * manifest points at a template (`ms_ac_template`) and the service worker
 * feeds it data, and the host does the binding. Other hosts would rather be
 * handed a card that is already bound.
 *
 * Both come from ONE layout function so the template and the bound card can
 * never drift: `field()` either emits a binding expression or the real value.
 */

const FALLBACK_IMAGE = 'https://three.ws/pwa-192x192.png';

export const ADAPTIVE_VERSION = '1.6';

function layout(field) {
	return {
		type: 'AdaptiveCard',
		$schema: 'http://adaptivecards.io/schemas/adaptive-card.json',
		version: ADAPTIVE_VERSION,
		body: [
			{
				type: 'ColumnSet',
				columns: [
					{
						type: 'Column',
						width: 'auto',
						items: [
							{
								type: 'Image',
								url: field('image', FALLBACK_IMAGE),
								size: 'Small',
								style: 'RoundedCorners',
								altText: field('name'),
							},
						],
					},
					{
						type: 'Column',
						width: 'stretch',
						items: [
							{ type: 'TextBlock', text: field('name'), weight: 'Bolder', size: 'Medium', wrap: false },
							{
								type: 'TextBlock',
								text: field('headline'),
								isSubtle: true,
								size: 'Small',
								wrap: true,
								maxLines: 2,
								spacing: 'None',
							},
						],
					},
				],
			},
			{
				type: 'ColumnSet',
				spacing: 'Medium',
				columns: [
					{
						type: 'Column',
						width: 'auto',
						items: [
							{
								type: 'TextBlock',
								text: field('metric.value'),
								size: 'ExtraLarge',
								weight: 'Bolder',
								spacing: 'None',
							},
						],
					},
					{
						type: 'Column',
						width: 'stretch',
						verticalContentAlignment: 'Bottom',
						items: [
							{
								type: 'TextBlock',
								text: field('metric.label'),
								isSubtle: true,
								size: 'Small',
								spacing: 'None',
							},
						],
					},
				],
			},
			{
				type: 'FactSet',
				spacing: 'Small',
				facts: [
					{ title: field('stats.0.label'), value: field('stats.0.value') },
					{ title: field('stats.1.label'), value: field('stats.1.value') },
					{ title: field('stats.2.label'), value: field('stats.2.value') },
				],
			},
		],
		actions: [
			{ type: 'Action.OpenUrl', title: 'Open agent', url: field('url') },
			{ type: 'Action.OpenUrl', title: 'New agent', url: field('createUrl', 'https://three.ws/create') },
		],
	};
}

/**
 * Layout paths are written the way JavaScript reads them (`stats.0.label`), but
 * Adaptive Expression Language indexes an array with brackets. A dot before a
 * number is a parse error there, and the templating engine throws on the whole
 * card rather than skipping the one binding, so a single dotted index leaves
 * the widget slot empty instead of merely missing a line.
 */
function binding(path) {
	return path.replace(/\.(\d+)/g, '[$1]');
}

/** The unbound template the Windows widgets board fetches once. */
export function adaptiveTemplate() {
	return layout((path, fallback) => {
		const expr = binding(path);
		return fallback ? `\${if(${expr}, ${expr}, '${fallback}')}` : `\${${expr}}`;
	});
}

/** The same card with this agent's values already in it. */
export function adaptiveCardFor(card) {
	return layout((path, fallback) => {
		const value = path.split('.').reduce((acc, key) => (acc == null ? acc : acc[key]), card);
		if (value === null || value === undefined || value === '') return fallback ?? '';
		return typeof value === 'number' ? String(value) : value;
	});
}