Open History to see the last commit
1/**
2 * Mistral — public OpenAPI 3.1 at docs.mistral.ai/openapi.yaml.
3 * Models listing requires MISTRAL_API_KEY.
4 */
5import type { Activity } from '#/db/schema.ts'
6import { listOpenAiCompatibleModels } from '../openai-compat.ts'
7import {
8 mistralGenerationEndpointId,
9 mistralModelActivity,
10} from '../model-meta.ts'
11import {
12 copyMistralAliasFacts,
13 mistralModelPricing,
14} from '../mistral-pricing.ts'
15import {
16 MISTRAL_OPENAPI_URL,
17 mistralChatWire,
18 mistralRequestMap,
19} from '../mistral-request.ts'
20import {
21 listsReasoning,
22 mistralModelReasoning,
23 overlayModelFacts,
24} from '../reasoning-config.ts'
25import { fetchOpenApi } from '../types.ts'
26import type {
27 ListModelsResult,
28 ModelInfo,
29 ProviderConfig,
30 ProviderSecrets,
31 SpecFetchResult,
32} from '../types.ts'
33
34const MISTRAL_MODELS_URL = 'https://api.mistral.ai/v1/models'
35
36/**
37 * Generation surface only. Chat/fim/agents-completions are chat;
38 * classifiers that are not moderation, OCR, conversations CRUD, files,
39 * fine-tune, batch, and admin classify to null.
40 */
41function classify(path: string): Activity | null {
42 const bare = path.includes('#') ? path.slice(0, path.indexOf('#')) : path
43 if (
44 bare === '/v1/chat/completions' ||
45 bare === '/v1/fim/completions' ||
46 bare === '/v1/agents/completions'
47 ) {
48 return 'chat'
49 }
50 if (bare === '/v1/embeddings') return 'embeddings'
51 if (bare === '/v1/moderations' || bare === '/v1/chat/moderations') {
52 return 'moderation'
53 }
54 if (bare === '/v1/audio/speech' || bare === '/v1/audio/transcriptions') {
55 return 'audio'
56 }
57 return null
58}
59
60/**
61 * A page we read that names no price clears a stored card. Mutual aliases
62 * of that page clear too, unless one of them has a price.
63 */
64function clearUnpriced(
65 models: Array<ModelInfo>,
66 seen: ReadonlySet<string>,
67 aliases: ReadonlyMap<string, ReadonlyArray<string>>,
68): void {
69 const byId = new Map(models.map((model) => [model.rawId, model]))
70 const clear = (model: ModelInfo | undefined) => {
71 if (!model || model.pricing != null) return
72 model.absent = { ...(model.absent ?? {}), pricing: 'cleared' }
73 }
74 for (const model of models) {
75 if (seen.has(model.rawId)) clear(model)
76 }
77 for (const [id, names] of aliases) {
78 const clique = [id, ...names].filter(
79 (other) =>
80 byId.has(other) && (other === id || aliases.get(other)?.includes(id)),
81 )
82 if (!clique.some((other) => seen.has(other))) continue
83 if (clique.some((other) => byId.get(other)?.pricing != null)) continue
84 for (const other of clique) clear(byId.get(other))
85 }
86}
87
88async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
89 const { spec, hash } = await fetchOpenApi(MISTRAL_OPENAPI_URL)
90 return {
91 specs: [spec],
92 sources: [{ url: MISTRAL_OPENAPI_URL, hash }],
93 outputStrategy: 'post-200',
94 }
95}
96
97export const provider: ProviderConfig = {
98 id: 'mistral',
99 displayName: 'Mistral',
100 authEnvVar: 'MISTRAL_API_KEY',
101 specSourceUrl: MISTRAL_OPENAPI_URL,
102 modelsEndpoint: MISTRAL_MODELS_URL,
103 defaultDerivation: 'upstream-spec',
104 fetchSpec,
105 listModels: async (env, kv): Promise<ListModelsResult> => {
106 const aliases = new Map<string, Array<string>>()
107 const listed = await listOpenAiCompatibleModels({
108 providerId: 'mistral',
109 url: MISTRAL_MODELS_URL,
110 env,
111 envVar: 'MISTRAL_API_KEY',
112 activity: mistralModelActivity,
113 extend: (row) => {
114 const names = row.aliases?.filter(
115 (id) => typeof id === 'string' && id.length > 0,
116 )
117 if (names && names.length > 0) aliases.set(row.id, names)
118 return Promise.resolve({})
119 },
120 })
121 if (listed.models.length === 0) return listed
122 const chat = listed.models.some((model) => model.activity === 'chat')
123 const [pricing, reasoning, wire] = await Promise.all([
124 mistralModelPricing(
125 kv,
126 listed.models.map((model) => model.rawId),
127 ),
128 mistralModelReasoning(kv),
129 chat ? mistralChatWire(kv) : Promise.resolve(null),
130 ])
131 const models = listed.models.map((model) =>
132 overlayModelFacts(
133 model,
134 pricing(model.rawId),
135 reasoning(model.rawId, listsReasoning(model.capabilities)),
136 ),
137 )
138 copyMistralAliasFacts(models, aliases)
139 if (wire) {
140 for (const model of models) {
141 if (model.activity !== 'chat') continue
142 model.requestMap = mistralRequestMap(wire, model.reasoning)
143 model.factSources = {
144 ...(model.factSources ?? {}),
145 requestMap: {
146 derivation: 'docs-derived',
147 sourceUrl: MISTRAL_OPENAPI_URL,
148 sourceHash: wire.hash,
149 path: '/components/schemas/ChatCompletionRequest',
150 },
151 }
152 }
153 }
154 clearUnpriced(models, pricing.seen, aliases)
155 return { ...listed, models }
156 },
157 classify,
158 generationEndpointId: ({ rawId, activity }) =>
159 mistralGenerationEndpointId(rawId, activity),
160}