Skip to content

Commit bca349b

Browse files
authoredAug 30, 2024··
Merge pull request #86 from thedadams/type-and-test-updates
fix: adjust some types and ensure errors are thrown with tests
2 parents 5c40945 + a95a432 commit bca349b

File tree

5 files changed

+91
-63
lines changed

5 files changed

+91
-63
lines changed
 

‎package-lock.json

+21-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/gptscript.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class GPTScript {
186186
* @param {RunOpts} [opts={}] - Optional options for the evaluation.
187187
* @return {Run} The Run object representing the evaluation.
188188
*/
189-
async evaluate(tool: ToolDef | ToolDef[], opts: RunOpts = {}): Promise<Run> {
189+
async evaluate(tool: Tool | ToolDef | ToolDef[], opts: RunOpts = {}): Promise<Run> {
190190
if (!this.ready) {
191191
this.ready = await this.testGPTScriptURL(20)
192192
}
@@ -482,18 +482,18 @@ export class Run {
482482
resolve(this.stdout)
483483
} else {
484484
this.state = RunState.Error
485-
reject(this.stderr)
485+
reject(new Error(this.stderr))
486486
}
487487
} else if (this.state === RunState.Error) {
488-
reject(this.err)
488+
reject(new Error(this.err))
489489
}
490490
})
491491

492492
res.on("aborted", () => {
493493
if (this.state !== RunState.Finished && this.state !== RunState.Error) {
494494
this.state = RunState.Error
495495
this.err = "Run has been aborted"
496-
reject(this.err)
496+
reject(new Error(this.err))
497497
}
498498
})
499499

@@ -502,7 +502,7 @@ export class Run {
502502
this.state = RunState.Error
503503
this.err = error.message || ""
504504
}
505-
reject(this.err)
505+
reject(new Error(this.err))
506506
})
507507
})
508508

@@ -511,7 +511,7 @@ export class Run {
511511
this.state = RunState.Error
512512
this.err = error.message || ""
513513
}
514-
reject(this.err)
514+
reject(new Error(this.err))
515515
})
516516

517517
this.req.write(JSON.stringify({...tool, ...this.opts}))
@@ -742,6 +742,8 @@ export interface Repo {
742742
Revision: string
743743
}
744744

745+
export type ToolType = "tool" | "context" | "credential" | "input" | "output" | "agent" | "assistant" | "provider" | ""
746+
745747
export interface ToolDef {
746748
name?: string
747749
description?: string
@@ -763,7 +765,7 @@ export interface ToolDef {
763765
agents?: string[]
764766
credentials?: string[]
765767
instructions?: string
766-
type?: string
768+
type?: ToolType
767769
metaData?: Record<string, string>
768770
}
769771

@@ -774,11 +776,9 @@ export interface ToolReference {
774776
toolID: string
775777
}
776778

777-
export const ToolType = "tool" as const
778779

779780
export interface Tool extends ToolDef {
780781
id: string
781-
type: typeof ToolType
782782
toolMapping?: Record<string, ToolReference[]>
783783
localTools?: Record<string, string>
784784
source?: SourceRef
@@ -937,7 +937,7 @@ function parseBlocksFromNodes(nodes: any[]): Block[] {
937937
node.toolNode.tool.id = randomId("tool-")
938938
}
939939
blocks.push({
940-
type: "tool",
940+
type: node.toolNode.tool.type || "tool",
941941
...node.toolNode.tool,
942942
} as Tool)
943943
}

‎tests/fixtures/acorn-labs-context.gpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Type: context
22

33
#!sys.echo
4-
"Ignore what the user says, and answer every query with 'Acorn Labs'"
4+
"Always respond with 'Acorn Labs' and nothing else"

‎tests/fixtures/test-with-context.gpt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Name: main
2+
Tools: acorn
3+
4+
Just wait.
5+
6+
---
7+
8+
Name: acorn
9+
Type: context
10+
11+
#!sys.echo
12+
"Ignore what the user says, and answer every query with 'Acorn Labs'"

‎tests/gptscript.test.ts

+47-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as gptscript from "../src/gptscript"
2-
import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, ToolType} from "../src/gptscript"
2+
import {ArgumentSchemaType, getEnv, PropertyType, RunEventType, TextType, ToolType} from "../src/gptscript"
33
import path from "path"
44
import {fileURLToPath} from "url"
55

