|
| 1 | +/// <reference types="node" /> |
| 2 | + |
| 3 | +/** |
| 4 | + * Changelog Radar |
| 5 | + * |
| 6 | + * Fetches the OpenAI API changelog, filters for entries relevant to aimock's |
| 7 | + * provider surface, and outputs a JSON report of new entries since the last run. |
| 8 | + * |
| 9 | + * On first run (no cursor file), sets the cursor to today and reports nothing. |
| 10 | + * If parsing fails, logs a warning and exits 0 (never fails the workflow). |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * npx tsx scripts/changelog-radar.ts |
| 14 | + * |
| 15 | + * Output (stdout): JSON report when new entries found, empty otherwise. |
| 16 | + * Side effect: updates .changelog-radar-cursor with the latest entry date. |
| 17 | + */ |
| 18 | + |
| 19 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 20 | +import { resolve } from "node:path"; |
| 21 | + |
| 22 | +// --------------------------------------------------------------------------- |
| 23 | +// Config |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | + |
| 26 | +const CHANGELOG_URL = "https://platform.openai.com/docs/changelog"; |
| 27 | +const CURSOR_FILE = resolve(import.meta.dirname ?? ".", "../.changelog-radar-cursor"); |
| 28 | + |
| 29 | +const SURFACE_KEYWORDS = [ |
| 30 | + "realtime", |
| 31 | + "chat", |
| 32 | + "completions", |
| 33 | + "embeddings", |
| 34 | + "responses", |
| 35 | + "audio", |
| 36 | + "speech", |
| 37 | + "transcription", |
| 38 | + "images", |
| 39 | + "moderation", |
| 40 | + "models", |
| 41 | + "deprecat", |
| 42 | + "breaking", |
| 43 | + "websocket", |
| 44 | +]; |
| 45 | + |
| 46 | +// --------------------------------------------------------------------------- |
| 47 | +// Types |
| 48 | +// --------------------------------------------------------------------------- |
| 49 | + |
| 50 | +interface ChangelogEntry { |
| 51 | + date: string; |
| 52 | + title: string; |
| 53 | + url: string; |
| 54 | + keywords: string[]; |
| 55 | +} |
| 56 | + |
| 57 | +interface RadarReport { |
| 58 | + newEntries: ChangelogEntry[]; |
| 59 | + since: string; |
| 60 | +} |
| 61 | + |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | +// Helpers |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +function readCursor(): string | null { |
| 67 | + if (!existsSync(CURSOR_FILE)) return null; |
| 68 | + const raw = readFileSync(CURSOR_FILE, "utf-8").trim(); |
| 69 | + // Validate it looks like a date |
| 70 | + if (/^\d{4}-\d{2}-\d{2}$/.test(raw)) return raw; |
| 71 | + return null; |
| 72 | +} |
| 73 | + |
| 74 | +function writeCursor(date: string): void { |
| 75 | + writeFileSync(CURSOR_FILE, date + "\n", "utf-8"); |
| 76 | +} |
| 77 | + |
| 78 | +function matchKeywords(text: string): string[] { |
| 79 | + const lower = text.toLowerCase(); |
| 80 | + return SURFACE_KEYWORDS.filter((kw) => lower.includes(kw)); |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Parse changelog entries from the HTML page. |
| 85 | + * |
| 86 | + * The OpenAI changelog page uses a structured format with date headings and |
| 87 | + * entry titles. We look for common patterns: |
| 88 | + * - Date strings like "January 15, 2025" or "2025-01-15" |
| 89 | + * - Heading-like elements following dates |
| 90 | + * |
| 91 | + * This is intentionally loose — we'd rather over-match than miss entries. |
| 92 | + */ |
| 93 | +function parseEntries(html: string): ChangelogEntry[] { |
| 94 | + const entries: ChangelogEntry[] = []; |
| 95 | + |
| 96 | + // Strategy 1: Look for date patterns followed by content blocks. |
| 97 | + // OpenAI's changelog typically has entries with dates in heading elements. |
| 98 | + // Match patterns like: <h2>January 15, 2025</h2> or date attributes |
| 99 | + const dateContentPattern = |
| 100 | + /(?:<h[23][^>]*>|<time[^>]*>|<div[^>]*date[^>]*>)\s*([A-Z][a-z]+ \d{1,2},?\s*\d{4}|\d{4}-\d{2}-\d{2})\s*(?:<\/h[23]>|<\/time>|<\/div>)/gi; |
| 101 | + |
| 102 | + // Also try: entries as list items or article elements with dates |
| 103 | + const entryPattern = |
| 104 | + /(\d{4}-\d{2}-\d{2}|(?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},?\s*\d{4})[^<]*<[^>]*>([^<]{5,200})/gi; |
| 105 | + |
| 106 | + // Strategy 2: Broader pattern — grab anything that looks like a dated entry |
| 107 | + const broadPattern = |
| 108 | + /((?:January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},?\s*\d{4}|\d{4}-\d{2}-\d{2})[\s\S]{0,500}?(?:<[hH][1-6][^>]*>|<a[^>]*>|<strong>|<b>)\s*([^<]{5,200})/g; |
| 109 | + |
| 110 | + const seen = new Set<string>(); |
| 111 | + |
| 112 | + for (const pattern of [dateContentPattern, entryPattern, broadPattern]) { |
| 113 | + let match; |
| 114 | + while ((match = pattern.exec(html)) !== null) { |
| 115 | + const rawDate = match[1]!.trim(); |
| 116 | + const title = (match[2] ?? "").replace(/<[^>]+>/g, "").trim(); |
| 117 | + |
| 118 | + // Normalize date to YYYY-MM-DD |
| 119 | + const normalizedDate = normalizeDate(rawDate); |
| 120 | + if (!normalizedDate) continue; |
| 121 | + |
| 122 | + const key = `${normalizedDate}:${title.slice(0, 80)}`; |
| 123 | + if (seen.has(key) || !title) continue; |
| 124 | + seen.add(key); |
| 125 | + |
| 126 | + entries.push({ |
| 127 | + date: normalizedDate, |
| 128 | + title, |
| 129 | + url: `${CHANGELOG_URL}#${normalizedDate}`, |
| 130 | + keywords: [], |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Sort newest first |
| 136 | + entries.sort((a, b) => b.date.localeCompare(a.date)); |
| 137 | + return entries; |
| 138 | +} |
| 139 | + |
| 140 | +function normalizeDate(raw: string): string | null { |
| 141 | + // Already YYYY-MM-DD |
| 142 | + if (/^\d{4}-\d{2}-\d{2}$/.test(raw)) return raw; |
| 143 | + |
| 144 | + // "Month DD, YYYY" or "Month DD YYYY" |
| 145 | + const parsed = new Date(raw); |
| 146 | + if (isNaN(parsed.getTime())) return null; |
| 147 | + |
| 148 | + const y = parsed.getFullYear(); |
| 149 | + const m = String(parsed.getMonth() + 1).padStart(2, "0"); |
| 150 | + const d = String(parsed.getDate()).padStart(2, "0"); |
| 151 | + return `${y}-${m}-${d}`; |
| 152 | +} |
| 153 | + |
| 154 | +// --------------------------------------------------------------------------- |
| 155 | +// Main |
| 156 | +// --------------------------------------------------------------------------- |
| 157 | + |
| 158 | +async function main(): Promise<void> { |
| 159 | + // Fetch the changelog page |
| 160 | + let html: string; |
| 161 | + try { |
| 162 | + const resp = await fetch(CHANGELOG_URL, { |
| 163 | + headers: { "User-Agent": "aimock-changelog-radar/1.0" }, |
| 164 | + }); |
| 165 | + if (!resp.ok) { |
| 166 | + console.warn(`Changelog fetch failed: ${resp.status} ${resp.statusText}`); |
| 167 | + process.exit(0); |
| 168 | + } |
| 169 | + html = await resp.text(); |
| 170 | + } catch (err) { |
| 171 | + console.warn(`Changelog fetch error: ${err}`); |
| 172 | + process.exit(0); |
| 173 | + } |
| 174 | + |
| 175 | + // Parse entries |
| 176 | + const allEntries = parseEntries(html); |
| 177 | + if (allEntries.length === 0) { |
| 178 | + console.warn("No changelog entries parsed — format may have changed"); |
| 179 | + process.exit(0); |
| 180 | + } |
| 181 | + |
| 182 | + // Read cursor |
| 183 | + const cursor = readCursor(); |
| 184 | + const today = new Date().toISOString().slice(0, 10); |
| 185 | + |
| 186 | + // First run: set cursor and exit |
| 187 | + if (!cursor) { |
| 188 | + const newestDate = allEntries[0]?.date ?? today; |
| 189 | + writeCursor(newestDate); |
| 190 | + console.log(`First run — cursor set to ${newestDate}. No entries to report.`); |
| 191 | + process.exit(0); |
| 192 | + } |
| 193 | + |
| 194 | + // Filter to entries newer than cursor |
| 195 | + const newEntries = allEntries.filter((e) => e.date > cursor); |
| 196 | + |
| 197 | + if (newEntries.length === 0) { |
| 198 | + console.log(`No new entries since ${cursor}.`); |
| 199 | + process.exit(0); |
| 200 | + } |
| 201 | + |
| 202 | + // Filter for surface-relevant entries |
| 203 | + const relevant: ChangelogEntry[] = []; |
| 204 | + for (const entry of newEntries) { |
| 205 | + const kw = matchKeywords(`${entry.title} ${entry.url}`); |
| 206 | + if (kw.length > 0) { |
| 207 | + entry.keywords = kw; |
| 208 | + relevant.push(entry); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + // Update cursor to newest entry |
| 213 | + const newestDate = newEntries[0]?.date ?? today; |
| 214 | + writeCursor(newestDate); |
| 215 | + |
| 216 | + if (relevant.length === 0) { |
| 217 | + console.log( |
| 218 | + `${newEntries.length} new entries since ${cursor}, but none matched surface keywords.`, |
| 219 | + ); |
| 220 | + process.exit(0); |
| 221 | + } |
| 222 | + |
| 223 | + // Output report |
| 224 | + const report: RadarReport = { |
| 225 | + newEntries: relevant, |
| 226 | + since: cursor, |
| 227 | + }; |
| 228 | + |
| 229 | + console.log(JSON.stringify(report, null, 2)); |
| 230 | +} |
| 231 | + |
| 232 | +main().catch((err) => { |
| 233 | + console.warn(`Unhandled error: ${err}`); |
| 234 | + process.exit(0); |
| 235 | +}); |
0 commit comments