diff --git a/config/routes.rb b/config/routes.rb index c623ff053..044347f47 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -61,6 +61,7 @@ Rails.application.routes.draw do resources :documents, only: [:index, :show, :create, :destroy] resources :assistant_responses resources :bulk_actions, only: [:create] + resources :copilot, only: [:create] end resources :agent_bots, only: [:index, :create, :show, :update, :destroy] do delete :avatar, on: :member @@ -124,7 +125,6 @@ Rails.application.routes.draw do post :unread post :custom_attributes get :attachments - post :copilot get :inbox_assistant end end diff --git a/enterprise/app/controllers/api/v1/accounts/captain/copilot_controller.rb b/enterprise/app/controllers/api/v1/accounts/captain/copilot_controller.rb new file mode 100644 index 000000000..a2759f122 --- /dev/null +++ b/enterprise/app/controllers/api/v1/accounts/captain/copilot_controller.rb @@ -0,0 +1,36 @@ +class Api::V1::Accounts::Captain::CopilotController < Api::V1::Accounts::BaseController + before_action :fetch_conversation, only: [:create] + + def create + # First try to get the user's preferred assistant from UI settings or from the request + assistant_id = copilot_params[:assistant_id] || current_user.ui_settings&.dig('preferred_captain_assistant_id') + + # Find the assistant either by ID or from inbox + assistant = if assistant_id.present? + Captain::Assistant.find_by(id: assistant_id, account_id: Current.account.id) + else + @conversation.inbox.captain_assistant + end + + return render json: { message: I18n.t('captain.copilot_error') } unless assistant + + response = Captain::Copilot::ChatService.new( + assistant, + previous_messages: copilot_params[:previous_messages], + conversation_history: @conversation.to_llm_text, + language: @conversation.account.locale_english_name + ).generate_response(copilot_params[:message]) + + render json: { message: response['response'] } + end + + private + + def copilot_params + params.permit(:message, :assistant_id, previous_messages: []) + end + + def fetch_conversation + @conversation = Current.account.conversations.find(params[:conversation_id]) + end +end diff --git a/enterprise/app/controllers/enterprise/api/v1/accounts/conversations_controller.rb b/enterprise/app/controllers/enterprise/api/v1/accounts/conversations_controller.rb index c382206b6..c668ebe18 100644 --- a/enterprise/app/controllers/enterprise/api/v1/accounts/conversations_controller.rb +++ b/enterprise/app/controllers/enterprise/api/v1/accounts/conversations_controller.rb @@ -4,29 +4,6 @@ module Enterprise::Api::V1::Accounts::ConversationsController before_action :set_assistant, only: [:copilot] end - def copilot - # First try to get the user's preferred assistant from UI settings or from the request - assistant_id = copilot_params[:assistant_id] || current_user.ui_settings&.dig('preferred_captain_assistant_id') - - # Find the assistant either by ID or from inbox - assistant = if assistant_id.present? - Captain::Assistant.find_by(id: assistant_id, account_id: Current.account.id) - else - @conversation.inbox.captain_assistant - end - - return render json: { message: I18n.t('captain.copilot_error') } unless assistant - - response = Captain::Copilot::ChatService.new( - assistant, - previous_messages: copilot_params[:previous_messages], - conversation_history: @conversation.to_llm_text, - language: @conversation.account.locale_english_name - ).generate_response(copilot_params[:message]) - - render json: { message: response['response'] } - end - def inbox_assistant assistant = @conversation.inbox.captain_assistant