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.

feat: add quota enforcement for avatar uploads and limit checks in creation flow

nirholas6260e437f4fc
diff --git a/api/_lib/avatars.js b/api/_lib/avatars.js
index 9f4aa5568..790fd4b81 100644
--- a/api/_lib/avatars.js
+++ b/api/_lib/avatars.js
@@ -173,7 +173,7 @@ export async function resolveAvatarUrl(row, { expiresIn = 600 } = {}) {
}
// ── quotas ───────────────────────────────────────────────────────────────────
-async function enforceQuotas(userId, incomingBytes) {
+export async function enforceQuotas(userId, incomingBytes) {
const rows = await sql`
select u.plan, q.max_avatars, q.max_bytes_per_avatar, q.max_total_bytes,
(select count(*) from avatars a where a.owner_id = u.id and a.deleted_at is null) as avatar_count,
diff --git a/api/_lib/schema.sql b/api/_lib/schema.sql
index 4d1d5638b..e977b455a 100644
--- a/api/_lib/schema.sql
+++ b/api/_lib/schema.sql
@@ -407,3 +407,116 @@ create table if not exists agent_registrations_pending (
create index if not exists agent_registrations_pending_user_expiry
on agent_registrations_pending(user_id, expires_at);
+
+-- ── Additive: ensure partial unique index exists (required by ON CONFLICT in agents.js) ──
+create unique index if not exists agent_identities_user_unique
+ on agent_identities(user_id) where deleted_at is null;
+
+-- ── agent_delegations — ERC-7710 signed delegation envelopes ────────────────
+create table if not exists agent_delegations (
+ id uuid primary key default gen_random_uuid(),
+ agent_id uuid not null references agent_identities(id) on delete cascade,
+ chain_id integer not null,
+ delegator_address text not null,
+ delegate_address text not null,
+ delegation_hash text not null unique,
+ delegation_json jsonb not null,
+ scope jsonb not null,
+ status text not null default 'active',
+ expires_at timestamptz not null,
+ created_at timestamptz not null default now(),
+ revoked_at timestamptz,
+ tx_hash_revoke text,
+ last_redeemed_at timestamptz,
+ redemption_count integer not null default 0,
+
+ constraint agent_delegations_status_check
+ check (status in ('active', 'revoked', 'expired')),
+ constraint agent_delegations_chain_id_check
+ check (chain_id > 0),
+ constraint agent_delegations_delegator_address_check
+ check (length(delegator_address) = 42 and delegator_address like '0x%'),
+ constraint agent_delegations_delegate_address_check
+ check (length(delegate_address) = 42 and delegate_address like '0x%')
+);
+
+create index if not exists idx_delegations_agent on agent_delegations(agent_id);
+create index if not exists idx_delegations_status on agent_delegations(status) where status = 'active';
+create index if not exists idx_delegations_delegator on agent_delegations(delegator_address);
+
+-- ── agent_subscriptions — recurring on-chain payment schedules ──────────────
+create table if not exists agent_subscriptions (
+ id uuid primary key default gen_random_uuid(),
+ user_id uuid not null references users(id) on delete cascade,
+ agent_id uuid not null references agent_identities(id) on delete cascade,
+ delegation_id uuid not null references agent_delegations(id) on delete cascade,
+ period_seconds integer not null,
+ amount_per_period text not null,
+ next_charge_at timestamptz not null,
+ last_charge_at timestamptz,
+ status text not null default 'active',
+ last_error text,
+ created_at timestamptz not null default now(),
+ canceled_at timestamptz,
+
+ constraint agent_subscriptions_status_check
+ check (status in ('active', 'canceled', 'paused')),
+ constraint agent_subscriptions_period_seconds_check
+ check (period_seconds > 0)
+);
+
+create index if not exists idx_subscriptions_due on agent_subscriptions(next_charge_at) where status = 'active';
+create index if not exists idx_subscriptions_user on agent_subscriptions(user_id);
+create index if not exists idx_subscriptions_agent on agent_subscriptions(agent_id);
+
+-- ── dca_strategies — DCA schedule configs ───────────────────────────────────
+create table if not exists dca_strategies (
+ id uuid primary key default gen_random_uuid(),
+ agent_id uuid not null,
+ delegation_id uuid not null,
+ chain_id integer not null default 84532,
+ token_in text not null,
+ token_out text not null,
+ token_out_symbol text not null default 'WETH',
+ amount_per_execution text not null,
+ period_seconds integer not null,
+ slippage_bps integer not null default 50,
+ status text not null default 'active',
+ next_execution_at timestamptz not null,
+ last_execution_at timestamptz,
+ created_at timestamptz not null default now(),
+ cancelled_at timestamptz,
+
+ constraint dca_strategies_status_check
+ check (status in ('active', 'paused', 'expired', 'cancelled')),
+ constraint dca_strategies_chain_id_check
+ check (chain_id > 0),
+ constraint dca_strategies_slippage_check
+ check (slippage_bps between 1 and 500),
+ constraint dca_strategies_period_check
+ check (period_seconds in (86400, 604800))
+);
+
+create index if not exists idx_dca_strategies_agent on dca_strategies(agent_id);
+create index if not exists idx_dca_strategies_next_exec on dca_strategies(next_execution_at) where status = 'active';
+
+-- ── dca_executions — per-cron swap attempt log ───────────────────────────────
+create table if not exists dca_executions (
+ id uuid primary key default gen_random_uuid(),
+ strategy_id uuid not null references dca_strategies(id) on delete cascade,
+ chain_id integer not null,
+ tx_hash text,
+ amount_in text not null,
+ quote_amount_out text,
+ amount_out text,
+ slippage_bps_used integer,
+ quote_divergence_bps integer,
+ status text not null default 'pending',
+ error text,
+ executed_at timestamptz not null default now(),
+
+ constraint dca_executions_status_check
+ check (status in ('pending', 'success', 'failed', 'aborted'))
+);
+
+create index if not exists idx_dca_executions_strategy on dca_executions(strategy_id);
diff --git a/api/avatars/presign.js b/api/avatars/presign.js
index 9a71efa88..73887aacd 100644
--- a/api/avatars/presign.js
+++ b/api/avatars/presign.js
@@ -3,7 +3,7 @@
import { getSessionUser, authenticateBearer, extractBearer, hasScope } from '../_lib/auth.js';
import { presignUpload } from '../_lib/r2.js';
-import { storageKeyFor } from '../_lib/avatars.js';
+import { storageKeyFor, enforceQuotas } from '../_lib/avatars.js';
import { cors, json, method, readJson, wrap, error } from '../_lib/http.js';
import { limits } from '../_lib/rate-limit.js';
import { parse, presignUploadBody, slug as slugSchema } from '../_lib/validate.js';
@@ -19,6 +19,13 @@ export default wrap(async (req, res) => {
if (!rl.success) return error(res, 429, 'rate_limited', 'upload rate exceeded');
const body = parse(presignUploadBody, await readJson(req));
+
+ try {
+ await enforceQuotas(userId, body.size_bytes);
+ } catch (err) {
+ return error(res, err.status || 402, err.code || 'plan_limit', err.message);
+ }
+
const bodyAny = body;
const slug = bodyAny.slug
? slugSchema.parse(bodyAny.slug)
diff --git a/src/create.js b/src/create.js
index 80c8c2372..78c30a929 100644
--- a/src/create.js
+++ b/src/create.js
@@ -14,7 +14,10 @@ async function boot() {
else window.location.href = '/';
});
- wireCard('card-default-editor', () => creator.openDefaultEditor());
+ wireCard('card-default-editor', async () => {
+ if (await isAtAvatarLimit()) return;
+ creator.openDefaultEditor();
+ });
wireCard('card-upload-glb', (e) => {
// Tooltip anchors live inside the card; let them navigate normally.
if (e && e.target && e.target.closest('a')) return;
@@ -60,6 +63,24 @@ async function handleGlbFile(file) {
await saveAndRedirect(file, { source: 'upload', name });
}
+async function isAtAvatarLimit() {
+ try {
+ const res = await apiFetch('/api/usage/summary');
+ if (!res.ok) return false;
+ const { counts, plan } = await res.json();
+ if (counts.avatars >= plan.max_avatars) {
+ showStatus(
+ `You've reached the ${plan.max_avatars}-avatar limit on the free plan. Delete an avatar to create a new one.`,
+ 'error',
+ );
+ return true;
+ }
+ } catch {
+ // network error — let the upload attempt proceed and fail naturally
+ }
+ return false;
+}
+
async function saveAndRedirect(blob, meta = {}) {
showStatus('Uploading avatar…', 'loading');
try {
@@ -72,6 +93,13 @@ async function saveAndRedirect(blob, meta = {}) {
window.location.replace('/login');
return;
}
+ if (err.data?.error === 'plan_limit_count') {
+ showStatus(
+ "You've reached your avatar limit. Delete an avatar to create a new one.",
+ 'error',
+ );
+ return;
+ }
if (!err.redirected) showStatus(err.message || 'Upload failed.', 'error');
}
}