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.

avatars(dashboard): resolve storage mode for a whole page of avatars in one query

nirholasea5208f202d2
diff --git a/api/_lib/storage-mode.js b/api/_lib/storage-mode.js
index 8761dbd74..16308ed70 100644
--- a/api/_lib/storage-mode.js
+++ b/api/_lib/storage-mode.js
@@ -74,3 +74,48 @@ export async function readStorageMode(avatarId) {
export function validateStorageMode(input) {
return storageModeSchema.parse(input);
}
+
+/**
+ * Batch sibling of readStorageMode: resolve the stored mode for many avatars in
+ * one indexed query instead of one round trip per row.
+ *
+ * The settings page lists every avatar the caller owns with its R2 / IPFS
+ * state, so the per-id endpoint would have turned one panel into N requests.
+ * Rows missing a stored mode fall back to the same default the single reader
+ * uses, and a deployment whose avatars table predates the storage_mode column
+ * gets an empty map rather than a 500 (identical guard to readStorageMode).
+ *
+ * @param {string[]} avatarIds
+ * @returns {Promise<Map<string, ReturnType<typeof defaultStorageMode>>>}
+ */
+export async function readStorageModes(avatarIds) {
+ const ids = [...new Set((avatarIds || []).filter(Boolean))];
+ if (!ids.length) return new Map();
+ try {
+ const rows = await sql`
+ SELECT id, storage_key, checksum_sha256, storage_mode
+ FROM avatars
+ WHERE id = ANY(${ids}::uuid[]) AND deleted_at IS NULL
+ `;
+ const out = new Map();
+ for (const row of rows) {
+ const base = defaultStorageMode(row);
+ out.set(
+ row.id,
+ row.storage_mode
+ ? {
+ ...base,
+ ...row.storage_mode,
+ r2: { ...base.r2, ...(row.storage_mode.r2 || {}) },
+ ipfs: { ...base.ipfs, ...(row.storage_mode.ipfs || {}) },
+ attestation: { ...base.attestation, ...(row.storage_mode.attestation || {}) },
+ }
+ : base,
+ );
+ }
+ return out;
+ } catch (err) {
+ if (/column .* does not exist/i.test(String(err?.message))) return new Map();
+ throw err;
+ }
+}
diff --git a/api/avatars/mine.js b/api/avatars/mine.js
index 36e8c5790..ff4417512 100644
--- a/api/avatars/mine.js
+++ b/api/avatars/mine.js
@@ -9,6 +9,7 @@
import { getSessionUser, authenticateBearer, extractBearer, hasScope } from '../_lib/auth.js';
import { listAvatars } from '../_lib/avatars.js';
+import { readStorageModes } from '../_lib/storage-mode.js';
import { cors, error, json, method, wrap, rateLimited } from '../_lib/http.js';
import { limits, clientIp } from '../_lib/rate-limit.js';
import { env } from '../_lib/env.js';
@@ -21,7 +22,7 @@ async function resolveAuth(req) {
return null;
}
-export function slimAvatar(av) {
+export function slimAvatar(av, storageMode) {
return {
id: av.id,
name: av.name || 'Untitled avatar',
@@ -30,6 +31,20 @@ export function slimAvatar(av) {
has_thumbnail: !!av.thumbnail_url,
thumb_url: `${env.APP_ORIGIN}/api/avatars/${av.id}/thumb`,
created_at: av.created_at,
+ size_bytes: av.size_bytes ?? null,
+ // Flattened storage state, so the dashboard settings page can render an
+ // R2-vs-IPFS row per avatar without a per-avatar /storage-mode request.
+ // The attestation block stays out: it is chain provenance, not storage,
+ // and /api/avatars/:id/storage-mode already serves the full record.
+ storage: storageMode
+ ? {
+ primary: storageMode.primary,
+ r2_present: !!storageMode.r2?.present,
+ ipfs_pinned: !!storageMode.ipfs?.pinned,
+ ipfs_cid: storageMode.ipfs?.cid ?? null,
+ ipfs_pinned_at: storageMode.ipfs?.pinned_at ?? null,
+ }
+ : null,
};
}
@@ -52,8 +67,10 @@ export default wrap(async (req, res) => {
cursor: url.searchParams.get('cursor'),
});
+ const modes = await readStorageModes(avatars.map((a) => a.id));
+
return json(res, 200, {
- avatars: avatars.map(slimAvatar),
+ avatars: avatars.map((a) => slimAvatar(a, modes.get(a.id) || null)),
next_cursor,
});
});