feat: Migrate ruby llm captain (#12981)
Co-authored-by: aakashb95 <aakash@chatwoot.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
@@ -7,7 +7,6 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
let(:assistant) { create(:captain_assistant, account: account) }
|
||||
let(:contact) { create(:contact, account: account) }
|
||||
let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact) }
|
||||
let(:mock_openai_client) { instance_double(OpenAI::Client) }
|
||||
let(:copilot_thread) { create(:captain_copilot_thread, account: account, user: user) }
|
||||
let!(:copilot_message) do
|
||||
create(
|
||||
@@ -20,13 +19,29 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
{ user_id: user.id, copilot_thread_id: copilot_thread.id, conversation_id: conversation.display_id }
|
||||
end
|
||||
|
||||
# RubyLLM mocks
|
||||
let(:mock_chat) { instance_double(RubyLLM::Chat) }
|
||||
let(:mock_response) do
|
||||
instance_double(RubyLLM::Message, content: '{ "content": "Hey", "reasoning": "Test reasoning", "reply_suggestion": false }')
|
||||
end
|
||||
|
||||
before do
|
||||
create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key')
|
||||
create(:installation_config, name: 'CAPTAIN_OPEN_AI_ENDPOINT', value: 'https://api.openai.com/')
|
||||
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
||||
allow(mock_openai_client).to receive(:chat).and_return({
|
||||
choices: [{ message: { content: '{ "content": "Hey" }' } }]
|
||||
}.with_indifferent_access)
|
||||
InstallationConfig.find_or_create_by(name: 'CAPTAIN_OPEN_AI_API_KEY') do |c|
|
||||
c.value = 'test-key'
|
||||
end
|
||||
|
||||
allow(RubyLLM).to receive(:chat).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:with_temperature).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:with_params).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:with_tool).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:with_instructions).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:add_message).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:on_new_message).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:on_end_message).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:on_tool_call).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:on_tool_result).and_return(mock_chat)
|
||||
allow(mock_chat).to receive(:messages).and_return([])
|
||||
allow(mock_chat).to receive(:ask).and_return(mock_response)
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
@@ -48,48 +63,6 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
expect(messages.second[:role]).to eq('system')
|
||||
expect(messages.second[:content]).to include(account.id.to_s)
|
||||
end
|
||||
|
||||
it 'initializes OpenAI client with configured endpoint' do
|
||||
expect(OpenAI::Client).to receive(:new).with(
|
||||
access_token: 'test-key',
|
||||
uri_base: 'https://api.openai.com/',
|
||||
log_errors: Rails.env.development?
|
||||
)
|
||||
|
||||
described_class.new(assistant, config)
|
||||
end
|
||||
|
||||
context 'when CAPTAIN_OPEN_AI_ENDPOINT is not configured' do
|
||||
before do
|
||||
InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_ENDPOINT')&.destroy
|
||||
end
|
||||
|
||||
it 'uses default OpenAI endpoint' do
|
||||
expect(OpenAI::Client).to receive(:new).with(
|
||||
access_token: 'test-key',
|
||||
uri_base: 'https://api.openai.com/',
|
||||
log_errors: Rails.env.development?
|
||||
)
|
||||
|
||||
described_class.new(assistant, config)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when custom endpoint is configured' do
|
||||
before do
|
||||
InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_ENDPOINT').update!(value: 'https://custom.azure.com/')
|
||||
end
|
||||
|
||||
it 'uses custom endpoint for OpenAI client' do
|
||||
expect(OpenAI::Client).to receive(:new).with(
|
||||
access_token: 'test-key',
|
||||
uri_base: 'https://custom.azure.com/',
|
||||
log_errors: Rails.env.development?
|
||||
)
|
||||
|
||||
described_class.new(assistant, config)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#generate_response' do
|
||||
@@ -112,82 +85,19 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
end
|
||||
|
||||
it 'returns the response from request_chat_completion' do
|
||||
expect(service.generate_response('Hello')).to eq({ 'content' => 'Hey' })
|
||||
result = service.generate_response('Hello')
|
||||
|
||||
expect(result).to eq({ 'content' => 'Hey', 'reasoning' => 'Test reasoning', 'reply_suggestion' => false })
|
||||
end
|
||||
|
||||
context 'when response contains tool calls' do
|
||||
before do
|
||||
allow(mock_openai_client).to receive(:chat).and_return(
|
||||
{
|
||||
choices: [{ message: { 'tool_calls' => tool_calls } }]
|
||||
}.with_indifferent_access,
|
||||
{
|
||||
choices: [{ message: { content: '{ "content": "Tool response processed" }' } }]
|
||||
}.with_indifferent_access
|
||||
)
|
||||
end
|
||||
|
||||
context 'when tool call is valid' do
|
||||
let(:tool_calls) do
|
||||
[{
|
||||
'id' => 'call_123',
|
||||
'function' => {
|
||||
'name' => 'get_conversation',
|
||||
'arguments' => "{ \"conversation_id\": #{conversation.display_id} }"
|
||||
}
|
||||
}]
|
||||
end
|
||||
|
||||
it 'processes tool calls and appends them to messages' do
|
||||
result = service.generate_response("Find conversation #{conversation.id}")
|
||||
|
||||
expect(result).to eq({ 'content' => 'Tool response processed' })
|
||||
expect(service.messages).to include(
|
||||
{ role: 'assistant', tool_calls: tool_calls }
|
||||
)
|
||||
expect(service.messages).to include(
|
||||
{
|
||||
role: 'tool', tool_call_id: 'call_123', content: conversation.to_llm_text
|
||||
}
|
||||
)
|
||||
|
||||
expect(result).to eq({ 'content' => 'Tool response processed' })
|
||||
end
|
||||
end
|
||||
|
||||
context 'when tool call is invalid' do
|
||||
let(:tool_calls) do
|
||||
[{
|
||||
'id' => 'call_123',
|
||||
'function' => {
|
||||
'name' => 'get_settings',
|
||||
'arguments' => '{}'
|
||||
}
|
||||
}]
|
||||
end
|
||||
|
||||
it 'handles invalid tool calls' do
|
||||
result = service.generate_response('Find settings')
|
||||
|
||||
expect(result).to eq({ 'content' => 'Tool response processed' })
|
||||
expect(service.messages).to include(
|
||||
{
|
||||
role: 'assistant', tool_calls: tool_calls
|
||||
}
|
||||
)
|
||||
expect(service.messages).to include(
|
||||
{
|
||||
role: 'tool',
|
||||
tool_call_id: 'call_123',
|
||||
content: 'Tool not available'
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
it 'increments response usage for the account' do
|
||||
expect do
|
||||
service.generate_response('Hello')
|
||||
end.to(change { account.reload.custom_attributes['captain_responses_usage'].to_i }.by(1))
|
||||
end
|
||||
end
|
||||
|
||||
describe '#setup_user' do
|
||||
describe 'user setup behavior' do
|
||||
it 'sets user when user_id is present in config' do
|
||||
service = described_class.new(assistant, { user_id: user.id })
|
||||
expect(service.user).to eq(user)
|
||||
@@ -199,7 +109,7 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#setup_message_history' do
|
||||
describe 'message history behavior' do
|
||||
context 'when copilot_thread_id is present' do
|
||||
it 'finds the copilot thread and sets previous history from it' do
|
||||
service = described_class.new(assistant, { copilot_thread_id: copilot_thread.id })
|
||||
@@ -227,7 +137,7 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#build_messages' do
|
||||
describe 'message building behavior' do
|
||||
it 'includes system message and account context' do
|
||||
service = described_class.new(assistant, {})
|
||||
messages = service.messages
|
||||
@@ -257,13 +167,9 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#persist_message' do
|
||||
describe 'message persistence behavior' do
|
||||
context 'when copilot_thread is present' do
|
||||
it 'creates a copilot message' do
|
||||
allow(mock_openai_client).to receive(:chat).and_return({
|
||||
choices: [{ message: { content: '{ "content": "Hey" }' } }]
|
||||
}.with_indifferent_access)
|
||||
|
||||
it 'creates a copilot message with the response' do
|
||||
expect do
|
||||
described_class.new(assistant, { copilot_thread_id: copilot_thread.id }).generate_response('Hello')
|
||||
end.to change(CopilotMessage, :count).by(1)
|
||||
@@ -276,10 +182,6 @@ RSpec.describe Captain::Copilot::ChatService do
|
||||
|
||||
context 'when copilot_thread is not present' do
|
||||
it 'does not create a copilot message' do
|
||||
allow(mock_openai_client).to receive(:chat).and_return({
|
||||
choices: [{ message: { content: '{ "content": "Hey" }' } }]
|
||||
}.with_indifferent_access)
|
||||
|
||||
expect do
|
||||
described_class.new(assistant, {}).generate_response('Hello')
|
||||
end.not_to(change(CopilotMessage, :count))
|
||||
|
||||
Reference in New Issue
Block a user