Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat(fusion): diversify panel by model family, not just provider

jiazhen7153e1d89ff2
diff --git a/server/src/__tests__/routes/fusion.test.ts b/server/src/__tests__/routes/fusion.test.ts
index ca65495d..3f8af961 100644
--- a/server/src/__tests__/routes/fusion.test.ts
+++ b/server/src/__tests__/routes/fusion.test.ts
@@ -3,8 +3,8 @@ import type { Express } from 'express';
import { createApp } from '../../app.js';
import { initDb, getDb, getUnifiedApiKey } from '../../db/index.js';
import { mintDashboardToken, isGatedApiPath } from '../helpers/auth.js';
-import { isFusionModel, fusionConfigSchema } from '../../services/fusion.js';
-import { getOrderedFusionChain, setRoutingStrategy, getRoutingStrategy } from '../../services/router.js';
+import { isFusionModel, fusionConfigSchema, familyKey, diversifyChain } from '../../services/fusion.js';
+import { getOrderedFusionChain, setRoutingStrategy, getRoutingStrategy, type FusionCandidate } from '../../services/router.js';
import { setCooldown } from '../../services/ratelimit.js';
let dashToken = '';
@@ -409,3 +409,56 @@ describe('fusion route (/v1/chat/completions, model: "fusion")', () => {
expect(text.indexOf('"event":"panel"')).toBeLessThan(text.indexOf('STREAMED SYNTHESIS'));
});
});
+
+describe('familyKey', () => {
+ it('strips the provider prefix and any tag suffix to a bare family', () => {
+ expect(familyKey('qwen/qwen3-coder:free')).toBe('qwen3-coder');
+ expect(familyKey('qwen3-coder:480b')).toBe('qwen3-coder');
+ expect(familyKey('accounts/fireworks/models/qwen3-coder')).toBe('qwen3-coder');
+ expect(familyKey('GLM-4.7')).toBe('glm-4.7');
+ });
+});
+
+describe('diversifyChain', () => {
+ const cand = (platform: string, modelId: string): FusionCandidate => ({
+ modelDbId: 0, platform, modelId, displayName: modelId,
+ sizeLabel: 'Large', supportsVision: 0, supportsTools: 0,
+ });
+
+ it('prefers a fresh-family model over a same-family model on a new provider', () => {
+ // Strategy order: same family on two providers, then a different family.
+ // Platform-only diversity would panel [or/qwen3-coder, cb/qwen3-coder] —
+ // perspective-redundant. Family-aware diversity surfaces deepseek instead.
+ const ordered = [
+ cand('openrouter', 'qwen3-coder'),
+ cand('cerebras', 'qwen3-coder'),
+ cand('openrouter', 'deepseek-v3.2'),
+ ];
+ const out = diversifyChain(ordered).slice(0, 2).map(c => `${c.platform}/${c.modelId}`);
+ expect(out).toEqual(['openrouter/qwen3-coder', 'openrouter/deepseek-v3.2']);
+ });
+
+ it('keeps strategy order when every candidate is a distinct family', () => {
+ const ordered = [
+ cand('groq', 'llama-3.3-70b'),
+ cand('cerebras', 'qwen3-coder'),
+ cand('openrouter', 'deepseek-v3.2'),
+ ];
+ expect(diversifyChain(ordered).map(c => c.modelId)).toEqual(
+ ordered.map(c => c.modelId),
+ );
+ });
+
+ it('demotes a both-axes-seen duplicate to the back as last-resort refill', () => {
+ const ordered = [
+ cand('groq', 'llama-3.3-70b'),
+ cand('groq', 'llama-3.3-70b'), // same platform AND family → tier 3
+ cand('cerebras', 'qwen3-coder'),
+ ];
+ expect(diversifyChain(ordered).map(c => `${c.platform}/${c.modelId}`)).toEqual([
+ 'groq/llama-3.3-70b',
+ 'cerebras/qwen3-coder',
+ 'groq/llama-3.3-70b',
+ ]);
+ });
+});
diff --git a/server/src/services/fusion.ts b/server/src/services/fusion.ts
index 4afcd767..01b29286 100644
--- a/server/src/services/fusion.ts
+++ b/server/src/services/fusion.ts
@@ -344,6 +344,56 @@ async function runJudgeStreaming(
return { ok: false, error: lastError ?? 'no available key for judge' };
}
+/**
+ * Collapse a provider-specific model id to its rough model FAMILY: drop the
+ * provider prefix (everything up to the last '/') and any ':tag'/':free' suffix,
+ * so e.g. `qwen/qwen3-coder:free` and `qwen3-coder:480b` map to one family.
+ * Deliberately a SIMPLE heuristic, not a maintained alias map — cross-provider
+ * id naming drifts constantly, so we only want a good-enough signal to avoid
+ * stacking the panel with the same model served under two providers.
+ */
+export function familyKey(modelId: string): string {
+ return modelId.toLowerCase().replace(/^.*\//, '').replace(/:.*$/, '');
+}
+
+/**
+ * Order a strategy-sorted servable chain for panel diversity along TWO axes:
+ * provider (platform) AND model family. Fusion's value comes from genuinely
+ * DIFFERENT perspectives (issue #326 spike: a panel only beats the best single
+ * model when its members actually disagree); the same model family served by
+ * two providers is platform-distinct but perspective-redundant — one viewpoint
+ * filling two slots.
+ *
+ * Two stable passes, each preserving the routing-strategy order it's handed:
+ * 1. Provider-first (the existing invariant): one model per distinct platform
+ * before doubling up, so the panel spans different backends / failure
+ * domains.
+ * 2. Family-dedup: within that provider-diverse order, demote any model whose
+ * family already appeared — a fresh family takes the slot first, and the
+ * redundant copy sinks to the refill tail rather than being dropped.
+ * Pure function of its input (unit-tested directly).
+ */
+export function diversifyChain(ordered: FusionCandidate[]): FusionCandidate[] {
+ // Pass 1 — provider diversity first, strategy order within.
+ const seenPlatform = new Set<string>();
+ const platformFirst: FusionCandidate[] = [];
+ const platformRest: FusionCandidate[] = [];
+ for (const c of ordered) {
+ if (seenPlatform.has(c.platform)) platformRest.push(c);
+ else { seenPlatform.add(c.platform); platformFirst.push(c); }
+ }
+ // Pass 2 — demote same-family duplicates so a fresh perspective wins the slot.
+ const seenFamily = new Set<string>();
+ const fresh: FusionCandidate[] = [];
+ const dupFamily: FusionCandidate[] = [];
+ for (const c of [...platformFirst, ...platformRest]) {
+ const fam = familyKey(c.modelId);
+ if (seenFamily.has(fam)) dupFamily.push(c);
+ else { seenFamily.add(fam); fresh.push(c); }
+ }
+ return [...fresh, ...dupFamily];
+}
+
/**
* Build the panel plus a refill queue:
* - `panel` — the K models to run first (explicit list, or provider-diverse
@@ -351,8 +401,9 @@ async function runJudgeStreaming(
* - `overflow` — the next servable models from the chain, used to refill a slot
* when a panel model fails outright (auto mode only; an explicit
* panel is run as-is with no substitution).
- * Diversity = one model per platform first, so both the panel and its refills
- * span genuinely different backends before doubling up on a platform.
+ * Diversity = distinct provider AND model family first (see diversifyChain), so
+ * both the panel and its refills span genuinely different perspectives before
+ * doubling up on either axis.
*/
function selectPanel(config: FusionConfig): { panel: FusionCandidate[]; overflow: FusionCandidate[]; dropped: string[] } {
const maxK = panelMaxK();
@@ -376,17 +427,10 @@ function selectPanel(config: FusionConfig): { panel: FusionCandidate[]; overflow
const k = Math.min(Math.max(config.k ?? panelDefaultK(), 1), maxK);
const ordered = getOrderedFusionChain();
- // Diversity-first ordering of the whole servable chain: one model per distinct
- // platform (strategy order), then the remaining models. The first K are the
- // panel; the rest are refill candidates that stay as diverse as possible.
- const seenPlatform = new Set<string>();
- const diverse: FusionCandidate[] = [];
- const rest: FusionCandidate[] = [];
- for (const c of ordered) {
- if (seenPlatform.has(c.platform)) rest.push(c);
- else { seenPlatform.add(c.platform); diverse.push(c); }
- }
- const full = [...diverse, ...rest];
+ // Diversity-first ordering of the whole servable chain along provider AND
+ // model family (see diversifyChain). The first K are the panel; the rest are
+ // refill candidates that stay as diverse as possible.
+ const full = diversifyChain(ordered);
const panel = full.slice(0, k);
// Cap refills so a run of failures can't sweep the entire catalog: try at most