diff --git a/changelog.d/changed-18590-betty-early-ack.md b/changelog.d/changed-18590-betty-early-ack.md new file mode 100644 index 0000000000..e55aa4f015 --- /dev/null +++ b/changelog.d/changed-18590-betty-early-ack.md @@ -0,0 +1 @@ +- Betty speaks her acknowledgement as soon as a partial transcript names her with a request, instead of after the final transcript, so the room hears she is on it within a couple of seconds (hivecommons/hive#18590). diff --git a/src/pkg/meetings/betty/betty.go b/src/pkg/meetings/betty/betty.go index 15cfec9b17..ced7c6d0e5 100644 --- a/src/pkg/meetings/betty/betty.go +++ b/src/pkg/meetings/betty/betty.go @@ -163,8 +163,9 @@ type Deps struct { // input is one queued engine input. type input struct { - line *Line - offer string + line *Line + partial *Line + offer string } // Engine is one meeting's persona. Observe, Offer, Restyle and Skip are @@ -200,6 +201,9 @@ type Engine struct { lastPost time.Time total int acks int + // earlyAck is when each speaker was acknowledged from a partial + // transcript; their final line then gets no second ack. + earlyAck map[string]time.Time } type timedLine struct { @@ -227,6 +231,7 @@ func New(s Settings, d Deps) *Engine { retrieved: make(chan retrieval, inboxDepth), answers: make(chan answered, inboxDepth), pending: map[int]*pending{}, + earlyAck: map[string]time.Time{}, done: make(chan struct{}), } } @@ -272,6 +277,25 @@ func (e *Engine) Observe(l Line) { } } +// ObservePartial offers one partial (not yet final) spoken line. When it +// already addresses the persona with a request and a spoken answer is +// wanted, the acknowledgement is spoken at once instead of after the +// final transcript; the answer still waits for the final line. Lines that +// do not address her are dropped here, so partials never crowd the queue. +func (e *Engine) ObservePartial(l Line) { + if !l.Spoken || strings.TrimSpace(l.Text) == "" { + return + } + if t := e.match.Match(l.Text, true); !t.Addressed || t.Intro { + return + } + select { + case e.in <- input{partial: &l}: + default: + // A partial is only an early hint; the final line follows. + } +} + // Offer queues one of Spek's chat posts as an addressed-class candidate: // it is posted unless the persona already covered the topic. func (e *Engine) Offer(text string) { @@ -382,9 +406,12 @@ func (e *Engine) run() { case <-e.ctx.Done(): return case in := <-e.in: - if in.line != nil { + switch { + case in.line != nil: e.onLine(*in.line) - } else { + case in.partial != nil: + e.onPartial(*in.partial) + default: e.onOffer(in.offer) } case id := <-e.timers: diff --git a/src/pkg/meetings/betty/fastack_test.go b/src/pkg/meetings/betty/fastack_test.go new file mode 100644 index 0000000000..996ab80d78 --- /dev/null +++ b/src/pkg/meetings/betty/fastack_test.go @@ -0,0 +1,72 @@ +package betty + +import ( + "testing" + "time" +) + +func answerLLM() *fakeLLM { + return &fakeLLM{reply: func(string, string) (string, error) { + return `{"answer":"It ships Friday.","confidence":0.9}`, nil + }} +} + +// A partial transcript that already names her with a request gets the +// spoken ack at once; the final line then gets the answer and no second ack. +func TestPartialTranscriptAcksEarlyOnce(t *testing.T) { + r := newRig(t, testSettings(), answerLLM()) + start := time.Now() + r.e.ObservePartial(Line{Who: "Bo", Text: "Betty, when does the", Spoken: true}) + ack := r.reply() + if ack.Kind != KindVoice || ack.Trigger != TriggerAck || time.Since(start) > time.Second { + t.Fatalf("early ack %+v", ack) + } + // The same utterance growing does not ack again. + r.e.ObservePartial(Line{Who: "Bo", Text: "Betty, when does the release", Spoken: true}) + r.none(200 * time.Millisecond) + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?", Spoken: true}) + ans := r.reply() + if ans.Trigger != TriggerAddressed || ans.Kind != KindVoice { + t.Fatalf("answer after an early ack %+v", ans) + } + r.none(200 * time.Millisecond) + // The next request gets its own ack again. + r.e.Observe(Line{Who: "Bo", Text: "Betty, and the deploy window?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck { + t.Fatalf("next request ack %+v", a) + } +} + +func TestPartialTranscriptNoAckCases(t *testing.T) { + r := newRig(t, testSettings(), answerLLM()) + for _, l := range []Line{ + {Who: "Bo", Text: "Betty", Spoken: true}, // may still become a hello + {Who: "Bo", Text: "Betty, when", Spoken: true}, // too little of a request yet + {Who: "Bo", Text: "when does the release ship", Spoken: true}, // not addressed + {Who: "Bo", Text: "Betty, when does the release ship"}, // chat is never partial + {Who: "Bo", Text: "Betty, who are you", Spoken: true}, // an intro waits for the final + } { + r.e.ObservePartial(l) + } + r.none(300 * time.Millisecond) + + noModel := newRig(t, testSettings(), nil) + noModel.e.ObservePartial(Line{Who: "Bo", Text: "Betty, when does the release ship", Spoken: true}) + noModel.none(300 * time.Millisecond) +} + +// An early ack whose final line did not address her does not swallow the +// ack of the speaker's next request. +func TestEarlyAckClearedByUnaddressedFinal(t *testing.T) { + r := newRig(t, testSettings(), answerLLM()) + r.e.ObservePartial(Line{Who: "Bo", Text: "Betty, when does the", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck { + t.Fatalf("early ack %+v", a) + } + r.e.Observe(Line{Who: "Bo", Text: "pretty much done with the release notes", Spoken: true}) + r.none(200 * time.Millisecond) + r.e.Observe(Line{Who: "Bo", Text: "Betty, when does the release ship?", Spoken: true}) + if a := r.reply(); a.Trigger != TriggerAck { + t.Fatalf("ack after a cleared early ack %+v", a) + } +} diff --git a/src/pkg/meetings/betty/queue.go b/src/pkg/meetings/betty/queue.go index af97c58799..95ce7c0371 100644 --- a/src/pkg/meetings/betty/queue.go +++ b/src/pkg/meetings/betty/queue.go @@ -102,6 +102,11 @@ func (e *Engine) onLine(l Line) { } now := e.d.Now() trig := e.match.Match(l.Text, l.Spoken) + if !trig.Addressed { + // Their final line did not address her after all: the next one + // that does gets its own ack. + delete(e.earlyAck, l.Who) + } topic := TopicKey(l.Text) question := IsQuestion(l.Text) if !trig.Addressed && !question { @@ -150,7 +155,7 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { e.drop("budget") return } - if trig.VoiceWanted && e.d.LLM != nil { + if trig.VoiceWanted && e.d.LLM != nil && !e.takeEarlyAck(l.Who, now) { // Speak at once so the room knows she heard; the answer follows. e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) } @@ -159,6 +164,53 @@ func (e *Engine) addressed(l Line, trig Trigger, now time.Time) { e.retrieve(p, trig.Rest) } +// Early acknowledgement from partial transcripts (hivecommons/hive#18590): +// the ack is spoken as soon as a partial line names her with a request, +// seconds before the final line arrives. +const ( + // earlyAckMinWords is how much of a request a partial must carry + // after her name before she acknowledges it: "Betty" alone may still + // become a hello, which gets no ack. + earlyAckMinWords = 2 + // earlyAckWindow is how long an early ack stands in for the ack of + // the same speaker's next final line. + earlyAckWindow = 30 * time.Second +) + +// onPartial speaks the acknowledgement for a partial spoken line that +// already addresses her with a request, once per speaker until their +// final line arrives. +func (e *Engine) onPartial(l Line) { + if meetings.IsBotName(l.Who, e.s.Persona.DisplayName) || e.leave.Match(l.Text) { + return + } + if e.d.LLM == nil || e.exhausted.Load() { + return + } + trig := e.match.Match(l.Text, true) + if !trig.Addressed || trig.Intro || !trig.VoiceWanted || len(strings.Fields(trig.Rest)) < earlyAckMinWords { + return + } + if _, ok := e.actionAddress(l.Text); ok { + return + } + now := e.d.Now() + if at, ok := e.earlyAck[l.Who]; ok && now.Sub(at) < earlyAckWindow { + return + } + if e.emit(Reply{Kind: KindVoice, Text: e.ack(), Trigger: TriggerAck, Question: l.Text, VoiceWanted: true}) { + e.earlyAck[l.Who] = now + } +} + +// takeEarlyAck reports whether this speaker was already acknowledged from +// a partial of the line now final, and clears the mark. +func (e *Engine) takeEarlyAck(who string, now time.Time) bool { + at, ok := e.earlyAck[who] + delete(e.earlyAck, who) + return ok && now.Sub(at) < earlyAckWindow +} + // minThoughtLines is the transcript a spoken "Betty?" needs before she // offers thoughts instead of a hello: the request itself plus one line. const minThoughtLines = 2 diff --git a/src/pkg/meetings/live/session.go b/src/pkg/meetings/live/session.go index be0b8a14ba..cfb6403550 100644 --- a/src/pkg/meetings/live/session.go +++ b/src/pkg/meetings/live/session.go @@ -226,6 +226,13 @@ func (s *session) onUpdate(u meetings.Update) { } switch { case ev.Type == meetings.EventSegment && ev.Final, ev.Type == meetings.EventChat: + case ev.Type == meetings.EventSegment: + // A partial transcript only lets the persona acknowledge a + // request early; Spek and open asks wait for the final line. + if s.persona != nil { + s.persona.ObservePartial(betty.Line{Who: ev.Who(), Text: ev.Text, Spoken: true}) + } + return default: return }