Skip to content

Commit cef19c8

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 cef19c8

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/gptscript.ts

+98
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,100 @@ 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+
// disableCache?: boolean,
268+
// content?: string,
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+
/**
286+
* Loads a file into a Program.
287+
*
288+
* @param {string} fileName - The name of the file to load.
289+
* @param {boolean} [disableCache] - Whether to disable the cache.
290+
* @param {string} [subTool] - The sub-tool to use.
291+
* @return {Promise<LoadResponse>} The loaded program.
292+
*/
293+
async loadFile(
294+
fileName: string,
295+
disableCache?: boolean,
296+
subTool?: string
297+
): Promise<LoadResponse> {
298+
return this.load({ file: fileName, disableCache, subTool });
299+
}
300+
301+
/**
302+
* Loads content into a Program.
303+
*
304+
* @param {string} content - The content to load.
305+
* @param {boolean} [disableCache] - Whether to disable the cache.
306+
* @param {string} [subTool] - The sub-tool to use.
307+
* @return {Promise<LoadResponse>} The loaded program.
308+
*/
309+
async loadContent(
310+
content: string,
311+
disableCache?: boolean,
312+
subTool?: string
313+
): Promise<LoadResponse> {
314+
return this.load({ content, disableCache, subTool });
315+
}
316+
317+
/**
318+
* Loads tools into a Program.
319+
*
320+
* @param {ToolDef[]} toolDefs - The tools to load.
321+
* @param {boolean} [disableCache] - Whether to disable the cache.
322+
* @param {string} [subTool] - The sub-tool to use.
323+
* @return {Promise<LoadResponse>} The loaded program.
324+
*/
325+
async loadTools(
326+
toolDefs: ToolDef[],
327+
disableCache?: boolean,
328+
subTool?: string
329+
): Promise<LoadResponse> {
330+
return this.load({ toolDefs, disableCache, subTool });
331+
}
332+
333+
/**
334+
* Helper method to handle the common logic for loading.
335+
*
336+
* @param {any} payload - The payload to send in the request.
337+
* @return {Promise<LoadResponse>} The loaded program.
338+
*/
339+
private async load(payload: any): Promise<LoadResponse> {
340+
if (!this.ready) {
341+
this.ready = await this.testGPTScriptURL(20);
342+
}
343+
const r: Run = new RunSubcommand("load", payload.toolDefs || [], {}, GPTScript.serverURL);
344+
345+
r.request(payload);
346+
return (await r.json()) as LoadResponse;
347+
}
348+
255349
private async testGPTScriptURL(count: number): Promise<boolean> {
256350
while (count > 0) {
257351
try {
@@ -812,6 +906,10 @@ export interface PromptResponse {
812906
responses: Record<string, string>
813907
}
814908

909+
export interface LoadResponse {
910+
program: Program;
911+
}
912+
815913
export function getEnv(key: string, def: string = ""): string {
816914
let v = process.env[key] || ""
817915
if (v == "") {

0 commit comments

Comments
 (0)