diff --git a/internal/channels/telegram/approval.go b/internal/channels/telegram/approval.go index e2b06f4de..2093d00ff 100644 --- a/internal/channels/telegram/approval.go +++ b/internal/channels/telegram/approval.go @@ -54,7 +54,19 @@ type approvalResult struct { } // Compile-time guard. -var _ messaging.ApprovalRequester = (*telegramApprover)(nil) +var ( + _ messaging.ApprovalRequester = (*telegramApprover)(nil) + _ messaging.CapabilityReporter = (*telegramApprover)(nil) +) + +// Capabilities reports that this approver renders a native approval prompt. +// It claims Approval only: clarify and picker have no native Telegram UI +// wired yet, so claiming them would advertise an interaction this type +// cannot carry. A caller that sees Clarify/Picker false uses the shared text +// fallback instead of silently dropping the question. +func (a *telegramApprover) Capabilities() messaging.AdapterCapabilities { + return messaging.AdapterCapabilities{Approval: true} +} // NewApprover returns an ApprovalRequester that renders prompts in the // given chat. recipient is the Telegram user who may approve; in a private diff --git a/internal/channels/telegram/approval_test.go b/internal/channels/telegram/approval_test.go index a3275c7f1..0a76a7218 100644 --- a/internal/channels/telegram/approval_test.go +++ b/internal/channels/telegram/approval_test.go @@ -413,3 +413,23 @@ func TestParseApprovalCallback(t *testing.T) { }) } } + +// The approver renders a native approval prompt, so it must report the +// Approval capability -- and must not claim the interactive capabilities it +// has no native UI for. A caller seeing Clarify/Picker false uses the shared +// text fallback rather than dropping the question. +func TestApproverReportsApprovalCapability(t *testing.T) { + g, b, _ := newApprovalTestGateway(t) + approver := newTestApprover(g, b) + + caps := messaging.CapabilitiesOf(approver) + if !caps.Approval { + t.Error("telegram approver does not report the Approval capability") + } + if caps.Clarify || caps.Picker { + t.Errorf("telegram approver over-claims interactive capabilities: %+v", caps) + } + if _, ok := messaging.ApproverOf(approver); !ok { + t.Error("ApproverOf rejected an approver that reports the Approval capability") + } +} diff --git a/internal/domain/messaging/approval.go b/internal/domain/messaging/approval.go index 15e1e9a98..ea7f9b48a 100644 --- a/internal/domain/messaging/approval.go +++ b/internal/domain/messaging/approval.go @@ -48,3 +48,16 @@ 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 +} diff --git a/internal/domain/messaging/interactive.go b/internal/domain/messaging/interactive.go new file mode 100644 index 000000000..d548b8f76 --- /dev/null +++ b/internal/domain/messaging/interactive.go @@ -0,0 +1,228 @@ +package messaging + +import ( + "context" + "errors" + "fmt" + "strconv" + "strings" +) + +// InteractiveChoice is one selectable option offered by a clarify or picker +// prompt. ID is the value carried back to the caller; Label is what the +// human sees. An adapter that renders a choice must return the option as it +// was given (ID preserved), never a Label the human typed: callers correlate +// on ID. +type InteractiveChoice struct { + ID string + Label string +} + +// ClarifyRequest asks a human a question the agent cannot answer itself. +// Suggestions are optional quick replies; a channel with no button UI may +// render them as hint text and accept a typed answer instead. +type ClarifyRequest struct { + Question string + Suggestions []InteractiveChoice +} + +// PickerRequest asks a human to select exactly one of Options. +type PickerRequest struct { + Prompt string + Options []InteractiveChoice +} + +// ClarifyRequester is the optional interface an adapter implements to pose a +// clarifying question and block for the human's answer. It is the interactive +// counterpart of ApprovalRequester: an adapter that cannot render a native +// question UI fulfils it with the text fallback rather than failing the turn. +type ClarifyRequester interface { + // RequestClarification shows req and returns the human's free-text + // answer. The returned string is the trimmed answer, never empty when + // err is nil. + RequestClarification(ctx context.Context, req ClarifyRequest) (string, error) +} + +// PickerRequester is the optional interface an adapter implements to present +// a single-choice selection and block for the human's chosen option. +type PickerRequester interface { + // RequestChoice shows req and returns the option the human picked. A + // request with no options cannot be answered and returns + // ErrNoPickerOptions rather than an empty choice. + RequestChoice(ctx context.Context, req PickerRequest) (InteractiveChoice, error) +} + +// Interactive sentinel errors, matched with errors.Is. +var ( + // ErrNoPickerOptions is returned when a picker is asked to present an + // empty option set: there is nothing to select, so the caller must + // decide what to do (fall back, or ask a different question) rather + // than receive a zero InteractiveChoice that looks like a valid answer. + ErrNoPickerOptions = errors.New("interactive: picker has no options") + + // ErrEmptyReply is returned when a human submits no answer. An empty + // string is not an answer to a question, so it must not be mistaken + // for one by a caller that only checks err. + ErrEmptyReply = errors.New("interactive: human reply was empty") + + // ErrTextFallbackNotConfigured is returned when a TextFallback is used + // without both transport halves. It is a construction defect, surfaced + // rather than panicking inside a turn. + ErrTextFallbackNotConfigured = errors.New("interactive: text fallback is missing Send or Reply") +) + +// ClarifierOf returns sender's ClarifyRequester only when sender reports the +// Clarify capability. A sender that implements the interface but reports +// Clarify:false is treated as unable to clarify -- the capability is the +// contract, not the method set -- and one that reports Clarify:true without +// implementing the interface is treated the same way, so a mis-reporting +// adapter degrades instead of panicking. It mirrors DeleterOf. +func ClarifierOf(sender any) (ClarifyRequester, bool) { + if !CapabilitiesOf(sender).Clarify { + return nil, false + } + clarifier, ok := sender.(ClarifyRequester) + return clarifier, ok +} + +// PickerOf returns sender's PickerRequester only when sender reports the +// Picker capability, with the same capability-over-method-set rule as +// ClarifierOf. +func PickerOf(sender any) (PickerRequester, bool) { + if !CapabilitiesOf(sender).Picker { + return nil, false + } + picker, ok := sender.(PickerRequester) + return picker, ok +} + +// FormatClarifyText renders a ClarifyRequest as a plain-text prompt for a +// channel with no native question UI. +func FormatClarifyText(req ClarifyRequest) string { + var b strings.Builder + b.WriteString(strings.TrimSpace(req.Question)) + if len(req.Suggestions) > 0 { + b.WriteString("\n\nSuggestions:") + for i, s := range req.Suggestions { + fmt.Fprintf(&b, "\n%d) %s", i+1, s.Label) + } + } + b.WriteString("\n\nReply with your answer.") + return b.String() +} + +// FormatPickerText renders a PickerRequest as a numbered plain-text prompt +// for a channel with no native selection UI. +func FormatPickerText(req PickerRequest) string { + var b strings.Builder + b.WriteString(strings.TrimSpace(req.Prompt)) + if len(req.Options) == 0 { + b.WriteString("\n\nNo options were offered.") + return b.String() + } + b.WriteString("\n") + for i, o := range req.Options { + fmt.Fprintf(&b, "\n%d) %s", i+1, o.Label) + } + b.WriteString("\n\nReply with the number of your choice.") + return b.String() +} + +// ParsePickerReply maps a human's typed reply to one of req's options. It +// accepts a 1-based index or an exact ID or Label, case-insensitively. +// ok is false when the reply matches nothing, so the caller re-prompts rather +// than guessing an option the human did not choose. +func ParsePickerReply(req PickerRequest, reply string) (InteractiveChoice, bool) { + trimmed := strings.TrimSpace(reply) + if trimmed == "" { + return InteractiveChoice{}, false + } + if n, err := strconv.Atoi(trimmed); err == nil { + if n >= 1 && n <= len(req.Options) { + return req.Options[n-1], true + } + // A numeric reply is an index and nothing else: a label that + // happens to look like a number must not rescue an out-of-range + // selection. + return InteractiveChoice{}, false + } + for _, opt := range req.Options { + if strings.EqualFold(opt.ID, trimmed) || strings.EqualFold(opt.Label, trimmed) { + return opt, true + } + } + return InteractiveChoice{}, false +} + +// TextFallback renders clarify and picker prompts as plain text and reads the +// human's typed reply. It is the degrade path for a channel with no native +// question or selection UI, and the behaviour an adapter claims when it +// reports Clarify/Picker without native widgets: the interaction still +// happens, it just costs a typed answer instead of a tap. +// +// Send delivers one prompt; Reply blocks for the next human text. Both are +// injected so the fallback carries no transport of its own. +type TextFallback struct { + Send func(ctx context.Context, text string) error + Reply func(ctx context.Context) (string, error) +} + +// Compile-time guards. +var ( + _ ClarifyRequester = (*TextFallback)(nil) + _ PickerRequester = (*TextFallback)(nil) +) + +// RequestClarification sends the rendered question and returns the human's +// trimmed answer. +func (f *TextFallback) RequestClarification(ctx context.Context, req ClarifyRequest) (string, error) { + if err := f.ready(); err != nil { + return "", err + } + if err := f.Send(ctx, FormatClarifyText(req)); err != nil { + return "", err + } + answer, err := f.Reply(ctx) + if err != nil { + return "", err + } + answer = strings.TrimSpace(answer) + if answer == "" { + return "", ErrEmptyReply + } + return answer, nil +} + +// RequestChoice sends the rendered options and returns the option the human +// selected. A reply that matches no option is an error rather than a +// zero-value guess. +func (f *TextFallback) RequestChoice(ctx context.Context, req PickerRequest) (InteractiveChoice, error) { + if len(req.Options) == 0 { + return InteractiveChoice{}, ErrNoPickerOptions + } + if err := f.ready(); err != nil { + return InteractiveChoice{}, err + } + if err := f.Send(ctx, FormatPickerText(req)); err != nil { + return InteractiveChoice{}, err + } + reply, err := f.Reply(ctx) + if err != nil { + return InteractiveChoice{}, err + } + choice, ok := ParsePickerReply(req, reply) + if !ok { + return InteractiveChoice{}, fmt.Errorf("interactive: reply %q does not match any offered option", strings.TrimSpace(reply)) + } + return choice, nil +} + +// ready reports whether both transport halves are present before any send is +// attempted, so a half-built fallback fails at the call rather than +// mid-interaction. +func (f *TextFallback) ready() error { + if f == nil || f.Send == nil || f.Reply == nil { + return ErrTextFallbackNotConfigured + } + return nil +} diff --git a/internal/domain/messaging/interactive_test.go b/internal/domain/messaging/interactive_test.go new file mode 100644 index 000000000..b906358b5 --- /dev/null +++ b/internal/domain/messaging/interactive_test.go @@ -0,0 +1,446 @@ +package messaging + +import ( + "context" + "errors" + "strings" + "testing" +) + +// ── Test doubles ──────────────────────────────────────────────────── +// +// These are the "test double adapter, not a live platform" the bead calls +// for: they exercise both the native method set and the capability report, +// and one of them deliberately mis-reports so the regression is pinned. + +// nativeInteractive is a well-behaved adapter: it implements both interactive +// interfaces and reports both capabilities. +type nativeInteractive struct { + answer string + choice InteractiveChoice + lastReq any + choiceErr error +} + +func (d *nativeInteractive) RequestClarification(_ context.Context, req ClarifyRequest) (string, error) { + d.lastReq = req + return d.answer, nil +} + +func (d *nativeInteractive) RequestChoice(_ context.Context, req PickerRequest) (InteractiveChoice, error) { + d.lastReq = req + return d.choice, d.choiceErr +} + +func (d *nativeInteractive) Capabilities() AdapterCapabilities { + return AdapterCapabilities{Clarify: true, Picker: true} +} + +// clarifierOnly implements only the clarify half and reports it honestly. +type clarifierOnly struct{} + +func (clarifierOnly) RequestClarification(context.Context, ClarifyRequest) (string, error) { + return "yes", nil +} + +func (clarifierOnly) Capabilities() AdapterCapabilities { + return AdapterCapabilities{Clarify: true} +} + +// interactiveMisreport implements both interfaces but denies both +// capabilities, so the capability -- not the method set -- is what callers +// must trust. +type interactiveMisreport struct{ *nativeInteractive } + +func (interactiveMisreport) Capabilities() AdapterCapabilities { + return AdapterCapabilities{} +} + +// interactiveOverclaim claims both capabilities without implementing either +// interface. +type interactiveOverclaim struct{} + +func (interactiveOverclaim) Capabilities() AdapterCapabilities { + return AdapterCapabilities{Clarify: true, Picker: true} +} + +// approvalDouble renders approvals and reports the capability. +type approvalDouble struct{} + +func (approvalDouble) RequestApproval(context.Context, string, string) (ApprovalDecision, error) { + return ApprovalApproved, nil +} + +func (approvalDouble) Capabilities() AdapterCapabilities { + return AdapterCapabilities{Approval: true} +} + +// approvalMisreport renders approvals but denies the capability. +type approvalMisreport struct{ approvalDouble } + +func (approvalMisreport) Capabilities() AdapterCapabilities { + return AdapterCapabilities{} +} + +// ── Capability agreement ──────────────────────────────────────────── + +// A claimed interactive capability must agree with the implemented interface +// for an honest adapter: the flag is the caller's way to know, ahead of a type +// assertion, whether an interaction is worth attempting, and a mismatch makes +// that signal lie. Over- and under-claiming are covered by the XerOf tests, +// which are the callers that must tolerate a mis-report. +func TestAdapterCapabilitiesInteractiveMatchesImplementedInterfaces(t *testing.T) { + tests := []struct { + name string + send any + clar bool + pick bool + appr bool + }{ + { + name: "implements and reports both interactive halves", + send: &nativeInteractive{}, + clar: true, + pick: true, + }, + { + name: "implements clarify only", + send: clarifierOnly{}, + clar: true, + pick: false, + }, + { + name: "implements and reports approval", + send: approvalDouble{}, + appr: true, + }, + { + name: "implements nothing", + send: struct{}{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + caps := CapabilitiesOf(tt.send) + if caps.Clarify != tt.clar { + t.Errorf("CapabilitiesOf().Clarify = %v, want %v", caps.Clarify, tt.clar) + } + if caps.Picker != tt.pick { + t.Errorf("CapabilitiesOf().Picker = %v, want %v", caps.Picker, tt.pick) + } + if caps.Approval != tt.appr { + t.Errorf("CapabilitiesOf().Approval = %v, want %v", caps.Approval, tt.appr) + } + + // The regression the acceptance criteria calls for: an honest + // adapter's claim must match its method set in both directions. + _, isClarifier := tt.send.(ClarifyRequester) + _, isPicker := tt.send.(PickerRequester) + _, isApprover := tt.send.(ApprovalRequester) + if caps.Clarify != isClarifier { + t.Errorf("Clarify claim (%v) disagrees with ClarifyRequester implementation (%v)", caps.Clarify, isClarifier) + } + if caps.Picker != isPicker { + t.Errorf("Picker claim (%v) disagrees with PickerRequester implementation (%v)", caps.Picker, isPicker) + } + if caps.Approval != isApprover { + t.Errorf("Approval claim (%v) disagrees with ApprovalRequester implementation (%v)", caps.Approval, isApprover) + } + }) + } +} + +// ── XerOf degradation ─────────────────────────────────────────────── + +func TestClarifierOf(t *testing.T) { + tests := []struct { + name string + sender any + want bool + }{ + {name: "capability and interface agree", sender: &nativeInteractive{}, want: true}, + {name: "interface without capability", sender: interactiveMisreport{&nativeInteractive{}}, want: false}, + {name: "capability without interface", sender: interactiveOverclaim{}, want: false}, + {name: "neither capability nor interface", sender: struct{}{}, want: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := ClarifierOf(tt.sender) + if ok != tt.want { + t.Fatalf("ClarifierOf(%s) ok = %v, want %v", tt.name, ok, tt.want) + } + if ok && got == nil { + t.Fatalf("ClarifierOf(%s) reported ok with a nil requester", tt.name) + } + if !ok && got != nil { + t.Fatalf("ClarifierOf(%s) reported !ok with a non-nil requester", tt.name) + } + }) + } +} + +func TestPickerOf(t *testing.T) { + tests := []struct { + name string + sender any + want bool + }{ + {name: "capability and interface agree", sender: &nativeInteractive{}, want: true}, + {name: "interface without capability", sender: interactiveMisreport{&nativeInteractive{}}, want: false}, + {name: "capability without interface", sender: interactiveOverclaim{}, want: false}, + {name: "clarify-only adapter has no picker", sender: clarifierOnly{}, want: false}, + {name: "neither capability nor interface", sender: struct{}{}, want: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := PickerOf(tt.sender) + if ok != tt.want { + t.Fatalf("PickerOf(%s) ok = %v, want %v", tt.name, ok, tt.want) + } + if ok && got == nil { + t.Fatalf("PickerOf(%s) reported ok with a nil requester", tt.name) + } + if !ok && got != nil { + t.Fatalf("PickerOf(%s) reported !ok with a non-nil requester", tt.name) + } + }) + } +} + +func TestApproverOf(t *testing.T) { + tests := []struct { + name string + sender any + want bool + }{ + {name: "capability and interface agree", sender: approvalDouble{}, want: true}, + {name: "interface without capability", sender: approvalMisreport{}, want: false}, + {name: "neither capability nor interface", sender: struct{}{}, want: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := ApproverOf(tt.sender) + if ok != tt.want { + t.Fatalf("ApproverOf(%s) ok = %v, want %v", tt.name, ok, tt.want) + } + if ok && got == nil { + t.Fatalf("ApproverOf(%s) reported ok with a nil requester", tt.name) + } + if !ok && got != nil { + t.Fatalf("ApproverOf(%s) reported !ok with a non-nil requester", tt.name) + } + }) + } +} + +// ── Text fallback rendering and parsing ───────────────────────────── + +func TestFormatClarifyText(t *testing.T) { + tests := []struct { + name string + req ClarifyRequest + want string + }{ + { + name: "question with suggestions", + req: ClarifyRequest{ + Question: "Which environment?", + Suggestions: []InteractiveChoice{{ID: "staging", Label: "Staging"}, {ID: "prod", Label: "Production"}}, + }, + want: "Which environment?\n\nSuggestions:\n1) Staging\n2) Production\n\nReply with your answer.", + }, + { + name: "question without suggestions", + req: ClarifyRequest{Question: "What did you mean?"}, + want: "What did you mean?\n\nReply with your answer.", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FormatClarifyText(tt.req); got != tt.want { + t.Errorf("FormatClarifyText() =\n%q\nwant\n%q", got, tt.want) + } + }) + } +} + +func TestFormatPickerText(t *testing.T) { + tests := []struct { + name string + req PickerRequest + want string + }{ + { + name: "prompt with options", + req: PickerRequest{ + Prompt: "Pick a branch", + Options: []InteractiveChoice{{ID: "a", Label: "main"}, {ID: "b", Label: "release"}}, + }, + want: "Pick a branch\n\n1) main\n2) release\n\nReply with the number of your choice.", + }, + { + name: "prompt with no options", + req: PickerRequest{Prompt: "Pick a branch"}, + want: "Pick a branch\n\nNo options were offered.", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := FormatPickerText(tt.req); got != tt.want { + t.Errorf("FormatPickerText() =\n%q\nwant\n%q", got, tt.want) + } + }) + } +} + +func TestParsePickerReply(t *testing.T) { + req := PickerRequest{ + Options: []InteractiveChoice{ + {ID: "main", Label: "main branch"}, + {ID: "release", Label: "release branch"}, + }, + } + tests := []struct { + name string + reply string + wantID string + wantOK bool + }{ + {name: "one-based index", reply: "2", wantID: "release", wantOK: true}, + {name: "index with whitespace", reply: " 1 ", wantID: "main", wantOK: true}, + {name: "exact id", reply: "release", wantID: "release", wantOK: true}, + {name: "id case-insensitive", reply: "MAIN", wantID: "main", wantOK: true}, + {name: "exact label", reply: "release branch", wantID: "release", wantOK: true}, + {name: "label case-insensitive", reply: "Main Branch", wantID: "main", wantOK: true}, + {name: "index out of range", reply: "3", wantID: "", wantOK: false}, + {name: "zero index", reply: "0", wantID: "", wantOK: false}, + {name: "unmatched text", reply: "something else", wantID: "", wantOK: false}, + {name: "empty reply", reply: " ", wantID: "", wantOK: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := ParsePickerReply(req, tt.reply) + if ok != tt.wantOK { + t.Fatalf("ParsePickerReply(%q) ok = %v, want %v", tt.reply, ok, tt.wantOK) + } + if got.ID != tt.wantID { + t.Errorf("ParsePickerReply(%q).ID = %q, want %q", tt.reply, got.ID, tt.wantID) + } + }) + } +} + +// ── Text fallback degrade path ────────────────────────────────────── + +// recordingFallback wires a TextFallback to a captured prompt and a scripted +// reply, the shape a text-only channel gives it. +func recordingFallback(reply string, replyErr error) (*TextFallback, *string) { + var sent string + f := &TextFallback{ + Send: func(_ context.Context, text string) error { + sent = text + return nil + }, + Reply: func(context.Context) (string, error) { + return reply, replyErr + }, + } + return f, &sent +} + +func TestTextFallbackRequestClarification(t *testing.T) { + f, sent := recordingFallback(" staged the release ", nil) + got, err := f.RequestClarification(context.Background(), ClarifyRequest{Question: "What next?"}) + if err != nil { + t.Fatalf("RequestClarification: %v", err) + } + if got != "staged the release" { + t.Errorf("answer = %q, want %q", got, "staged the release") + } + if !strings.Contains(*sent, "What next?") { + t.Errorf("sent prompt %q does not carry the question", *sent) + } +} + +func TestTextFallbackRequestClarificationPropagatesReplyError(t *testing.T) { + wantErr := errors.New("chat closed") + f, _ := recordingFallback("", wantErr) + if _, err := f.RequestClarification(context.Background(), ClarifyRequest{Question: "?"}); !errors.Is(err, wantErr) { + t.Fatalf("RequestClarification error = %v, want %v", err, wantErr) + } +} + +func TestTextFallbackRequestChoice(t *testing.T) { + f, sent := recordingFallback("2", nil) + req := PickerRequest{ + Prompt: "Pick", + Options: []InteractiveChoice{{ID: "a", Label: "first"}, {ID: "b", Label: "second"}}, + } + got, err := f.RequestChoice(context.Background(), req) + if err != nil { + t.Fatalf("RequestChoice: %v", err) + } + if got.ID != "b" { + t.Errorf("chosen ID = %q, want %q", got.ID, "b") + } + if !strings.Contains(*sent, "1) first") || !strings.Contains(*sent, "2) second") { + t.Errorf("sent prompt %q does not carry the numbered options", *sent) + } +} + +// The degrade path the bead calls for: an adapter with no native UI still +// carries the selection rather than failing. +func TestTextFallbackDegradesToTextWithoutNativeUI(t *testing.T) { + f, _ := recordingFallback("release", nil) + got, err := f.RequestChoice(context.Background(), PickerRequest{ + Prompt: "Pick a branch", + Options: []InteractiveChoice{{ID: "main", Label: "main"}, {ID: "release", Label: "release"}}, + }) + if err != nil { + t.Fatalf("RequestChoice: %v", err) + } + if got.ID != "release" { + t.Errorf("chosen ID = %q, want %q", got.ID, "release") + } +} + +func TestTextFallbackRequestChoiceRejectsUnknownReply(t *testing.T) { + f, _ := recordingFallback("nonsense", nil) + _, err := f.RequestChoice(context.Background(), PickerRequest{ + Prompt: "Pick", + Options: []InteractiveChoice{{ID: "a", Label: "first"}}, + }) + if err == nil { + t.Fatal("RequestChoice returned a choice for an unmatched reply") + } + if errors.Is(err, ErrNoPickerOptions) { + t.Fatalf("RequestChoice returned ErrNoPickerOptions for a matched-but-unknown reply: %v", err) + } +} + +func TestTextFallbackRequestChoiceRejectsEmptyOptions(t *testing.T) { + f, _ := recordingFallback("1", nil) + _, err := f.RequestChoice(context.Background(), PickerRequest{Prompt: "Pick"}) + if !errors.Is(err, ErrNoPickerOptions) { + t.Fatalf("RequestChoice error = %v, want ErrNoPickerOptions", err) + } +} + +func TestTextFallbackRequestClarificationRejectsEmptyReply(t *testing.T) { + f, _ := recordingFallback(" ", nil) + _, err := f.RequestClarification(context.Background(), ClarifyRequest{Question: "?"}) + if !errors.Is(err, ErrEmptyReply) { + t.Fatalf("RequestClarification error = %v, want ErrEmptyReply", err) + } +} + +func TestTextFallbackUnconfigured(t *testing.T) { + var f TextFallback + if _, err := f.RequestClarification(context.Background(), ClarifyRequest{Question: "?"}); !errors.Is(err, ErrTextFallbackNotConfigured) { + t.Fatalf("RequestClarification error = %v, want ErrTextFallbackNotConfigured", err) + } + if _, err := f.RequestChoice(context.Background(), PickerRequest{Prompt: "Pick", Options: []InteractiveChoice{{ID: "a"}}}); !errors.Is(err, ErrTextFallbackNotConfigured) { + t.Fatalf("RequestChoice error = %v, want ErrTextFallbackNotConfigured", err) + } +} diff --git a/internal/domain/messaging/media_sender.go b/internal/domain/messaging/media_sender.go index af16e3cf8..a6e285878 100644 --- a/internal/domain/messaging/media_sender.go +++ b/internal/domain/messaging/media_sender.go @@ -21,6 +21,20 @@ type AdapterCapabilities struct { // cannot delete still delivers ephemeral replies, it just leaves them // in place. Delete bool + // Clarify reports whether the sender can pose a clarifying question and + // carry the human's answer back (ClarifyRequester). The capability is + // about carrying the interaction, not about native widgets: an adapter + // with no button UI fulfils it through the text fallback rather than + // reporting false and stranding the question. + Clarify bool + // Picker reports whether the sender can present a single-choice + // selection and carry the chosen option back (PickerRequester), on the + // same native-or-fallback rule as Clarify. + Picker bool + // Approval reports whether the sender renders approval prompts through + // the ApprovalRequester contract, so a caller can know a sender can + // present an approve/deny prompt before a gated action needs one. + Approval bool } // MediaSender delivers a MessageEvent carrying MediaAttachments through a diff --git a/internal/gateway/interactive.go b/internal/gateway/interactive.go new file mode 100644 index 000000000..b0bd92423 --- /dev/null +++ b/internal/gateway/interactive.go @@ -0,0 +1,51 @@ +// Package gateway: interactive.go re-exports the adapter interactive-UX +// contract from internal/domain/messaging so gateway-internal code and +// channel tests can name it without qualifying the domain package. +// +// The contract is the interactive counterpart of the media/delete +// capabilities in adapter_capabilities.go: clarify and picker are optional +// interfaces an adapter type-asserts through ClarifierOf/PickerOf, and +// approval reuses the existing ApprovalRequester flow rather than a second +// decision model. TextFallback is the shared text degrade for an adapter +// with no native question or selection UI. + +package gateway + +import "github.com/samcharles93/archie-core/internal/domain/messaging" + +// Interactive request and answer types. +type ( + InteractiveChoice = messaging.InteractiveChoice + ClarifyRequest = messaging.ClarifyRequest + PickerRequest = messaging.PickerRequest +) + +// Optional interactive interfaces an adapter may implement. +type ( + ClarifyRequester = messaging.ClarifyRequester + PickerRequester = messaging.PickerRequester +) + +// TextFallback is the shared text-based degrade. +type TextFallback = messaging.TextFallback + +// Re-exported sentinel errors. +var ( + ErrNoPickerOptions = messaging.ErrNoPickerOptions + ErrEmptyReply = messaging.ErrEmptyReply + ErrTextFallbackNotConfigured = messaging.ErrTextFallbackNotConfigured +) + +// Re-exported capability lookups. +var ( + ClarifierOf = messaging.ClarifierOf + PickerOf = messaging.PickerOf + ApproverOf = messaging.ApproverOf +) + +// Re-exported text rendering and parsing helpers. +var ( + FormatClarifyText = messaging.FormatClarifyText + FormatPickerText = messaging.FormatPickerText + ParsePickerReply = messaging.ParsePickerReply +)