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

fix: allow retry of failed runs #47

Merged
merged 1 commit into from
Jun 4, 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ None of the options is required, and the defaults will reduce the number of call
- `chatState`: The chat state to continue, or null to start a new chat and return the state
- `confirm`: Prompt before running potentially dangerous commands
- `prompt`: Allow scripts to prompt the user for input
- `env`: Extra environment variables to pass to the script in the form `KEY=VAL`

## Functions

Expand Down Expand Up @@ -240,7 +241,7 @@ const gptscript = require('@gptscript-ai/gptscript');
const opts = {
disableCache: true,
input: "--testin how high is that there mouse?",
confirm: true
prompt: true
};

async function streamExecFileWithEvents() {
Expand Down
17 changes: 9 additions & 8 deletions src/gptscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface RunOpts {
chatState?: string
confirm?: boolean
prompt?: boolean
env?: string[]
}

export enum RunEventType {
Expand Down Expand Up @@ -242,25 +243,25 @@ export class Run {
}

nextChat(input: string = ""): Run {
if (this.state === RunState.Finished || this.state === RunState.Error) {
throw (new Error("Run already finished"))
if (this.state !== RunState.Continue && this.state !== RunState.Creating && this.state !== RunState.Error) {
throw (new Error(`Run must in creating, continue or error state, not ${this.state}`))
}

let run = this
if (run.state !== RunState.Creating) {
run = new (this.constructor as any)(this.requestPath, this.filePath, this.content, this.opts, this.gptscriptURL)
}

if (this.chatState) {
run.chatState = this.chatState
} else if (this.opts.chatState) {
run.chatState = this.opts.chatState
if (this.chatState && this.state === RunState.Continue) {
// Only update the chat state if the previous run didn't error.
// The chat state on opts will be the chat state for the last successful run.
this.opts.chatState = this.chatState
}
run.opts.input = input
if (run.content !== "") {
run.request({content: this.content, chatState: run.chatState})
run.request({content: this.content, ...this.opts})
} else {
run.request({file: this.filePath, chatState: run.chatState})
run.request({file: this.filePath, ...this.opts})
}

return run
Expand Down
30 changes: 30 additions & 0 deletions tests/gptscript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,34 @@ describe("gptscript module", () => {
expect(run.err).toContain("prompt occurred")
expect(promptFound).toBeFalsy()
})

test("retry failed run", async () => {
let shebang = `#!/bin/bash\nexit \${EXIT_CODE}`
if (process.platform == "win32") {
shebang = "#!/usr/bin/env powershell.exe\n$e = $env:EXIT_CODE;\nif ($e) { Exit 1; }"
}
const t = {
instructions: "say hello",
context: ["my-context"]
} as gptscript.ToolDef
const contextTool = {
name: "my-context",
instructions: `${shebang}\nexit \${EXIT_CODE}`
} as gptscript.ToolDef

let run = await client.evaluate([t, contextTool], {disableCache: true, env: ["EXIT_CODE=1"]})
try {
await run.text()
} catch {
}

expect(run.err).not.toEqual("")

run.opts.env = []
run = run.nextChat()

await run.text()

expect(run.err).toEqual("")
})
})