@@ -154,22 +154,24 @@ func (r *Run) ChatState() string {
154154// NextChat will pass input and create the next run in a chat.
155155// The new Run will be returned.
156156func (r * Run ) NextChat (ctx context.Context , input string ) (* Run , error ) {
157- if r .state != Creating && r .state != Continue {
158- return nil , fmt .Errorf ("run must be in creating or continue state not %q" , r .state )
157+ if r .state != Creating && r .state != Continue && r . state != Error {
158+ return nil , fmt .Errorf ("run must be in creating, continue, or error state not %q" , r .state )
159159 }
160160
161161 run := & Run {
162162 url : r .url ,
163163 requestPath : r .requestPath ,
164164 state : Creating ,
165- chatState : r .chatState ,
166165 toolPath : r .toolPath ,
167166 content : r .content ,
168167 opts : r .opts ,
169168 }
169+
170170 run .opts .Input = input
171- if run .chatState != "" {
172- run .opts .ChatState = run .chatState
171+ if r .chatState != "" && r .state != Error {
172+ // If the previous run errored, then don't update the chat state.
173+ // opts.ChatState will be the last chat state where an error did not occur.
174+ run .opts .ChatState = r .chatState
173175 }
174176
175177 var payload any
@@ -350,10 +352,15 @@ func (r *Run) request(ctx context.Context, payload any) (err error) {
350352 r .parentCallFrameID = event .Call .ID
351353 }
352354 r .callsLock .Unlock ()
353- } else if event .Run != nil && event .Run .Type == EventTypeRunStart {
354- r .callsLock .Lock ()
355- r .program = & event .Run .Program
356- r .callsLock .Unlock ()
355+ } else if event .Run != nil {
356+ if event .Run .Type == EventTypeRunStart {
357+ r .callsLock .Lock ()
358+ r .program = & event .Run .Program
359+ r .callsLock .Unlock ()
360+ } else if event .Run .Type == EventTypeRunFinish && event .Run .Error != "" {
361+ r .state = Error
362+ r .err = fmt .Errorf (event .Run .Error )
363+ }
357364 }
358365
359366 if r .opts .IncludeEvents {
0 commit comments