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.

4.2 KB
Raw Blame
// GET /api/news/feed
// ---------------------------------------------------------------------------
// Live aggregated crypto news across the native source registry
// (api/_lib/news-sources.js, the single source of truth for which first-party
// publisher feeds we carry; feeds are added and retired there, so read the
// count from `meta=1` rather than any number written here). Powers
// /markets/news, the /markets hub strip, and anything else that wants a real
// cross-source headline feed.
//
// Query params:
//   category  one of NEWS_CATEGORIES (default: all)
//   source    a single source_key (overrides category and lang)
//   lang      'en' (default), any of NEWS_LANGUAGES, or 'all'. The registry
//             carries international feeds in 17 languages; they are opt-in so
//             the default feed does not interleave languages.
//   q         case-insensitive full-text filter (title/description/tickers)
//   featured  '1' narrows sources to the majors (tier1/tier2 or credibility
//             ≥ 0.85) — the Featured tab on /markets/news
//   limit     1–50 (default 30)
//   offset    pagination offset
//   meta=1    include the source registry + category + language lists
//   raw=1     bypass the editorial display gate (crypto-relevant + above the
//             quality floor) and return the unfiltered aggregate. Debugging and
//             parity checks only; the human-facing feeds never set it.
//
// Aggregation results are cached per source for 5 minutes inside
// api/_lib/news.js, so this endpoint is cheap under load; CDN caches 120s.

import { cors, json, method, wrap, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { getNews } from '../_lib/news.js';
import { NEWS_SOURCES, NEWS_CATEGORIES, NEWS_LANGUAGES } from '../_lib/news-sources.js';

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

	const rl = await limits.marketFeedIp(clientIp(req));
	if (!rl.success) return rateLimited(res, rl);

	const params = new URL(req.url, 'http://x').searchParams;
	const category = (params.get('category') || '').trim().toLowerCase() || undefined;
	const source = (params.get('source') || '').trim().toLowerCase() || undefined;
	const q = (params.get('q') || '').trim().slice(0, 80) || undefined;
	const lang = (params.get('lang') || 'en').trim().toLowerCase();
	const featured = params.get('featured') === '1';
	const limit = Math.min(Math.max(1, parseInt(params.get('limit') || '30', 10) || 30), 50);
	const offset = Math.max(0, parseInt(params.get('offset') || '0', 10) || 0);

	if (category && category !== 'all' && !NEWS_CATEGORIES.includes(category)) {
		return json(res, 400, {
			error: 'bad_category',
			message: `unknown category "${category}"`,
			categories: NEWS_CATEGORIES,
		});
	}
	if (source && !NEWS_SOURCES[source]) {
		return json(res, 400, { error: 'bad_source', message: `unknown source "${source}"` });
	}
	if (lang !== 'all' && lang !== 'en' && !NEWS_LANGUAGES.includes(lang)) {
		return json(res, 400, {
			error: 'bad_language',
			message: `unknown language "${lang}"`,
			languages: ['en', ...NEWS_LANGUAGES, 'all'],
		});
	}

	// Human-facing feed: apply the editorial display gate (crypto-relevant +
	// above the quality floor). raw=1 bypasses it for debugging / parity checks.
	const curated = params.get('raw') !== '1';
	const result = await getNews({ category, source, lang, q, limit, offset, featured, curated });

	const body = {
		articles: result.articles,
		total: result.total,
		limit,
		offset,
		lang,
		sources_ok: result.sources_ok,
		sources_total: result.sources_total,
		fetched_at: new Date().toISOString(),
	};
	if (params.get('meta') === '1') {
		body.categories = NEWS_CATEGORIES;
		// 'en' is the untagged default; 'all' mixes every language into one feed.
		body.languages = ['en', ...NEWS_LANGUAGES];
		body.sources = Object.entries(NEWS_SOURCES).map(([key, s]) => ({
			key,
			name: s.name,
			category: s.category,
			...(s.tier ? { tier: s.tier } : {}),
			...(s.language ? { language: s.language } : {}),
		}));
	}
	return json(res, 200, body, {
		'cache-control': 'public, max-age=60, s-maxage=120, stale-while-revalidate=600',
	});
});