From 893ad33b7d4676af6f9a51b516d8552496da581c Mon Sep 17 00:00:00 2001 From: Donnie Adams Date: Wed, 26 Jun 2024 16:16:23 -0400 Subject: [PATCH] chore: remove the gptscript packaged binary Signed-off-by: Donnie Adams --- README.md | 9 ++- package-lock.json | 1 - package.json | 3 - scripts/install-binary.js | 130 -------------------------------------- src/gptscript.ts | 8 ++- 5 files changed, 9 insertions(+), 142 deletions(-) delete mode 100644 scripts/install-binary.js diff --git a/README.md b/README.md index 565bf86..9dda5f3 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,14 @@ To use this module, you need to have Node.js installed on your system. Then, you npm install @gptscript-ai/gptscript ``` -This will install the gptscript binary in the `node_modules/@gptscript-ai/gptscript/bin` directory. - -You can opt out of this behavior by setting the `NODE_GPTSCRIPT_SKIP_INSTALL_BINARY=true` environment variable before -running `npm install`. +In order to use the SDK, you also need the `gptscript` CLI installed. To direct `node-gptscript` to use a specific +binary, you can set the `GPTSCRIPT_BIN` environment variable to the path of the binary. Otherwise, `node-gptscript` will +use what is on your `PATH`. ## Usage To use the module and run gptscripts, you need to first set the `OPENAI_API_KEY` environment variable to your OpenAI API -key. You can also set the `GPTSCRIPT_BIN` environment variable to change the execution of the gptscripts. +key. To ensure it is working properly, you can run the following command: diff --git a/package-lock.json b/package-lock.json index b54f3ac..9766312 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "@gptscript-ai/gptscript", "version": "v0.8.5", - "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { "@types/sync-fetch": "^0.4.3", diff --git a/package.json b/package.json index ba24fe0..9a4dd0b 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,7 @@ "gptscript": "bin/gptscript" }, "scripts": { - "pretest": "npm run install-binary", - "install-binary": "node scripts/install-binary.js", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", - "postinstall": "node scripts/install-binary.js", "clean": "rm -rf dist", "build": "tsc" }, diff --git a/scripts/install-binary.js b/scripts/install-binary.js deleted file mode 100644 index 2223426..0000000 --- a/scripts/install-binary.js +++ /dev/null @@ -1,130 +0,0 @@ -#!/usr/bin/env node - -'use strict' - -import {DownloaderHelper} from 'node-downloader-helper'; -import fs from 'fs'; -import path from 'path'; -import AdmZip from 'adm-zip'; -import tar from 'tar'; -import util from 'util'; -import child_process from 'child_process' - -const exec = util.promisify(child_process.exec); - -async function downloadAndExtract(url, saveDirectory) { - const dlh = new DownloaderHelper(url, saveDirectory); - - return new Promise((resolve, reject) => { - dlh.on('end', () => { - if (url.endsWith('.zip')) { - const zip = new AdmZip(path.join(dlh.getDownloadPath())); - zip.extractAllTo(saveDirectory, true); - } else if (url.endsWith('.tar.gz')) { - tar.x({ - file: path.join(dlh.getDownloadPath()), - cwd: saveDirectory, - }); - } - resolve(); - }); - dlh.on('error', (error) => reject(error)); - dlh.on('progress.throttled', (downloadEvents) => { - const percentageComplete = - downloadEvents.progress < 100 - ? downloadEvents.progress.toFixed(2) - : 100; - console.info(`downloaded: ${percentageComplete}%`); - }); - - dlh.start(); - }); -} - -async function versions_match() { - try { - const command = path.join(outputDir, gptscriptBinaryName) + ' --version'; - const {stdout} = await exec(command); - return stdout.toString().includes(gptscript_info.version); - } catch (err) { - console.error('Error checking gptscript version:', err); - return false; - } -} - -const platform = process.platform; -let arch = process.arch; -if (process.platform === 'darwin') { - arch = 'universal'; -} else if (process.arch === 'x64') { - arch = 'amd64'; -} - -let gptscriptBinaryName = 'gptscript'; -if (process.platform === 'win32') { - gptscriptBinaryName = 'gptscript.exe'; -} - -const gptscript_info = { - name: "gptscript", - url: "https://github.com/gptscript-ai/gptscript/releases/download/", - version: "v0.8.5" -} - -const pltfm = { - win32: "windows", - linux: "linux", - darwin: "macOS" -}[platform]; - -const suffix = { - win32: 'zip', - linux: 'tar.gz', - darwin: 'tar.gz' -}[platform]; - -const url = `${gptscript_info.url}${gptscript_info.version}/gptscript-${gptscript_info.version}-${pltfm}-${arch}.${suffix}`; - -const outputDir = path.resolve('bin'); - -const fileExist = (path) => { - try { - fs.accessSync(path); - return true; - } catch (err) { - return false; - } -} - -if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); - console.info(`${outputDir} directory was created`) -} - -async function needToInstall() { - if (fileExist(path.join(outputDir, gptscriptBinaryName))) { - console.log('gptscript is installed...') - const versions = await versions_match(); - if (versions) { - console.log('gptscript version is up to date...exiting') - process.exit(0); - } - } -} - -(async () => { - await needToInstall(); - if (process.env.NODE_GPTSCRIPT_SKIP_INSTALL_BINARY === 'true') { - console.info('Skipping binary download'); - process.exit(0); - } - - console.log(`Downloading and extracting gptscript binary from ${url}...`); - try { - downloadAndExtract(url, outputDir) - } catch (error) { - console.error('Error downloading and extracting:', error) - } -})(); - - diff --git a/src/gptscript.ts b/src/gptscript.ts index a5c28a3..1220c98 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -1,7 +1,5 @@ import http from "http" -import path from "path" import child_process from "child_process" -import {fileURLToPath} from "url" import net from "net" export interface GlobalOpts { @@ -791,7 +789,11 @@ function getCmdPath(): string { return process.env.GPTSCRIPT_BIN } - return path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "bin", "gptscript") + if (process.platform == "win32") { + return "gptscript.exe" + } + + return "gptscript" } function parseBlocksFromNodes(nodes: any[]): Block[] {