feat: add campaign context to Captain v2 prompts (#13644)
Co-authored-by: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com>
This commit is contained in:
@@ -97,7 +97,8 @@ RSpec.describe Concerns::Agentable do
|
||||
expected_context = {
|
||||
base_key: 'base_value',
|
||||
conversation: { id: 123 },
|
||||
contact: { name: 'John' }
|
||||
contact: { name: 'John' },
|
||||
campaign: {}
|
||||
}
|
||||
|
||||
expect(Captain::PromptRenderer).to receive(:render).with(
|
||||
@@ -108,6 +109,26 @@ RSpec.describe Concerns::Agentable do
|
||||
dummy_instance.agent_instructions(context_double)
|
||||
end
|
||||
|
||||
it 'merges campaign data from context state' do
|
||||
context_double = instance_double(Agents::RunContext,
|
||||
context: {
|
||||
state: {
|
||||
conversation: { id: 123 },
|
||||
contact: { name: 'John' },
|
||||
campaign: { id: 10, title: 'Summer Sale', message: 'Check it out' }
|
||||
}
|
||||
})
|
||||
|
||||
expect(Captain::PromptRenderer).to receive(:render).with(
|
||||
'dummy_class',
|
||||
hash_including(
|
||||
campaign: { id: 10, title: 'Summer Sale', message: 'Check it out' }
|
||||
)
|
||||
)
|
||||
|
||||
dummy_instance.agent_instructions(context_double)
|
||||
end
|
||||
|
||||
it 'handles context without state' do
|
||||
context_double = instance_double(Agents::RunContext, context: {})
|
||||
|
||||
@@ -116,7 +137,8 @@ RSpec.describe Concerns::Agentable do
|
||||
hash_including(
|
||||
base_key: 'base_value',
|
||||
conversation: {},
|
||||
contact: {}
|
||||
contact: {},
|
||||
campaign: {}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -394,6 +394,34 @@ RSpec.describe Captain::Assistant::AgentRunnerService do
|
||||
)
|
||||
end
|
||||
|
||||
it 'does not include campaign when conversation has no campaign' do
|
||||
state = service.send(:build_state)
|
||||
|
||||
expect(state).not_to have_key(:campaign)
|
||||
end
|
||||
|
||||
context 'when conversation has a campaign' do
|
||||
let(:campaign) { create(:campaign, account: account, title: 'Summer Sale', message: 'Check out our deals!', description: 'Seasonal promo') }
|
||||
let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact, campaign: campaign) }
|
||||
|
||||
it 'includes campaign attributes in state' do
|
||||
state = service.send(:build_state)
|
||||
|
||||
expect(state[:campaign]).to include(
|
||||
id: campaign.id,
|
||||
title: 'Summer Sale',
|
||||
message: 'Check out our deals!',
|
||||
description: 'Seasonal promo'
|
||||
)
|
||||
end
|
||||
|
||||
it 'only includes attributes defined in CAMPAIGN_STATE_ATTRIBUTES' do
|
||||
state = service.send(:build_state)
|
||||
|
||||
expect(state[:campaign].keys).to match_array(described_class::CAMPAIGN_STATE_ATTRIBUTES)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation is nil' do
|
||||
subject(:service) { described_class.new(assistant: assistant, conversation: nil) }
|
||||
|
||||
@@ -407,6 +435,7 @@ RSpec.describe Captain::Assistant::AgentRunnerService do
|
||||
)
|
||||
expect(state).not_to have_key(:conversation)
|
||||
expect(state).not_to have_key(:contact)
|
||||
expect(state).not_to have_key(:campaign)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -477,5 +506,11 @@ RSpec.describe Captain::Assistant::AgentRunnerService do
|
||||
:id, :name, :email, :phone_number, :identifier, :contact_type
|
||||
)
|
||||
end
|
||||
|
||||
it 'defines campaign state attributes' do
|
||||
expect(described_class::CAMPAIGN_STATE_ATTRIBUTES).to include(
|
||||
:id, :title, :message, :campaign_type, :description
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user