feat(ee): Add transcription support for audio messages (#11670)
<img width="419" alt="Screenshot 2025-06-03 at 4 25 37 PM" src="https://github.com/user-attachments/assets/4b6ddd11-9b91-4981-a571-83746cc4d40b" /> Fixes https://github.com/chatwoot/chatwoot/issues/10182 --------- Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Messages::AudioTranscriptionJob do
|
||||
subject(:job) { described_class.perform_later(attachment_id) }
|
||||
|
||||
let(:message) { create(:message) }
|
||||
let(:attachment) do
|
||||
message.attachments.create!(
|
||||
account_id: message.account_id,
|
||||
file_type: :audio,
|
||||
file: fixture_file_upload('public/audio/widget/ding.mp3')
|
||||
)
|
||||
end
|
||||
let(:attachment_id) { attachment.id }
|
||||
let(:conversation) { message.conversation }
|
||||
let(:transcription_service) { instance_double(Messages::AudioTranscriptionService) }
|
||||
|
||||
it 'enqueues the job' do
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.with(attachment_id)
|
||||
.on_queue('low')
|
||||
end
|
||||
|
||||
context 'when performing the job' do
|
||||
before do
|
||||
allow(Messages::AudioTranscriptionService).to receive(:new).with(attachment).and_return(transcription_service)
|
||||
allow(transcription_service).to receive(:perform)
|
||||
end
|
||||
|
||||
it 'calls AudioTranscriptionService with the attachment' do
|
||||
expect(Messages::AudioTranscriptionService).to receive(:new).with(attachment)
|
||||
expect(transcription_service).to receive(:perform)
|
||||
described_class.perform_now(attachment_id)
|
||||
end
|
||||
|
||||
it 'does nothing when attachment is not found' do
|
||||
expect(Messages::AudioTranscriptionService).not_to receive(:new)
|
||||
described_class.perform_now(999_999)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,70 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Messages::AudioTranscriptionService, type: :service do
|
||||
let(:account) { create(:account, audio_transcriptions: true) }
|
||||
let(:conversation) { create(:conversation, account: account) }
|
||||
let(:message) { create(:message, conversation: conversation) }
|
||||
let(:attachment) { message.attachments.create!(account: account, file_type: :audio) }
|
||||
|
||||
before do
|
||||
# Create required installation configs
|
||||
create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-api-key')
|
||||
create(:installation_config, name: 'CAPTAIN_OPEN_AI_MODEL', value: 'gpt-4o-mini')
|
||||
|
||||
# Mock usage limits for transcription to be available
|
||||
allow(account).to receive(:usage_limits).and_return({ captain: { responses: { current_available: 100 } } })
|
||||
end
|
||||
|
||||
describe '#perform' do
|
||||
let(:service) { described_class.new(attachment) }
|
||||
|
||||
context 'when transcription is successful' do
|
||||
before do
|
||||
# Mock can_transcribe? to return true and transcribe_audio method
|
||||
allow(service).to receive(:can_transcribe?).and_return(true)
|
||||
allow(service).to receive(:transcribe_audio).and_return('Hello world transcription')
|
||||
end
|
||||
|
||||
it 'returns successful transcription' do
|
||||
result = service.perform
|
||||
expect(result).to eq({ success: true, transcriptions: 'Hello world transcription' })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when audio transcriptions are disabled' do
|
||||
before do
|
||||
account.update!(audio_transcriptions: false)
|
||||
end
|
||||
|
||||
it 'returns error for transcription limit exceeded' do
|
||||
result = service.perform
|
||||
expect(result).to eq({ error: 'Transcription limit exceeded' })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when attachment already has transcribed text' do
|
||||
before do
|
||||
attachment.update!(meta: { transcribed_text: 'Existing transcription' })
|
||||
allow(service).to receive(:can_transcribe?).and_return(true)
|
||||
end
|
||||
|
||||
it 'returns existing transcription without calling API' do
|
||||
result = service.perform
|
||||
expect(result).to eq({ success: true, transcriptions: 'Existing transcription' })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when transcription fails' do
|
||||
before do
|
||||
allow(service).to receive(:can_transcribe?).and_return(true)
|
||||
allow(service).to receive(:transcribe_audio).and_raise(StandardError.new('API error'))
|
||||
allow(ChatwootExceptionTracker).to receive(:new).and_return(instance_double(ChatwootExceptionTracker, capture_exception: nil))
|
||||
end
|
||||
|
||||
it 'returns error response' do
|
||||
result = service.perform
|
||||
expect(result).to eq({ error: 'Transcription failed: API error' })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user