dispatchHook is the common dispatch path shared by every hook callsite: resolve the pre-built executor, dispatch, and emit any [Result.SystemMessage] as a Warning event. Errors are logged at warn level and surfaced as nil results so callers can use a single nil check to mean "nothing useful came bac
( ctx context.Context, a *agent.Agent, event hooks.EventType, input *hooks.Input, events EventSink, )
| 91 | // silently logged, since those events are themselves the user-facing |
| 92 | // notification mechanism. |
| 93 | func (r *LocalRuntime) dispatchHook( |
| 94 | ctx context.Context, |
| 95 | a *agent.Agent, |
| 96 | event hooks.EventType, |
| 97 | input *hooks.Input, |
| 98 | events EventSink, |
| 99 | ) *hooks.Result { |
| 100 | exec := r.hooksExec(a) |
| 101 | if exec == nil || !exec.Has(event) { |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | // Auto-fill AgentName for events whose Input builder omits it (tool |
| 106 | // events via toolexec.NewHooksInput), mirroring Cwd auto-fill in |
| 107 | // Executor.Dispatch. Caller-set values win. |
| 108 | if input != nil && input.AgentName == "" { |
| 109 | input.AgentName = a.Name() |
| 110 | } |
| 111 | |
| 112 | started := time.Now() |
| 113 | if events != nil { |
| 114 | events.Emit(HookStarted(event, input.SessionID, a.Name())) |
| 115 | } |
| 116 | result, err := exec.Dispatch(ctx, event, input) |
| 117 | if events != nil { |
| 118 | events.Emit(HookFinished(event, input.SessionID, result, err, time.Since(started), a.Name())) |
| 119 | } |
| 120 | if err != nil { |
| 121 | slog.WarnContext(ctx, "Hook execution failed", "event", event, "agent", a.Name(), "error", err) |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | if events != nil && result.SystemMessage != "" { |
| 126 | events.Emit(Warning(result.SystemMessage, a.Name())) |
| 127 | } |
| 128 | return result |
| 129 | } |
| 130 | |
| 131 | // executeSessionStartHooks fires session_start once at the top of |
| 132 | // RunStream and returns its AdditionalContext as transient system |
no test coverage detected