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.7 KB
Raw Blame
import { z } from 'zod';
import { sql } from '../../_lib/db.js';
import { getSessionUser, authenticateBearer, extractBearer } from '../../_lib/auth.js';
import { cors, json, error, method, readJson, wrap, rateLimited } from '../../_lib/http.js';
import { limits } from '../../_lib/rate-limit.js';
import { parse, isUuid } from '../../_lib/validate.js';

const rateSchema = z.object({
	rating: z.number().int().min(1).max(5),
});

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

	const id = req.query?.id;
	if (!id || !isUuid(id)) return error(res, 404, 'not_found', 'skill not found');

	const session = await getSessionUser(req);
	const bearer = session ? null : await authenticateBearer(extractBearer(req));
	if (!session && !bearer) return error(res, 401, 'unauthorized', 'sign in required');
	const userId = session?.id ?? bearer.userId;

	const rl = await limits.chatUser(userId);
	if (!rl.success) return rateLimited(res, rl);

	const [skill] = await sql`SELECT id FROM marketplace_skills WHERE id = ${id} AND is_public = true`;
	if (!skill) return error(res, 404, 'not_found', 'skill not found');

	const { rating } = parse(rateSchema, await readJson(req));

	await sql`
		INSERT INTO skill_ratings (user_id, skill_id, rating)
		VALUES (${userId}, ${id}, ${rating})
		ON CONFLICT (user_id, skill_id) DO UPDATE SET rating = EXCLUDED.rating, created_at = now()
	`;

	const [stats] = await sql`
		SELECT
			ROUND(AVG(rating)::numeric, 1)::float AS avg_rating,
			COUNT(*)::int AS rating_count
		FROM skill_ratings
		WHERE skill_id = ${id}
	`;

	return json(res, 200, {
		avg_rating: Number(stats.avg_rating) || 0,
		rating_count: stats.rating_count || 0,
	});
});