Open History to see the last commit
1/**
2 * Gap report: which facts `@tanstack/ai-models` needs are still missing,
3 * per provider, over chat rows. One HTTP request; no per-model detail calls.
4 *
5 * pnpm run gap:report # JSON
6 * pnpm run gap:report -- --table # readable, worst score first
7 * pnpm run gap:report -- --base http://localhost:3100
8 * pnpm run gap:report -- --check --providers grok,mistral --target 0.9
9 *
10 * `docs/source-silent/<provider>.md` lists facts a provider does not publish; those
11 * are left out of that provider's score and listed under `silent`. `flags`
12 * lists rows whose stored shape is wrong; flags do not change the score.
13 */
14import { existsSync, readFileSync, readdirSync } from 'node:fs'
15import { join } from 'node:path'
16import { parseArgs } from 'node:util'
17
18import {
19 FACT_KEYS,
20 FLAG_KEYS,
21 buildReport,
22 parseLedger,
23} from '../src/lib/completeness.ts'
24import type { GapReport, Ledger, ModelRow } from '../src/lib/completeness.ts'
25
26// The scoring itself is shared with `GET /v1/status`.
27export * from '../src/lib/completeness.ts'
28
29const LEDGER_DIR = join(import.meta.dirname, '..', 'docs', 'source-silent')
30
31/** The on-disk ledger; the Worker bundles the same files (`source-silent.ts`). */
32export function readLedger(dir = LEDGER_DIR): Ledger | undefined {
33 if (!existsSync(dir)) return undefined
34 return parseLedger(
35 readdirSync(dir)
36 .filter((file) => file.endsWith('.md') && file !== 'README.md')
37 .map((file) => readFileSync(join(dir, file), 'utf8'))
38 .join('\n'),
39 )
40}
41
42/** Providers below `target`. With no `names`, every provider with chat rows. */
43export function failing(
44 report: GapReport,
45 target: number,
46 names?: Array<string>,
47): Array<string> {
48 if (!names) {
49 return report.providers
50 .filter((p) => p.chat > 0 && p.score != null && p.score < target)
51 .map((p) => p.provider)
52 }
53 const scores = new Map(report.providers.map((p) => [p.provider, p.score]))
54 // A named provider with no rows at all fails. A provider that does not
55 // serve chat scores null and is not below target.
56 return names.filter((name) => {
57 const score = scores.get(name)
58 if (score === undefined) return true
59 if (score === null) return false
60 return score < target
61 })
62}
63
64export function formatTable(report: GapReport): string {
65 const header = [
66 'provider',
67 'score',
68 'rows',
69 'chat',
70 'noAct',
71 'm.dev',
72 ...FACT_KEYS,
73 ...FLAG_KEYS,
74 ]
75 const body = report.providers.map((p) => [
76 p.provider,
77 p.score == null ? 'n/a' : p.score.toFixed(2),
78 String(p.rows),
79 String(p.chat),
80 String(p.noActivity),
81 String(p.fromModelsDev),
82 ...FACT_KEYS.map((key) =>
83 p.silent.includes(key)
84 ? 'silent'
85 : `${p.facts[key].have}/${p.facts[key].need}${p.modelSilent?.[key] ? ` (${p.modelSilent[key]} model-silent)` : ''}`,
86 ),
87 ...FLAG_KEYS.map((key) => String(p.flags[key].length)),
88 ])
89 const width = (i: number) =>
90 Math.max(...[header, ...body].map((line) => line[i]?.length ?? 0))
91 return [header, ...body]
92 .map((line) =>
93 line
94 .map((cell, i) =>
95 i === 0 ? cell.padEnd(width(i)) : cell.padStart(width(i)),
96 )
97 .join(' '),
98 )
99 .join('\n')
100}
101
102export async function main(
103 argv: Array<string> = process.argv.slice(2),
104): Promise<number> {
105 const { values } = parseArgs({
106 args: argv,
107 options: {
108 base: { type: 'string', default: 'https://modelschemas.com' },
109 table: { type: 'boolean', default: false },
110 check: { type: 'boolean', default: false },
111 providers: { type: 'string' },
112 target: { type: 'string', default: '1' },
113 },
114 })
115 const target = Number(values.target)
116 if (!(target >= 0 && target <= 1)) {
117 throw new Error(`--target must be between 0 and 1, got "${values.target}"`)
118 }
119
120 const url = new URL('/v1/models?pricing=1&limit=20000', values.base)
121 const response = await fetch(url)
122 if (!response.ok) throw new Error(`GET ${url.href} → ${response.status}`)
123 const { models } = (await response.json()) as { models: Array<ModelRow> }
124
125 const report = buildReport(models, readLedger())
126
127 console.log(
128 values.table ? formatTable(report) : JSON.stringify(report, null, 2),
129 )
130
131 if (!values.check) return 0
132 const names = values.providers?.split(',').filter(Boolean)
133 const below = failing(report, target, names)
134 if (below.length > 0) {
135 console.error(`below target ${target}: ${below.join(', ')}`)
136 return 1
137 }
138 return 0
139}
140
141if (import.meta.main) {
142 process.exit(await main())
143}