# Pull Request Template ## Linear Link https://linear.app/chatwoot/issue/CW-4480/expose-custom-attributes-in-conversation-to-captain-so-that-it-can ## Description Expose custom attributes in conversation to Captain so that it can provide more information ## Type of change - [ ] New feature (non-breaking change which adds functionality) ## How Has This Been Tested?   ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
48 lines
1.4 KiB
Ruby
48 lines
1.4 KiB
Ruby
class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
|
|
def format(config = {})
|
|
sections = []
|
|
sections << "Conversation ID: ##{@record.display_id}"
|
|
sections << "Channel: #{@record.inbox.channel.name}"
|
|
sections << 'Message History:'
|
|
sections << if @record.messages.any?
|
|
build_messages
|
|
else
|
|
'No messages in this conversation'
|
|
end
|
|
|
|
sections << "Contact Details: #{@record.contact.to_llm_text}" if config[:include_contact_details]
|
|
|
|
attributes = build_attributes
|
|
if attributes.present?
|
|
sections << 'Conversation Attributes:'
|
|
sections << attributes
|
|
end
|
|
|
|
sections.join("\n")
|
|
end
|
|
|
|
private
|
|
|
|
def build_messages
|
|
return "No messages in this conversation\n" if @record.messages.empty?
|
|
|
|
message_text = ''
|
|
@record.messages.chat.order(created_at: :asc).each do |message|
|
|
message_text << format_message(message)
|
|
end
|
|
message_text
|
|
end
|
|
|
|
def format_message(message)
|
|
sender = message.message_type == 'incoming' ? 'User' : 'Support agent'
|
|
"#{sender}: #{message.content}\n"
|
|
end
|
|
|
|
def build_attributes
|
|
attributes = @record.account.custom_attribute_definitions.with_attribute_model('conversation_attribute').map do |attribute|
|
|
"#{attribute.attribute_display_name}: #{@record.custom_attributes[attribute.attribute_key]}"
|
|
end
|
|
attributes.join("\n")
|
|
end
|
|
end
|