Skip to content

Commit 474eff6

Browse files
committed
fix: adjust some types and ensure errors are thrown with tests
Signed-off-by: Donnie Adams <[email protected]>
1 parent 5c40945 commit 474eff6

File tree

4 files changed

+89
-62
lines changed

4 files changed

+89
-62
lines changed

package-lock.json

Lines changed: 21 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gptscript.ts

Lines changed: 10 additions & 10 deletions
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/test-with-context.gpt

Lines changed: 12 additions & 0 deletions
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

Lines changed: 46 additions & 13 deletions
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

@@ -213,7 +213,7 @@ describe("gptscript module", () => {
213213
await run.text()
214214
err = run.err
215215
} catch (error: any) {
216-
errMessage = error
216+
errMessage = error.toString()
217217
}
218218

219219
expect(errMessage).toContain("aborted")
@@ -285,6 +285,35 @@ describe("gptscript module", () => {
285285
expect(response).toHaveLength(0)
286286
}, 30000)
287287

288+
test("parse non-existent file", async () => {
289+
try {
290+
await g.parse(path.join(__dirname, "fixtures", "non-existent.gpt"))
291+
} catch (e) {
292+
expect(e).toBeDefined()
293+
return
294+
}
295+
expect(false).toBeTruthy()
296+
}, 30000)
297+
298+
test("parse non-existent url", async () => {
299+
try {
300+
await g.parse("github.com/thedadams/dne")
301+
} catch (e) {
302+
expect(e).toBeDefined()
303+
return
304+
}
305+
expect(false).toBeTruthy()
306+
}, 30000)
307+
308+
test("parse file with context", async () => {
309+
const response = await g.parse(path.join(__dirname, "fixtures", "test-with-context.gpt"))
310+
expect(response).toBeDefined()
311+
expect(response).toHaveLength(2)
312+
expect((response[0] as gptscript.Tool).instructions).toEqual("Just wait.")
313+
expect((response[0] as gptscript.Tool).type).toEqual("tool")
314+
expect((response[1] as gptscript.Tool).type).toEqual("context")
315+
}, 30000)
316+
288317
test("parse file with metadata", async () => {
289318
const response = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
290319
expect(response).toBeDefined()
@@ -337,7 +366,7 @@ describe("gptscript module", () => {
337366
test("format tool", async () => {
338367
const tool = {
339368
id: "my-tool",
340-
type: ToolType,
369+
type: "tool" as ToolType,
341370
tools: ["sys.write", "sys.read"],
342371
instructions: "This is a test",
343372
arguments: {
@@ -579,8 +608,8 @@ describe("gptscript module", () => {
579608

580609
try {
581610
await run.text()
582-
} catch (e) {
583-
expect(e).toContain("prompt occurred")
611+
} catch (e: any) {
612+
expect(e.toString()).toContain("prompt occurred")
584613
}
585614
expect(run.err).toContain("prompt occurred")
586615
expect(promptFound).toBeFalsy()
@@ -645,15 +674,19 @@ describe("gptscript module", () => {
645674
test("run parsed tool with metadata", async () => {
646675
let err = undefined
647676
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
677+
const tools = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
678+
679+
for (const t of tools) {
680+
if (t.type && t.type !== TextType) {
681+
const run = await g.evaluate(t)
682+
try {
683+
out = await run.text()
684+
} catch (e) {
685+
err = e
686+
}
687+
}
656688
}
689+
657690
expect(err).toEqual(undefined)
658691
expect(out).toEqual("200")
659692
}, 20000)

0 commit comments

Comments
 (0)