Class: Assistant::TechnicalSupport::ResponseGroundingTurn

Inherits:
Object
  • Object
show all
Defined in:
app/services/assistant/technical_support/response_grounding_turn.rb

Overview

Resolves the persisted user and final assistant messages that bound one
Technical Support response, including recovery paths without a supplied ID.

Instance Method Summary collapse

Constructor Details

#initialize(conversation:, supplied_user_message_id: nil, assistant_message_id: nil) ⇒ ResponseGroundingTurn

Builds a resolver for one persisted conversation turn.

Parameters:

  • conversation (AssistantConversation)

    Sunny conversation

  • supplied_user_message_id (Integer, nil) (defaults to: nil)

    triggering user-message ID

  • assistant_message_id (Integer, nil) (defaults to: nil)

    exact final response ID



13
14
15
16
17
# File 'app/services/assistant/technical_support/response_grounding_turn.rb', line 13

def initialize(conversation:, supplied_user_message_id: nil, assistant_message_id: nil)
  @conversation = conversation
  @supplied_user_message_id = supplied_user_message_id
  @assistant_message_id = assistant_message_id
end

Instance Method Details

#assistant_messageAssistantMessage?

Returns the exact or latest nonblank assistant answer after the user.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/assistant/technical_support/response_grounding_turn.rb', line 31

def assistant_message
  return unless user_message_id

  scope = conversation.assistant_messages
                      .where(role: 'assistant')
                      .where('id > ?', user_message_id)
                      .where.not(content: [nil, ''])
  scope = scope.where(id: ...next_user_message_id) if next_user_message_id
  return scope.find_by(id: assistant_message_id) if assistant_message_id

  scope.order(:id).last
end

#user_message_idInteger?

Returns the persisted triggering user-message ID.

Returns:

  • (Integer, nil)

    supplied valid ID or latest persisted user message



22
23
24
25
26
# File 'app/services/assistant/technical_support/response_grounding_turn.rb', line 22

def user_message_id
  return @user_message_id if defined?(@user_message_id)

  @user_message_id = persisted_supplied_user_message_id || latest_user_message_id
end