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.

fix: widget ID validation, lazy knowledge import, agent-identity join, fallback model

nirholas2ecd5cba72de
diff --git a/api/chat.js b/api/chat.js
index 2a2594ada..935f47a24 100644
--- a/api/chat.js
+++ b/api/chat.js
@@ -436,6 +436,7 @@ function pickProvider(requested, model, userKeys = {}) {
const FALLBACK_SIBLINGS = {
openrouter: [
'meta-llama/llama-3.3-70b-instruct:free',
+ 'meta-llama/llama-3.2-3b-instruct:free',
'mistralai/mistral-7b-instruct:free',
],
groq: ['llama-3.3-70b-versatile', 'llama-3.1-8b-instant'],
diff --git a/api/cron/[name].js b/api/cron/[name].js
index de948a7ea..88521964c 100644
--- a/api/cron/[name].js
+++ b/api/cron/[name].js
@@ -2905,7 +2905,16 @@ async function setTriggerState(t, state) {
async function loadAvatarFromAgent(agentId) {
if (!agentId) return null;
- const r = await sql`select id, name, description, agent_id from avatars where id = ${agentId} or agent_id::text = ${agentId} limit 1`;
+ // avatars has no direct agent_id column — the link is agent_identities.avatar_id.
+ // Look up the avatar by joining through agent_identities, or fall back to the
+ // case where the caller passes an avatar id directly (legacy behavior).
+ const r = await sql`
+ select a.id, a.name, a.description, ai.id as agent_id
+ from avatars a
+ left join agent_identities ai on ai.avatar_id = a.id
+ where a.id::text = ${agentId} OR ai.id::text = ${agentId}
+ limit 1
+ `;
return r[0] || null;
}
diff --git a/api/widgets/[id]/[action].js b/api/widgets/[id]/[action].js
index fd4ced3aa..5d5e70669 100644
--- a/api/widgets/[id]/[action].js
+++ b/api/widgets/[id]/[action].js
@@ -18,7 +18,9 @@ import { decorate } from '../index.js';
import { redactPii } from '../../_lib/pii.js';
import { embed, cosine, embeddingsConfigured } from '../../_lib/embeddings.js';
import { listTranscripts, getTranscript } from './_transcripts.js';
-import { listKnowledge, ingestKnowledge, deleteKnowledge, testRetrieval } from './_knowledge.js';
+// _knowledge.js is loaded on demand — its jsdom→html-encoding-sniffer→@exodus/bytes
+// transitive dep chain causes ERR_REQUIRE_ESM at import time on some Node versions,
+// which would crash the entire function and take down stats/transcripts as well.
export default wrap(async (req, res) => {
const action = req.query?.action;
@@ -714,10 +716,14 @@ function writeSse(res, event, data) {
function chatIdFromReq(req) {
const fromQuery = req.query?.id;
- if (typeof fromQuery === 'string' && fromQuery) return fromQuery;
+ if (typeof fromQuery === 'string' && fromQuery) {
+ return isValidWidgetId(fromQuery) ? fromQuery : null;
+ }
const path = new URL(req.url, 'http://x').pathname;
const m = path.match(/\/api\/widgets\/([^/]+)\/chat/);
- return m ? decodeURIComponent(m[1]) : null;
+ if (!m) return null;
+ const id = decodeURIComponent(m[1]);
+ return isValidWidgetId(id) ? id : null;
}
function clampInt(n, min, max) {
@@ -770,10 +776,14 @@ function trim(s, max) {
function duplicateIdFromReq(req) {
const fromQuery = req.query?.id;
- if (typeof fromQuery === 'string' && fromQuery) return fromQuery;
+ if (typeof fromQuery === 'string' && fromQuery) {
+ return isValidWidgetId(fromQuery) ? fromQuery : null;
+ }
const path = new URL(req.url, 'http://x').pathname;
const m = path.match(/\/api\/widgets\/([^/]+)\/duplicate/);
- return m ? decodeURIComponent(m[1]) : null;
+ if (!m) return null;
+ const id = decodeURIComponent(m[1]);
+ return isValidWidgetId(id) ? id : null;
}
async function resolveDuplicateAuth(req) {
@@ -1037,12 +1047,24 @@ function startOfUtcDay(d) {
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
}
+// Widget IDs are prefixed with "wdgt_". Reject placeholder strings that the
+// frontend can accidentally emit before the real ID is resolved — prevents
+// 500s from the DB receiving values like "undefined" or "null".
+const WIDGET_ID_RE = /^wdgt_[A-Za-z0-9_-]{1,128}$/;
+function isValidWidgetId(id) {
+ return typeof id === 'string' && WIDGET_ID_RE.test(id);
+}
+
function statsIdFromReq(req) {
const fromQuery = req.query?.id;
- if (typeof fromQuery === 'string' && fromQuery) return fromQuery;
+ if (typeof fromQuery === 'string' && fromQuery) {
+ return isValidWidgetId(fromQuery) ? fromQuery : null;
+ }
const path = new URL(req.url, 'http://x').pathname;
const m = path.match(/\/api\/widgets\/([^/]+)\/stats/);
- return m ? decodeURIComponent(m[1]) : null;
+ if (!m) return null;
+ const id = decodeURIComponent(m[1]);
+ return isValidWidgetId(id) ? id : null;
}
async function resolveStatsAuth(req) {
@@ -1184,6 +1206,15 @@ async function handleKnowledge(req, res) {
`;
if (!w) return error(res, 404, 'not_found', 'widget not found or not yours');
+ let knowledgeMod;
+ try {
+ knowledgeMod = await import('./_knowledge.js');
+ } catch (err) {
+ captureException(err, { route: 'widget/knowledge', stage: 'import' });
+ return error(res, 503, 'knowledge_unavailable', 'knowledge service is temporarily unavailable');
+ }
+ const { listKnowledge, ingestKnowledge, deleteKnowledge, testRetrieval } = knowledgeMod;
+
if (req.method === 'GET') {
const url = new URL(req.url, 'http://x');
// ?test=<query> — Inkeep-style retrieval debugger. Returns top-K
@@ -1254,11 +1285,15 @@ async function handleKnowledge(req, res) {
function actionIdFromReq(req, action) {
const fromQuery = req.query?.id;
- if (typeof fromQuery === 'string' && fromQuery) return fromQuery;
+ if (typeof fromQuery === 'string' && fromQuery) {
+ return isValidWidgetId(fromQuery) ? fromQuery : null;
+ }
const path = new URL(req.url, 'http://x').pathname;
const re = new RegExp(`/api/widgets/([^/]+)/${action}`);
const m = path.match(re);
- return m ? decodeURIComponent(m[1]) : null;
+ if (!m) return null;
+ const id = decodeURIComponent(m[1]);
+ return isValidWidgetId(id) ? id : null;
}
async function resolveOwnerAuth(req, requiredScope) {
diff --git a/package-lock.json b/package-lock.json
index f024de34a..46ef17d45 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -6276,9 +6276,9 @@
"license": "MIT"
},
"node_modules/@esbuild-plugins/node-globals-polyfill": {
- "version": "0.1.1",
- "resolved": "https://registry.npmjs.org/@esbuild-plugins/node-globals-polyfill/-/node-globals-polyfill-0.1.1.tgz",
- "integrity": "sha512-MR0oAA+mlnJWrt1RQVQ+4VYuRJW/P2YmRTv1AsplObyvuBMnPHiizUF95HHYiSsMGLhyGtWufaq2XQg6+iurBg==",
+ "version": "0.2.3",
+ "resolved": "https://registry.npmjs.org/@esbuild-plugins/node-globals-polyfill/-/node-globals-polyfill-0.2.3.tgz",
+ "integrity": "sha512-r3MIryXDeXDOZh7ih1l/yE9ZLORCd5e8vWg02azWRGj5SPTuoh69A2AIyn0Z31V/kHBfZ4HgWJ+OK3GTTwLmnw==",
"dev": true,
"license": "ISC",
"peerDependencies": {
diff --git a/package.json b/package.json
index db90348dd..8a8c24074 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,8 @@
"author": "nirholas",
"license": "Apache-2.0",
"overrides": {
- "serialize-javascript": ">=7.0.5"
+ "serialize-javascript": ">=7.0.5",
+ "@esbuild-plugins/node-globals-polyfill": "^0.2.0"
},
"engines": {
"node": "24.x"