The optional-capability pattern had media and delete but no interactive UX: a channel could ask a human to approve an action through ApprovalRequester, but there was no domain contract for posing a clarifying question or presenting a single-choice picker, and no capability flag a caller could read before attempting one. Add ClarifyRequester and PickerRequester to internal/domain/messaging alongside ApprovalRequester, with Clarify/Picker/Approval flags on AdapterCapabilities and ClarifierOf/PickerOf/ApproverOf lookups that treat the capability as the contract rather than the method set, so a mis-reporting adapter degrades instead of panicking. TextFallback is the shared text degrade: a channel with no native question or selection UI still carries the interaction (numbered prompt, parsed reply) instead of failing or silently dropping it. FormatClarifyText/FormatPickerText/ParsePickerReply hold the rendering and matching logic once. The approval half reuses the existing decision model: Telegram's telegramApprover already renders a native prompt, so it now reports the Approval capability and deliberately does not claim Clarify/Picker it has no native UI for. Gateway re-exports the contract. Red-first: internal/domain/messaging/interactive_test.go drives the test double adapters (native, degrade, mis-reporting, over-claiming) and the text rendering/parsing; telegram/approval_test.go pins the honest capability report.
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package messaging
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// ApprovalRequester asks a human to approve or deny a named action.
|
|
type ApprovalRequester interface {
|
|
RequestApproval(ctx context.Context, action, description string) (ApprovalDecision, error)
|
|
}
|
|
|
|
// ApprovalDecision is the outcome of a human consent request.
|
|
type ApprovalDecision int
|
|
|
|
const (
|
|
// ApprovalApproved means the human approved this one execution.
|
|
ApprovalApproved ApprovalDecision = iota
|
|
|
|
// ApprovalPermanentlyApproved means the human approved this action
|
|
// for the lifetime of the session.
|
|
ApprovalPermanentlyApproved
|
|
|
|
// ApprovalDenied means the human explicitly refused.
|
|
ApprovalDenied
|
|
)
|
|
|
|
// ErrApprovalDenied is returned when the human 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 a human 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 only when sender reports the
|
|
// Approval capability. Like ClarifierOf and PickerOf, the capability wins
|
|
// over the method set: a sender that renders approvals but denies the
|
|
// capability is treated as unable to, and one that claims it without
|
|
// implementing the interface degrades instead of panicking.
|
|
func ApproverOf(sender any) (ApprovalRequester, bool) {
|
|
if !CapabilitiesOf(sender).Approval {
|
|
return nil, false
|
|
}
|
|
approver, ok := sender.(ApprovalRequester)
|
|
return approver, ok
|
|
}
|