From a2857cac38de2d4636cf4c16a68b5d6bbe18288c Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma <32020192+tds-1@users.noreply.github.com> Date: Fri, 20 Jun 2025 23:28:00 +0530 Subject: [PATCH] feat: Expose custom attributes in conversation to Captain (#11769) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 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? ![Screenshot 2025-06-19 at 9 50 45 AM](https://github.com/user-attachments/assets/5216e116-bd89-4d0c-b6a6-416b082638f7) ![Screenshot 2025-06-19 at 9 50 40 AM](https://github.com/user-attachments/assets/a81cb4ad-973b-405c-b188-295d1acce814) ## 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 --- .../conversation_llm_formatter.rb | 14 +++++++++++ .../conversation_llm_formatter_spec.rb | 25 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/app/services/llm_formatter/conversation_llm_formatter.rb b/app/services/llm_formatter/conversation_llm_formatter.rb index 1444d75c1..a43471e5b 100644 --- a/app/services/llm_formatter/conversation_llm_formatter.rb +++ b/app/services/llm_formatter/conversation_llm_formatter.rb @@ -11,6 +11,13 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter 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 @@ -30,4 +37,11 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter 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 diff --git a/spec/services/llm_formatter/conversation_llm_formatter_spec.rb b/spec/services/llm_formatter/conversation_llm_formatter_spec.rb index 93fec14f7..49fcc1a18 100644 --- a/spec/services/llm_formatter/conversation_llm_formatter_spec.rb +++ b/spec/services/llm_formatter/conversation_llm_formatter_spec.rb @@ -61,5 +61,30 @@ RSpec.describe LlmFormatter::ConversationLlmFormatter do expect(formatter.format(include_contact_details: true)).to eq(expected_output) end end + + context 'when conversation has custom attributes' do + it 'includes formatted custom attributes in the output' do + create( + :custom_attribute_definition, + account: account, + attribute_display_name: 'Order ID', + attribute_key: 'order_id', + attribute_model: :conversation_attribute + ) + + conversation.update(custom_attributes: { 'order_id' => '12345' }) + + expected_output = [ + "Conversation ID: ##{conversation.display_id}", + "Channel: #{conversation.inbox.channel.name}", + 'Message History:', + 'No messages in this conversation', + 'Conversation Attributes:', + 'Order ID: 12345' + ].join("\n") + + expect(formatter.format).to eq(expected_output) + end + end end end