Skip to content

Commit 9ad960e

Browse files
committed
chore: make ToolDef fields optional for easier definition
1 parent 0990616 commit 9ad960e

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

src/gptscript.ts

+33-25
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,10 @@ interface ChatState {
572572

573573
export type Arguments = string | Record<string, string>
574574

575+
export const ArgumentSchemaType = "object" as const
576+
575577
export interface ArgumentSchema {
576-
type: "object"
578+
type: typeof ArgumentSchemaType
577579
properties?: Record<string, Property>
578580
required?: string[]
579581
}
@@ -584,8 +586,10 @@ export interface Program {
584586
openAPICache: Record<string, any>
585587
}
586588

589+
export const PropertyType = "string" as const
590+
587591
export interface Property {
588-
type: "string"
592+
type: typeof PropertyType
589593
description: string
590594
default?: string
591595
}
@@ -599,26 +603,26 @@ export interface Repo {
599603
}
600604

601605
export interface ToolDef {
602-
name: string
603-
description: string
604-
maxTokens: number
605-
modelName: string
606-
modelProvider: boolean
607-
jsonResponse: boolean
606+
name?: string
607+
description?: string
608+
maxTokens?: number
609+
modelName?: string
610+
modelProvider?: boolean
611+
jsonResponse?: boolean
608612
temperature?: number
609613
cache?: boolean
610-
chat: boolean
614+
chat?: boolean
611615
internalPrompt?: boolean
612-
arguments: ArgumentSchema
613-
tools: string[]
614-
globalTools: string[]
615-
globalModelName: string
616-
context: string[]
617-
exportContext: string[]
618-
export: string[]
619-
agents: string[]
620-
credentials: string[]
621-
instructions: string
616+
arguments?: ArgumentSchema
617+
tools?: string[]
618+
globalTools?: string[]
619+
globalModelName?: string
620+
context?: string[]
621+
exportContext?: string[]
622+
export?: string[]
623+
agents?: string[]
624+
credentials?: string[]
625+
instructions?: string
622626
}
623627

624628
export interface ToolReference {
@@ -628,13 +632,15 @@ export interface ToolReference {
628632
toolID: string
629633
}
630634

635+
export const ToolType = "tool" as const
636+
631637
export interface Tool extends ToolDef {
632638
id: string
633-
type: "tool"
634-
toolMapping: Record<string, ToolReference[]>
635-
localTools: Record<string, string>
636-
source: SourceRef
637-
workingDir: string
639+
type: typeof ToolType
640+
toolMapping?: Record<string, ToolReference[]>
641+
localTools?: Record<string, string>
642+
source?: SourceRef
643+
workingDir?: string
638644
}
639645

640646
export interface SourceRef {
@@ -643,9 +649,11 @@ export interface SourceRef {
643649
repo?: Repo
644650
}
645651

652+
export const TextType = "text" as const
653+
646654
export interface Text {
647655
id: string
648-
type: "text"
656+
type: typeof TextType
649657
format: string
650658
content: string
651659
}

tests/gptscript.test.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as gptscript from "../src/gptscript"
2+
import {ArgumentSchemaType, PropertyType, ToolType} from "../src/gptscript"
23
import path from "path"
34
import {fileURLToPath} from "url"
45

@@ -45,7 +46,7 @@ describe("gptscript module", () => {
4546
instructions: "who was the president of the united states in 1928?"
4647
}
4748

48-
const run = await g.evaluate(t as any)
49+
const run = await g.evaluate(t)
4950
expect(run).toBeDefined()
5051
expect(await run.text()).toContain("Calvin Coolidge")
5152
})
@@ -60,7 +61,7 @@ describe("gptscript module", () => {
6061
disableCache: true,
6162
}
6263

63-
const run = await g.evaluate(t as any, opts)
64+
const run = await g.evaluate(t, opts)
6465
run.on(gptscript.RunEventType.CallProgress, (data: gptscript.CallFrame) => {
6566
for (let output of data.output) out += `system: ${output.content}`
6667
})
@@ -80,7 +81,7 @@ describe("gptscript module", () => {
8081
context: [path.join(__dirname, "fixtures", "acorn-labs-context.gpt")]
8182
}
8283

83-
const run = await g.evaluate(t as any, {disableCache: true})
84+
const run = await g.evaluate(t, {disableCache: true})
8485
out = await run.text()
8586
err = run.err
8687

@@ -167,13 +168,18 @@ describe("gptscript module", () => {
167168
name: "ask",
168169
description: "This tool is used to ask a question",
169170
arguments: {
170-
type: "object",
171-
question: "The question to ask"
171+
type: ArgumentSchemaType,
172+
properties: {
173+
question: {
174+
type: PropertyType,
175+
description: "The question to ask",
176+
}
177+
}
172178
},
173179
instructions: "${question}"
174180
}
175181

176-
const response = await (await g.evaluate([t0 as any, t1 as any])).text()
182+
const response = await (await g.evaluate([t0, t1])).text()
177183
expect(response).toBeDefined()
178184
expect(response).toContain("Calvin Coolidge")
179185
}, 30000)
@@ -182,11 +188,11 @@ describe("gptscript module", () => {
182188
const t0 = {
183189
tools: ["ask"],
184190
instructions: "Only use the ask tool to ask who was the president of the united states in 1928?"
185-
} as any
191+
}
186192
const t1 = {
187193
name: "other",
188194
instructions: "Who was the president of the united states in 1986?"
189-
} as any
195+
}
190196
const t2 = {
191197
name: "ask",
192198
description: "This tool is used to ask a question",
@@ -195,7 +201,7 @@ describe("gptscript module", () => {
195201
question: "The question to ask"
196202
},
197203
instructions: "${question}"
198-
} as any
204+
}
199205

200206
const response = await (await g.evaluate([t0, t1, t2], {subTool: "other"})).text()
201207
expect(response).toBeDefined()
@@ -246,21 +252,22 @@ describe("gptscript module", () => {
246252

247253
test("format tool", async () => {
248254
const tool = {
249-
type: "tool",
255+
id: "my-tool",
256+
type: ToolType,
250257
tools: ["sys.write", "sys.read"],
251258
instructions: "This is a test",
252259
arguments: {
253-
type: "object",
260+
type: ArgumentSchemaType,
254261
properties: {
255262
text: {
256-
type: "string",
263+
type: PropertyType,
257264
description: "The text to write"
258265
}
259266
}
260267
}
261268
}
262269

263-
const response = await g.stringify([tool as any])
270+
const response = await g.stringify([tool])
264271
expect(response).toBeDefined()
265272
expect(response).toContain("Tools: sys.write, sys.read")
266273
expect(response).toContain("This is a test")
@@ -277,7 +284,7 @@ describe("gptscript module", () => {
277284
const opts = {
278285
disableCache: true,
279286
}
280-
let run = await g.evaluate(t as any, opts)
287+
let run = await g.evaluate(t, opts)
281288

282289
const inputs = [
283290
"List the three largest states in the United States by area.",
@@ -375,14 +382,14 @@ describe("gptscript module", () => {
375382
instructions: "You are a chat bot. Don't finish the conversation until I say 'bye'.",
376383
tools: ["sys.chat.finish"]
377384
}
378-
let run = await g.evaluate(t as any, {disableCache: true})
385+
let run = await g.evaluate(t, {disableCache: true})
379386

380387
run = run.nextChat("List the three largest states in the United States by area.")
381388
expect(await run.text()).toContain("California")
382389
expect(run.err).toEqual("")
383390
expect(run.state).toEqual(gptscript.RunState.Continue)
384391

385-
run = await g.evaluate(t as any, {
392+
run = await g.evaluate(t, {
386393
disableCache: true,
387394
input: "What is the capital of the second one?",
388395
chatState: run.currentChatState()
@@ -399,7 +406,7 @@ describe("gptscript module", () => {
399406
instructions: "List the files in the current working directory.",
400407
tools: ["sys.exec"]
401408
}
402-
const run = await g.evaluate(t as any, {confirm: true})
409+
const run = await g.evaluate(t, {confirm: true})
403410
run.on(gptscript.RunEventType.CallConfirm, async (data: gptscript.CallFrame) => {
404411
expect(data.input).toContain(`"ls"`)
405412
confirmFound = true
@@ -417,7 +424,7 @@ describe("gptscript module", () => {
417424
instructions: "List the files in the current working directory.",
418425
tools: ["sys.exec"]
419426
}
420-
const run = await g.evaluate(t as any, {confirm: true})
427+
const run = await g.evaluate(t, {confirm: true})
421428
run.on(gptscript.RunEventType.CallConfirm, async (data: gptscript.CallFrame) => {
422429
expect(data.input).toContain(`"ls"`)
423430
confirmFound = true
@@ -435,7 +442,7 @@ describe("gptscript module", () => {
435442
instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
436443
tools: ["sys.prompt"]
437444
}
438-
const run = await g.evaluate(t as any, {prompt: true})
445+
const run = await g.evaluate(t, {prompt: true})
439446
run.on(gptscript.RunEventType.Prompt, async (data: gptscript.PromptFrame) => {
440447
expect(data.message).toContain("first name")
441448
expect(data.fields.length).toEqual(1)
@@ -457,7 +464,7 @@ describe("gptscript module", () => {
457464
instructions: "Use the sys.prompt user to ask the user for 'first name' which is not sensitive. After you get their first name, say hello.",
458465
tools: ["sys.prompt"]
459466
}
460-
const run = await g.evaluate(t as any)
467+
const run = await g.evaluate(t)
461468
run.on(gptscript.RunEventType.Prompt, async (data: gptscript.PromptFrame) => {
462469
promptFound = true
463470
})

0 commit comments

Comments
 (0)