From 4bce38f010e736dd40a49259a8898e588d777593 Mon Sep 17 00:00:00 2001 From: Aldo Date: Sun, 27 Sep 2026 08:17:53 +0000 Subject: [PATCH 1/3] fix(providers): detect free models from input/output pricing and free tags Model discovery only read OpenRouter-style `pricing.prompt` / `pricing.completion`. Vercel AI Gateway's /v1/models publishes `pricing.input` / `pricing.output` and a `tags: ["free"]` array, so its zero-priced models synced without `isFree` and never appeared under the provider page's "Free only" filter. - Read `input`/`output` as fallbacks for `prompt`/`completion`. - Accept a `free` tag as live evidence unless the same payload publishes a non-zero price for that model. - Let the default (non-strict) provider-page badge honor explicit `isFree === true` on every provider, as strict mode already does, so live discovery evidence is not dropped for providers outside the static free catalog. --- src/lib/providerModels/modelDiscovery.ts | 60 ++++++++++++++----- src/shared/utils/freeModels.ts | 4 +- tests/unit/free-badge-provider-gate.test.ts | 16 +++++ .../model-discovery-free-metadata.test.ts | 25 ++++++++ 4 files changed, 88 insertions(+), 17 deletions(-) diff --git a/src/lib/providerModels/modelDiscovery.ts b/src/lib/providerModels/modelDiscovery.ts index 805f708a0604..bc3aabe2312f 100644 --- a/src/lib/providerModels/modelDiscovery.ts +++ b/src/lib/providerModels/modelDiscovery.ts @@ -27,17 +27,53 @@ function isZeroPrice(value: unknown): boolean { return Number.isFinite(parsed) && parsed === 0; } +function isPositivePrice(value: unknown): boolean { + if (typeof value === "number") return Number.isFinite(value) && value > 0; + if (typeof value !== "string" || value.trim().length === 0) return false; + const parsed = Number(value); + return Number.isFinite(parsed) && parsed > 0; +} + +type DiscoveredPrice = string | number | undefined; + +function readPrice(value: unknown): DiscoveredPrice { + return typeof value === "string" || typeof value === "number" ? value : undefined; +} + +/** + * Per-token prompt/completion prices from a /models entry. OpenRouter-shaped catalogs publish + * `pricing.prompt` / `pricing.completion`; Vercel AI Gateway publishes `pricing.input` / + * `pricing.output`. The OpenRouter names win when both are present. + */ +function readDiscoveredPricing(record: JsonRecord): { + prompt: DiscoveredPrice; + completion: DiscoveredPrice; +} { + const pricing = asRecord(record.pricing); + return { + prompt: readPrice(pricing.prompt) ?? readPrice(pricing.input), + completion: readPrice(pricing.completion) ?? readPrice(pricing.output), + }; +} + +function hasFreeTag(record: JsonRecord): boolean { + return ( + Array.isArray(record.tags) && + record.tags.some((tag) => typeof tag === "string" && tag.trim().toLowerCase() === "free") + ); +} + function hasLiveFreeEvidence( id: string, record: JsonRecord, - promptPrice: string | number | undefined, - completionPrice: string | number | undefined + promptPrice: DiscoveredPrice, + completionPrice: DiscoveredPrice ): boolean { - return ( - record.isFree === true || - id.endsWith(":free") || - (isZeroPrice(promptPrice) && isZeroPrice(completionPrice)) - ); + if (record.isFree === true || id.endsWith(":free")) return true; + if (isZeroPrice(promptPrice) && isZeroPrice(completionPrice)) return true; + // A `free` tag (Vercel AI Gateway) counts only when the same payload does not publish a + // non-zero price for the model: a tag must never badge a model that bills per token. + return hasFreeTag(record) && !isPositivePrice(promptPrice) && !isPositivePrice(completionPrice); } /** @@ -668,15 +704,7 @@ export function normalizeDiscoveredModels( // models reached the catalog with no vision flag and vision-capable models // (which work at request time) showed up as non-vision after import. const supportsVision = detectVisionInput(record); - const pricing = asRecord(record.pricing); - const promptPrice = - typeof pricing.prompt === "string" || typeof pricing.prompt === "number" - ? pricing.prompt - : undefined; - const completionPrice = - typeof pricing.completion === "string" || typeof pricing.completion === "number" - ? pricing.completion - : undefined; + const { prompt: promptPrice, completion: completionPrice } = readDiscoveredPricing(record); // Persist only evidence present in this discovery payload. Static catalog // membership is intentionally not evidence about this connection's economics. const isFree = hasLiveFreeEvidence(id, record, promptPrice, completionPrice); diff --git a/src/shared/utils/freeModels.ts b/src/shared/utils/freeModels.ts index 328973013b34..85fb218e9bf6 100644 --- a/src/shared/utils/freeModels.ts +++ b/src/shared/utils/freeModels.ts @@ -128,7 +128,8 @@ export const FREE_BADGE_STRICT_FLAG = "FREE_BADGE_REQUIRES_PROVIDER_FREE_TIER"; /** * Whether the provider-page model list shows the "Free" badge for a model row. * - * Default (`strict: false`) is the historical rule, unchanged: any truthy `free` field, + * Default (`strict: false`) is the historical rule: any truthy `free` field, an explicit + * `isFree === true` (live discovery evidence, honored on every provider as in strict mode), * a `:free` id suffix, "free"/"grátis" in the display name, or `isFreeModel`. * * With `strict: true` (feature flag FREE_BADGE_REQUIRES_PROVIDER_FREE_TIER) only badges @@ -149,6 +150,7 @@ export function isModelFreeBadge( if (!options.strict) { return ( Boolean(model.free) || + model.isFree === true || model.id.endsWith(":free") || /\bgr[aá]tis\b|\bfree\b/i.test(model.name || "") || isFreeModel(provider, { id: model.id, isFree: model.isFree as boolean | undefined }) diff --git a/tests/unit/free-badge-provider-gate.test.ts b/tests/unit/free-badge-provider-gate.test.ts index 34c1fdc0d5cf..0ee16a830b4c 100644 --- a/tests/unit/free-badge-provider-gate.test.ts +++ b/tests/unit/free-badge-provider-gate.test.ts @@ -66,6 +66,22 @@ describe("A. flag off: historical badge rule unchanged", () => { assert.equal(legacy(PAID_REGISTERED, { id: "gpt-9:free" }), true); }); + it("keeps the badge for explicit isFree evidence persisted by discovery", () => { + // Strict mode honors `isFree === true`; the default rule must be a superset of it, + // including on registered providers outside the static free catalog (e.g. a gateway + // whose live /models payload prices the model at 0). + assert.equal(legacy(PAID_REGISTERED, { id: "stealth/pixel-canary", isFree: true }), true); + assert.equal( + isModelFreeBadge( + PAID_REGISTERED, + { id: "stealth/pixel-canary", isFree: true }, + { strict: true } + ), + true + ); + assert.equal(legacy(PAID_REGISTERED, { id: "stealth/pixel-canary", isFree: "true" }), false); + }); + it("does not badge a plain paid model", () => { assert.equal(legacy(PAID_REGISTERED, { id: "gpt-9", name: "GPT 9" }), false); assert.equal(legacy(PAID_REGISTERED, { id: "gpt-9", name: "Freeform writer" }), false); diff --git a/tests/unit/model-discovery-free-metadata.test.ts b/tests/unit/model-discovery-free-metadata.test.ts index d704a2ed9f6f..7f58ce0c419b 100644 --- a/tests/unit/model-discovery-free-metadata.test.ts +++ b/tests/unit/model-discovery-free-metadata.test.ts @@ -24,6 +24,31 @@ test("normalizeDiscoveredModels records only free evidence present in discovery assert.equal(byId.get("paid")?.isFree, undefined); }); +test("normalizeDiscoveredModels reads Vercel AI Gateway input/output pricing and free tags", () => { + // Shape of https://ai-gateway.vercel.sh/v1/models: `pricing.input` / `pricing.output` + // (not OpenRouter's prompt/completion) plus an optional `tags` array. + const models = normalizeDiscoveredModels( + [ + { id: "stealth/pixel-canary", pricing: { input: "0", output: "0" } }, + { id: "poolside/laguna-s-2.1-free", pricing: { input: "0", output: "0" }, tags: ["free"] }, + { id: "vendor/tagged-no-price", tags: ["tool-use", "free"] }, + { id: "vendor/tagged-but-priced", pricing: { input: "0.1", output: "0.4" }, tags: ["free"] }, + { id: "vendor/half-free", pricing: { input: "0", output: "0.4" } }, + { id: "vendor/paid", pricing: { input: "0.1", output: "0.4" }, tags: ["reasoning"] }, + ], + "vercel-ai-gateway" + ); + const byId = new Map(models.map((model) => [model.id, model])); + + assert.equal(byId.get("stealth/pixel-canary")?.isFree, true); + assert.equal(byId.get("poolside/laguna-s-2.1-free")?.isFree, true); + assert.equal(byId.get("vendor/tagged-no-price")?.isFree, true); + // A published non-zero price outranks a tag: never badge a model that bills. + assert.equal(byId.get("vendor/tagged-but-priced")?.isFree, undefined); + assert.equal(byId.get("vendor/half-free")?.isFree, undefined); + assert.equal(byId.get("vendor/paid")?.isFree, undefined); +}); + test("normalizeSyncedAvailableModels preserves discovery free metadata", () => { const [model] = normalizeSyncedAvailableModels([ { id: "live-free", name: "Live Free", source: "imported", isFree: true }, From fd511f1b9bad9057c4a380b10d2894521569caef Mon Sep 17 00:00:00 2001 From: Aldo Date: Sun, 27 Sep 2026 08:31:57 +0000 Subject: [PATCH 2/3] docs(changelog): add fragment for #14930 --- changelog.d/fixes/14930-discovery-free-input-output-pricing.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/fixes/14930-discovery-free-input-output-pricing.md diff --git a/changelog.d/fixes/14930-discovery-free-input-output-pricing.md b/changelog.d/fixes/14930-discovery-free-input-output-pricing.md new file mode 100644 index 000000000000..3b92548753d8 --- /dev/null +++ b/changelog.d/fixes/14930-discovery-free-input-output-pricing.md @@ -0,0 +1 @@ +- **fix(providers):** Model discovery marks free models on catalogs that publish `pricing.input`/`pricing.output` or a `free` tag (Vercel AI Gateway), and the provider-page Free badge honors explicit `isFree` on every provider ([#14930](https://github.com/diegosouzapw/OmniRoute/pull/14930)) — thanks @aldoeliacim From f35c18ad8233835bddc0fb099c892384a65ab4cb Mon Sep 17 00:00:00 2001 From: Aldo Date: Sun, 27 Sep 2026 09:54:34 +0000 Subject: [PATCH 3/3] fix(providers): one payload free-evidence predicate for every /models reader Discovery, the import-only-free filter and the OpenRouter catalog each re-implemented "is this /models entry free", and each read only OpenRouter's pricing.prompt/pricing.completion. Measured on 2026-09-27 across the 47 distinct public provider catalogs reachable from the registry's /models URLs, that dropped real evidence in both directions: - input/output pricing and `free` tags were ignored (Vercel AI Gateway, xKiro): 34 zero-priced models never marked free; - a provider's explicit `isFree: false` / `free: false` was overridden by a 0/0 token price (Kilo, LLM Gateway): 48 models wrongly marked free; - a 0/0 token price marked models billed per request, image, second or minute as free (EUrouter, FastRouter, MegaNova, OFox): 23 models; - a top-level `free: true` with no pricing object was ignored (g4f): 1. hasPayloadFreeEvidence (src/shared/utils/payloadFreeEvidence.ts) applies one precedence to every reader: explicit boolean flag (top-level or in pricing) > `:free` suffix > zero prompt/input and completion/output with no positive price anywhere else in pricing > `free` tag under the same condition. Ratio fields (discount, multiplier, coefficient, thresholds) are not prices. Across those 47 catalogs the free set goes from 277 to 241 models: +35 detected, -71 false positives removed, no other differences. --- ...930-discovery-free-input-output-pricing.md | 2 +- src/app/api/v1/models/catalogOpenrouter.ts | 9 +- src/lib/providerModels/modelDiscovery.ts | 60 +-------- src/shared/utils/freeModels.ts | 21 +--- src/shared/utils/payloadFreeEvidence.ts | 97 ++++++++++++++ .../model-discovery-free-metadata.test.ts | 24 ++++ tests/unit/payload-free-evidence.test.ts | 118 ++++++++++++++++++ 7 files changed, 253 insertions(+), 78 deletions(-) create mode 100644 src/shared/utils/payloadFreeEvidence.ts create mode 100644 tests/unit/payload-free-evidence.test.ts diff --git a/changelog.d/fixes/14930-discovery-free-input-output-pricing.md b/changelog.d/fixes/14930-discovery-free-input-output-pricing.md index 3b92548753d8..b05ec18d0831 100644 --- a/changelog.d/fixes/14930-discovery-free-input-output-pricing.md +++ b/changelog.d/fixes/14930-discovery-free-input-output-pricing.md @@ -1 +1 @@ -- **fix(providers):** Model discovery marks free models on catalogs that publish `pricing.input`/`pricing.output` or a `free` tag (Vercel AI Gateway), and the provider-page Free badge honors explicit `isFree` on every provider ([#14930](https://github.com/diegosouzapw/OmniRoute/pull/14930)) — thanks @aldoeliacim +- **fix(providers):** Free-model detection reads each provider's own `/models` verdict through one shared predicate: `input`/`output` pricing and `free` tags count (Vercel AI Gateway, xKiro), explicit `isFree`/`free: false` vetoes a zero token price (Kilo, LLM Gateway), and a zero token price no longer marks models billed per request, image, second or minute as free ([#14930](https://github.com/diegosouzapw/OmniRoute/pull/14930)) — thanks @aldoeliacim diff --git a/src/app/api/v1/models/catalogOpenrouter.ts b/src/app/api/v1/models/catalogOpenrouter.ts index 07d5e00adbd5..8ba5964d525a 100644 --- a/src/app/api/v1/models/catalogOpenrouter.ts +++ b/src/app/api/v1/models/catalogOpenrouter.ts @@ -3,6 +3,8 @@ // model-type inference, and the free-model / display-name heuristics that shape // OpenRouter entries in `getUnifiedModelsResponse`. +import { hasPayloadFreeEvidence } from "@/shared/utils/payloadFreeEvidence"; + export function qualifyOpenRouterModelId(modelId: string): string { return modelId.startsWith("openrouter/") ? modelId : `openrouter/${modelId}`; } @@ -30,10 +32,11 @@ export function isZeroPrice(value: unknown) { export function isOpenRouterFreeModel(model: { id?: string; - pricing?: { prompt?: string; completion?: string }; + pricing?: unknown; + isFree?: unknown; + tags?: unknown; }) { - if (typeof model.id === "string" && model.id.endsWith(":free")) return true; - return isZeroPrice(model.pricing?.prompt) && isZeroPrice(model.pricing?.completion); + return hasPayloadFreeEvidence(model); } export function getOpenRouterDisplayName(model: { diff --git a/src/lib/providerModels/modelDiscovery.ts b/src/lib/providerModels/modelDiscovery.ts index bc3aabe2312f..969e15ae5cc8 100644 --- a/src/lib/providerModels/modelDiscovery.ts +++ b/src/lib/providerModels/modelDiscovery.ts @@ -9,6 +9,7 @@ import { CANONICAL_EFFORT_VALUES } from "@/shared/reasoning/effortStandardizatio import { isObsoleteKiroModelAlias } from "@omniroute/open-sse/services/kiroModels.ts"; import { filterSelectableModels } from "@omniroute/open-sse/services/modelLifecycle.ts"; import { getEmbeddingProvider } from "@omniroute/open-sse/config/embeddingRegistry.ts"; +import { hasPayloadFreeEvidence } from "@/shared/utils/payloadFreeEvidence"; type JsonRecord = Record; @@ -20,62 +21,6 @@ function toNonEmptyString(value: unknown): string | null { return typeof value === "string" && value.trim().length > 0 ? value.trim() : null; } -function isZeroPrice(value: unknown): boolean { - if (typeof value === "number") return value === 0; - if (typeof value !== "string" || value.trim().length === 0) return false; - const parsed = Number(value); - return Number.isFinite(parsed) && parsed === 0; -} - -function isPositivePrice(value: unknown): boolean { - if (typeof value === "number") return Number.isFinite(value) && value > 0; - if (typeof value !== "string" || value.trim().length === 0) return false; - const parsed = Number(value); - return Number.isFinite(parsed) && parsed > 0; -} - -type DiscoveredPrice = string | number | undefined; - -function readPrice(value: unknown): DiscoveredPrice { - return typeof value === "string" || typeof value === "number" ? value : undefined; -} - -/** - * Per-token prompt/completion prices from a /models entry. OpenRouter-shaped catalogs publish - * `pricing.prompt` / `pricing.completion`; Vercel AI Gateway publishes `pricing.input` / - * `pricing.output`. The OpenRouter names win when both are present. - */ -function readDiscoveredPricing(record: JsonRecord): { - prompt: DiscoveredPrice; - completion: DiscoveredPrice; -} { - const pricing = asRecord(record.pricing); - return { - prompt: readPrice(pricing.prompt) ?? readPrice(pricing.input), - completion: readPrice(pricing.completion) ?? readPrice(pricing.output), - }; -} - -function hasFreeTag(record: JsonRecord): boolean { - return ( - Array.isArray(record.tags) && - record.tags.some((tag) => typeof tag === "string" && tag.trim().toLowerCase() === "free") - ); -} - -function hasLiveFreeEvidence( - id: string, - record: JsonRecord, - promptPrice: DiscoveredPrice, - completionPrice: DiscoveredPrice -): boolean { - if (record.isFree === true || id.endsWith(":free")) return true; - if (isZeroPrice(promptPrice) && isZeroPrice(completionPrice)) return true; - // A `free` tag (Vercel AI Gateway) counts only when the same payload does not publish a - // non-zero price for the model: a tag must never badge a model that bills per token. - return hasFreeTag(record) && !isPositivePrice(promptPrice) && !isPositivePrice(completionPrice); -} - /** * Resolve a positive integer token limit from a list of candidate values. * Used to fall back across the differently-named context/output fields that @@ -704,10 +649,9 @@ export function normalizeDiscoveredModels( // models reached the catalog with no vision flag and vision-capable models // (which work at request time) showed up as non-vision after import. const supportsVision = detectVisionInput(record); - const { prompt: promptPrice, completion: completionPrice } = readDiscoveredPricing(record); // Persist only evidence present in this discovery payload. Static catalog // membership is intentionally not evidence about this connection's economics. - const isFree = hasLiveFreeEvidence(id, record, promptPrice, completionPrice); + const isFree = hasPayloadFreeEvidence({ ...record, id }); deduped.set(id, { id, diff --git a/src/shared/utils/freeModels.ts b/src/shared/utils/freeModels.ts index 85fb218e9bf6..3ad0e11fb11e 100644 --- a/src/shared/utils/freeModels.ts +++ b/src/shared/utils/freeModels.ts @@ -1,6 +1,7 @@ import { FREE_MODEL_BUDGETS, grantsFreeAccess } from "@omniroute/open-sse/config/freeModelCatalog"; import { getProviderById, resolveProviderId } from "@/shared/constants/providers"; import { globToRegex } from "@/shared/utils/globPattern"; +import { hasPayloadFreeEvidence } from "@/shared/utils/payloadFreeEvidence"; import { AI_MODELS } from "@/shared/constants/models"; /** @@ -71,17 +72,12 @@ export function providerHasFreeModels(providerId: string | undefined | null): bo ); } -function isZeroPrice(value: unknown): boolean { - if (typeof value === "number") return value === 0; - if (typeof value !== "string") return false; - const parsed = Number(value); - return Number.isFinite(parsed) && parsed === 0; -} - export interface FreeModelCandidate { id?: string; - pricing?: { prompt?: string | number; completion?: string | number }; + /** Raw `pricing` object from the provider's /models entry (any shape). */ + pricing?: unknown; isFree?: boolean; + tags?: unknown; } /** Shipped-catalog entry for this provider (id or alias): trusted on its own. */ @@ -93,20 +89,13 @@ function isCatalogFreeModel(provider: string, modelId: unknown): boolean { ); } -/** Payload-supplied free signals: `isFree:true`, a `:free` id suffix, or zero prices. */ -function hasPayloadFreeSignal(model: FreeModelCandidate): boolean { - if (model.isFree === true) return true; - if (typeof model.id === "string" && model.id.endsWith(":free")) return true; - return isZeroPrice(model.pricing?.prompt) && isZeroPrice(model.pricing?.completion); -} - /** * Whether a single fetched model qualifies as free for the given provider (id or alias): a * shipped-catalog entry, or a payload signal on a provider with a documented free tier. */ export function isFreeModel(provider: string, model: FreeModelCandidate): boolean { if (isCatalogFreeModel(provider, model.id)) return true; - return providerHasFreeModels(provider) && hasPayloadFreeSignal(model); + return providerHasFreeModels(provider) && hasPayloadFreeEvidence(model); } /** Reusable free predicate for fetched payloads — provider must have a documented free tier. */ diff --git a/src/shared/utils/payloadFreeEvidence.ts b/src/shared/utils/payloadFreeEvidence.ts new file mode 100644 index 000000000000..25599529cdd1 --- /dev/null +++ b/src/shared/utils/payloadFreeEvidence.ts @@ -0,0 +1,97 @@ +/** + * Payload free-evidence: whether a provider's own `/models` entry says the model costs nothing. + * + * One predicate for every place that reads a live catalog entry, so discovery, the + * import-only-free filter and the OpenRouter catalog cannot drift apart. + * + * Precedence, strongest first: + * 1. An explicit boolean flag (`isFree` / `is_free` / `free`, top-level or inside `pricing`) + * is the provider's own verdict and wins both ways: `false` vetoes a zero price. + * 2. A `:free` id suffix. + * 3. Zero per-token price (`prompt`/`completion` or `input`/`output`) with no positive price + * anywhere else in `pricing` (per-request, per-image, per-second, audio, ...). + * 4. A `free` tag, under the same no-positive-price condition. + */ + +type JsonRecord = Record; + +const FREE_FLAG_KEYS = ["isFree", "is_free", "free"] as const; + +/** Numeric fields inside `pricing` that are ratios/limits, not prices. */ +const NON_PRICE_KEYS = new Set([ + "discount", + "multiplier", + "coefficient", + "prompt_tokens_threshold", + "threshold", +]); + +function asRecord(value: unknown): JsonRecord | null { + return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : null; +} + +function toPrice(value: unknown): number | null { + if (typeof value === "number") return Number.isFinite(value) ? value : null; + if (typeof value !== "string" || value.trim().length === 0) return null; + const parsed = Number(value); + return Number.isFinite(parsed) ? parsed : null; +} + +function explicitFreeFlag(entry: JsonRecord, pricing: JsonRecord | null): boolean | null { + for (const source of [entry, pricing]) { + if (!source) continue; + for (const key of FREE_FLAG_KEYS) { + if (typeof source[key] === "boolean") return source[key] as boolean; + } + } + return null; +} + +function hasPositivePrice(value: unknown, depth = 0): boolean { + if (depth > 4) return false; + if (Array.isArray(value)) return value.some((item) => hasPositivePrice(item, depth + 1)); + const record = asRecord(value); + if (record) { + return Object.entries(record).some( + ([key, item]) => !NON_PRICE_KEYS.has(key) && hasPositivePrice(item, depth + 1) + ); + } + if (typeof value === "boolean") return false; + const price = toPrice(value); + return price !== null && price > 0; +} + +function firstPrice(pricing: JsonRecord, keys: readonly string[]): number | null { + for (const key of keys) { + const price = toPrice(pricing[key]); + if (price !== null) return price; + } + return null; +} + +function hasFreeTag(entry: JsonRecord): boolean { + return ( + Array.isArray(entry.tags) && + entry.tags.some((tag) => typeof tag === "string" && tag.trim().toLowerCase() === "free") + ); +} + +export function hasPayloadFreeEvidence(entry: unknown): boolean { + const record = asRecord(entry); + if (!record) return false; + const pricing = asRecord(record.pricing); + + const flag = explicitFreeFlag(record, pricing); + if (flag !== null) return flag; + + if (typeof record.id === "string" && record.id.endsWith(":free")) return true; + + if (pricing && hasPositivePrice(pricing)) return false; + + if (pricing) { + const prompt = firstPrice(pricing, ["prompt", "input"]); + const completion = firstPrice(pricing, ["completion", "output"]); + if (prompt === 0 && completion === 0) return true; + } + return hasFreeTag(record); +} diff --git a/tests/unit/model-discovery-free-metadata.test.ts b/tests/unit/model-discovery-free-metadata.test.ts index 7f58ce0c419b..1dffbd720442 100644 --- a/tests/unit/model-discovery-free-metadata.test.ts +++ b/tests/unit/model-discovery-free-metadata.test.ts @@ -49,6 +49,30 @@ test("normalizeDiscoveredModels reads Vercel AI Gateway input/output pricing and assert.equal(byId.get("vendor/paid")?.isFree, undefined); }); +test("normalizeDiscoveredModels honors provider free:false vetoes and non-token billing", () => { + // Real shapes: Kilo marks per-generation models isFree:false with 0/0 token prices, + // LLM Gateway marks routers free:false, EUrouter bills per request. + const models = normalizeDiscoveredModels( + [ + { + id: "google/lyria-3-pro-preview", + isFree: false, + pricing: { prompt: "0", completion: "0" }, + }, + { id: "auto", free: false, pricing: { prompt: "0", completion: "0", request: "0" } }, + { id: "deepseek-ocr-2", pricing: { prompt: "0", completion: "0", request: "0.02" } }, + { id: "agnes-3-0-flash", pricing: { free: true, input: null, output: null } }, + ], + "generic-gateway" + ); + const byId = new Map(models.map((model) => [model.id, model])); + + assert.equal(byId.get("google/lyria-3-pro-preview")?.isFree, undefined); + assert.equal(byId.get("auto")?.isFree, undefined); + assert.equal(byId.get("deepseek-ocr-2")?.isFree, undefined); + assert.equal(byId.get("agnes-3-0-flash")?.isFree, true); +}); + test("normalizeSyncedAvailableModels preserves discovery free metadata", () => { const [model] = normalizeSyncedAvailableModels([ { id: "live-free", name: "Live Free", source: "imported", isFree: true }, diff --git a/tests/unit/payload-free-evidence.test.ts b/tests/unit/payload-free-evidence.test.ts new file mode 100644 index 000000000000..16aad1a323fe --- /dev/null +++ b/tests/unit/payload-free-evidence.test.ts @@ -0,0 +1,118 @@ +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; + +import { hasPayloadFreeEvidence } from "../../src/shared/utils/payloadFreeEvidence.ts"; + +// Entry shapes are trimmed from real public /models payloads (2026-09-27). +describe("hasPayloadFreeEvidence", () => { + it("accepts OpenRouter-shaped zero prompt/completion pricing", () => { + assert.equal( + hasPayloadFreeEvidence({ id: "a/b", pricing: { prompt: "0", completion: "0" } }), + true + ); + }); + + it("accepts input/output pricing (Vercel AI Gateway, xKiro)", () => { + assert.equal( + hasPayloadFreeEvidence({ id: "stealth/pixel-canary", pricing: { input: "0", output: "0" } }), + true + ); + assert.equal( + hasPayloadFreeEvidence({ + id: "cohere/command-a", + pricing: { currency: "USD", unit: "per_1m_tokens", input: 0, output: 0 }, + }), + true + ); + }); + + it("accepts a free tag when no price is published", () => { + assert.equal(hasPayloadFreeEvidence({ id: "p/m", tags: ["tool-use", "Free"] }), true); + }); + + it("accepts the :free suffix", () => { + assert.equal(hasPayloadFreeEvidence({ id: "q/m:free" }), true); + }); + + it("lets an explicit flag win in both directions", () => { + // Kilo: isFree:false on a 0/0-priced model (billed per generation elsewhere). + assert.equal( + hasPayloadFreeEvidence({ + id: "google/lyria-3-pro-preview", + isFree: false, + pricing: { prompt: "0", completion: "0" }, + }), + false + ); + // LLM Gateway: free:false on routers whose token price is 0. + assert.equal( + hasPayloadFreeEvidence({ + id: "auto", + free: false, + pricing: { prompt: "0", completion: "0" }, + }), + false + ); + // Kenari: pricing.free:true with null token prices. + assert.equal( + hasPayloadFreeEvidence({ + id: "agnes-3-0-flash:free", + pricing: { free: true, input: null, output: null }, + }), + true + ); + // g4f: top-level free:true, no pricing at all. + assert.equal(hasPayloadFreeEvidence({ id: "sana", free: true }), true); + assert.equal(hasPayloadFreeEvidence({ id: "x:free", isFree: false }), false); + }); + + it("rejects zero token prices when another unit is billed", () => { + // EUrouter: per-request price. + assert.equal( + hasPayloadFreeEvidence({ + id: "deepseek-ocr-2", + pricing: { prompt: "0", completion: "0", request: "0.02", discount: 1 }, + }), + false + ); + // MegaNova: per-minute audio. + assert.equal( + hasPayloadFreeEvidence({ + id: "whisper", + pricing: { prompt: "0.00", completion: "0.00", audio_per_minute: "0.0060" }, + }), + false + ); + // FastRouter: nested per-resolution video cost. + assert.equal( + hasPayloadFreeEvidence({ + id: "v", + pricing: { prompt: "0", completion: "0", videoCost: [{ "720p": 0.1 }] }, + }), + false + ); + assert.equal( + hasPayloadFreeEvidence({ id: "t", tags: ["free"], pricing: { input: "0.1", output: "0" } }), + false + ); + }); + + it("ignores ratio fields that are not prices", () => { + assert.equal( + hasPayloadFreeEvidence({ id: "k", pricing: { prompt: "0", completion: "0", discount: 0.5 } }), + true + ); + }); + + it("rejects missing, partial, or malformed evidence", () => { + assert.equal(hasPayloadFreeEvidence(null), false); + assert.equal(hasPayloadFreeEvidence({ id: "m" }), false); + assert.equal(hasPayloadFreeEvidence({ id: "m", pricing: { input: "0" } }), false); + assert.equal(hasPayloadFreeEvidence({ id: "m", pricing: { input: "", output: "" } }), false); + assert.equal(hasPayloadFreeEvidence({ id: "m", isFree: "true" }), false); + assert.equal( + hasPayloadFreeEvidence({ id: "m", pricing: { input: "0", output: "0.000001" } }), + false + ); + }); +});