feat: use new helpers
This commit is contained in:
@@ -47,8 +47,7 @@ class Captain::Conversation::ResponseBuilderJob < ApplicationJob
|
||||
return process_action('handoff') if handoff_requested?
|
||||
|
||||
create_messages
|
||||
Rails.logger.info("[CAPTAIN][ResponseBuilderJob] Incrementing response usage for #{account.id}")
|
||||
account.increment_response_usage
|
||||
account.increment_response_usage_for_model(account.captain_assistant_model)
|
||||
end
|
||||
|
||||
def collect_previous_messages
|
||||
|
||||
@@ -4,10 +4,10 @@ class Captain::Copilot::ChatService < Llm::BaseAiService
|
||||
attr_reader :assistant, :account, :user, :copilot_thread, :previous_history, :messages
|
||||
|
||||
def initialize(assistant, config)
|
||||
super()
|
||||
|
||||
@assistant = assistant
|
||||
@account = assistant.account
|
||||
super(account: @account)
|
||||
|
||||
@user = nil
|
||||
@copilot_thread = nil
|
||||
@previous_history = []
|
||||
@@ -24,16 +24,17 @@ class Captain::Copilot::ChatService < Llm::BaseAiService
|
||||
response = request_chat_completion
|
||||
|
||||
Rails.logger.debug { "#{self.class.name} Assistant: #{@assistant.id}, Received response #{response}" }
|
||||
Rails.logger.info(
|
||||
"#{self.class.name} Assistant: #{@assistant.id}, Incrementing response usage for account #{@account.id}"
|
||||
)
|
||||
@account.increment_response_usage
|
||||
increment_usage
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def setup_model
|
||||
@model = @account.captain_copilot_model
|
||||
end
|
||||
|
||||
def setup_user(config)
|
||||
@user = @account.users.find_by(id: config[:user_id]) if config[:user_id].present?
|
||||
end
|
||||
|
||||
@@ -2,9 +2,10 @@ class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
||||
include Captain::ChatHelper
|
||||
|
||||
def initialize(assistant: nil, conversation_id: nil)
|
||||
super()
|
||||
|
||||
@assistant = assistant
|
||||
@account = @assistant&.account
|
||||
super(account: @account)
|
||||
|
||||
@conversation_id = conversation_id
|
||||
|
||||
@messages = [system_message]
|
||||
@@ -27,6 +28,10 @@ class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
||||
|
||||
private
|
||||
|
||||
def setup_model
|
||||
@model = @account&.captain_assistant_model || DEFAULT_MODEL
|
||||
end
|
||||
|
||||
def build_tools
|
||||
[Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
|
||||
end
|
||||
|
||||
@@ -8,7 +8,8 @@ class Llm::BaseAiService
|
||||
|
||||
attr_reader :model, :temperature
|
||||
|
||||
def initialize
|
||||
def initialize(account: nil)
|
||||
@account = account
|
||||
Llm::Config.initialize!
|
||||
setup_model
|
||||
setup_temperature
|
||||
@@ -18,6 +19,12 @@ class Llm::BaseAiService
|
||||
RubyLLM.chat(model: model).with_temperature(temperature)
|
||||
end
|
||||
|
||||
def increment_usage
|
||||
return unless @account
|
||||
|
||||
@account.increment_response_usage_for_model(@model)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def setup_model
|
||||
|
||||
@@ -28,8 +28,6 @@ module Enterprise::Captain::BaseTaskService
|
||||
end
|
||||
|
||||
def increment_usage
|
||||
credits = account.using_openai_hook_key? ? 1 : Llm::Models.credit_multiplier_for(configured_model)
|
||||
Rails.logger.info("[CAPTAIN][#{self.class.name}] Incrementing response usage for account #{account.id} by #{credits} credits")
|
||||
account.increment_response_usage(credits: credits)
|
||||
account.increment_response_usage_for_model(configured_model)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Captain::Conversation::ResponseBuilderJob, type: :job do
|
||||
let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }) }
|
||||
# Use gpt-5-mini (credit_multiplier: 1) to keep credit consumption predictable in tests
|
||||
let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }, captain_models: { 'assistant' => 'gpt-5-mini' }) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:assistant) { create(:captain_assistant, account: account) }
|
||||
let(:captain_inbox_association) { create(:captain_inbox, captain_assistant: assistant, inbox: inbox) }
|
||||
|
||||
@@ -254,24 +254,6 @@ RSpec.describe Captain::BaseTaskService do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#api_key' do
|
||||
context 'when openai hook is configured' do
|
||||
let(:hook) { create(:integrations_hook, account: account, app_id: 'openai', status: 'enabled', settings: { 'api_key' => 'hook-key' }) }
|
||||
|
||||
before { hook }
|
||||
|
||||
it 'uses api key from hook' do
|
||||
expect(service.send(:api_key)).to eq('hook-key')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when openai hook is not configured' do
|
||||
it 'uses system api key' do
|
||||
expect(service.send(:api_key)).to eq('test-key')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#prompt_from_file' do
|
||||
it 'reads prompt from file' do
|
||||
allow(Rails.root).to receive(:join).and_return(instance_double(Pathname, read: 'Test prompt content'))
|
||||
|
||||
Reference in New Issue
Block a user