Skip to content

Commit

Permalink
feat: add load method
Browse files Browse the repository at this point in the history
Add a method to load a set of tool definitions into a program.

Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Aug 14, 2024
1 parent cdca82b commit 4724d2c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoadResponse>} The loaded program.
*/
async load(
toolDefs: ToolDef | ToolDef[],
content?: string,
disableCache?: boolean,
subTool?: string,
file?: string
): Promise<LoadResponse> {
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<LoadResponse>;
}

private async testGPTScriptURL(count: number): Promise<boolean> {
while (count > 0) {
try {
Expand Down Expand Up @@ -812,6 +843,10 @@ export interface PromptResponse {
responses: Record<string, string>
}

export interface LoadResponse {
program: Program;
}

export function getEnv(key: string, def: string = ""): string {
let v = process.env[key] || ""
if (v == "") {
Expand Down

0 comments on commit 4724d2c

Please sign in to comment.