Skip to content

Commit b593a0f

Browse files
committed
feat: add load method
Add a method to load a set of tool definitions into a program. Signed-off-by: Nick Hale <[email protected]>
1 parent cdca82b commit b593a0f

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/gptscript.ts

+69-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export class GPTScript {
188188
return parseBlocksFromNodes((await r.json()).nodes)
189189
}
190190

191-
async parseTool(toolContent: string): Promise<Block[]> {
191+
async parseContent(toolContent: string): Promise<Block[]> {
192192
if (!this.ready) {
193193
this.ready = await this.testGPTScriptURL(20)
194194
}
@@ -252,6 +252,70 @@ export class GPTScript {
252252
}
253253
}
254254

255+
/**
256+
* Loads a file into a Program.
257+
*
258+
* @param {string} fileName - The name of the file to load.
259+
* @param {boolean} [disableCache] - Whether to disable the cache.
260+
* @param {string} [subTool] - The sub-tool to use.
261+
* @return {Promise<LoadResponse>} The loaded program.
262+
*/
263+
async load(
264+
fileName: string,
265+
disableCache?: boolean,
266+
subTool?: string
267+
): Promise<LoadResponse> {
268+
return this._load({ file: fileName, disableCache, subTool });
269+
}
270+
271+
/**
272+
* Loads content into a Program.
273+
*
274+
* @param {string} content - The content to load.
275+
* @param {boolean} [disableCache] - Whether to disable the cache.
276+
* @param {string} [subTool] - The sub-tool to use.
277+
* @return {Promise<LoadResponse>} The loaded program.
278+
*/
279+
async loadContent(
280+
content: string,
281+
disableCache?: boolean,
282+
subTool?: string
283+
): Promise<LoadResponse> {
284+
return this._load({ content, disableCache, subTool });
285+
}
286+
287+
/**
288+
* Loads tools into a Program.
289+
*
290+
* @param {ToolDef[]} toolDefs - The tools to load.
291+
* @param {boolean} [disableCache] - Whether to disable the cache.
292+
* @param {string} [subTool] - The sub-tool to use.
293+
* @return {Promise<LoadResponse>} The loaded program.
294+
*/
295+
async loadTools(
296+
toolDefs: ToolDef[],
297+
disableCache?: boolean,
298+
subTool?: string
299+
): Promise<LoadResponse> {
300+
return this._load({ toolDefs, disableCache, subTool });
301+
}
302+
303+
/**
304+
* Helper method to handle the common logic for loading.
305+
*
306+
* @param {any} payload - The payload to send in the request.
307+
* @return {Promise<LoadResponse>} The loaded program.
308+
*/
309+
private async _load(payload: any): Promise<LoadResponse> {
310+
if (!this.ready) {
311+
this.ready = await this.testGPTScriptURL(20);
312+
}
313+
const r: Run = new RunSubcommand("load", payload.toolDefs || [], {}, GPTScript.serverURL);
314+
315+
r.request(payload);
316+
return (await r.json()) as LoadResponse;
317+
}
318+
255319
private async testGPTScriptURL(count: number): Promise<boolean> {
256320
while (count > 0) {
257321
try {
@@ -812,6 +876,10 @@ export interface PromptResponse {
812876
responses: Record<string, string>
813877
}
814878

879+
export interface LoadResponse {
880+
program: Program;
881+
}
882+
815883
export function getEnv(key: string, def: string = ""): string {
816884
let v = process.env[key] || ""
817885
if (v == "") {

tests/gptscript.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ describe("gptscript module", () => {
258258

259259
test("parse string tool", async () => {
260260
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
261-
const response = await g.parseTool(tool)
261+
const response = await g.parseContent(tool)
262262
expect(response).toBeDefined()
263263
expect(response).toHaveLength(1)
264264
expect((response[0] as gptscript.Tool).instructions).toEqual(tool)
265265
}, 30000)
266266

267267
test("parse empty string tool", async () => {
268-
const response = await g.parseTool("")
268+
const response = await g.parseContent("")
269269
expect(response).toBeDefined()
270270
expect(response).toHaveLength(0)
271271
}, 30000)
272272

273273
test("parse string tool with text node", async () => {
274274
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?\n---\n!markdown\nThis is a text node"
275-
const response = await g.parseTool(tool)
275+
const response = await g.parseContent(tool)
276276
expect(response).toBeDefined()
277277
expect(response).toHaveLength(2)
278278
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
@@ -281,7 +281,7 @@ describe("gptscript module", () => {
281281

282282
test("parse string tool global tools", async () => {
283283
const tool = "Global Tools: acorn, do-work\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
284-
const response = await g.parseTool(tool)
284+
const response = await g.parseContent(tool)
285285
expect(response).toBeDefined()
286286
expect(response).toHaveLength(1)
287287
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
@@ -290,7 +290,7 @@ describe("gptscript module", () => {
290290

291291
test("parse string tool first line shebang", async () => {
292292
const tool = "\n#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
293-
const response = await g.parseTool(tool)
293+
const response = await g.parseContent(tool)
294294
expect(response).toBeDefined()
295295
expect(response).toHaveLength(1)
296296
expect((response[0] as gptscript.Tool).instructions).toEqual("#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?")

0 commit comments

Comments
 (0)