Repository: menloresearch/jan
File: plugins/inference-plugin/module.ts (TypeScript)

Code before:
        reject("Model not found, please download again.");
      }
      if (subprocess) {
        console.error(
          "A subprocess is already running. Attempt to kill then reinit."
        );
        killSubprocess();
      }
      resolve(fileName);
    })
      // Kill port process if it is already in use
      .then((fileName) =>
        tcpPortUsed
          .waitUntilFree(PORT, 200, 3000)
          .catch(() => killPortProcess(PORT))
          .then(() => fileName)
      )
      // Spawn Nitro subprocess to load model
      .then(() => {
        let binaryFolder = path.join(__dirname, "nitro"); // Current directory by default

        const config = {
          llama_model_path: "",
          ctx_len: 2048,
          ngl: 100,
          embedding: true // Always enable embedding mode on
        }

        const modelPath = path.join(app.getPath("userData"), fileName);

        config.llama_model_path = modelPath;

        let binaryName;

        if (process.platform === "win32") {
          binaryName = "nitro_windows_amd64_cuda.exe";
        } else if (process.platform === "darwin") {
          // Mac OS platform
          binaryName =
            process.arch === "arm64" ? "nitro_mac_arm64" : "nitro_mac_amd64";
        } else {
          // Linux
          binaryName = "nitro_linux_amd64_cuda"; // For other platforms
        }

        const binaryPath = path.join(binaryFolder, binaryName);

        // Execute the binary

        subprocess = spawn(binaryPath, { cwd: binaryFolder });


Review comment (suggestion):
```suggestion
        const llama_model_path = path.join(app.getPath("userData"), fileName);
        const config = {
          llama_model_path,
          ctx_len: 2048,
          ngl: 100,
          embedding: true // Always enable embedding mode on
```

Code after:
        reject("Model not found, please download again.");
      }
      if (subprocess) {
        console.error("A subprocess is already running. Attempt to kill then reinit.");
        killSubprocess();
      }
      resolve(fileName);
    })
      // Kill port process if it is already in use
      .then((fileName) =>
        tcpPortUsed
          .waitUntilFree(PORT, 200, 3000)
          .catch(() => killPortProcess(PORT))
          .then(() => fileName)
      )
      // Spawn Nitro subprocess to load model
      .then(() => {
        let binaryFolder = path.join(__dirname, "nitro"); // Current directory by default
        let binaryName;

        if (process.platform === "win32") {
          binaryName = "nitro_windows_amd64_cuda.exe";
        } else if (process.platform === "darwin") {
          // Mac OS platform
          binaryName = process.arch === "arm64" ? "nitro_mac_arm64" : "nitro_mac_intel";
        } else {
          // Linux
          binaryName = "nitro_linux_amd64_cuda"; // For other platforms
        }

        const binaryPath = path.join(binaryFolder, binaryName);

        // Execute the binary
        subprocess = spawn(binaryPath, { cwd: binaryFolder });

        // Handle subprocess output
        subprocess.stdout.on("data", (data) => {
          console.log(`stdout: ${data}`);
        });

        subprocess.stderr.on("data", (data) => {
          console.error(`stderr: ${data}`);
        });

        subprocess.on("close", (code) => {
          console.log(`child process exited with code ${code}`);
          subprocess = null;
        });
      })
      .then(() => tcpPortUsed.waitUntilUsed(PORT, 300, 30000))
      .then(() => {