Unverified Commit 37e03bec authored by Pol Dellaiera's avatar Pol Dellaiera Committed by GitHub
Browse files

opencode: use `models-dev` package instead of fetching from online api (#425045)

parents bc12c67a a317d6f6
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
--- 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 || '{}';
 }
+20 −0
Original line number Diff line number Diff line
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
 }
+10 −11
Original line number Diff line number Diff line
@@ -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 \