diff --git a/cmd/mavend/clarify.go b/cmd/mavend/clarify.go index 413b219..41e0b49 100644 --- a/cmd/mavend/clarify.go +++ b/cmd/mavend/clarify.go @@ -527,20 +527,22 @@ func (h *reactiveHandler) finishClarified(ctx context.Context, dec router.Decisi return reply } +// maxCarriedHistory — how many turns of PRIOR history (beyond the immediate +// last turn) rememberTurn carries forward. The session ends up holding this +// many plus the one just-finished turn, so callers describing the total +// depth (chatHistory's doc comment, this one) say "up to 4". +const maxCarriedHistory = 3 + // rememberTurn stores this turn as the dialogue session the next follow-up // inherits from, carrying up to 4 prior turns of history for anaphora. Capped so // one long conversation can't grow the session unboundedly. func (h *reactiveHandler) rememberTurn(ctx context.Context, prev *dialogue.Session, dec router.Decision, now time.Time) { var history []dialogue.Turn if prev != nil { - history = append(history, dialogue.Turn{ - Intent: prev.Intent, - Slots: prev.Slots, - Text: prev.Slots.Text, - }) + history = append(history, sessionAsTurn(prev)) maxHist := len(prev.History) - if maxHist > 3 { - maxHist = 3 + if maxHist > maxCarriedHistory { + maxHist = maxCarriedHistory } history = append(history, prev.History[:maxHist]...) } diff --git a/cmd/mavend/voice.go b/cmd/mavend/voice.go index 1b783d9..c39ef07 100644 --- a/cmd/mavend/voice.go +++ b/cmd/mavend/voice.go @@ -528,6 +528,17 @@ func (h *reactiveHandler) replySystem(ctx context.Context, dec router.Decision) } } +// sessionAsTurn projects a stored session onto the dialogue.Turn shape used in +// history lists. Shared by chatHistory and rememberTurn (clarify.go) so the +// same session is described the same way in both places. +func sessionAsTurn(s *dialogue.Session) dialogue.Turn { + return dialogue.Turn{ + Intent: s.Intent, + Slots: s.Slots, + Text: s.Slots.Text, + } +} + // chatHistory collects dialogue turns from the session store for the current // conversation. Returns prior user utterances (newest last) up to a depth of // 4 turns. Returns nil when there's no session or no history. @@ -541,13 +552,9 @@ func (h *reactiveHandler) chatHistory(ctx context.Context) []dialogue.Turn { return nil } // History already includes the immediate prior turn (set by the dialogue - // merge at lines 373-395), plus up to 3 more from deeper history. + // merge in runTurn's step 6, above), plus up to 3 more from deeper history. out := make([]dialogue.Turn, 0, 1+len(prev.History)) - out = append(out, dialogue.Turn{ - Intent: prev.Intent, - Slots: prev.Slots, - Text: prev.Slots.Text, - }) + out = append(out, sessionAsTurn(prev)) out = append(out, prev.History...) return out }