feat: allow captain to access contact attributes (#13850)

# Pull Request Template

## Description

Captain v1 does not have access to contact attributes. Added a toggle to
let user choose if they want contact information available to Captain.

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration.

Specs and locally
<img width="1924" height="740" alt="CleanShot 2026-03-19 at 18 48 19@2x"
src="https://github.com/user-attachments/assets/353cfeaa-cd58-40eb-89e7-d660a1dc1185"
/>

![Uploading CleanShot 2026-03-19 at 18.53.26@2x.png…]()


## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules
This commit is contained in:
Aakash Bakhle
2026-03-20 16:15:06 +05:30
committed by GitHub
parent a9123e7d66
commit 290dd3abf5
17 changed files with 159 additions and 45 deletions

View File

@@ -393,7 +393,7 @@ RSpec.describe Captain::Assistant::AgentRunnerService do
)
end
it 'includes contact attributes when contact is present' do
it 'always includes contact attributes in state for tool access' do
state = service.send(:build_state)
expect(state[:contact]).to include(

View File

@@ -30,7 +30,7 @@ RSpec.describe Captain::Llm::AssistantChatService do
describe 'instrumentation metadata' do
it 'passes channel_type to the agent session instrumentation' do
service = described_class.new(assistant: assistant, conversation_id: conversation.display_id)
service = described_class.new(assistant: assistant, conversation: conversation)
expect(service).to receive(:instrument_agent_session).with(
hash_including(metadata: hash_including(channel_type: conversation.inbox.channel_type))
@@ -61,7 +61,7 @@ RSpec.describe Captain::Llm::AssistantChatService do
with: ['https://example.com/screenshot.png']
).and_return(mock_response)
service = described_class.new(assistant: assistant, conversation_id: conversation.display_id)
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: message_history)
end
end
@@ -84,7 +84,7 @@ RSpec.describe Captain::Llm::AssistantChatService do
with: ['https://example.com/photo.jpg']
).and_return(mock_response)
service = described_class.new(assistant: assistant, conversation_id: conversation.display_id)
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: message_history)
end
end
@@ -99,7 +99,7 @@ RSpec.describe Captain::Llm::AssistantChatService do
it 'sends the text without attachments' do
expect(mock_chat).to receive(:ask).with('Hello, how can you help me?').and_return(mock_response)
service = described_class.new(assistant: assistant, conversation_id: conversation.display_id)
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: message_history)
end
end
@@ -139,9 +139,53 @@ RSpec.describe Captain::Llm::AssistantChatService do
# Current message asked via chat.ask
expect(mock_chat).to receive(:ask).with('It still does not work').and_return(mock_response)
service = described_class.new(assistant: assistant, conversation_id: conversation.display_id)
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: message_history)
end
end
end
describe 'contact attributes in system prompt' do
let(:contact) { create(:contact, account: account, name: 'Diep Bui', email: 'diep@example.com', custom_attributes: { 'plan' => 'pro' }) }
let(:conversation) { create(:conversation, account: account, contact: contact) }
context 'when feature_contact_attributes is enabled' do
before { assistant.update!(config: assistant.config.merge('feature_contact_attributes' => true)) }
it 'includes contact information in the system prompt' do
allow(mock_chat).to receive(:ask).and_return(mock_response)
expect(mock_chat).to receive(:with_instructions).with(a_string_including('[Contact Information]')) do |_instructions|
mock_chat
end
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: [{ role: 'user', content: 'Hello' }])
end
it 'includes custom attributes in the system prompt' do
allow(mock_chat).to receive(:ask).and_return(mock_response)
expect(mock_chat).to receive(:with_instructions).with(a_string_including('plan: pro')) do |_instructions|
mock_chat
end
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: [{ role: 'user', content: 'Hello' }])
end
end
context 'when feature_contact_attributes is disabled' do
it 'does not include contact information in the system prompt' do
allow(mock_chat).to receive(:ask).and_return(mock_response)
expect(mock_chat).to receive(:with_instructions).with(satisfy { |s| s.exclude?('[Contact Information]') }) do |_instructions|
mock_chat
end
service = described_class.new(assistant: assistant, conversation: conversation)
service.generate_response(message_history: [{ role: 'user', content: 'Hello' }])
end
end
end
end