(ctx context.Context, req *proto.ExecRequest, el executor.EventLog, registry map[string]agent.Agent, handler ExecHandler)
| 88 | } |
| 89 | |
| 90 | func (d *Controller) tryResuming(ctx context.Context, req *proto.ExecRequest, el executor.EventLog, registry map[string]agent.Agent, handler ExecHandler) (history []*proto.Message, done bool, err error) { |
| 91 | events, err := el.Events(ctx, req.ConversationId) |
| 92 | if err != nil { |
| 93 | return nil, false, fmt.Errorf("failed to retrieve execution history: %w", err) |
| 94 | } |
| 95 | var pendingExecID string |
| 96 | for _, ev := range events { |
| 97 | if ev.ExecId != "" && ev.State == proto.State_STATE_PENDING { |
| 98 | pendingExecID = ev.ExecId |
| 99 | } |
| 100 | if ev.ExecId == pendingExecID && ev.State == proto.State_STATE_COMPLETED { |
| 101 | pendingExecID = "" |
| 102 | } |
| 103 | history = append(history, ev.Messages...) |
| 104 | } |
| 105 | |
| 106 | if req.LastSeq != 0 { |
| 107 | found := false |
| 108 | for _, ev := range events { |
| 109 | if ev.Seq == req.LastSeq { |
| 110 | found = true |
| 111 | } |
| 112 | if ev.Seq > req.LastSeq { |
| 113 | if err := handler(&proto.ExecResponse{ |
| 114 | Outputs: ev.Messages, |
| 115 | Seq: ev.Seq, |
| 116 | }); err != nil { |
| 117 | return nil, false, err |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | if !found { |
| 122 | return nil, false, fmt.Errorf("last_seq %d not found", req.LastSeq) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if pendingExecID == "" { |
| 127 | return history, false, nil |
| 128 | } |
| 129 | |
| 130 | // Find the pending event. |
| 131 | execEvents, err := el.ExecEvents(ctx, pendingExecID) |
| 132 | if err != nil { |
| 133 | return nil, false, fmt.Errorf("failed to retrieve execution events: %w", err) |
| 134 | } |
| 135 | |
| 136 | // TODO(jbd): Merge ExecutionEvent and ConversationEvent? |
| 137 | var pendingEvent *proto.ExecutionEvent |
| 138 | for _, ev := range execEvents { |
| 139 | if ev.State == proto.State_STATE_PENDING { |
| 140 | pendingEvent = ev |
| 141 | break |
| 142 | } |
| 143 | } |
| 144 | if pendingEvent == nil { |
| 145 | return nil, false, fmt.Errorf("failed to retrieve pending event: %w", err) |
| 146 | } |
| 147 | if err := d.execute( |
no test coverage detected