Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 44 additions & 16 deletions src/lib/providerModels/modelDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/shared/utils/freeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 })
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/free-badge-provider-gate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/model-discovery-free-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
Loading