diff --git a/src/server/providers/adapters/mistral.test.ts b/src/server/providers/adapters/mistral.test.ts index cf1308cc..08e581f4 100644 --- a/src/server/providers/adapters/mistral.test.ts +++ b/src/server/providers/adapters/mistral.test.ts @@ -1,5 +1,7 @@ import { readFileSync } from 'node:fs' +import { price } from '@modelschemas/rate-card' +import type { RateCard } from '@modelschemas/rate-card' import { describe, expect, it } from 'vitest' import { @@ -249,6 +251,239 @@ describe('mistral listModels modalities', () => { }) }) +describe('mistral medium 3.5 alias prices (#785)', () => { + it('gives listing aliases the named id’s catalog card when another alias disagrees', async () => { + const record = (overrides: Record) => ({ + slug: 'mistral-medium-3-5-26-04', + status: 'deprecated', + modelIds: ['mistral-medium-3-5', 'mistral-medium-latest'], + shortDescription: '', + deprecatedAt: '2026-10-31T00:00:00.000Z', + retiredAt: null, + replacedBy: null, + inputs: ['text'], + outputs: ['text'], + features: [], + maxOutputTokens: null, + releasedAt: '2026-04-28T00:00:00.000Z', + pricing: [ + { + direction: 'input', + label: 'Input', + unit: '/M Tokens', + priceUsd: 1.5, + }, + { + direction: 'output', + label: 'Output', + unit: '/M Tokens', + priceUsd: 7.5, + }, + ], + isFree: false, + ...overrides, + }) + const catalogHtml = (models: Array) => { + const stream = `1:${JSON.stringify(models)}\n` + return `Medium` + } + const medium = record({}) + const successor = record({ + slug: 'mistral-large-4-0', + status: 'ga', + modelIds: ['mistral-large-4', 'mistral-large-latest'], + deprecatedAt: null, + pricing: [ + { + direction: 'input', + label: 'Input', + unit: '/M Tokens', + priceUsd: 0.7, + }, + { + direction: 'output', + label: 'Output', + unit: '/M Tokens', + priceUsd: 2.1, + }, + ], + }) + const magistral = record({ + slug: 'magistral-medium-1-2-25-09', + status: 'ga', + modelIds: ['magistral-medium-latest'], + deprecatedAt: null, + releasedAt: '2025-09-18T00:00:00.000Z', + pricing: [ + { + direction: 'input', + label: 'Input', + unit: '/M Tokens', + priceUsd: 2, + }, + { + direction: 'output', + label: 'Output', + unit: '/M Tokens', + priceUsd: 5, + }, + ], + }) + const small = record({ + slug: 'mistral-small-4-0-26-03', + status: 'ga', + modelIds: ['mistral-small-latest'], + deprecatedAt: null, + pricing: [ + { + direction: 'input', + label: 'Input', + unit: '/M Tokens', + priceUsd: 0.15, + }, + { + direction: 'output', + label: 'Output', + unit: '/M Tokens', + priceUsd: 0.6, + }, + ], + }) + const magistralSmall = record({ + slug: 'magistral-small-1-2-25-09', + status: 'ga', + modelIds: ['magistral-small-latest'], + deprecatedAt: null, + pricing: [ + { + direction: 'input', + label: 'Input', + unit: '/M Tokens', + priceUsd: 0.5, + }, + { + direction: 'output', + label: 'Output', + unit: '/M Tokens', + priceUsd: 1.5, + }, + ], + }) + const alias = (id: string, name: string, aliases: Array) => ({ + id, + name, + aliases, + capabilities: { completion_chat: true }, + }) + const mediumAliases = [ + 'mistral-medium-latest', + 'mistral-medium-2604', + 'mistral-vibe-cli-latest', + 'magistral-medium-latest', + ] + const smallAliases = [ + 'mistral-small-latest', + 'mistral-vibe-cli-fast', + 'magistral-small-latest', + ] + const pages: Record = { + 'https://api.mistral.ai/v1/models': JSON.stringify({ + data: [ + alias( + 'mistral-medium-latest', + 'mistral-medium-latest', + mediumAliases, + ), + alias('mistral-medium-2604', 'mistral-medium-latest', mediumAliases), + alias( + 'mistral-vibe-cli-latest', + 'mistral-medium-latest', + mediumAliases, + ), + alias( + 'magistral-medium-latest', + 'mistral-medium-latest', + mediumAliases, + ), + alias('mistral-small-latest', 'mistral-small-latest', smallAliases), + alias('mistral-vibe-cli-fast', 'mistral-small-latest', smallAliases), + alias('magistral-small-latest', 'mistral-small-latest', smallAliases), + ], + }), + 'https://docs.mistral.ai/openapi.yaml': `components: + schemas: + ChatCompletionRequest: + properties: + max_tokens: { type: integer } +`, + 'https://docs.mistral.ai/inference/pricing': `

Flagship models

+

Prices /M Tokens

+ + +
Small$0.15—$0.6
`, + 'https://docs.mistral.ai/resources/changelogs': '', + [MISTRAL_WEBSEARCH_URL]: MISTRAL_WEBSEARCH_GUIDE, + 'https://docs.mistral.ai/models': catalogHtml([ + medium, + magistral, + magistralSmall, + ]), + 'https://docs.mistral.ai/models/mistral-medium-3-5-26-04': catalogHtml([ + medium, + successor, + ]), + 'https://docs.mistral.ai/models/magistral-medium-1-2-25-09': catalogHtml([ + magistral, + ]), + 'https://docs.mistral.ai/models/mistral-small-4-0-26-03': catalogHtml([ + small, + ]), + 'https://docs.mistral.ai/models/magistral-small-1-2-25-09': catalogHtml([ + magistralSmall, + ]), + } + const original = globalThis.fetch + globalThis.fetch = ((url: string) => { + const body = pages[String(url)] + return Promise.resolve( + body === undefined + ? new Response('missing', { status: 404 }) + : new Response(body), + ) + }) as typeof fetch + try { + const { models } = await provider.listModels({ + MISTRAL_API_KEY: 'k', + }) + const byId = new Map(models.map((model) => [model.rawId, model])) + const isCard = (value: unknown): value is RateCard => + typeof value === 'object' && value !== null && 'price' in value + const perMillion = (id: string): number => { + const card = byId.get(id)?.pricing + if (!isCard(card)) throw new Error(`${id} has no card`) + return price( + card, + {}, + { input_tokens: 1_000_000, output_tokens: 1_000_000 }, + ) + } + expect(perMillion('mistral-medium-latest')).toBeCloseTo(9, 6) + expect(perMillion('mistral-medium-2604')).toBeCloseTo(9, 6) + expect(perMillion('mistral-vibe-cli-latest')).toBeCloseTo(9, 6) + expect( + byId.get('mistral-medium-2604')?.factSources?.pricing?.sourceUrl, + ).toBe('https://docs.mistral.ai/models/mistral-medium-3-5-26-04') + expect(perMillion('magistral-medium-latest')).toBeCloseTo(7, 6) + // Live listing names vibe-cli-fast as small, so the medium card is wrong. + expect(perMillion('mistral-vibe-cli-fast')).toBeCloseTo(0.75, 6) + expect(perMillion('magistral-small-latest')).toBeCloseTo(2, 6) + expect(perMillion('mistral-small-latest')).toBeCloseTo(0.75, 6) + } finally { + globalThis.fetch = original + } + }) +}) + describe('mistral listModels release dates', () => { it('lets the listing alias settle an id two pages date differently', async () => { const page = (ids: Array) => diff --git a/src/server/providers/adapters/mistral.ts b/src/server/providers/adapters/mistral.ts index fcd9f33e..31290ab2 100644 --- a/src/server/providers/adapters/mistral.ts +++ b/src/server/providers/adapters/mistral.ts @@ -179,6 +179,8 @@ export const provider: ProviderConfig = { fetchSpec, listModels: async (env, kv): Promise => { const aliases = new Map>() + // Listing `name` when it is another row's id. A display string is not. + const namedAs = new Map() const listed = await listOpenAiCompatibleModels({ providerId: 'mistral', url: MISTRAL_MODELS_URL, @@ -190,6 +192,7 @@ export const provider: ProviderConfig = { (id) => typeof id === 'string' && id.length > 0, ) if (names && names.length > 0) aliases.set(row.id, names) + if (row.name && row.name !== row.id) namedAs.set(row.id, row.name) // `created` is the request time on every row, not a release date // (#708). The date comes from the model page's catalog record. return Promise.resolve({ releasedAt: null, ...mistralAliasFacts(row) }) @@ -251,7 +254,7 @@ export const provider: ProviderConfig = { : unavailable('maker'), ), ) - copyMistralAliasFacts(models, aliases) + copyMistralAliasFacts(models, aliases, namedAs) if (wire) { for (const model of models) { if (model.activity !== 'chat') continue diff --git a/src/server/providers/mistral-pricing.test.ts b/src/server/providers/mistral-pricing.test.ts index be3d562c..190e13f9 100644 --- a/src/server/providers/mistral-pricing.test.ts +++ b/src/server/providers/mistral-pricing.test.ts @@ -908,6 +908,61 @@ describe('mistral alias facts', () => { }) expect(models[4]?.pricing).toBeUndefined() }) + + it('copies the listing name when the alias group disagrees', () => { + const mediumSource = { + derivation: 'docs-derived' as const, + sourceUrl: 'https://docs.mistral.ai/models/mistral-medium-3-5-26-04', + path: 'Pricing', + } + const magistralSource = { + derivation: 'docs-derived' as const, + sourceUrl: 'https://docs.mistral.ai/models/magistral-medium-1-2-25-09', + path: 'Pricing', + } + const medium = { price: 1.5 } + const magistral = { price: 2 } + const models = [ + { + rawId: 'mistral-medium-latest', + pricing: medium, + factSources: { pricing: mediumSource }, + }, + { + rawId: 'magistral-medium-latest', + pricing: magistral, + factSources: { pricing: magistralSource }, + }, + { rawId: 'mistral-medium-2604' }, + { rawId: 'mistral-vibe-cli-latest' }, + { rawId: 'display-name' }, + ] + const group = [ + 'mistral-medium-latest', + 'magistral-medium-latest', + 'mistral-medium-2604', + 'mistral-vibe-cli-latest', + 'display-name', + ] + copyMistralAliasFacts( + models, + new Map(group.map((id) => [id, group.filter((other) => other !== id)])), + new Map([ + ['mistral-medium-2604', 'mistral-medium-latest'], + ['mistral-vibe-cli-latest', 'mistral-medium-latest'], + ['magistral-medium-latest', 'mistral-medium-latest'], + ['display-name', 'Mistral Medium'], + ]), + ) + expect(models[0]?.pricing).toEqual(medium) + expect(models[1]?.pricing).toEqual(magistral) + expect(models[2]).toMatchObject({ + pricing: medium, + factSources: { pricing: mediumSource }, + }) + expect(models[3]?.pricing).toEqual(medium) + expect(models[4]?.pricing).toBeUndefined() + }) }) describe('usdExpr', () => { diff --git a/src/server/providers/mistral-pricing.ts b/src/server/providers/mistral-pricing.ts index eeb6162a..37e462f6 100644 --- a/src/server/providers/mistral-pricing.ts +++ b/src/server/providers/mistral-pricing.ts @@ -28,6 +28,9 @@ * models overview pairs each API id with the page slug, including * deprecated rows the index-slug rules miss * (`mistral-medium-3-5` → `mistral-medium-3-5-26-04`). + * A listing alias the page does not name takes that page's card when + * the listing `name` is an id the page does name. A mutual alias with + * its own different card keeps that card (`magistral-medium-latest`). * * `releasedAt` is the catalog record's `releasedAt` (#708). The markdown * twin's `Release date:` line is the older statement. The listing's @@ -1894,11 +1897,15 @@ const ALIAS_FACTS = [ /** * Copy a fact onto mutual listing aliases when every member that has it - * agrees. A conflict copies nothing. The copy keeps the donor's docs URL. + * agrees. When they do not, an id whose listing `name` is another member + * of the group still takes that member's fact, and a member that already + * has its own fact keeps it. A `name` that is not a listed id copies + * nothing. The copy keeps the donor's docs URL. */ export function copyMistralAliasFacts( models: Array, aliases: ReadonlyMap>, + namedAs: ReadonlyMap = new Map(), ): void { const byId = new Map(models.map((model) => [model.rawId, model])) const parent = new Map() @@ -1943,14 +1950,19 @@ export function copyMistralAliasFacts( const first = donors[0] if (!first) continue const serialized = JSON.stringify(first[fact]) - if (donors.some((donor) => JSON.stringify(donor[fact]) !== serialized)) { - continue - } + const agreed = donors.every( + (donor) => JSON.stringify(donor[fact]) === serialized, + ) for (const id of group) { const target = byId.get(id) if (!target || target[fact] != null) continue - Object.assign(target, { [fact]: first[fact] }) - const source = first.factSources?.[fact] + const named = namedAs.get(id) + const namedDonor = + named && group.includes(named) ? byId.get(named) : undefined + const donor = agreed ? first : namedDonor + if (!donor || donor[fact] == null) continue + Object.assign(target, { [fact]: donor[fact] }) + const source = donor.factSources?.[fact] if (!source) continue target.factSources = { ...target.factSources, [fact]: source } } diff --git a/src/server/providers/openai-compat.ts b/src/server/providers/openai-compat.ts index e89d306a..7e3bcbf8 100644 --- a/src/server/providers/openai-compat.ts +++ b/src/server/providers/openai-compat.ts @@ -133,6 +133,12 @@ export interface OpenAiCompatModelRow { capabilities?: Record /** mistral: other ids that resolve to this same row. */ aliases?: Array + /** + * mistral: the listed id this row is (`mistral-medium-2604` names + * `mistral-medium-latest`). A display string that matches no listed id + * is ignored. + */ + name?: string /** groq: the maker's name (`OpenAI`, `Alibaba Cloud`). */ owned_by?: string /** fireworks: `HF_BASE_MODEL`, `EMBEDDING_MODEL`, `ROUTER`, `CUSTOM_MODEL`. */