chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env bun
|
||||
// Generates src/generated.ts (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 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.
|
||||
|
||||
/** 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
|
||||
`,
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await generate()
|
||||
console.log("generated src/generated.ts and src/snapshot.js")
|
||||
}
|
||||
Reference in New Issue
Block a user