Skip to content

Commit eb01fa4

Browse files
committed
feat: add new metadata and type fields for tools
Signed-off-by: Donnie Adams <[email protected]>
1 parent fe3dae2 commit eb01fa4

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/gptscript.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import http from "http"
22
import path from "path"
33
import child_process from "child_process"
44
import {fileURLToPath} from "url"
5-
import {gunzipSync} from "zlib";
5+
import {gunzipSync} from "zlib"
66

77
export interface GlobalOpts {
88
APIKey?: string
@@ -679,6 +679,7 @@ export interface ToolDef {
679679
agents?: string[]
680680
credentials?: string[]
681681
instructions?: string
682+
type?: string
682683
}
683684

684685
export interface ToolReference {
@@ -694,6 +695,7 @@ export interface Tool extends ToolDef {
694695
id: string
695696
type: typeof ToolType
696697
toolMapping?: Record<string, ToolReference[]>
698+
metaData?: Record<string, string>
697699
localTools?: Record<string, string>
698700
source?: SourceRef
699701
workingDir?: string
@@ -810,15 +812,15 @@ export interface PromptResponse {
810812
responses: Record<string, string>
811813
}
812814

813-
export function getEnv(key: string, def: string = ''): string {
814-
let v = process.env[key] || ''
815-
if (v == '') {
815+
export function getEnv(key: string, def: string = ""): string {
816+
let v = process.env[key] || ""
817+
if (v == "") {
816818
return def
817819
}
818820

819-
if (v.startsWith('{"_gz":"') && v.endsWith('"}')) {
821+
if (v.startsWith("{\"_gz\":\"") && v.endsWith("\"}")) {
820822
try {
821-
return gunzipSync(Buffer.from(v.slice(8, -2), 'base64')).toString('utf8')
823+
return gunzipSync(Buffer.from(v.slice(8, -2), "base64")).toString("utf8")
822824
} catch (e) {
823825
}
824826
}

tests/gptscript.test.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ describe("gptscript module", () => {
241241
expect((response[0] as gptscript.Tool).instructions).toEqual("who was the president in 1928?")
242242
}, 30000)
243243

244+
test("parse file with metadata", async () => {
245+
const response = await g.parse(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
246+
expect(response).toBeDefined()
247+
expect(response).toHaveLength(2)
248+
expect((response[0] as gptscript.Tool).instructions).toContain("requests.get")
249+
expect((response[0] as gptscript.Tool).metaData).toEqual({"requirements.txt": "requests"})
250+
expect((response[1] as gptscript.Text).format).toEqual("metadata:foo:requirements.txt")
251+
}, 30000)
252+
244253
test("parse string tool", async () => {
245254
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
246255
const response = await g.parseTool(tool)
@@ -537,13 +546,27 @@ describe("gptscript module", () => {
537546
})
538547

539548
test("test get_env default", async () => {
540-
const env = getEnv('TEST_ENV_MISSING', 'foo')
541-
expect(env).toEqual('foo')
549+
const env = getEnv("TEST_ENV_MISSING", "foo")
550+
expect(env).toEqual("foo")
542551
})
543552

544553
test("test get_env", async () => {
545-
process.env.TEST_ENV = '{"_gz":"H4sIAEosrGYC/ytJLS5RKEvMKU0FACtB3ewKAAAA"}'
546-
const env = getEnv('TEST_ENV', 'missing')
547-
expect(env).toEqual('test value')
554+
process.env.TEST_ENV = "{\"_gz\":\"H4sIAEosrGYC/ytJLS5RKEvMKU0FACtB3ewKAAAA\"}"
555+
const env = getEnv("TEST_ENV", "missing")
556+
expect(env).toEqual("test value")
557+
})
558+
559+
test("run file with metadata", async () => {
560+
let err = undefined
561+
let out = ""
562+
let run = await g.run(path.join(__dirname, "fixtures", "parse-with-metadata.gpt"))
563+
564+
try {
565+
out = await run.text()
566+
} catch (e) {
567+
err = e
568+
}
569+
expect(err).toEqual(undefined)
570+
expect(out).toEqual("200")
548571
})
549-
})
572+
})

0 commit comments

Comments
 (0)