mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-04 04:57:51 +00:00 
			
		
		
		
	feat: Expose custom attributes in conversation to Captain (#11769)
# 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
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							293a29ec98
						
					
				
				
					commit
					a2857cac38
				
			@@ -11,6 +11,13 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
 | 
				
			|||||||
                end
 | 
					                end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sections << "Contact Details: #{@record.contact.to_llm_text}" if config[:include_contact_details]
 | 
					    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")
 | 
					    sections.join("\n")
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -30,4 +37,11 @@ class LlmFormatter::ConversationLlmFormatter < LlmFormatter::DefaultLlmFormatter
 | 
				
			|||||||
    sender = message.message_type == 'incoming' ? 'User' : 'Support agent'
 | 
					    sender = message.message_type == 'incoming' ? 'User' : 'Support agent'
 | 
				
			||||||
    "#{sender}: #{message.content}\n"
 | 
					    "#{sender}: #{message.content}\n"
 | 
				
			||||||
  end
 | 
					  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
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,5 +61,30 @@ RSpec.describe LlmFormatter::ConversationLlmFormatter do
 | 
				
			|||||||
        expect(formatter.format(include_contact_details: true)).to eq(expected_output)
 | 
					        expect(formatter.format(include_contact_details: true)).to eq(expected_output)
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
    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
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user