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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(x-content): give the editor's model a second and third try on a dropped connection
nirholasd65da33a3f25
diff --git a/api/_lib/x-content/llm.js b/api/_lib/x-content/llm.jsindex bc39bcfa2..5fa2a6ffb 100644--- a/api/_lib/x-content/llm.js+++ b/api/_lib/x-content/llm.js@@ -29,21 +29,59 @@ async function viaVertex({ system, parts }) {return { model: `vertex:${EDITOR_MODEL}`, text: body.content.filter((block) => block.type === 'text').map((block) => block.text).join('') };}-async function viaChatCompletions({ system, parts }, { url, key, model, label, extraHeaders = {}, textOnly = false }) {+// One rung, one model. A dropped connection, a 5xx, or a plain rate limit is+// the provider having a bad minute, and while every paid rung is out of+// billing the free one is the only editor left, so it is worth another try.+// A refusal about billing or credentials would only repeat, so it falls+// through to the next rung at once.+export const RUNG_ATTEMPTS = 3;+export const RUNG_RETRY_MS = 4000;++export function isRetryable({ status = null, body = '', networkError = false } = {}) {+ if (networkError) return true;+ if (status >= 500) return true;+ return status === 429 && !/billing|credit|quota|insufficient|not active/i.test(String(body));+}++const pause = (ms) => new Promise((done) => setTimeout(done, ms));++async function viaChatCompletions({ system, parts }, { url, key, model, label, extraHeaders = {}, textOnly = false, fetchImpl = fetch, attempts = RUNG_ATTEMPTS, retryMs = RUNG_RETRY_MS }) {if (!key) return null;if (textOnly && parts.some((part) => part.type === 'image')) return null;const content = parts.map((part) => (part.type === 'image' ? { type: 'image_url', image_url: { url: `data:${part.mime};base64,${part.data}` } } : { type: 'text', text: part.text }));- const response = await fetch(url, {- method: 'POST',- headers: { authorization: `Bearer ${key}`, 'content-type': 'application/json', ...extraHeaders },- body: JSON.stringify({ model, messages: [{ role: 'system', content: system }, { role: 'user', content }] }),- signal: AbortSignal.timeout(600_000),- });- if (!response.ok) throw new Error(`${label} ${response.status}: ${(await response.text()).slice(0, 200)}`);- const body = await response.json();- return { model: `${label}:${model}`, text: body.choices?.[0]?.message?.content || '' };+ const body = JSON.stringify({ model, messages: [{ role: 'system', content: system }, { role: 'user', content }] });+ for (let attempt = 1; ; attempt++) {+ let response;+ try {+ response = await fetchImpl(url, {+ method: 'POST',+ headers: { authorization: `Bearer ${key}`, 'content-type': 'application/json', ...extraHeaders },+ body,+ signal: AbortSignal.timeout(600_000),+ });+ } catch (err) {+ if (attempt < attempts && isRetryable({ networkError: true })) {+ await pause(retryMs * attempt);+ continue;+ }+ throw new Error(`${label} ${err?.message || err}${attempt > 1 ? ` (after ${attempt} tries)` : ''}`);+ }+ if (response.ok) {+ const parsed = await response.json();+ return { model: `${label}:${model}`, text: parsed.choices?.[0]?.message?.content || '' };+ }+ const text = (await response.text()).slice(0, 200);+ if (attempt < attempts && isRetryable({ status: response.status, body: text })) {+ await pause(retryMs * attempt);+ continue;+ }+ throw new Error(`${label} ${response.status}: ${text}${attempt > 1 ? ` (after ${attempt} tries)` : ''}`);+ }}+// Exported for tests: one chat-completions rung with an injected fetch.+export const chatCompletionsRung = viaChatCompletions;+export function modelRungs(request, env = process.env) {return [() => viaVertex(request),diff --git a/tests/x-content-llm.test.js b/tests/x-content-llm.test.jsnew file mode 100644index 000000000..0cd153813--- /dev/null+++ b/tests/x-content-llm.test.js@@ -0,0 +1,63 @@+import { describe, it, expect } from 'vitest';+import { chatCompletionsRung, isRetryable } from '../api/_lib/x-content/llm.js';++const request = { system: 'Return JSON.', parts: [{ type: 'text', text: 'Review this.' }] };+const rung = { url: 'https://integrate.api.nvidia.com/v1/chat/completions', key: 'k', model: 'moonshotai/kimi-k3', label: 'nvidia', retryMs: 0 };+const ok = (text) => ({ ok: true, status: 200, json: async () => ({ choices: [{ message: { content: text } }] }), text: async () => '' });+const fail = (status, text) => ({ ok: false, status, json: async () => ({}), text: async () => text });++function scripted(...steps) {+ const calls = [];+ return {+ calls,+ fetchImpl: async (url, init) => {+ calls.push(init);+ const step = steps[calls.length - 1];+ if (step instanceof Error) throw step;+ return step;+ },+ };+}++describe('which failures a rung tries again', () => {+ it('retries a dropped connection, a 5xx and a plain rate limit', () => {+ expect(isRetryable({ networkError: true })).toBe(true);+ expect(isRetryable({ status: 502 })).toBe(true);+ expect(isRetryable({ status: 429, body: 'Too Many Requests' })).toBe(true);+ });++ it('falls through at once on a refusal about billing or credentials', () => {+ expect(isRetryable({ status: 429, body: 'Your account is not active, please check your billing details' })).toBe(false);+ expect(isRetryable({ status: 402, body: 'Insufficient credits' })).toBe(false);+ expect(isRetryable({ status: 403, body: 'insufficient authentication scopes' })).toBe(false);+ expect(isRetryable({ status: 400, body: 'bad request' })).toBe(false);+ });+});++describe('a chat-completions rung', () => {+ it('answers after a dropped connection and a 503', async () => {+ const { calls, fetchImpl } = scripted(new TypeError('fetch failed'), fail(503, 'unavailable'), ok('{"verdict":"publish"}'));+ const result = await chatCompletionsRung(request, { ...rung, fetchImpl });+ expect(result).toEqual({ model: 'nvidia:moonshotai/kimi-k3', text: '{"verdict":"publish"}' });+ expect(calls).toHaveLength(3);+ expect(calls[0].body).toBe(calls[2].body);+ });++ it('gives up after its last try and says how many it took', async () => {+ const { calls, fetchImpl } = scripted(new TypeError('fetch failed'), new TypeError('fetch failed'), new TypeError('fetch failed'));+ await expect(chatCompletionsRung(request, { ...rung, fetchImpl })).rejects.toThrow('nvidia fetch failed (after 3 tries)');+ expect(calls).toHaveLength(3);+ });++ it('does not repeat a billing refusal', async () => {+ const { calls, fetchImpl } = scripted(fail(429, 'Your account is not active, please check your billing details'));+ await expect(chatCompletionsRung(request, { ...rung, fetchImpl })).rejects.toThrow('nvidia 429: Your account is not active');+ expect(calls).toHaveLength(1);+ });++ it('skips a text-only rung for a request that carries an image, and a rung with no key', async () => {+ const withImage = { system: 's', parts: [{ type: 'image', mime: 'image/jpeg', data: 'AA' }] };+ expect(await chatCompletionsRung(withImage, { ...rung, textOnly: true, fetchImpl: async () => ok('x') })).toBe(null);+ expect(await chatCompletionsRung(request, { ...rung, key: '', fetchImpl: async () => ok('x') })).toBe(null);+ });+});