From 4724d2ca4647336257189fa74411de0d0ccb2d1a Mon Sep 17 00:00:00 2001 From: Nick Hale <4175918+njhale@users.noreply.github.com> Date: Tue, 13 Aug 2024 23:56:32 -0400 Subject: [PATCH] feat: add load method Add a method to load a set of tool definitions into a program. Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com> --- src/gptscript.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/gptscript.ts b/src/gptscript.ts index 5cbcd16..4f80488 100644 --- a/src/gptscript.ts +++ b/src/gptscript.ts @@ -252,6 +252,37 @@ export class GPTScript { } } + /** + * Loads a tool or tools with the specified options. + * + * @param {ToolDef | ToolDef[]} toolDefs - The tool or tools to load. + * @param {string} [content] - The content of the tool. + * @param {boolean} [disableCache] - Whether to disable the cache. + * @param {string} [subTool] - The sub-tool to use. + * @param {string} [file] - The file to load. + * @return {Promise} The loaded program. + */ + async load( + toolDefs: ToolDef | ToolDef[], + content?: string, + disableCache?: boolean, + subTool?: string, + file?: string + ): Promise { + if (!this.ready) { + this.ready = await this.testGPTScriptURL(20); + } + const r: Run = new RunSubcommand("load", toolDefs, {}, GPTScript.serverURL); + const requestPayload: any = { toolDefs: Array.isArray(toolDefs) ? toolDefs : [toolDefs] }; + if (content) requestPayload.content = content; + if (disableCache !== undefined) requestPayload.disableCache = disableCache; + if (subTool) requestPayload.subTool = subTool; + if (file) requestPayload.file = file; + + r.request(requestPayload); + return r.json() as Promise; + } + private async testGPTScriptURL(count: number): Promise { while (count > 0) { try { @@ -812,6 +843,10 @@ export interface PromptResponse { responses: Record } +export interface LoadResponse { + program: Program; +} + export function getEnv(key: string, def: string = ""): string { let v = process.env[key] || "" if (v == "") {