Open History to see the last commit
1/**
2 * Write a provider's deprecations page onto its rows (issue #410).
3 *
4 * `models.deprecation` holds the page's entry as stated. A row with one is
5 * deprecated: `deprecatedAt` is the stated date, or when we first saw it
6 * deprecated when the page gives none. An id the page names that has no
7 * row gets a retired one, so a model shut down before we polled it still
8 * answers "retired, use X". Every change to a row's entry is one
9 * `model.deprecated` change.
10 */
11import { eq } from 'drizzle-orm'
12import type { Db } from '#/db/index.ts'
13import { changes, models } from '#/db/schema.ts'
14import type { ModelDeprecation } from '#/server/providers/types.ts'
15
16/** Row id → page entry. */
17export type DeprecationIndex = ReadonlyMap<string, ModelDeprecation>
18
19/**
20 * The entry a row carries this poll. No index (no page, or it failed)
21 * keeps what is stored.
22 */
23export function rowDeprecation(
24 index: DeprecationIndex | null,
25 id: string,
26 stored: unknown,
27): ModelDeprecation | null {
28 if (index) return index.get(id) ?? null
29 return (stored as ModelDeprecation | null | undefined) ?? null
30}
31
32function sameDeprecation(a: unknown, b: ModelDeprecation | null): boolean {
33 return JSON.stringify(a ?? null) === JSON.stringify(b)
34}
35
36function day(epoch: number | null): string | null {
37 return epoch === null
38 ? null
39 : new Date(epoch * 1000).toISOString().slice(0, 10)
40}
41
42function summary(rawId: string, entry: ModelDeprecation | null): string {
43 if (!entry) return `Model ${rawId} no longer on the deprecations page`
44 const parts = [`Model ${rawId} deprecated`]
45 const retires = day(entry.retiresAt)
46 if (retires) parts.push(`retires ${retires}`)
47 if (entry.replacedBy) parts.push(`use ${entry.replacedBy.join(' or ')}`)
48 return parts.join('; ')
49}
50
51/** Apply the page to every row of the provider. */
52export async function applyDeprecations(
53 db: Db,
54 providerId: string,
55 index: DeprecationIndex,
56 now: number,
57): Promise<void> {
58 const rows = await db
59 .select({
60 id: models.id,
61 rawId: models.rawId,
62 deprecatedAt: models.deprecatedAt,
63 deprecation: models.deprecation,
64 })
65 .from(models)
66 .where(eq(models.providerId, providerId))
67 const stored = new Set(rows.map((row) => row.id))
68 const writes = []
69 for (const row of rows) {
70 const entry = index.get(row.id) ?? null
71 if (sameDeprecation(row.deprecation, entry)) continue
72 writes.push(
73 db
74 .update(models)
75 .set({
76 deprecation: entry,
77 // A withdrawn entry leaves deprecatedAt to the listing: the poll
78 // already cleared it on a listed row, an unlisted one stays gone.
79 ...(entry
80 ? { deprecatedAt: entry.deprecatedAt ?? row.deprecatedAt ?? now }
81 : {}),
82 })
83 .where(eq(models.id, row.id)),
84 db.insert(changes).values({
85 id: crypto.randomUUID(),
86 type: 'model.deprecated',
87 providerId,
88 subjectId: row.id,
89 summary: summary(row.rawId, entry),
90 payload: { before: row.deprecation ?? null, after: entry },
91 createdAt: now,
92 }),
93 )
94 }
95 for (const [id, entry] of index) {
96 if (stored.has(id)) continue
97 writes.push(
98 db.insert(models).values({
99 id,
100 providerId,
101 rawId: entry.rawId,
102 firstSeenAt: now,
103 lastSeenAt: now,
104 deprecatedAt: entry.deprecatedAt ?? now,
105 deprecation: entry,
106 }),
107 db.insert(changes).values({
108 id: crypto.randomUUID(),
109 type: 'model.deprecated',
110 providerId,
111 subjectId: id,
112 summary: summary(entry.rawId, entry),
113 payload: { before: null, after: entry },
114 createdAt: now,
115 }),
116 )
117 }
118 // Batched: a first poll writes a provider's whole deprecation history.
119 for (let i = 0; i < writes.length; i += 40) {
120 const [first, ...rest] = writes.slice(i, i + 40)
121 if (first) await db.batch([first, ...rest])
122 }
123}