Skip to content

Commit 4724d2c

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 4724d2c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/gptscript.ts

+35
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,37 @@ export class GPTScript {
252252
}
253253
}
254254

255+
/**
256+
* Loads a tool or tools with the specified options.
257+
*
258+
* @param {ToolDef | ToolDef[]} toolDefs - The tool or tools to load.
259+
* @param {string} [content] - The content of the tool.
260+
* @param {boolean} [disableCache] - Whether to disable the cache.
261+
* @param {string} [subTool] - The sub-tool to use.
262+
* @param {string} [file] - The file to load.
263+
* @return {Promise<LoadResponse>} The loaded program.
264+
*/
265+
async load(
266+
toolDefs: ToolDef | ToolDef[],
267+
content?: string,
268+
disableCache?: boolean,
269+
subTool?: string,
270+
file?: string
271+
): Promise<LoadResponse> {
272+
if (!this.ready) {
273+
this.ready = await this.testGPTScriptURL(20);
274+
}
275+
const r: Run = new RunSubcommand("load", toolDefs, {}, GPTScript.serverURL);
276+
const requestPayload: any = { toolDefs: Array.isArray(toolDefs) ? toolDefs : [toolDefs] };
277+
if (content) requestPayload.content = content;
278+
if (disableCache !== undefined) requestPayload.disableCache = disableCache;
279+
if (subTool) requestPayload.subTool = subTool;
280+
if (file) requestPayload.file = file;
281+
282+
r.request(requestPayload);
283+
return r.json() as Promise<LoadResponse>;
284+
}
285+
255286
private async testGPTScriptURL(count: number): Promise<boolean> {
256287
while (count > 0) {
257288
try {
@@ -812,6 +843,10 @@ export interface PromptResponse {
812843
responses: Record<string, string>
813844
}
814845

846+
export interface LoadResponse {
847+
program: Program;
848+
}
849+
815850
export function getEnv(key: string, def: string = ""): string {
816851
let v = process.env[key] || ""
817852
if (v == "") {

0 commit comments

Comments
 (0)