81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
#!/usr/bin/env bun
|
|
// Generates src/generated.ts (known provider IDs + model family union) and
|
|
// src/snapshot.js (the bundled data snapshot) from this repository's TOMLs.
|
|
|
|
import path from "node:path"
|
|
import { generateCatalog, ModelFamilyValues } from "@models.dev/core"
|
|
|
|
const root = path.join(import.meta.dirname, "..", "..", "..")
|
|
const src = path.join(import.meta.dirname, "..", "src")
|
|
|
|
function sortRecord<T>(record: Record<string, T>): Record<string, T> {
|
|
return Object.fromEntries(Object.entries(record).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)))
|
|
}
|
|
|
|
/** Deterministic catalog: provider, per-provider model, and metadata keys sorted. */
|
|
export async function loadCatalog() {
|
|
const catalog = await generateCatalog(root)
|
|
const providers = sortRecord(
|
|
Object.fromEntries(
|
|
Object.entries(catalog.providers).map(([id, provider]) => [id, { ...provider, models: sortRecord(provider.models) }]),
|
|
),
|
|
)
|
|
return { providers, models: sortRecord(catalog.models) }
|
|
}
|
|
|
|
/** The exact JSON payload embedded in src/snapshot.js. Used by publish to diff against npm. */
|
|
export function snapshotPayload(catalog: Awaited<ReturnType<typeof loadCatalog>>) {
|
|
return JSON.stringify(catalog)
|
|
}
|
|
|
|
function union(values: string[]) {
|
|
return values.map((value) => ` | ${JSON.stringify(value)}`).join("\n")
|
|
}
|
|
|
|
export async function generate() {
|
|
const catalog = await loadCatalog()
|
|
|
|
const providerIDs = Object.keys(catalog.providers)
|
|
const families = [...new Set<string>(ModelFamilyValues)].sort()
|
|
await Bun.write(
|
|
path.join(src, "generated.ts"),
|
|
`// Generated by script/generate.ts. Do not edit; run \`bun run generate\` in packages/sdk.
|
|
|
|
/** Provider IDs known when this SDK version was generated. Newer providers may exist; the API is the source of truth. */
|
|
export const KNOWN_PROVIDER_IDS = [
|
|
${providerIDs.map((id) => ` ${JSON.stringify(id)},`).join("\n")}
|
|
] as const
|
|
|
|
export type KnownProviderID = (typeof KNOWN_PROVIDER_IDS)[number]
|
|
|
|
/** Model family identifiers used to group related models. */
|
|
export type ModelFamily =
|
|
${union(families)}
|
|
`,
|
|
)
|
|
|
|
await Bun.write(
|
|
path.join(src, "snapshot.js"),
|
|
`// Generated by script/generate.ts. Do not edit; run \`bun run generate\` in packages/sdk.
|
|
const data = /* @__PURE__ */ JSON.parse(${JSON.stringify(snapshotPayload(catalog))})
|
|
export const providers = data.providers
|
|
export const models = data.models
|
|
export const generatedAt = ${JSON.stringify(new Date().toISOString())}
|
|
export default data
|
|
`,
|
|
)
|
|
|
|
return catalog
|
|
}
|
|
|
|
/** Generates once if outputs are missing; used by tests that import the snapshot. */
|
|
export async function ensureGenerated() {
|
|
if (await Bun.file(path.join(src, "snapshot.js")).exists()) return
|
|
await generate()
|
|
}
|
|
|
|
if (import.meta.main) {
|
|
await generate()
|
|
console.log("generated src/generated.ts and src/snapshot.js")
|
|
}
|