Class: SpreeMenuChat::Conversation

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree_menu_chat/conversation.rb

Overview

A loosely-grouped set of chat messages for one (store, identifier) — persisted for admin QA visibility (M6), not to power real multi-turn context in generation yet (SpreeMenuChat::AnswerGenerator still answers each question independently; see the plan's open decision on this).

Grouped by a time-window heuristic rather than an explicit client-sent session id: the widget has no session/cookie of its own (M4 built it deliberately stateless — every request is just a question + the store's publishable key), and adding one purely to correlate log rows felt like more surface area than M6's actual goal (giving admins something to look at) justified. The tradeoff is real: two different customers behind the same NAT/office IP within the window land in one Conversation. Acceptable for a QA log, not something to build real per-customer behavior on top of without revisiting this.

Constant Summary collapse

SESSION_WINDOW =

A gap this long since the identifier's last message starts a new Conversation instead of continuing the old one.

30.minutes

Class Method Summary collapse

Class Method Details

.find_or_create_for(store:, identifier:) ⇒ Object



34
35
36
37
# File 'app/models/spree_menu_chat/conversation.rb', line 34

def self.find_or_create_for(store:, identifier:)
  recent = where(store: store, identifier: identifier).where('updated_at > ?', SESSION_WINDOW.ago).order(updated_at: :desc).first
  recent || create!(store: store, identifier: identifier)
end