Skip to content

Commit a35c08c

Browse files
committed
fix: allow retrying of failed runs
Signed-off-by: Donnie Adams <[email protected]>
1 parent 8452bda commit a35c08c

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ func (c *client) Evaluate(ctx context.Context, opts Options, tools ...fmt.String
119119
state: Creating,
120120
opts: opts,
121121
content: concatTools(tools),
122-
chatState: opts.ChatState,
123122
}).NextChat(ctx, opts.Input)
124123
}
125124

@@ -130,7 +129,6 @@ func (c *client) Run(ctx context.Context, toolPath string, opts Options) (*Run,
130129
state: Creating,
131130
opts: opts,
132131
toolPath: toolPath,
133-
chatState: opts.ChatState,
134132
}).NextChat(ctx, opts.Input)
135133
}
136134

run.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,24 @@ func (r *Run) ChatState() string {
106106
// NextChat will pass input and create the next run in a chat.
107107
// The new Run will be returned.
108108
func (r *Run) NextChat(ctx context.Context, input string) (*Run, error) {
109-
if r.state != Creating && r.state != Continue {
110-
return nil, fmt.Errorf("run must be in creating or continue state not %q", r.state)
109+
if r.state != Creating && r.state != Continue && r.state != Error {
110+
return nil, fmt.Errorf("run must be in creating, continue, or error state not %q", r.state)
111111
}
112112

113113
run := &Run{
114114
url: r.url,
115115
requestPath: r.requestPath,
116116
state: Creating,
117-
chatState: r.chatState,
118117
toolPath: r.toolPath,
119118
content: r.content,
120119
opts: r.opts,
121120
}
121+
122122
run.opts.Input = input
123-
if run.chatState != "" {
124-
run.opts.ChatState = run.chatState
123+
if r.chatState != "" && r.state != Error {
124+
// If the previous run errored, then don't update the chat state.
125+
// opts.ChatState will be the last chat state where an error did not occur.
126+
run.opts.ChatState = r.chatState
125127
}
126128

127129
var payload any
@@ -287,6 +289,11 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
287289
return
288290
}
289291

292+
if event.Run != nil && event.Run.Type == EventTypeRunFinish && event.Run.Error != "" {
293+
r.state = Error
294+
r.err = fmt.Errorf(event.Run.Error)
295+
}
296+
290297
if r.opts.IncludeEvents {
291298
r.events <- event
292299
}

run_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gptscript
2+
3+
import (
4+
"context"
5+
"testing"
6+
)
7+
8+
func TestRestartingErrorRun(t *testing.T) {
9+
tool := &ToolDef{
10+
Instructions: "Say hello",
11+
}
12+
run, err := c.Evaluate(context.Background(), Options{}, tool)
13+
if err != nil {
14+
t.Errorf("Error executing tool: %v", err)
15+
}
16+
17+
// Wait for the run to complete
18+
_, err = run.Text()
19+
if err != nil {
20+
t.Fatalf("run returned error: %v", err)
21+
}
22+
23+
// Set the run to error, so we can test the retry.
24+
run.state = Error
25+
run, err = run.NextChat(context.Background(), "")
26+
if err != nil {
27+
t.Errorf("Error executing next run: %v", err)
28+
}
29+
30+
_, err = run.Text()
31+
if err != nil {
32+
t.Errorf("executing run with input of 0 should not fail: %v", err)
33+
}
34+
}

0 commit comments

Comments
 (0)