On subscribes to events from this session. Events include assistant messages, tool executions, errors, and session state changes. Multiple handlers can be registered and will all receive events. Handlers are called synchronously in the order they were registered. The returned function can be calle
(handler SessionEventHandler)
| 545 | // // Later, to stop receiving events: |
| 546 | // unsubscribe() |
| 547 | func (s *Session) On(handler SessionEventHandler) func() { |
| 548 | s.handlerMutex.Lock() |
| 549 | defer s.handlerMutex.Unlock() |
| 550 | |
| 551 | id := s.nextHandlerID |
| 552 | s.nextHandlerID++ |
| 553 | s.handlers = append(s.handlers, sessionHandler{id: id, fn: handler}) |
| 554 | |
| 555 | // Return unsubscribe function |
| 556 | return func() { |
| 557 | s.handlerMutex.Lock() |
| 558 | defer s.handlerMutex.Unlock() |
| 559 | |
| 560 | for i, h := range s.handlers { |
| 561 | if h.id == id { |
| 562 | s.handlers = append(s.handlers[:i], s.handlers[i+1:]...) |
| 563 | break |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | // registerTools registers tool handlers for this session. |
| 570 | // |