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.0 KB
Raw Blame
/**
 * GET  /api/agent-strategy?id=<agent_id>  — fetch the strategy JSON stored on this agent.
 * POST /api/agent-strategy?id=<agent_id>  — replace it.  Body: { strategy: <any JSON> }
 *
 * The strategy is stored under agent_identities.meta.strategy. Owner-only.
 * For typed strategy execution (DCA, etc.) see api/cron/run-dca and the
 * dca_strategies table. This endpoint is the generic "freeform agent
 * configuration" slot.
 */
import { sql } from './_lib/db.js';
import { authenticateBearer, extractBearer, getSessionUser } from './_lib/auth.js';
import { cors, error, json, method, readJson, wrap, rateLimited } from './_lib/http.js';
import { requireCsrf } from './_lib/csrf.js';
import { clientIp, limits } from './_lib/rate-limit.js';

async function resolveAuth(req) {
	const session = await getSessionUser(req);
	if (session) return { userId: session.id };
	const bearer = await authenticateBearer(extractBearer(req));
	if (bearer) return { userId: bearer.userId };
	return null;
}

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

	const auth = await resolveAuth(req);
	if (!auth) return error(res, 401, 'unauthorized', 'sign in required');

	const url = new URL(req.url, 'http://x');
	const agentId = url.searchParams.get('id');
	if (!agentId) return error(res, 400, 'validation_error', 'id query param required');

	const [agent] = await sql`
		SELECT id, user_id, meta FROM agent_identities
		WHERE id = ${agentId} AND deleted_at IS NULL
	`;
	if (!agent) return error(res, 404, 'not_found', 'agent not found');
	if (agent.user_id !== auth.userId) return error(res, 403, 'forbidden', 'not your agent');

	if (req.method === 'GET') {
		const rl = await limits.widgetRead(clientIp(req));
		if (!rl.success) return rateLimited(res, rl);

		// "no strategy set" is a valid empty state, not an error — return null with
		// 200 so owners viewing an un-configured agent don't generate console 404s.
		const strategy = agent.meta?.strategy ?? null;
		return json(res, 200, { data: { strategy } });
	}

	// POST / PUT — replace. Cookie-session mutations need a CSRF token (bearer
	// callers are exempt inside requireCsrf).
	if (!(await requireCsrf(req, res, auth.userId))) return;

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

	const body = await readJson(req).catch(() => null);
	if (!body || body.strategy === undefined) {
		return error(res, 400, 'validation_error', 'request body must include `strategy`');
	}
	if (typeof body.strategy === 'string' && body.strategy.length > 64_000) {
		return error(res, 413, 'payload_too_large', 'strategy exceeds 64 KB limit');
	}

	const nextMeta = { ...(agent.meta || {}), strategy: body.strategy };
	await sql`
		UPDATE agent_identities SET meta = ${JSON.stringify(nextMeta)}::jsonb, updated_at = now()
		WHERE id = ${agentId}
	`;

	return json(res, 200, { data: { ok: true, strategy: body.strategy } });
});