(ctx context.Context, events <-chan dagentruntime.Event, out chan<- Event, cancel context.CancelFunc, runID int)
| 265 | } |
| 266 | |
| 267 | func (s *Session) forwardEvents(ctx context.Context, events <-chan dagentruntime.Event, out chan<- Event, cancel context.CancelFunc, runID int) { |
| 268 | defer close(out) |
| 269 | defer cancel() |
| 270 | defer func() { |
| 271 | s.mu.Lock() |
| 272 | defer s.mu.Unlock() |
| 273 | if s.activeRun == runID { |
| 274 | s.activeCancel = nil |
| 275 | } |
| 276 | }() |
| 277 | |
| 278 | emit := func(e Event) bool { |
| 279 | select { |
| 280 | case out <- e: |
| 281 | return true |
| 282 | case <-ctx.Done(): |
| 283 | return false |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | errSent := false |
| 288 | for event := range events { |
| 289 | if ctx.Err() != nil { |
| 290 | continue |
| 291 | } |
| 292 | |
| 293 | switch e := event.(type) { |
| 294 | case *dagentruntime.ToolCallConfirmationEvent: |
| 295 | if errSent { |
| 296 | s.rt.Resume(ctx, dagentruntime.ResumeReject("The run was aborted.")) |
| 297 | continue |
| 298 | } |
| 299 | if !emit(Event{RuntimeEvent: event, Tool: &ToolActivity{Call: e.ToolCall, Def: e.ToolDefinition, NeedsConfirmation: true}}) { |
| 300 | s.rt.Resume(ctx, dagentruntime.ResumeReject("The run was aborted.")) |
| 301 | } |
| 302 | case *dagentruntime.ElicitationRequestEvent: |
| 303 | // This headless wrapper has no built-in elicitation UI. Decline so the |
| 304 | // run cannot hang forever; embedders that need elicitation can consume |
| 305 | // RuntimeEvent directly by driving the runtime themselves. |
| 306 | _ = s.rt.ResumeElicitation(ctx, tools.ElicitationActionDecline, nil) |
| 307 | case *dagentruntime.MaxIterationsReachedEvent: |
| 308 | s.rt.Resume(ctx, dagentruntime.ResumeReject("")) |
| 309 | case *dagentruntime.ErrorEvent: |
| 310 | if errSent { |
| 311 | continue |
| 312 | } |
| 313 | if !emit(Event{RuntimeEvent: event, Err: errors.New(e.Error)}) { |
| 314 | return |
| 315 | } |
| 316 | errSent = true |
| 317 | default: |
| 318 | if errSent { |
| 319 | continue |
| 320 | } |
| 321 | if translated, ok := TranslateRuntimeEvent(event); ok { |
| 322 | if !emit(translated) { |
| 323 | return |
| 324 | } |
no test coverage detected