fix: log only on system api key failures (#13968)

Removes sentry flooding of unnecessary rubyllm logs of wrong API key.
Logs only system api key error since it would be P0.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Aakash Bakhle
2026-04-09 18:04:52 +05:30
committed by GitHub
parent f1da7b8afa
commit f13f3ba446
8 changed files with 109 additions and 20 deletions

View File

@@ -141,15 +141,15 @@ RSpec.describe Captain::ConversationCompletionService do
service.perform
end
it 'falls back to the account hook key when no system key exists' do
it 'does not fall back to the account hook key when no system key exists' do
InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_API_KEY').update!(value: nil)
expect(Llm::Config).to receive(:with_api_key).with('customer-own-key', api_base: anything).and_yield(mock_context)
allow(mock_chat).to receive(:ask).and_return(
instance_double(RubyLLM::Message, content: { 'complete' => true, 'reason' => 'Done' }, input_tokens: 10, output_tokens: 5)
)
expect(Llm::Config).not_to receive(:with_api_key)
service.perform
result = service.perform
expect(result[:complete]).to be false
expect(result[:reason]).to eq(I18n.t('captain.api_key_missing'))
end
end

View File

@@ -258,6 +258,18 @@ RSpec.describe Captain::BaseTaskService do
expect(result[:error]).to eq('API Error')
expect(result[:request_messages]).to eq(messages)
end
it 'does not track exceptions for account hook failures' do
create(:integrations_hook, :openai, account: account, settings: { 'api_key' => 'hook-key' })
expect(Llm::Config).to receive(:with_api_key).with('hook-key', api_base: anything).and_raise(error)
expect(ChatwootExceptionTracker).not_to receive(:new)
result = service.send(:make_api_call, model: model, messages: messages)
expect(result[:error]).to eq('API Error')
expect(result[:request_messages]).to eq(messages)
end
end
describe '#api_key' do
@@ -276,6 +288,16 @@ RSpec.describe Captain::BaseTaskService do
expect(service.send(:api_key)).to eq('test-key')
end
end
context 'when no API key is configured' do
before do
InstallationConfig.find_by(name: 'CAPTAIN_OPEN_AI_API_KEY')&.destroy
end
it 'returns nil' do
expect(service.send(:api_key)).to be_nil
end
end
end
describe '#prompt_from_file' do

View File

@@ -0,0 +1,28 @@
require 'rails_helper'
RSpec.describe Integrations::LlmBaseService do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:conversation) { create(:conversation, account: account, inbox: inbox) }
let(:hook) { create(:integrations_hook, :openai, account: account, settings: { 'api_key' => 'hook-key' }) }
let(:event) { { 'name' => 'summarize', 'data' => { 'conversation_display_id' => conversation.display_id } } }
let(:service) { described_class.new(hook: hook, event: event) }
let(:error) { StandardError.new('API Error') }
let(:body) { { model: 'gpt-4', messages: [{ role: 'user', content: 'Hello' }] }.to_json }
describe '#make_api_call' do
before do
allow(service).to receive(:instrument_llm_call).and_yield
allow(Llm::Config).to receive(:with_api_key).and_raise(error)
end
it 'does not track exceptions for hook key failures' do
expect(ChatwootExceptionTracker).not_to receive(:new)
result = service.send(:make_api_call, body)
expect(result[:error]).to eq('API Error')
expect(result[:request_messages]).to eq([{ 'role' => 'user', 'content' => 'Hello' }])
end
end
end