| 1 | package bot |
| 2 | |
| 3 | import ( |
| 4 | "reasonix/internal/control" |
| 5 | ) |
| 6 | |
| 7 | func bindBotSessionWriteAuthority(state *sessionState) error { |
| 8 | if state == nil || state.leases == nil { |
| 9 | return nil |
| 10 | } |
| 11 | ctrl, ok := state.ctrl.(*control.Controller) |
| 12 | if !ok || ctrl == nil { |
| 13 | return nil |
| 14 | } |
| 15 | if err := state.leases.BindControllerAuthority(ctrl); err != nil { |
| 16 | return err |
| 17 | } |
| 18 | if state.onSessionTransition != nil { |
| 19 | ctrl.SetOnSessionTransition(state.onSessionTransition) |
| 20 | } |
| 21 | return nil |
| 22 | } |
| 23 | |
| 24 | func rebindBotSessionWriteAuthority(state *sessionState, path string) error { |
| 25 | if state == nil || state.leases == nil { |
| 26 | return nil |
| 27 | } |
| 28 | if err := state.leases.Rebind(path); err != nil { |
| 29 | return err |
| 30 | } |
| 31 | return bindBotSessionWriteAuthority(state) |
| 32 | } |
| 33 | |
| 34 | func (gw *BotGateway) botSessionTransitionHandler(key string, msg InboundMessage, state *sessionState) func(control.SessionTransitionInfo) error { |
| 35 | return func(info control.SessionTransitionInfo) error { |
| 36 | if state == nil || state.leases == nil { |
| 37 | return nil |
| 38 | } |
| 39 | state.lifecycleMu.Lock() |
| 40 | defer state.lifecycleMu.Unlock() |
| 41 | if state.retired { |
| 42 | return errBotSessionRetired |
| 43 | } |
| 44 | if err := state.leases.HandleSessionTransition(info); err != nil { |
| 45 | return err |
| 46 | } |
| 47 | targetPath := canonicalBotPath(info.TargetPath) |
| 48 | live := false |
| 49 | gw.mu.Lock() |
| 50 | if gw.controllers[key] == state { |
| 51 | live = true |
| 52 | state.sessionPath = targetPath |
| 53 | if override, ok := gw.sessionOverrides[key]; ok { |
| 54 | override.sessionPath = targetPath |
| 55 | gw.sessionOverrides[key] = override |
| 56 | } |
| 57 | } |
| 58 | gw.mu.Unlock() |
| 59 | if live { |
| 60 | gw.rememberSessionPath(msg, targetPath) |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | } |
| 65 |