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

fix: models endpoint to return unique model_ids

Tazrif Raim6e86f7d06679
diff --git a/server/src/__tests__/routes/proxy-auto-model.test.ts b/server/src/__tests__/routes/proxy-auto-model.test.ts
index fac241d7..e456a1cb 100644
--- a/server/src/__tests__/routes/proxy-auto-model.test.ts
+++ b/server/src/__tests__/routes/proxy-auto-model.test.ts
@@ -70,6 +70,14 @@ describe('Virtual "auto" model', () => {
expect(body.data.length).toBeGreaterThan(1);
});
+ it('returns unique model ids from /v1/models', async () => {
+ const { status, body } = await request(app, 'GET', '/v1/models');
+ expect(status).toBe(200);
+
+ const ids = body.data.map((model: { id: string }) => model.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ });
+
it('treats model:"auto" as auto-route instead of a 400', async () => {
const origFetch = global.fetch;
diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.ts
index 9a7d81a2..e3fc6a9b 100644
--- a/server/src/routes/proxy.ts
+++ b/server/src/routes/proxy.ts
@@ -2,7 +2,7 @@ import crypto from 'crypto';
import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
-import type { ChatMessage } from '@freellmapi/shared/types.js';
+import type { ChatMessage, ModelListRow } from '@freellmapi/shared/types.js';
import { routeRequest, recordRateLimitHit, recordSuccess, hasEnabledVisionModel, hasEnabledToolsModel, type RouteResult } from '../services/router.js';
import { recordRequest, recordTokens, setCooldown, getCooldownDurationForLimit, PAYMENT_REQUIRED_COOLDOWN_MS } from '../services/ratelimit.js';
import { pruneRequestAnalytics } from '../services/request-retention.js';
@@ -105,7 +105,21 @@ export function setStickyModel(messages: ChatMessage[], modelDbId: number) {
// OpenAI-compatible /models endpoint (used by Hermes for metadata)
proxyRouter.get('/models', (_req: Request, res: Response) => {
const db = getDb();
- const models = db.prepare('SELECT platform, model_id, display_name, context_window FROM models WHERE enabled = 1 ORDER BY intelligence_rank').all() as any[];
+ const models = db.prepare(`
+ SELECT platform, model_id, display_name, context_window
+ FROM (
+ SELECT platform, model_id, display_name, context_window, intelligence_rank, id,
+ ROW_NUMBER() OVER (
+ PARTITION BY model_id
+ ORDER BY intelligence_rank ASC, id ASC
+ ) AS rn
+ FROM models
+ WHERE enabled = 1
+ )
+ WHERE rn = 1
+ ORDER BY intelligence_rank ASC, id ASC
+ `).all() as ModelListRow[];
+
res.json({
object: 'list',
data: [
diff --git a/shared/types.ts b/shared/types.ts
index cdbfc53f..458a4b22 100644
--- a/shared/types.ts
+++ b/shared/types.ts
@@ -48,6 +48,13 @@ export interface Model {
supportsTools: boolean;
}
+export interface ModelListRow {
+ platform: string;
+ model_id: string;
+ display_name: string;
+ context_window: number | null;
+}
+
export type KeyStatus = 'healthy' | 'rate_limited' | 'invalid' | 'error' | 'unknown';
export interface ApiKey {