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 == "") {