From b98c614669e59c7b63305f6ba0d838b38cd9951a Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 25 Feb 2026 18:33:37 +0530 Subject: [PATCH] feat: add campaign context to Captain v2 prompts (#13644) Co-authored-by: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com> --- enterprise/app/models/concerns/agentable.rb | 4 ++- .../captain/assistant/agent_runner_service.rb | 3 ++ .../lib/captain/prompts/assistant.liquid | 6 +++- .../lib/captain/prompts/scenario.liquid | 6 +++- .../captain/prompts/snippets/campaign.liquid | 8 +++++ .../models/concerns/agentable_spec.rb | 26 ++++++++++++-- .../assistant/agent_runner_service_spec.rb | 35 +++++++++++++++++++ 7 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 enterprise/lib/captain/prompts/snippets/campaign.liquid diff --git a/enterprise/app/models/concerns/agentable.rb b/enterprise/app/models/concerns/agentable.rb index e5b0b8eef..ed8e0a89f 100644 --- a/enterprise/app/models/concerns/agentable.rb +++ b/enterprise/app/models/concerns/agentable.rb @@ -19,9 +19,11 @@ module Concerns::Agentable state = context.context[:state] || {} conversation_data = state[:conversation] || {} contact_data = state[:contact] || {} + campaign_data = state[:campaign] || {} enhanced_context = enhanced_context.merge( conversation: conversation_data, - contact: contact_data + contact: contact_data, + campaign: campaign_data ) end diff --git a/enterprise/app/services/captain/assistant/agent_runner_service.rb b/enterprise/app/services/captain/assistant/agent_runner_service.rb index 72c44024f..8238ca4d8 100644 --- a/enterprise/app/services/captain/assistant/agent_runner_service.rb +++ b/enterprise/app/services/captain/assistant/agent_runner_service.rb @@ -16,6 +16,8 @@ class Captain::Assistant::AgentRunnerService custom_attributes additional_attributes ].freeze + CAMPAIGN_STATE_ATTRIBUTES = %i[id title message campaign_type description].freeze + def initialize(assistant:, conversation: nil, callbacks: {}) @assistant = assistant @conversation = conversation @@ -129,6 +131,7 @@ class Captain::Assistant::AgentRunnerService state[:conversation] = @conversation.attributes.symbolize_keys.slice(*CONVERSATION_STATE_ATTRIBUTES) state[:channel_type] = @conversation.inbox&.channel_type state[:contact] = @conversation.contact.attributes.symbolize_keys.slice(*CONTACT_STATE_ATTRIBUTES) if @conversation.contact + state[:campaign] = @conversation.campaign.attributes.symbolize_keys.slice(*CAMPAIGN_STATE_ATTRIBUTES) if @conversation.campaign end state diff --git a/enterprise/lib/captain/prompts/assistant.liquid b/enterprise/lib/captain/prompts/assistant.liquid index 0dc7d8577..7b20a089e 100644 --- a/enterprise/lib/captain/prompts/assistant.liquid +++ b/enterprise/lib/captain/prompts/assistant.liquid @@ -8,7 +8,7 @@ You are {{name}}, a helpful and knowledgeable assistant. Your role is to primari Don't digress away from your instructions, and use all the available tools at your disposal for solving customer issues. If you are to state something factual about {{product_name}} ensure you source that information from the FAQs only. Use the `captain--tools--faq_lookup` tool for this. -{% if conversation || contact -%} +{% if conversation || contact || campaign.id -%} # Current Context Here's the metadata we have about the current conversation and the contact associated with it: @@ -20,6 +20,10 @@ Here's the metadata we have about the current conversation and the contact assoc {% if contact -%} {% render 'contact' %} {% endif -%} + +{% if campaign.id -%} +{% render 'campaign' %} +{% endif -%} {% endif -%} {% if response_guidelines.size > 0 -%} diff --git a/enterprise/lib/captain/prompts/scenario.liquid b/enterprise/lib/captain/prompts/scenario.liquid index 1148a7c3a..879a39f52 100644 --- a/enterprise/lib/captain/prompts/scenario.liquid +++ b/enterprise/lib/captain/prompts/scenario.liquid @@ -8,7 +8,7 @@ You are a specialized agent called "{{ title }}", your task is to handle the fol If you believe the user's request is not within the scope of your role, you can assign this conversation back to the orchestrator agent using the `handoff_to_{{ assistant_name }}` tool -{% if conversation || contact %} +{% if conversation || contact || campaign.id %} # Current Context Here's the metadata we have about the current conversation and the contact associated with it: @@ -20,6 +20,10 @@ Here's the metadata we have about the current conversation and the contact assoc {% if contact -%} {% render 'contact' %} {% endif -%} + +{% if campaign.id -%} +{% render 'campaign' %} +{% endif -%} {% endif -%} diff --git a/enterprise/lib/captain/prompts/snippets/campaign.liquid b/enterprise/lib/captain/prompts/snippets/campaign.liquid new file mode 100644 index 000000000..db2ac0e8e --- /dev/null +++ b/enterprise/lib/captain/prompts/snippets/campaign.liquid @@ -0,0 +1,8 @@ +# Campaign Context +This conversation was initiated in response to a campaign message. +- Campaign: {{ campaign.title }} +- Type: {{ campaign.campaign_type }} +{% if campaign.description -%} +- Description: {{ campaign.description }} +{% endif -%} +- Original Message Sent: {{ campaign.message }} diff --git a/spec/enterprise/models/concerns/agentable_spec.rb b/spec/enterprise/models/concerns/agentable_spec.rb index 6b170e8d7..fbf6a58dc 100644 --- a/spec/enterprise/models/concerns/agentable_spec.rb +++ b/spec/enterprise/models/concerns/agentable_spec.rb @@ -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: {} ) ) diff --git a/spec/enterprise/services/captain/assistant/agent_runner_service_spec.rb b/spec/enterprise/services/captain/assistant/agent_runner_service_spec.rb index 13e4804ce..0d22b8266 100644 --- a/spec/enterprise/services/captain/assistant/agent_runner_service_spec.rb +++ b/spec/enterprise/services/captain/assistant/agent_runner_service_spec.rb @@ -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