Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add load method #81

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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