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(mcp): resolve agent-brain parallel-evolution merge defect

nirholasa1ae3a48ca70
diff --git a/api/_lib/agent-model.js b/api/_lib/agent-model.js
index 862a89811..0e4bdf099 100644
--- a/api/_lib/agent-model.js
+++ b/api/_lib/agent-model.js
@@ -116,14 +116,11 @@ export function resolveMessageModel({ requested = null, agentMeta = null, purpos
* platform chain stays behind it (also without tools for that turn) so the
* message is still answered when every route of the model is down.
* @param {string|null} model a catalog id, or null for the platform chain
- * @param {{ grokKey?: string|null }} [opts] `grokKey`: the agent owner's decrypted
- * BYOK xAI key (api/_lib/agent-grok-key.js), threaded ahead of the server's
- * GROK_API_KEY rung when `model` is a Grok model.
* @returns {{ chain: object[], tools: boolean }}
*/
-export function modelChain(model, opts = {}) {
+export function modelChain(model) {
if (!model) return { chain: providerChain(), tools: true };
- if (MODEL_CATALOG[model]?.tools) return { chain: providerChainFor(model, opts), tools: true };
+ if (MODEL_CATALOG[model]?.tools) return { chain: providerChainFor(model), tools: true };
const own = MODEL_CATALOG[model]?.provider === 'roster' ? rosterTransports(model) : [];
const seen = new Set(own.map((r) => `${r.name}|${r.model}`));
return { chain: [...own, ...providerChain().filter((p) => !seen.has(`${p.name}|${p.model}`))], tools: false };
diff --git a/api/_lib/llm-tool-chain.js b/api/_lib/llm-tool-chain.js
index 2ca99503b..e52a8e00d 100644
--- a/api/_lib/llm-tool-chain.js
+++ b/api/_lib/llm-tool-chain.js
@@ -234,26 +234,15 @@ export function modelRung(model) {
* model (model-roster.js) contributes one rung per reachable route; any other
* catalog model contributes its single lane. Models without tool calling have
* no rungs: the tool loop must never be pointed at them.
- *
- * A Grok model additionally gets an owner-key rung ahead of the server-key
- * rung when `opts.grokKey` is a decrypted BYOK key: the agent's owner saved
- * their own xAI key, so their key answers before the platform's GROK_API_KEY
- * (and before the free default chain, appended by providerChainFor).
* @param {string} requested a MODEL_CATALOG id (a retired id maps forward)
- * @param {{ grokKey?: string|null }} [opts]
*/
-export function modelRungs(requested, opts = {}) {
+export function modelRungs(requested) {
const model = resolveModelId(requested);
const meta = MODEL_CATALOG[model];
if (!meta || !meta.tools) return [];
if (meta.provider === 'roster') return rosterTransports(model);
- const rungs = [];
- if (meta.provider === 'grok' && opts.grokKey) {
- rungs.push({ name: 'grok', url: LANE_ENDPOINTS.grok.url, key: opts.grokKey, model, catalogModel: model, keySource: 'owner' });
- }
const rung = singleLaneRung(model, meta);
- if (rung) rungs.push({ ...rung, keySource: rung.keySource || 'platform' });
- return rungs;
+ return rung ? [rung] : [];
}
function singleLaneRung(model, meta) {
@@ -282,12 +271,11 @@ function singleLaneRung(model, meta) {
* rungs first (every route that serves it, in order), then the free-first
* platform chain behind them, minus any rung that would repeat one of them.
* @param {string|null} [model]
- * @param {{ grokKey?: string|null }} [opts] forwarded to modelRungs
*/
-export function providerChainFor(model, opts = {}) {
+export function providerChainFor(model) {
const chain = providerChain();
if (!model) return chain;
- const rungs = modelRungs(model, opts);
+ const rungs = modelRungs(model);
if (!rungs.length) return chain;
const seen = new Set(rungs.map((r) => `${r.name}|${r.model}`));
return [...rungs, ...chain.filter((p) => !seen.has(`${p.name}|${p.model}`))];
diff --git a/tests/agent-brain-default.test.js b/tests/agent-brain-default.test.js
index e3cbc4045..4aaea9dea 100644
--- a/tests/agent-brain-default.test.js
+++ b/tests/agent-brain-default.test.js
@@ -1,7 +1,8 @@
-// Order 044, task 1: profile chat (api/chat.js) and the web copilot
-// (api/agents/copilot.js) resolve an agent's chosen brain through the same
-// function other surfaces use (api/_lib/agent-model.js), never silently on
-// the platform default.
+// Order 044, task 1: profile chat (api/chat.js) resolves an agent's chosen
+// brain through the same function other surfaces use (api/_lib/agent-model.js),
+// never silently on the platform default. The web copilot's own brain
+// resolution (api/agents/copilot.js, via resolveAgentBrain) is covered in
+// tests/agent-brain.test.js.
import { describe, it, expect, vi } from 'vitest';
@@ -28,7 +29,6 @@ vi.mock('../api/_lib/auth.js', () => ({
vi.mock('../api/_lib/db.js', () => ({ sql: vi.fn(async () => []) }));
const { agentBrainDefaultFor } = await import('../api/chat.js');
-const { copilotModel } = await import('../api/agents/copilot.js');
const GROK_MODEL = 'grok-4.7';
@@ -75,23 +75,3 @@ describe('agentBrainDefaultFor (api/chat.js)', () => {
})).toBeNull();
});
});
-
-describe('copilotModel (api/agents/copilot.js)', () => {
- it('honors an explicit grok override', () => {
- const choice = copilotModel(GROK_MODEL, {});
- expect(choice.model).toBe(GROK_MODEL);
- expect(choice.source).toBe('message');
- });
-
- it('falls through to the agent default when no override is given', () => {
- const choice = copilotModel(undefined, { runtime: { model: GROK_MODEL } });
- expect(choice.model).toBe(GROK_MODEL);
- expect(choice.source).toBe('agent');
- });
-
- it('falls through to the platform chain when the agent default has no tool calling', () => {
- const choice = copilotModel(undefined, { runtime: { model: 'deepseek-r1' } });
- expect(choice.model).toBeNull();
- expect(choice.source).toBe('platform');
- });
-});
diff --git a/tests/agent-grok-brain.test.js b/tests/agent-grok-brain.test.js
index 4b4e99433..6b48b9d12 100644
--- a/tests/agent-grok-brain.test.js
+++ b/tests/agent-grok-brain.test.js
@@ -2,7 +2,7 @@
// agent-turn surface, with the owner's saved xAI key leading the server key
// and the server key leading the default chain.
-import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { describe, it, expect, vi, beforeEach } from 'vitest';
const sqlMock = vi.fn();
vi.mock('../api/_lib/db.js', () => ({
@@ -25,10 +25,7 @@ vi.mock('../api/_lib/provider-keys.js', () => ({
}));
const { loadOwnerGrokKey, GROK_PROVIDER } = await import('../api/_lib/agent-grok-key.js');
-const { modelRungs, providerChainFor } = await import('../api/_lib/llm-tool-chain.js');
-const { modelChain } = await import('../api/_lib/agent-model.js');
-const GROK_MODEL = 'grok-4.7';
const USER_ID = 'ca7f8e0a-29d0-4e2e-8a2a-2a5a5a5a5a5a';
describe('loadOwnerGrokKey', () => {
@@ -60,99 +57,3 @@ describe('loadOwnerGrokKey', () => {
await expect(loadOwnerGrokKey(USER_ID)).resolves.toBeNull();
});
});
-
-describe('modelRungs / providerChainFor: grok owner-key rung', () => {
- const prevGrokKey = process.env.GROK_API_KEY;
- const prevOpenrouterKey = process.env.OPENROUTER_API_KEY;
-
- afterEach(() => {
- process.env.GROK_API_KEY = prevGrokKey;
- process.env.OPENROUTER_API_KEY = prevOpenrouterKey;
- });
-
- it('leads with the owner key ahead of the server key', () => {
- process.env.GROK_API_KEY = 'xai-server-key';
- const rungs = modelRungs(GROK_MODEL, { grokKey: 'xai-owner-key' });
- expect(rungs).toHaveLength(2);
- expect(rungs[0]).toMatchObject({ name: 'grok', key: 'xai-owner-key', keySource: 'owner', model: GROK_MODEL });
- expect(rungs[1]).toMatchObject({ name: 'grok', key: 'xai-server-key', keySource: 'platform', model: GROK_MODEL });
- });
-
- it('serves only the server-key rung without an owner key', () => {
- process.env.GROK_API_KEY = 'xai-server-key';
- const rungs = modelRungs(GROK_MODEL, {});
- expect(rungs).toHaveLength(1);
- expect(rungs[0]).toMatchObject({ name: 'grok', key: 'xai-server-key', keySource: 'platform' });
- });
-
- it('serves only the owner-key rung when no server key is configured, so the agent still answers', () => {
- delete process.env.GROK_API_KEY;
- const rungs = modelRungs(GROK_MODEL, { grokKey: 'xai-owner-key' });
- expect(rungs).toHaveLength(1);
- expect(rungs[0]).toMatchObject({ name: 'grok', key: 'xai-owner-key', keySource: 'owner' });
- });
-
- it('has no rungs at all with neither key: the platform default chain is the only path left', () => {
- delete process.env.GROK_API_KEY;
- expect(modelRungs(GROK_MODEL, {})).toEqual([]);
- });
-
- it('providerChainFor puts the grok rungs ahead of the free default chain', () => {
- process.env.GROK_API_KEY = 'xai-server-key';
- delete process.env.OPENROUTER_API_KEY;
- const chain = providerChainFor(GROK_MODEL, { grokKey: 'xai-owner-key' });
- expect(chain[0]).toMatchObject({ name: 'grok', keySource: 'owner' });
- expect(chain[1]).toMatchObject({ name: 'grok', keySource: 'platform' });
- });
-
- it('providerChainFor falls back to the default chain alone when grok has no reachable rung', () => {
- delete process.env.GROK_API_KEY;
- process.env.OPENROUTER_API_KEY = 'or-key';
- const chain = providerChainFor(GROK_MODEL, {});
- expect(chain.every((r) => r.name !== 'grok')).toBe(true);
- expect(chain.some((r) => r.name === 'openrouter')).toBe(true);
- });
-});
-
-describe('modelChain: threading grokKey through to providerChainFor', () => {
- const prevGrokKey = process.env.GROK_API_KEY;
- afterEach(() => { process.env.GROK_API_KEY = prevGrokKey; });
-
- it('the owner rung leads a grok model chain', () => {
- process.env.GROK_API_KEY = 'xai-server-key';
- const { chain, tools } = modelChain(GROK_MODEL, { grokKey: 'xai-owner-key' });
- expect(tools).toBe(true);
- expect(chain[0]).toMatchObject({ name: 'grok', keySource: 'owner' });
- });
-
- it('a null model returns the platform chain untouched by grokKey', () => {
- const { chain } = modelChain(null, { grokKey: 'xai-owner-key' });
- expect(chain.every((r) => r.keySource !== 'owner')).toBe(true);
- });
-});
-
-describe('web copilot (api/agents/copilot.js): same owner-key-then-server-key composition', () => {
- beforeEach(() => { sqlMock.mockReset(); });
- const prevGrokKey = process.env.GROK_API_KEY;
- afterEach(() => { process.env.GROK_API_KEY = prevGrokKey; });
-
- it('mirrors copilot.js line-for-line: grokKey loaded only for a grok choice, then threaded into modelChain', async () => {
- process.env.GROK_API_KEY = 'xai-server-key';
- sqlMock.mockResolvedValueOnce([{ provider_keys: { [GROK_PROVIDER]: 'enc(xai-owner-key)' } }]);
- const { MODEL_CATALOG } = await import('../api/_lib/chat-models.js');
- const choice = { model: GROK_MODEL };
- const row = { user_id: USER_ID };
- const grokKey = choice.model && MODEL_CATALOG[choice.model]?.provider === 'grok' ? await loadOwnerGrokKey(row.user_id) : null;
- const { chain } = modelChain(choice.model, { grokKey });
- expect(chain[0]).toMatchObject({ name: 'grok', keySource: 'owner', key: 'xai-owner-key' });
- });
-
- it('never queries for a key when the choice is not grok', async () => {
- const choice = { model: 'claude-haiku-4-5-20251001' };
- const row = { user_id: USER_ID };
- const { MODEL_CATALOG } = await import('../api/_lib/chat-models.js');
- const grokKey = choice.model && MODEL_CATALOG[choice.model]?.provider === 'grok' ? await loadOwnerGrokKey(row.user_id) : null;
- expect(grokKey).toBeNull();
- expect(sqlMock).not.toHaveBeenCalled();
- });
-});