Skip to content

Commit

Permalink
Merge pull request #81 from njhale/feat/add-load
Browse files Browse the repository at this point in the history
feat: add load method
  • Loading branch information
njhale authored Aug 14, 2024
2 parents cdca82b + b593a0f commit 3421ad3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
70 changes: 69 additions & 1 deletion src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class GPTScript {
return parseBlocksFromNodes((await r.json()).nodes)
}

async parseTool(toolContent: string): Promise<Block[]> {
async parseContent(toolContent: string): Promise<Block[]> {
if (!this.ready) {
this.ready = await this.testGPTScriptURL(20)
}
Expand Down Expand Up @@ -252,6 +252,70 @@ export class GPTScript {
}
}

/**
* Loads a file into a Program.
*
* @param {string} fileName - The name of the file to load.
* @param {boolean} [disableCache] - Whether to disable the cache.
* @param {string} [subTool] - The sub-tool to use.
* @return {Promise<LoadResponse>} The loaded program.
*/
async load(
fileName: string,
disableCache?: boolean,
subTool?: string
): Promise<LoadResponse> {
return this._load({ file: fileName, disableCache, subTool });
}

/**
* Loads content into a Program.
*
* @param {string} content - The content to load.
* @param {boolean} [disableCache] - Whether to disable the cache.
* @param {string} [subTool] - The sub-tool to use.
* @return {Promise<LoadResponse>} The loaded program.
*/
async loadContent(
content: string,
disableCache?: boolean,
subTool?: string
): Promise<LoadResponse> {
return this._load({ content, disableCache, subTool });
}

/**
* Loads tools into a Program.
*
* @param {ToolDef[]} toolDefs - The tools to load.
* @param {boolean} [disableCache] - Whether to disable the cache.
* @param {string} [subTool] - The sub-tool to use.
* @return {Promise<LoadResponse>} The loaded program.
*/
async loadTools(
toolDefs: ToolDef[],
disableCache?: boolean,
subTool?: string
): Promise<LoadResponse> {
return this._load({ toolDefs, disableCache, subTool });
}

/**
* Helper method to handle the common logic for loading.
*
* @param {any} payload - The payload to send in the request.
* @return {Promise<LoadResponse>} The loaded program.
*/
private async _load(payload: any): Promise<LoadResponse> {
if (!this.ready) {
this.ready = await this.testGPTScriptURL(20);
}
const r: Run = new RunSubcommand("load", payload.toolDefs || [], {}, GPTScript.serverURL);

r.request(payload);
return (await r.json()) as LoadResponse;
}

private async testGPTScriptURL(count: number): Promise<boolean> {
while (count > 0) {
try {
Expand Down Expand Up @@ -812,6 +876,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
10 changes: 5 additions & 5 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,21 @@ describe("gptscript module", () => {

test("parse string tool", async () => {
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await g.parseTool(tool)
const response = await g.parseContent(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual(tool)
}, 30000)

test("parse empty string tool", async () => {
const response = await g.parseTool("")
const response = await g.parseContent("")
expect(response).toBeDefined()
expect(response).toHaveLength(0)
}, 30000)

test("parse string tool with text node", async () => {
const tool = "How much wood would a woodchuck chuck if a woodchuck could chuck wood?\n---\n!markdown\nThis is a text node"
const response = await g.parseTool(tool)
const response = await g.parseContent(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(2)
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
Expand All @@ -281,7 +281,7 @@ describe("gptscript module", () => {

test("parse string tool global tools", async () => {
const tool = "Global Tools: acorn, do-work\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await g.parseTool(tool)
const response = await g.parseContent(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual("How much wood would a woodchuck chuck if a woodchuck could chuck wood?")
Expand All @@ -290,7 +290,7 @@ describe("gptscript module", () => {

test("parse string tool first line shebang", async () => {
const tool = "\n#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?"
const response = await g.parseTool(tool)
const response = await g.parseContent(tool)
expect(response).toBeDefined()
expect(response).toHaveLength(1)
expect((response[0] as gptscript.Tool).instructions).toEqual("#!/usr/bin/env python\nHow much wood would a woodchuck chuck if a woodchuck could chuck wood?")
Expand Down

0 comments on commit 3421ad3

Please sign in to comment.