feat: include chatwoot metadata with each tool call (#12907)

This commit is contained in:
Shivam Mishra
2025-12-10 15:25:18 +05:30
committed by GitHub
parent 20fa5eeaa5
commit 0d8e249fe4
4 changed files with 257 additions and 3 deletions

View File

@@ -327,6 +327,98 @@ RSpec.describe Captain::CustomTool, type: :model do
end
end
describe '#build_metadata_headers' do
let(:tool) { create(:captain_custom_tool, account: account, slug: 'custom_test_tool') }
let(:conversation) { create(:conversation, account: account) }
let(:contact) { conversation.contact }
let(:state) do
{
account_id: account.id,
assistant_id: 123,
conversation: {
id: conversation.id,
display_id: conversation.display_id
},
contact: {
id: contact.id,
email: contact.email,
phone_number: contact.phone_number
}
}
end
it 'includes account and assistant metadata' do
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Account-Id']).to eq(account.id.to_s)
expect(headers['X-Chatwoot-Assistant-Id']).to eq('123')
end
it 'includes tool slug' do
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Tool-Slug']).to eq('custom_test_tool')
end
it 'includes conversation metadata when present' do
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Conversation-Id']).to eq(conversation.id.to_s)
expect(headers['X-Chatwoot-Conversation-Display-Id']).to eq(conversation.display_id.to_s)
end
it 'includes contact metadata when present' do
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Contact-Id']).to eq(contact.id.to_s)
expect(headers['X-Chatwoot-Contact-Email']).to eq(contact.email)
end
it 'handles missing conversation gracefully' do
state[:conversation] = nil
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Conversation-Id']).to be_nil
expect(headers['X-Chatwoot-Conversation-Display-Id']).to be_nil
expect(headers['X-Chatwoot-Account-Id']).to eq(account.id.to_s)
end
it 'handles missing contact gracefully' do
state[:contact] = nil
headers = tool.build_metadata_headers(state)
expect(headers['X-Chatwoot-Contact-Id']).to be_nil
expect(headers['X-Chatwoot-Contact-Email']).to be_nil
expect(headers['X-Chatwoot-Account-Id']).to eq(account.id.to_s)
end
it 'handles empty state' do
headers = tool.build_metadata_headers({})
expect(headers).to be_a(Hash)
expect(headers['X-Chatwoot-Tool-Slug']).to eq('custom_test_tool')
end
it 'omits contact email header when email is blank' do
state[:contact][:email] = ''
headers = tool.build_metadata_headers(state)
expect(headers).not_to have_key('X-Chatwoot-Contact-Email')
end
it 'omits contact phone header when phone number is blank' do
state[:contact][:phone_number] = ''
headers = tool.build_metadata_headers(state)
expect(headers).not_to have_key('X-Chatwoot-Contact-Phone')
end
end
describe '#to_tool_metadata' do
it 'returns tool metadata hash with custom flag' do
tool = create(:captain_custom_tool, account: account,