Open History to see the last commit
1/**
2 * Provider deprecation pages (issue #410): when a model was deprecated, when
3 * it shuts down, and what the provider says to use instead. Every value is
4 * read off the page as written; a cell the parser cannot read throws, so a
5 * reshaped page fails the source (`tryDocs`) instead of writing guesses.
6 */
7import type { DocsRun } from './model-facts.ts'
8import { tryDocs } from './model-facts.ts'
9import { fetchText } from './types.ts'
10import type { ModelDeprecation } from './types.ts'
11
12export const GROQ_DEPRECATIONS_URL =
13 'https://console.groq.com/docs/deprecations.md'
14export const MISTRAL_DEPRECATIONS_URL = 'https://docs.mistral.ai/models.md'
15
16const MONTHS = [
17 'january',
18 'february',
19 'march',
20 'april',
21 'may',
22 'june',
23 'july',
24 'august',
25 'september',
26 'october',
27 'november',
28 'december',
29]
30
31const MODEL_ID = /^[a-z0-9][a-z0-9._/:-]*$/
32
33function utc(year: number, month: number, day: number, text: string): number {
34 const ms = Date.UTC(year, month - 1, day)
35 const date = new Date(ms)
36 if (
37 month < 1 ||
38 month > 12 ||
39 date.getUTCFullYear() !== year ||
40 date.getUTCDate() !== day
41 ) {
42 throw new Error(`deprecations: not a date: ${JSON.stringify(text)}`)
43 }
44 return ms / 1000
45}
46
47/** `August 24, 2026` → epoch seconds, or null when `text` is not one. */
48function monthDayYear(text: string): number | null {
49 const m = /^([A-Za-z]+) (\d{1,2}), (\d{4})$/.exec(text.trim())
50 const month = m ? MONTHS.indexOf((m[1] ?? '').toLowerCase()) + 1 : 0
51 if (!m || month === 0) return null
52 return utc(Number(m[3]), month, Number(m[2]), text)
53}
54
55/** Table rows of a markdown block, header and divider dropped. */
56function tableRows(block: string): Array<Array<string>> {
57 const lines = block.split('\n').filter((line) => line.startsWith('|'))
58 return lines.slice(2).map((line) =>
59 line
60 .slice(
61 1,
62 line.trimEnd().endsWith('|') ? line.trimEnd().length - 1 : undefined,
63 )
64 .split('|')
65 .map((cell) => cell.trim()),
66 )
67}
68
69function requireId(id: string, source: string): string {
70 if (!MODEL_ID.test(id)) {
71 throw new Error(`${source}: not a model id: ${JSON.stringify(id)}`)
72 }
73 return id
74}
75
76/** A replacement id: an id with a digit, `-` or `/`, never a bare word. */
77function requireReplacement(id: string, source: string): string {
78 if (!/[0-9/-]/.test(id)) {
79 throw new Error(`${source}: not a replacement id: ${JSON.stringify(id)}`)
80 }
81 return requireId(id, source)
82}
83
84/**
85 * Groq's deprecation history: one `###` section per shutdown, each with a
86 * table of Model ID | Shutdown Date (`MM/DD/YY`) | Recommended Replacement
87 * Model ID. A replacement cell names one or more ids (`a or b`, `a b`,
88 * `a (text-only workloads)`), or `—` for none. The announcement date is
89 * the section prose's "on <Month D, YYYY>, we emailed/announced"; a
90 * section that states none, or only a month, leaves `deprecatedAt` null.
91 * The page is newest first, so an id listed twice keeps its latest entry.
92 */
93export function parseGroqDeprecations(
94 markdown: string,
95): Array<ModelDeprecation> {
96 const source = 'groq deprecations'
97 const history = markdown.split(/^## \[Deprecation History\]/m)[1]
98 if (history === undefined)
99 throw new Error(`${source}: no Deprecation History`)
100 const out = new Map<string, ModelDeprecation>()
101 for (const section of history.split(/^### /m).slice(1)) {
102 const announced =
103 /\b[Oo]n ([A-Z][a-z]+ \d{1,2}, \d{4}), we (?:emailed|announced)\b/.exec(
104 section,
105 )?.[1]
106 const deprecatedAt = announced ? monthDayYear(announced) : null
107 const header = section
108 .split('\n')
109 .find((line) => line.startsWith('|'))
110 ?.split('|')
111 .map((cell) => cell.trim())
112 .filter(Boolean)
113 if (
114 !header ||
115 !/^(Deprecated Model|Model ID)$/.test(header[0] ?? '') ||
116 header[1] !== 'Shutdown Date' ||
117 header[2] !== 'Recommended Replacement Model ID'
118 ) {
119 throw new Error(
120 `${source}: unexpected table in ${JSON.stringify(section.split('\n')[0])}`,
121 )
122 }
123 for (const row of tableRows(section)) {
124 if (row.length !== 3) {
125 throw new Error(`${source}: row has ${String(row.length)} cells`)
126 }
127 const [rawId, shutdown, replacement] = row as [string, string, string]
128 const date = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/.exec(shutdown)
129 if (!date) {
130 throw new Error(`${source}: shutdown date ${JSON.stringify(shutdown)}`)
131 }
132 const replacedBy =
133 replacement === '—' || replacement === ''
134 ? null
135 : replacement
136 .replace(/\([^)]*\)/g, ' ')
137 .split(/\s+/)
138 .filter((word) => word !== '' && word !== 'or')
139 .map((id) => requireReplacement(id, source))
140 requireId(rawId, source)
141 if (out.has(rawId)) continue
142 out.set(rawId, {
143 rawId,
144 deprecatedAt,
145 retiresAt: utc(
146 2000 + Number(date[3]),
147 Number(date[1]),
148 Number(date[2]),
149 shutdown,
150 ),
151 replacedBy,
152 sourceUrl: GROQ_DEPRECATIONS_URL,
153 })
154 }
155 }
156 if (out.size === 0) throw new Error(`${source}: parsed 0 model rows`)
157 return [...out.values()]
158}
159
160/** Mistral date cell: `2026-05-22` or `April 30, 2026`; empty is unstated. */
161function mistralDate(cell: string, source: string): number | null {
162 if (cell === '') return null
163 const iso = /^(\d{4})-(\d{2})-(\d{2})$/.exec(cell)
164 if (iso) return utc(Number(iso[1]), Number(iso[2]), Number(iso[3]), cell)
165 const prose = monthDayYear(cell)
166 if (prose === null) {
167 throw new Error(`${source}: date ${JSON.stringify(cell)}`)
168 }
169 return prose
170}
171
172const LINKED = /^\[[^\]]+\]\((\/models\/[a-z0-9-]+)\)$/
173const API_NAME = /^`([^`]+)`$/
174
175/**
176 * Mistral's Models Overview: the "Deprecated and retired models" table,
177 * Model | API name | Status | Context | Summary | Deprecation | Retirement
178 * | Replacement. The replacement is a link to a model page; its API name is
179 * the one the same page's tables give that link; a link no table names
180 * throws. Rows with no API name name no callable id and are skipped. An
181 * API name listed twice keeps its first (newest) row.
182 */
183export function parseMistralDeprecations(
184 markdown: string,
185): Array<ModelDeprecation> {
186 const source = 'mistral deprecations'
187 const [current, retired] = markdown.split(
188 /^## Deprecated and retired models$/m,
189 )
190 if (retired === undefined) {
191 throw new Error(`${source}: no "Deprecated and retired models" section`)
192 }
193 const header = retired
194 .split('\n')
195 .find((line) => line.startsWith('|'))
196 ?.split('|')
197 .map((cell) => cell.trim())
198 .filter(Boolean)
199 .join(' | ')
200 const expected =
201 'Model | API name | Status | Context | Summary | Deprecation | Retirement | Replacement'
202 if (header !== expected) {
203 throw new Error(`${source}: table header ${JSON.stringify(header)}`)
204 }
205 const apiNameOf = new Map<string, string>()
206 for (const row of [...tableRows(current ?? ''), ...tableRows(retired)]) {
207 const path = LINKED.exec(row[0] ?? '')?.[1]
208 const id = API_NAME.exec(row[1] ?? '')?.[1]
209 if (path && id && !apiNameOf.has(path)) apiNameOf.set(path, id)
210 }
211 const out = new Map<string, ModelDeprecation>()
212 for (const row of tableRows(retired)) {
213 if (row.length !== 8) {
214 throw new Error(`${source}: row has ${String(row.length)} cells`)
215 }
216 const [, apiName, status, , , deprecation, retirement, replacement] =
217 row as [string, string, string, string, string, string, string, string]
218 if (status !== 'Deprecated' && status !== 'Retired') {
219 throw new Error(`${source}: status ${JSON.stringify(status)}`)
220 }
221 if (apiName === '') continue
222 const rawId = API_NAME.exec(apiName)?.[1]
223 if (!rawId)
224 throw new Error(`${source}: API name ${JSON.stringify(apiName)}`)
225 requireId(rawId, source)
226 let replacedBy: Array<string> | null = null
227 if (replacement !== '') {
228 const path = LINKED.exec(replacement)?.[1]
229 if (!path) {
230 throw new Error(`${source}: replacement ${JSON.stringify(replacement)}`)
231 }
232 const id = apiNameOf.get(path)
233 if (!id) {
234 throw new Error(`${source}: replacement ${path} has no API name`)
235 }
236 replacedBy = [id]
237 }
238 if (out.has(rawId)) continue
239 out.set(rawId, {
240 rawId,
241 deprecatedAt: mistralDate(deprecation, source),
242 retiresAt: mistralDate(retirement, source),
243 replacedBy,
244 sourceUrl: MISTRAL_DEPRECATIONS_URL,
245 })
246 }
247 if (out.size === 0) throw new Error(`${source}: parsed 0 model rows`)
248 return [...out.values()]
249}
250
251/** Load one deprecations page through `tryDocs`; null when it failed. */
252export function loadDeprecations(
253 run: DocsRun,
254 kv: KVNamespace | undefined,
255 url: string,
256 parse: (markdown: string) => Array<ModelDeprecation>,
257): Promise<Array<ModelDeprecation> | null> {
258 return tryDocs(run, url, (cached) =>
259 cached(kv, url, async () => parse(await fetchText(url))),
260 )
261}