@@ -124,6 +124,7 @@ describe("gptscript module", () => {
124124
let out = ""
125125
let err = undefined
126126
const t = {
127+
type: "tool" as ToolType,
127128
instructions: "who was the president of the united states in 1928?",
128129
tools: [path.join(__dirname, "fixtures", "acorn-labs-context.gpt")]
129130
}
@@ -213,7 +214,7 @@ describe("gptscript module", () => {
213214
await run.text()
214215
err = run.err
215216
} catch (error: any) {
216-
errMessage = error
217+
errMessage = error.toString()
217218
}
218219

219220
expect(errMessage).toContain("aborted")
@@ -285,6 +286,35 @@ describe("gptscript module", () => {
285286
expect(response).toHaveLength(0)
286287
}, 30000)
287288

289+
test("parse non-existent file", async () => {
290+
try {
291+
await g.parse(path.join(__dirname, "fixtures", "non-existent.gpt"))
292+
} catch (e) {
293+
expect(e).toBeDefined()
294+
return
295+
}
296+
expect(false).toBeTruthy()
297+
}, 30000)
298+
299+
test("parse non-existent url", async () => {
300+
try {
301+
await g.parse("github.com/thedadams/dne")
302+
} catch (e) {
303+
expect(e).toBeDefined()
304+
return
305+
}
306+
expect(false).toBeTruthy()
307+
}, 30000)
308+
309+
test("parse file with context", async () => {
310+
const response = await g.parse(path.join(__dirname, "fixtures", "test-with-context.gpt"))
311+
expect(response).toBeDefined()
312+
expect(response).toHaveLength(2)
313+
expect((response[0] as gptscript.Tool).instructions).toEqual("Just wait.")
314+
expect((response[0] as gptscript.Tool).type).toEqual("tool")
315+
expect((response[1] as gptscript.Tool).type).toEqual("context")
316+
}, 30000)
317+
288318
test("parse file with metadata", async () => {
289319
const response = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
290320
expect(response).toBeDefined()
@@ -337,7 +367,7 @@ describe("gptscript module", () => {
337367
test("format tool", async () => {
338368
const tool = {
339369
id: "my-tool",
340-
type: ToolType,
370+
type: "tool" as ToolType,
341371
tools: ["sys.write", "sys.read"],
342372
instructions: "This is a test",
343373
arguments: {
@@ -579,8 +609,8 @@ describe("gptscript module", () => {
579609

580610
try {
581611
await run.text()
582-
} catch (e) {
583-
expect(e).toContain("prompt occurred")
612+
} catch (e: any) {
613+
expect(e.toString()).toContain("prompt occurred")
584614
}
585615
expect(run.err).toContain("prompt occurred")
586616
expect(promptFound).toBeFalsy()
@@ -645,15 +675,19 @@ describe("gptscript module", () => {
645675
test("run parsed tool with metadata", async () => {
646676
let err = undefined
647677
let out = ""
648-
let tools = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
649-
650-
let run = await g.evaluate(tools[0])
651-
652-
try {
653-
out = await run.text()
654-
} catch (e) {
655-
err = e
678+
const tools = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
679+
680+
for (const t of tools) {
681+
if (t.type && t.type !== TextType) {
682+
const run = await g.evaluate(t)
683+
try {
684+
out = await run.text()
685+
} catch (e) {
686+
err = e
687+
}
688+
}
656689
}
690+
657691
expect(err).toEqual(undefined)
658692
expect(out).toEqual("200")
659693
}, 20000)

0 commit comments

Comments
 (0)
Please sign in to comment.