feat(messaging): add a per-turn interactive carrier for clarify and picker

The contract could carry a question but nothing carried the ability to ask
one. Add Interactive, resolved from a channel adapter's own capability
report through InteractiveOf, with WithInteractive / InteractiveFromContext
as the per-turn carrier -- the same shape WithApprovalRequester already uses
for consent. The capability still wins over the method set, so a
mis-reporting adapter contributes nothing instead of being called.

Tests: TestInteractiveOfTrustsCapabilityReport and
TestInteractiveCarrierRoundTrip pin the resolution and the round trip.
This commit is contained in:
sam committed 2026-10-02 11:52:06 +10:00
1 parent ab86442156
commit c02852d87f
2 files changed
+69

No files matched your search

+39
View File
@@ -96,6 +96,45 @@ func PickerOf(sender any) (PickerRequester, bool) {
return picker, ok
}
// Interactive bundles the human-interaction requesters a channel adapter can
// carry for one turn, resolved once from its capability report. The zero value
// carries nothing, which is what a turn whose channel cannot ask a question
// sees; the gateway turn reads it to decide whether to offer a question tool
// at all rather than one that always fails.
type Interactive struct {
Clarifier ClarifyRequester
Picker PickerRequester
}
// Carries reports whether the adapter can carry at least one interaction.
func (i Interactive) Carries() bool { return i.Clarifier != nil || i.Picker != nil }
// InteractiveOf resolves sender into the requesters it can carry, trusting the
// capability report over the method set through ClarifierOf and PickerOf. A
// sender that implements a method but denies the capability contributes
// nothing, so a mis-reporting adapter degrades instead of being called.
func InteractiveOf(sender any) Interactive {
clarifier, _ := ClarifierOf(sender)
picker, _ := PickerOf(sender)
return Interactive{Clarifier: clarifier, Picker: picker}
}
type interactiveCtxKey struct{}
// WithInteractive stores a channel's interactive requesters on ctx for one
// turn, the way WithApprovalRequester stores consent. The gateway turn reads
// it to know whether it can ask the human a question.
func WithInteractive(ctx context.Context, i Interactive) context.Context {
return context.WithValue(ctx, interactiveCtxKey{}, i)
}
// InteractiveFromContext returns the requesters stored on ctx, or the zero
// value when the turn's channel cannot carry an interaction.
func InteractiveFromContext(ctx context.Context) Interactive {
i, _ := ctx.Value(interactiveCtxKey{}).(Interactive)
return i
}
// FormatClarifyText renders a ClarifyRequest as a plain-text prompt for a
// channel with no native question UI.
func FormatClarifyText(req ClarifyRequest) string {
@@ -444,3 +444,33 @@ func TestTextFallbackUnconfigured(t *testing.T) {
t.Fatalf("RequestChoice error = %v, want ErrTextFallbackNotConfigured", err)
}
}
func TestInteractiveOfTrustsCapabilityReport(t *testing.T) {
got := InteractiveOf(&nativeInteractive{})
if !got.Carries() {
t.Fatal("a channel reporting Clarify and Picker produced no requesters")
}
if got.Clarifier == nil || got.Picker == nil {
t.Fatalf("Interactive = %+v, want both requesters", got)
}
// The capability is the contract: a sender that claims an interaction it
// does not implement must contribute nothing rather than be called.
if over := InteractiveOf(interactiveOverclaim{}); over.Carries() {
t.Errorf("over-claiming adapter produced requesters: %+v", over)
}
if (Interactive{}).Carries() {
t.Error("the zero Interactive reports that it carries an interaction")
}
}
func TestInteractiveCarrierRoundTrip(t *testing.T) {
ctx := WithInteractive(context.Background(), InteractiveOf(&nativeInteractive{}))
got := InteractiveFromContext(ctx)
if !got.Carries() {
t.Fatalf("InteractiveFromContext = %+v, want the stored requesters", got)
}
if InteractiveFromContext(context.Background()).Carries() {
t.Error("a context without a carrier reported one")
}
}