feat: Add support for persistent copilot threads and messages (#11489)
The agents can see the previous conversations with the copilot if needed with this change. We would have to cleanup the data after a while. For now, that is not considered. This PR adds: - A new model for copilot_threads (intentionally named thread instead of conversation to avoid confusion), copilot_messages - Add the controller to fetch previous threads and messages.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Captain::CopilotMessagesController', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:user) { create(:user, account: account, role: :administrator) }
|
||||
let(:copilot_thread) { create(:captain_copilot_thread, account: account, user: user) }
|
||||
let!(:copilot_message) { create(:captain_copilot_message, copilot_thread: copilot_thread, user: user, account: account) }
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/captain/copilot_threads/{thread.uuid}/copilot_messages' do
|
||||
context 'when it is an authenticated user' do
|
||||
it 'returns all messages' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/copilot_threads/#{copilot_thread.uuid}/copilot_messages",
|
||||
headers: user.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
json_response = response.parsed_body
|
||||
expect(json_response['payload'].length).to eq(1)
|
||||
expect(json_response['payload'][0]['id']).to eq(copilot_message.id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when thread uuid is invalid' do
|
||||
it 'returns not found error' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/copilot_threads/invalid-uuid/copilot_messages",
|
||||
headers: user.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,50 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Captain::CopilotThreads', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
def json_response
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/captain/copilot_threads' do
|
||||
context 'when it is an un-authenticated user' do
|
||||
it 'does not fetch copilot threads' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/copilot_threads",
|
||||
as: :json
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an agent' do
|
||||
it 'fetches copilot threads for the current user' do
|
||||
# Create threads for the current agent
|
||||
create_list(:captain_copilot_thread, 3, account: account, user: agent)
|
||||
# Create threads for another user (should not be included)
|
||||
create_list(:captain_copilot_thread, 2, account: account, user: admin)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/captain/copilot_threads",
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response[:payload].length).to eq(3)
|
||||
|
||||
expect(json_response[:payload].map { |thread| thread[:user][:id] }.uniq).to eq([agent.id])
|
||||
end
|
||||
|
||||
it 'returns threads in descending order of creation' do
|
||||
threads = create_list(:captain_copilot_thread, 3, account: account, user: agent)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/captain/copilot_threads",
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response[:payload].pluck(:id)).to eq(threads.reverse.pluck(:id))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
9
spec/factories/captain/copilot_message.rb
Normal file
9
spec/factories/captain/copilot_message.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
FactoryBot.define do
|
||||
factory :captain_copilot_message, class: 'CopilotMessage' do
|
||||
account
|
||||
user
|
||||
copilot_thread { association :captain_copilot_thread }
|
||||
message { { content: 'This is a test message' } }
|
||||
message_type { 'user' }
|
||||
end
|
||||
end
|
||||
8
spec/factories/captain/copilot_thread.rb
Normal file
8
spec/factories/captain/copilot_thread.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
FactoryBot.define do
|
||||
factory :captain_copilot_thread, class: 'CopilotThread' do
|
||||
account
|
||||
user
|
||||
title { Faker::Lorem.sentence }
|
||||
uuid { SecureRandom.uuid }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user