chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bun
|
||||
// Builds dist/: regenerates snapshot + generated types, compiles with tsc,
|
||||
// and copies the snapshot module (which tsc does not process) into dist.
|
||||
|
||||
import path from "node:path"
|
||||
import { rm } from "node:fs/promises"
|
||||
import { $ } from "bun"
|
||||
import { generate } from "./generate.ts"
|
||||
|
||||
const pkg = path.join(import.meta.dirname, "..")
|
||||
const dist = path.join(pkg, "dist")
|
||||
|
||||
export async function build() {
|
||||
await generate()
|
||||
await rm(dist, { recursive: true, force: true })
|
||||
await $`bunx tsc -p tsconfig.build.json`.cwd(pkg)
|
||||
await Bun.write(path.join(dist, "snapshot.js"), Bun.file(path.join(pkg, "src", "snapshot.js")))
|
||||
await Bun.write(path.join(dist, "snapshot.d.ts"), Bun.file(path.join(pkg, "src", "snapshot.d.ts")))
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
await build()
|
||||
console.log("built dist/")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bun
|
||||
// Publishes @opencode-ai/models to npm, opencode-style:
|
||||
// - the version is never stored in git: it is read from npm
|
||||
// plus a semver bump computed here (patch by default);
|
||||
// - `--if-changed` (scheduled data releases) skips publishing when the
|
||||
// freshly generated snapshot payload is byte-identical to the one inside
|
||||
// the currently published tarball;
|
||||
// - package.json is restored after publishing.
|
||||
//
|
||||
// Auth: npm Trusted Publishing (OIDC) in CI — no token needed once the
|
||||
// package is linked to this repo+workflow on npmjs.com. `--provenance` is
|
||||
// added automatically when running in GitHub Actions.
|
||||
|
||||
import path from "node:path"
|
||||
import { appendFile, mkdtemp, rm } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
import { $ } from "bun"
|
||||
import { loadCatalog, snapshotPayload } from "./generate.ts"
|
||||
|
||||
const pkg = path.join(import.meta.dirname, "..")
|
||||
const packageName = "@opencode-ai/models"
|
||||
const packageJsonPath = path.join(pkg, "package.json")
|
||||
|
||||
const bumpArg = process.argv.find((argument) => argument.startsWith("--bump="))?.slice("--bump=".length) ?? "patch"
|
||||
const ifChanged = process.argv.includes("--if-changed")
|
||||
|
||||
if (!["patch", "minor", "major"].includes(bumpArg)) {
|
||||
console.error(`Invalid --bump=${bumpArg}; expected patch, minor, or major`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
async function currentVersion(): Promise<string> {
|
||||
return (await $`npm view ${packageName} version`.text()).trim()
|
||||
}
|
||||
|
||||
function bump(version: string, kind: string): string {
|
||||
const [major = 0, minor = 0, patch = 0] = version.split(".").map((part) => Number.parseInt(part, 10))
|
||||
if (kind === "major") return `${major + 1}.0.0`
|
||||
if (kind === "minor") return `${major}.${minor + 1}.0`
|
||||
return `${major}.${minor}.${patch + 1}`
|
||||
}
|
||||
|
||||
/** The `const data = ...` line of the published dist/snapshot.js, or undefined. */
|
||||
async function publishedSnapshotLine(): Promise<string | undefined> {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), "models-dev-publish-"))
|
||||
try {
|
||||
const tarball = (await $`npm pack ${packageName}@latest --pack-destination ${directory}`.cwd(directory).text())
|
||||
.trim()
|
||||
.split("\n")
|
||||
.at(-1)!
|
||||
await $`tar -xzf ${path.join(directory, tarball)} -C ${directory}`
|
||||
const file = Bun.file(path.join(directory, "package", "dist", "snapshot.js"))
|
||||
if (!(await file.exists())) return undefined
|
||||
const text = await file.text()
|
||||
return text.split("\n").find((line) => line.startsWith("const data = "))
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
|
||||
if (ifChanged) {
|
||||
const catalog = await loadCatalog()
|
||||
const fresh = `const data = /* @__PURE__ */ JSON.parse(${JSON.stringify(snapshotPayload(catalog))})`
|
||||
const published = await publishedSnapshotLine()
|
||||
if (published === fresh) {
|
||||
console.log("Snapshot unchanged since the published version; skipping publish")
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
const current = await currentVersion()
|
||||
const next = bump(current, bumpArg)
|
||||
|
||||
console.log(`Publishing ${packageName}@${next} (${bumpArg} bump from ${current})`)
|
||||
|
||||
const packageJsonText = await Bun.file(packageJsonPath).text()
|
||||
const packageJson = JSON.parse(packageJsonText)
|
||||
|
||||
try {
|
||||
packageJson.version = next
|
||||
await Bun.write(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n")
|
||||
|
||||
const provenance = process.env["GITHUB_ACTIONS"] === "true" ? ["--provenance"] : []
|
||||
await $`npm publish --access public ${provenance}`.cwd(pkg)
|
||||
|
||||
const output = process.env["GITHUB_OUTPUT"]
|
||||
if (output !== undefined) await appendFile(output, `version=${next}\n`)
|
||||
console.log(`Published ${packageName}@${next}`)
|
||||
} finally {
|
||||
await Bun.write(packageJsonPath, packageJsonText)
|
||||
}
|
||||
Reference in New Issue
Block a user