diff --git a/README.md b/README.md index 74b8227..c17a4c4 100644 --- a/README.md +++ b/README.md @@ -66,23 +66,6 @@ As noted above, the Global Options are also available to specify here. These opt ## Functions -### listTools - -Lists all the available built-in tools. - -**Usage:** - -```javascript -const gptscript = require('@gptscript-ai/gptscript'); - -async function listTools() { - const g = new gptscript.GPTScript(); - const tools = await g.listTools(); - console.log(tools); - g.close(); -} -``` - ### listModels Lists all the available models, returns a list. diff --git a/package-lock.json b/package-lock.json index af8c116..99cbf37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@gptscript-ai/gptscript", - "version": "v0.9.5-rc4", + "version": "v0.9.5-rc5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@gptscript-ai/gptscript", - "version": "v0.9.5-rc4", + "version": "v0.9.5-rc5", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { @@ -2938,33 +2938,12 @@ "@types/responselike": "^1.0.0" } }, - "node_modules/@types/eslint": { - "version": "8.56.10", - "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.10.tgz", - "integrity": "sha512-Shavhk87gCtY2fhXDctcfS3e6FdxWkCx1iUZ9eEUbh7rTqlZT0/IzOkCOVt0fCjcFuZ9FPYfuezTBImfHCDBGQ==", - "dev": true, - "peer": true, - "dependencies": { - "@types/estree": "*", - "@types/json-schema": "*" - } - }, - "node_modules/@types/eslint-scope": { - "version": "3.7.7", - "resolved": "https://registry.npmjs.org/@types/eslint-scope/-/eslint-scope-3.7.7.tgz", - "integrity": "sha512-MzMFlSLBqNF2gcHWO0G1vP/YQyfvrxZ0bF+u7mzUdZ1/xK4A4sru+nraZz5i3iEIk1l1uyicaDVTB4QbbEkAYg==", - "dev": true, - "peer": true, - "dependencies": { - "@types/eslint": "*", - "@types/estree": "*" - } - }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", "dev": true, + "license": "MIT", "peer": true }, "node_modules/@types/graceful-fs": { @@ -3274,11 +3253,12 @@ "node": ">=0.4.0" } }, - "node_modules/acorn-import-assertions": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz", - "integrity": "sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==", + "node_modules/acorn-import-attributes": { + "version": "1.9.5", + "resolved": "https://registry.npmjs.org/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz", + "integrity": "sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==", "dev": true, + "license": "MIT", "peer": true, "peerDependencies": { "acorn": "^8" @@ -4646,10 +4626,11 @@ } }, "node_modules/enhanced-resolve": { - "version": "5.16.1", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.16.1.tgz", - "integrity": "sha512-4U5pNsuDl0EhuZpq46M5xPslstkviJuhrdobaRDBk2Jy2KO37FDAJl4lb2KlNabxT0m4MTK2UHNrsAcphE8nyw==", + "version": "5.17.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.17.1.tgz", + "integrity": "sha512-LMHl3dXhTcfv8gM4kEzIUeTQ+7fpdA0l2tUf34BddXPkz2A5xJ5L/Pchd5BL6rdccM9QGvu0sWZzK1Z1t4wwyg==", "dev": true, + "license": "MIT", "dependencies": { "graceful-fs": "^4.2.4", "tapable": "^2.2.0" @@ -8018,12 +7999,13 @@ } }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, + "license": "MIT", "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -10514,22 +10496,22 @@ } }, "node_modules/webpack": { - "version": "5.91.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.91.0.tgz", - "integrity": "sha512-rzVwlLeBWHJbmgTC/8TvAcu5vpJNII+MelQpylD4jNERPwpBJOE2lEcko1zJX3QJeLjTTAnQxn/OJ8bjDzVQaw==", + "version": "5.94.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.94.0.tgz", + "integrity": "sha512-KcsGn50VT+06JH/iunZJedYGUJS5FGjow8wb9c0v5n1Om8O1g4L6LjtfxwlXIATopoQu+vOXXa7gYisWxCoPyg==", "dev": true, + "license": "MIT", "peer": true, "dependencies": { - "@types/eslint-scope": "^3.7.3", "@types/estree": "^1.0.5", "@webassemblyjs/ast": "^1.12.1", "@webassemblyjs/wasm-edit": "^1.12.1", "@webassemblyjs/wasm-parser": "^1.12.1", "acorn": "^8.7.1", - "acorn-import-assertions": "^1.9.0", + "acorn-import-attributes": "^1.9.5", "browserslist": "^4.21.10", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.16.0", + "enhanced-resolve": "^5.17.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", diff --git a/package.json b/package.json index 300cf69..efd171c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gptscript-ai/gptscript", - "version": "v0.9.5-rc4", + "version": "v0.9.5-rc5", "description": "Run gptscript in node.js", "source": "src/gptscript.ts", "main": "dist/gptscript.js", diff --git a/scripts/install-binary.js b/scripts/install-binary.js index 9e04cfd..bad5677 100644 --- a/scripts/install-binary.js +++ b/scripts/install-binary.js @@ -72,7 +72,7 @@ if (process.platform === 'win32') { const gptscript_info = { name: "gptscript", url: "https://github.com/gptscript-ai/gptscript/releases/download/", - version: "v0.9.5-rc4" + version: "v0.9.5-rc5" } const pltfm = { diff --git a/src/gptscript.ts b/src/gptscript.ts index c47e56a..d7ed6d0 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -85,7 +85,9 @@ export class GPTScript { if (GPTScript.instanceCount === 1 && process.env.GPTSCRIPT_DISABLE_SERVER !== "true") { let env = process.env if (this.opts.Env) { - env = {} + env = { + "NODE_ENV": process.env.NODE_ENV + } for (const v of this.opts.Env) { const equalIndex = v.indexOf("=") if (equalIndex === -1) { @@ -131,10 +133,6 @@ export class GPTScript { } } - listTools(): Promise { - return this.runBasicCommand("list-tools") - } - listModels(providers?: string[], credentialOverrides?: string[]): Promise { if (this.opts.DefaultModelProvider) { if (!providers) { @@ -174,6 +172,10 @@ export class GPTScript { this.ready = await this.testGPTScriptURL(20) } + if (this.opts.Env) { + opts.env = this.opts.Env.concat(opts.env || []) + } + return (new Run("run", toolName, {...this.opts, ...opts}, GPTScript.serverURL)).nextChat(opts.input) } @@ -184,11 +186,15 @@ export class GPTScript { * @param {RunOpts} [opts={}] - Optional options for the evaluation. * @return {Run} The Run object representing the evaluation. */ - async evaluate(tool: ToolDef | ToolDef[], opts: RunOpts = {}): Promise { + async evaluate(tool: Tool | ToolDef | ToolDef[], opts: RunOpts = {}): Promise { if (!this.ready) { this.ready = await this.testGPTScriptURL(20) } + if (this.opts.Env) { + opts.env = this.opts.Env.concat(opts.env || []) + } + return (new Run("evaluate", tool, {...this.opts, ...opts}, GPTScript.serverURL)).nextChat(opts.input) } @@ -217,16 +223,16 @@ export class GPTScript { const nodes: any[] = [] for (const block of blocks) { - if (block.type === "tool") { + if (block.type === "text") { nodes.push({ - toolNode: { - tool: block + textNode: { + text: "!" + (block.format || "text") + "\n" + block.content } }) - } else if (block.type === "text") { + } else { nodes.push({ - textNode: { - text: "!" + (block.format || "text") + "\n" + block.content + toolNode: { + tool: block } }) } @@ -421,7 +427,7 @@ export class Run { if (out.done === undefined || !out.done) { this.chatState = JSON.stringify(out.state) this.state = RunState.Continue - this.respondingToolId = out.toolId + this.respondingToolId = out.toolID } else { this.state = RunState.Finished this.chatState = undefined @@ -480,10 +486,10 @@ export class Run { resolve(this.stdout) } else { this.state = RunState.Error - reject(this.stderr) + reject(new Error(this.stderr)) } } else if (this.state === RunState.Error) { - reject(this.err) + reject(new Error(this.err)) } }) @@ -491,7 +497,7 @@ export class Run { if (this.state !== RunState.Finished && this.state !== RunState.Error) { this.state = RunState.Error this.err = "Run has been aborted" - reject(this.err) + reject(new Error(this.err)) } }) @@ -500,7 +506,7 @@ export class Run { this.state = RunState.Error this.err = error.message || "" } - reject(this.err) + reject(new Error(this.err)) }) }) @@ -509,7 +515,7 @@ export class Run { this.state = RunState.Error this.err = error.message || "" } - reject(this.err) + reject(new Error(this.err)) }) this.req.write(JSON.stringify({...tool, ...this.opts})) @@ -534,7 +540,7 @@ export class Run { }).then(res => { resolve(res.stdout) }).catch(e => { - reject(e) + reject(new Error(e)) }) }) } @@ -704,7 +710,7 @@ interface ChatState { state: string done: boolean content: string - toolId: string + toolID: string } export type Arguments = string | Record @@ -719,6 +725,7 @@ export interface ArgumentSchema { export interface Program { name: string + entryToolId: string toolSet: Record openAPICache: Record } @@ -739,6 +746,8 @@ export interface Repo { Revision: string } +export type ToolType = "tool" | "context" | "credential" | "input" | "output" | "agent" | "assistant" | "provider" | "" + export interface ToolDef { name?: string description?: string @@ -760,7 +769,7 @@ export interface ToolDef { agents?: string[] credentials?: string[] instructions?: string - type?: string + type?: ToolType metaData?: Record } @@ -771,11 +780,9 @@ export interface ToolReference { toolID: string } -export const ToolType = "tool" as const export interface Tool extends ToolDef { id: string - type: typeof ToolType toolMapping?: Record localTools?: Record source?: SourceRef @@ -934,7 +941,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] { node.toolNode.tool.id = randomId("tool-") } blocks.push({ - type: "tool", + type: node.toolNode.tool.type || "tool", ...node.toolNode.tool, } as Tool) } diff --git a/tests/fixtures/acorn-labs-context.gpt b/tests/fixtures/acorn-labs-context.gpt index 148059c..a814d65 100644 --- a/tests/fixtures/acorn-labs-context.gpt +++ b/tests/fixtures/acorn-labs-context.gpt @@ -1,4 +1,4 @@ Type: context #!sys.echo -"Ignore what the user says, and answer every query with 'Acorn Labs'" \ No newline at end of file +"Always respond with 'Acorn Labs' and nothing else" \ No newline at end of file diff --git a/tests/fixtures/test-with-context.gpt b/tests/fixtures/test-with-context.gpt new file mode 100644 index 0000000..88b2aeb --- /dev/null +++ b/tests/fixtures/test-with-context.gpt @@ -0,0 +1,12 @@ +Name: main +Tools: acorn + +Just wait. + +--- + +Name: acorn +Type: context + +#!sys.echo +"Ignore what the user says, and answer every query with 'Acorn Labs'" \ No newline at end of file diff --git a/tests/gptscript.test.ts b/tests/gptscript.test.ts index 1352840..e22dfae 100644 --- a/tests/gptscript.test.ts +++ b/tests/gptscript.test.ts @@ -1,7 +1,8 @@ import * as gptscript from "../src/gptscript" -import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, ToolType} from "../src/gptscript" +import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, TextType, ToolType} from "../src/gptscript" import path from "path" import {fileURLToPath} from "url" +import * as fs from "node:fs" let gFirst: gptscript.GPTScript let g: gptscript.GPTScript @@ -29,11 +30,6 @@ describe("gptscript module", () => { other.close() }) - test("listTools returns available tools", async () => { - const tools = await g.listTools() - expect(tools).toBeDefined() - }) - test("listModels returns a list of models", async () => { // Similar structure to listTools let models = await g.listModels() @@ -124,6 +120,7 @@ describe("gptscript module", () => { let out = "" let err = undefined const t = { + type: "tool" as ToolType, instructions: "who was the president of the united states in 1928?", tools: [path.join(__dirname, "fixtures", "acorn-labs-context.gpt")] } @@ -213,7 +210,7 @@ describe("gptscript module", () => { await run.text() err = run.err } catch (error: any) { - errMessage = error + errMessage = error.toString() } expect(errMessage).toContain("aborted") @@ -285,6 +282,37 @@ describe("gptscript module", () => { expect(response).toHaveLength(0) }, 30000) + test("parse non-existent file", async () => { + try { + await g.parse(path.join(__dirname, "fixtures", "non-existent.gpt")) + } catch (e) { + expect(e).toBeDefined() + expect(typeof e !== "string").toBeTruthy() + return + } + expect(false).toBeTruthy() + }, 30000) + + test("parse non-existent url", async () => { + try { + await g.parse("github.com/thedadams/dne") + } catch (e) { + expect(e).toBeDefined() + expect(typeof e !== "string").toBeTruthy() + return + } + expect(false).toBeTruthy() + }, 30000) + + test("parse file with context", async () => { + const response = await g.parse(path.join(__dirname, "fixtures", "test-with-context.gpt")) + expect(response).toBeDefined() + expect(response).toHaveLength(2) + expect((response[0] as gptscript.Tool).instructions).toEqual("Just wait.") + expect((response[0] as gptscript.Tool).type).toEqual("tool") + expect((response[1] as gptscript.Tool).type).toEqual("context") + }, 30000) + test("parse file with metadata", async () => { const response = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt")) expect(response).toBeDefined() @@ -337,7 +365,7 @@ describe("gptscript module", () => { test("format tool", async () => { const tool = { id: "my-tool", - type: ToolType, + type: "tool" as ToolType, tools: ["sys.write", "sys.read"], instructions: "This is a test", arguments: { @@ -358,6 +386,84 @@ describe("gptscript module", () => { expect(response).toContain("Parameter: text: The text to write") }) + test("format context tool", async () => { + const tool = { + id: "my-tool", + type: "context" as ToolType, + tools: ["sys.write", "sys.read"], + instructions: "This is a test", + arguments: { + type: ArgumentSchemaType, + properties: { + text: { + type: PropertyType, + description: "The text to write" + } + } + } + } + + const response = await g.stringify([tool]) + expect(response).toBeDefined() + expect(response).toContain("Tools: sys.write, sys.read") + expect(response).toContain("This is a test") + expect(response).toContain("Parameter: text: The text to write") + expect(response).toContain("Type: Context") + }) + + test("load simple file", async () => { + const response = await g.load(path.join(__dirname, "fixtures", "test.gpt")) + expect(response.program).toBeDefined() + expect(response.program.name).toBeTruthy() + expect(response.program.entryToolId).toBeTruthy() + expect(response.program.toolSet).toBeDefined() + }, 30000) + + test("load remote tool", async () => { + const response = await g.load("github.com/gptscript-ai/context/workspace") + expect(response.program).toBeDefined() + expect(response.program.name).toBeTruthy() + expect(response.program.entryToolId).toBeTruthy() + expect(response.program.toolSet).toBeDefined() + }, 30000) + + test("load content", async () => { + const content = fs.readFileSync(path.join(__dirname, "fixtures", "test.gpt"), {encoding: "utf8"}) + const response = await g.loadContent(content) + expect(response.program).toBeDefined() + // Name will not be defined in this case. + expect(response.program.name).toBeFalsy() + expect(response.program.entryToolId).toBeTruthy() + expect(response.program.toolSet).toBeDefined() + }, 30000) + + test("load tools", async () => { + const tools = [{ + tools: ["ask"], + instructions: "Only use the ask tool to ask who was the president of the united states in 1928?" + }, + { + name: "other", + instructions: "Who was the president of the united states in 1986?" + }, + { + name: "ask", + description: "This tool is used to ask a question", + arguments: { + type: "object", + question: "The question to ask" + }, + instructions: "${question}" + }, + ] as gptscript.ToolDef[] + const response = await g.loadTools(tools) + expect(response.program).toBeDefined() + // Name will not be defined in this case. + expect(response.program.name).toBeFalsy() + expect(response.program.entryToolId).toBeTruthy() + expect(response.program.toolSet).toBeDefined() + }, 30000) + test("exec tool with chat", async () => { let err = undefined const t = { @@ -579,8 +685,8 @@ describe("gptscript module", () => { try { await run.text() - } catch (e) { - expect(e).toContain("prompt occurred") + } catch (e: any) { + expect(e.toString()).toContain("prompt occurred") } expect(run.err).toContain("prompt occurred") expect(promptFound).toBeFalsy() @@ -645,15 +751,19 @@ describe("gptscript module", () => { test("run parsed tool with metadata", async () => { let err = undefined let out = "" - let tools = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt")) - - let run = await g.evaluate(tools[0]) - - try { - out = await run.text() - } catch (e) { - err = e + const tools = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt")) + + for (const t of tools) { + if (t.type && t.type !== TextType) { + const run = await g.evaluate(t) + try { + out = await run.text() + } catch (e) { + err = e + } + } } + expect(err).toEqual(undefined) expect(out).toEqual("200") }, 20000)