From 34e929c7056240b1fc64ec006634f897f4f0215e Mon Sep 17 00:00:00 2001 From: Pranav Date: Sun, 11 May 2025 00:37:59 -0700 Subject: [PATCH] Move copilot to websockets --- app/channels/copilot_channel.rb | 35 ++++++ .../dashboard/api/inbox/conversation.js | 4 - .../components-next/copilot/Copilot.vue | 22 ++-- .../components/copilot/CopilotContainer.vue | 91 ++++++++++------ .../copilot/CopilotThinkingBlock.vue | 24 +++++ .../helpers/CopilotActionCableConnector.js | 49 +++++++++ config/routes.rb | 1 - .../v1/accounts/captain/copilot_controller.rb | 36 ------- enterprise/app/helpers/captain/chat_helper.rb | 101 ++++++++---------- .../captain/process_copilot_message_job.rb | 50 +++++++++ .../services/captain/copilot/chat_service.rb | 36 ++++--- .../captain/llm/assistant_chat_service.rb | 4 +- .../captain/llm/system_prompts_service.rb | 5 +- .../services/captain/tool_registry_service.rb | 32 ++++++ .../services/captain/tools/base_service.rb | 34 ++++++ .../tools/search_documentation_service.rb | 49 +++++++++ 16 files changed, 422 insertions(+), 151 deletions(-) create mode 100644 app/channels/copilot_channel.rb create mode 100644 app/javascript/dashboard/components/copilot/CopilotThinkingBlock.vue create mode 100644 app/javascript/dashboard/helpers/CopilotActionCableConnector.js delete mode 100644 enterprise/app/controllers/api/v1/accounts/captain/copilot_controller.rb create mode 100644 enterprise/app/jobs/captain/process_copilot_message_job.rb create mode 100644 enterprise/app/services/captain/tool_registry_service.rb create mode 100644 enterprise/app/services/captain/tools/base_service.rb create mode 100644 enterprise/app/services/captain/tools/search_documentation_service.rb diff --git a/app/channels/copilot_channel.rb b/app/channels/copilot_channel.rb new file mode 100644 index 000000000..2fde1c32f --- /dev/null +++ b/app/channels/copilot_channel.rb @@ -0,0 +1,35 @@ +class CopilotChannel < ApplicationCable::Channel + def subscribed + current_user + current_account + ensure_stream + end + + def message(data) + return if @current_account.blank? + + Captain::ProcessCopilotMessageJob.perform_later( + assistant_id: data['assistant_id'], + message: data['message'], + options: { + user_id: @current_user.id, + conversation_id: data['conversation_id'], + previous_history: data['previous_history'] + } + ) + end + + def ensure_stream + stream_from "copilot_#{@current_account.id}_#{@current_user.id}" + end + + def current_user + @current_user ||= User.find(params[:user_id]) + end + + def current_account + return if current_user.blank? + + @current_account ||= @current_user.accounts.find(params[:account_id]) + end +end diff --git a/app/javascript/dashboard/api/inbox/conversation.js b/app/javascript/dashboard/api/inbox/conversation.js index 39546096f..1a87e6d96 100644 --- a/app/javascript/dashboard/api/inbox/conversation.js +++ b/app/javascript/dashboard/api/inbox/conversation.js @@ -134,10 +134,6 @@ class ConversationApi extends ApiClient { return axios.get(`${this.url}/${conversationId}/attachments`); } - requestCopilot(conversationId, body) { - return axios.post(`${this.url}/${conversationId}/copilot`, body); - } - getInboxAssistant(conversationId) { return axios.get(`${this.url}/${conversationId}/inbox_assistant`); } diff --git a/app/javascript/dashboard/components-next/copilot/Copilot.vue b/app/javascript/dashboard/components-next/copilot/Copilot.vue index cc445257a..903450bdd 100644 --- a/app/javascript/dashboard/components-next/copilot/Copilot.vue +++ b/app/javascript/dashboard/components-next/copilot/Copilot.vue @@ -8,6 +8,7 @@ import CopilotInput from './CopilotInput.vue'; import CopilotLoader from './CopilotLoader.vue'; import CopilotAgentMessage from './CopilotAgentMessage.vue'; import CopilotAssistantMessage from './CopilotAssistantMessage.vue'; +import CopilotThinkingBlock from 'dashboard/components/copilot/CopilotThinkingBlock.vue'; import ToggleCopilotAssistant from './ToggleCopilotAssistant.vue'; import Icon from '../icon/Icon.vue'; import Button from '../button/Button.vue'; @@ -43,8 +44,6 @@ const emit = defineEmits(['sendMessage', 'reset', 'setAssistant', 'close']); const { t } = useI18n(); -const COPILOT_USER_ROLES = ['assistant', 'system']; - const sendMessage = message => { emit('sendMessage', message); useTrack(COPILOT_EVENTS.SEND_MESSAGE); @@ -113,11 +112,11 @@ watch( -
-
+
+
diff --git a/app/javascript/dashboard/components/copilot/CopilotContainer.vue b/app/javascript/dashboard/components/copilot/CopilotContainer.vue index 1fed0e861..fd10489dc 100644 --- a/app/javascript/dashboard/components/copilot/CopilotContainer.vue +++ b/app/javascript/dashboard/components/copilot/CopilotContainer.vue @@ -1,8 +1,8 @@