opencode: use models-dev package instead of fetching from online api

- Added `models-dev` to `nativeBuildInputs`.
- Removed the old patch that fetched data from a remote URL and
  introduced a new patch to read from a local file if a path is provided
  via the `MODELS_DEV_API_JSON` environment variable.
This commit is contained in:
Thierry Delafontaine 2025-07-11 20:46:56 +02:00
parent 0eec779fcc
commit a317d6f6ce
No known key found for this signature in database
GPG Key ID: 00926686981863CB
3 changed files with 30 additions and 19 deletions

View File

@ -1,8 +0,0 @@
--- a/packages/opencode/src/provider/models-macro.ts
+++ b/packages/opencode/src/provider/models-macro.ts
@@ -1,4 +1,3 @@
export async function data() {
- const json = await fetch("https://models.dev/api.json").then((x) => x.text())
- return json
+ return process.env.MODELS_JSON || '{}';
}

View File

@ -0,0 +1,20 @@
diff --git i/packages/opencode/src/provider/models-macro.ts w/packages/opencode/src/provider/models-macro.ts
index 91a0348..4f60069 100644
--- i/packages/opencode/src/provider/models-macro.ts
+++ w/packages/opencode/src/provider/models-macro.ts
@@ -1,4 +1,15 @@
export async function data() {
+ const localApiJsonPath = process.env.MODELS_DEV_API_JSON
+
+ // Try to read from local file if path is provided
+ if (localApiJsonPath) {
+ const localFile = Bun.file(localApiJsonPath)
+ if (await localFile.exists()) {
+ return await localFile.text()
+ }
+ }
+
+ // Fallback to fetching from remote URL
const json = await fetch("https://models.dev/api.json").then((x) => x.text())
return json
}

View File

@ -4,7 +4,7 @@
buildGoModule,
bun,
fetchFromGitHub,
fetchurl,
models-dev,
nix-update-script,
testers,
writableTmpDirAsHomeHook,
@ -106,17 +106,15 @@ stdenvNoCC.mkDerivation (finalAttrs: {
outputHashMode = "recursive";
};
models-dev-data = fetchurl {
url = "https://models.dev/api.json";
sha256 = "sha256-igxQOC+Hz2FnXIW/S4Px9WhRuBhcIQIHO+7U8jHU1TQ=";
};
nativeBuildInputs = [ bun ];
nativeBuildInputs = [
bun
models-dev
];
patches = [
# Patch `packages/opencode/src/provider/models-macro.ts` to load the prefetched `models.dev/api.json`
# from the `MODELS_JSON` environment variable instead of fetching it at build time.
./fix-models-macro.patch
# Patch `packages/opencode/src/provider/models-macro.ts` to get contents of
# `api.json` from the file bundled with `bun build`.
./local-models-dev.patch
];
configurePhase = ''
@ -127,10 +125,11 @@ stdenvNoCC.mkDerivation (finalAttrs: {
runHook postConfigure
'';
env.MODELS_DEV_API_JSON = "${models-dev}/dist/api.json";
buildPhase = ''
runHook preBuild
export MODELS_JSON="$(cat ${finalAttrs.models-dev-data})"
bun build \
--define OPENCODE_VERSION="'${finalAttrs.version}'" \
--compile \