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.

1.8 KB
Raw Blame
// GET /api/companion/events → the triaged feed.
//
// Query:
//   limit=<1-50>            page size (default 30)
//   before=<iso timestamp>  cursor for "load more"
//   pending=1               only what has not been delivered or dismissed
//   min_importance=<0-100>  floor, for the "only the loud ones" view

import { cors, json, method, wrap, rateLimited } from '../../_lib/http.js';
import { limits } from '../../_lib/rate-limit.js';
import { listEvents, getSettings } from '../../_lib/companion/store.js';
import { companionCaller } from '../../_lib/companion/caller.js';

export default wrap(async (req, res) => {
	if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
	if (!method(req, res, ['GET'])) return;

	// The bridge token reaches this route: it is the credential the desktop app,
	// the CLI and the MCP server hold. It resolves to its owner only.
	const user = await companionCaller(req, res, { bridge: true });
	if (!user) return;

	const rl = await limits.companionRead(user.id);
	if (!rl.success) return rateLimited(res, rl);

	const params = new URL(req.url, 'http://x').searchParams;
	const limitRaw = Number.parseInt(params.get('limit') || '30', 10);
	const limit = Number.isFinite(limitRaw) ? Math.min(50, Math.max(1, limitRaw)) : 30;
	const beforeRaw = params.get('before');
	const before = beforeRaw && !Number.isNaN(Date.parse(beforeRaw)) ? new Date(beforeRaw) : null;
	const minRaw = Number.parseInt(params.get('min_importance') || '0', 10);
	const minImportance = Number.isFinite(minRaw) ? Math.min(100, Math.max(0, minRaw)) : 0;

	const [events, settings] = await Promise.all([
		listEvents(user.id, { limit, before, pendingOnly: params.get('pending') === '1', minImportance }),
		getSettings(user.id),
	]);

	return json(res, 200, {
		events,
		threshold: settings.threshold,
		has_more: events.length === limit,
	});
});