package messaging import ( "context" "errors" "time" ) // ApprovalRequester asks an operator to approve or deny a named action. type ApprovalRequester interface { RequestApproval(ctx context.Context, action, description string) (ApprovalDecision, error) } // ApprovalDecision is the outcome of an operator consent request. type ApprovalDecision int const ( // ApprovalApproved means the operator approved this one execution. ApprovalApproved ApprovalDecision = iota // ApprovalPermanentlyApproved means the operator approved this action // for the lifetime of the session. ApprovalPermanentlyApproved // ApprovalDenied means the operator explicitly refused. ApprovalDenied ) // ErrApprovalDenied is returned when the operator refuses the action. var ErrApprovalDenied = errors.New("approval denied") // ErrApprovalNotConfigured is returned when a tool requires approval but no // ApprovalRequester is wired. var ErrApprovalNotConfigured = errors.New("approval is not configured") // ToolApprovalTimeout bounds how long dispatch waits for an operator to act. const ToolApprovalTimeout = 2 * time.Minute type approvalCtxKey struct{} // WithApprovalRequester stores an ApprovalRequester on ctx. func WithApprovalRequester(ctx context.Context, a ApprovalRequester) context.Context { return context.WithValue(ctx, approvalCtxKey{}, a) } // ApprovalFromContext returns the ApprovalRequester stored on ctx. func ApprovalFromContext(ctx context.Context) ApprovalRequester { a, _ := ctx.Value(approvalCtxKey{}).(ApprovalRequester) return a } // ApproverOf returns sender's ApprovalRequester when it reports the // Approval capability and implements the interface. func ApproverOf(sender any) (ApprovalRequester, bool) { if !CapabilitiesOf(sender).Approval { return nil, false } approver, ok := sender.(ApprovalRequester) return approver, ok }