Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c85eff59eb | ||
|
|
1134871cbc | ||
|
|
749e4726ff | ||
|
|
57a571ea87 | ||
|
|
3a4249da11 | ||
|
|
dfbcfaa525 | ||
|
|
4a43b3611c | ||
|
|
95c2915723 | ||
|
|
960de3e545 |
@@ -173,6 +173,7 @@ gem 'pgvector'
|
||||
# Convert Website HTML to Markdown
|
||||
gem 'reverse_markdown'
|
||||
|
||||
gem 'iso-639'
|
||||
gem 'ruby-openai'
|
||||
|
||||
gem 'shopify_api'
|
||||
|
||||
@@ -378,6 +378,8 @@ GEM
|
||||
io-console (0.6.0)
|
||||
irb (1.7.2)
|
||||
reline (>= 0.3.6)
|
||||
iso-639 (0.3.8)
|
||||
csv
|
||||
jbuilder (2.11.5)
|
||||
actionview (>= 5.0.0)
|
||||
activesupport (>= 5.0.0)
|
||||
@@ -913,6 +915,7 @@ DEPENDENCIES
|
||||
hashie
|
||||
html2text
|
||||
image_processing
|
||||
iso-639
|
||||
jbuilder
|
||||
json_refs
|
||||
json_schemer
|
||||
|
||||
@@ -21,6 +21,7 @@ class Api::V1::Accounts::SearchController < Api::V1::Accounts::BaseController
|
||||
SearchService.new(
|
||||
current_user: Current.user,
|
||||
current_account: Current.account,
|
||||
current_account_user: Current.account_user,
|
||||
search_type: search_type,
|
||||
params: params
|
||||
).perform
|
||||
|
||||
@@ -13,6 +13,9 @@ const attachment = computed(() => {
|
||||
|
||||
<template>
|
||||
<BaseBubble class="bg-transparent" data-bubble-name="audio">
|
||||
<AudioChip :attachment="attachment" class="p-2 text-n-slate-12" />
|
||||
<AudioChip
|
||||
:attachment="attachment"
|
||||
class="p-2 text-n-slate-12 skip-context-menu"
|
||||
/>
|
||||
</BaseBubble>
|
||||
</template>
|
||||
|
||||
@@ -56,6 +56,7 @@ const downloadAttachment = async () => {
|
||||
</div>
|
||||
<div v-else class="relative group rounded-lg overflow-hidden">
|
||||
<img
|
||||
class="skip-context-menu"
|
||||
:src="attachment.dataUrl"
|
||||
:width="attachment.width"
|
||||
:height="attachment.height"
|
||||
@@ -63,8 +64,9 @@ const downloadAttachment = async () => {
|
||||
@error="handleError"
|
||||
/>
|
||||
<div
|
||||
class="inset-0 p-2 absolute bg-gradient-to-tl from-n-slate-12/30 dark:from-n-slate-1/50 via-transparent to-transparent hidden group-hover:flex items-end justify-end gap-1.5"
|
||||
>
|
||||
class="inset-0 p-2 pointer-events-none absolute bg-gradient-to-tl from-n-slate-12/30 dark:from-n-slate-1/50 via-transparent to-transparent hidden group-hover:flex"
|
||||
/>
|
||||
<div class="absolute right-2 bottom-2 hidden group-hover:flex gap-2">
|
||||
<Button xs solid slate icon="i-lucide-expand" class="opacity-60" />
|
||||
<Button
|
||||
xs
|
||||
|
||||
@@ -41,13 +41,13 @@ const onVideoLoadError = () => {
|
||||
<div v-if="content" v-dompurify-html="formattedContent" class="mb-2" />
|
||||
<img
|
||||
v-if="!hasImgStoryError"
|
||||
class="rounded-lg max-w-80"
|
||||
class="rounded-lg max-w-80 skip-context-menu"
|
||||
:src="attachment.dataUrl"
|
||||
@error="onImageLoadError"
|
||||
/>
|
||||
<video
|
||||
v-else-if="!hasVideoStoryError"
|
||||
class="rounded-lg max-w-80"
|
||||
class="rounded-lg max-w-80 skip-context-menu"
|
||||
controls
|
||||
:src="attachment.dataUrl"
|
||||
@error="onVideoLoadError"
|
||||
|
||||
@@ -35,13 +35,13 @@ const isReel = computed(() => {
|
||||
<div class="relative group rounded-lg overflow-hidden">
|
||||
<div
|
||||
v-if="isReel"
|
||||
class="absolute p-2 flex items-start justify-end right-0"
|
||||
class="absolute p-2 flex items-start justify-end right-0 pointer-events-none"
|
||||
>
|
||||
<Icon icon="i-lucide-instagram" class="text-white shadow-lg" />
|
||||
</div>
|
||||
<video
|
||||
controls
|
||||
class="rounded-lg"
|
||||
class="rounded-lg skip-context-menu"
|
||||
:src="attachment.dataUrl"
|
||||
:class="{
|
||||
'max-w-48': isReel,
|
||||
|
||||
@@ -36,7 +36,7 @@ const handleError = () => {
|
||||
</div>
|
||||
<img
|
||||
v-else
|
||||
class="object-cover w-full h-full"
|
||||
class="object-cover w-full h-full skip-context-menu"
|
||||
:src="attachment.dataUrl"
|
||||
@error="handleError"
|
||||
/>
|
||||
|
||||
+174
-172
@@ -166,188 +166,190 @@ onMounted(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<woot-modal
|
||||
v-model:show="show"
|
||||
full-width
|
||||
:show-close-button="false"
|
||||
:on-close="onClose"
|
||||
>
|
||||
<div
|
||||
class="bg-n-background flex flex-col h-[inherit] w-[inherit] overflow-hidden select-none"
|
||||
@click="onClose"
|
||||
<Teleport to="body">
|
||||
<woot-modal
|
||||
v-model:show="show"
|
||||
full-width
|
||||
:show-close-button="false"
|
||||
:on-close="onClose"
|
||||
>
|
||||
<header
|
||||
class="z-10 flex items-center justify-between w-full h-16 px-6 py-2 bg-n-background border-b border-n-weak"
|
||||
@click.stop
|
||||
<div
|
||||
class="bg-n-background flex flex-col h-[inherit] w-[inherit] overflow-hidden select-none"
|
||||
@click="onClose"
|
||||
>
|
||||
<div
|
||||
v-if="senderDetails"
|
||||
class="flex items-center min-w-[15rem] shrink-0"
|
||||
<header
|
||||
class="z-10 flex items-center justify-between w-full h-16 px-6 py-2 bg-n-background border-b border-n-weak"
|
||||
@click.stop
|
||||
>
|
||||
<Thumbnail
|
||||
v-if="senderDetails.avatar"
|
||||
:username="senderDetails.name"
|
||||
:src="senderDetails.avatar"
|
||||
class="flex-shrink-0"
|
||||
/>
|
||||
<div class="flex flex-col ml-2 rtl:ml-0 rtl:mr-2 overflow-hidden">
|
||||
<h3 class="text-base leading-5 m-0 font-medium">
|
||||
<span
|
||||
class="overflow-hidden text-n-slate-12 whitespace-nowrap text-ellipsis"
|
||||
>
|
||||
{{ senderDetails.name }}
|
||||
</span>
|
||||
</h3>
|
||||
<span
|
||||
class="text-xs text-n-slate-11 whitespace-nowrap text-ellipsis"
|
||||
>
|
||||
{{ readableTime }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="flex-1 mx-2 px-2 truncate text-sm font-medium text-center text-n-slate-12"
|
||||
>
|
||||
<span v-dompurify-html="fileNameFromDataUrl" class="truncate" />
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2 ml-2 shrink-0">
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-zoom-in"
|
||||
slate
|
||||
ghost
|
||||
@click="onZoom(0.1)"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-zoom-out"
|
||||
slate
|
||||
ghost
|
||||
@click="onZoom(-0.1)"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-rotate-ccw"
|
||||
slate
|
||||
ghost
|
||||
@click="onRotate('counter-clockwise')"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-rotate-cw"
|
||||
slate
|
||||
ghost
|
||||
@click="onRotate('clockwise')"
|
||||
/>
|
||||
<NextButton
|
||||
icon="i-lucide-download"
|
||||
slate
|
||||
ghost
|
||||
:is-loading="isDownloading"
|
||||
:disabled="isDownloading"
|
||||
@click="onClickDownload"
|
||||
/>
|
||||
<NextButton icon="i-lucide-x" slate ghost @click="onClose" />
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="flex items-stretch flex-1 h-full overflow-hidden">
|
||||
<div class="flex items-center justify-center w-16 shrink-0">
|
||||
<NextButton
|
||||
v-if="hasMoreThanOneAttachment"
|
||||
icon="i-lucide-chevron-left"
|
||||
class="z-10"
|
||||
blue
|
||||
faded
|
||||
lg
|
||||
:disabled="activeImageIndex === 0"
|
||||
@click.stop="
|
||||
onClickChangeAttachment(
|
||||
allAttachments[activeImageIndex - 1],
|
||||
activeImageIndex - 1
|
||||
)
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 flex items-center justify-center overflow-hidden">
|
||||
<div
|
||||
v-if="isImage"
|
||||
:style="imageWrapperStyle"
|
||||
class="flex items-center justify-center origin-center"
|
||||
:class="{
|
||||
// Adjust dimensions when rotated 90/270 degrees to maintain visibility
|
||||
// and prevent image from overflowing container in different aspect ratios
|
||||
'w-[calc(100dvh-8rem)] h-[calc(100dvw-7rem)]':
|
||||
activeImageRotation % 180 !== 0,
|
||||
'size-full': activeImageRotation % 180 === 0,
|
||||
}"
|
||||
v-if="senderDetails"
|
||||
class="flex items-center min-w-[15rem] shrink-0"
|
||||
>
|
||||
<img
|
||||
ref="imageRef"
|
||||
:key="activeAttachment.message_id"
|
||||
:src="activeAttachment.data_url"
|
||||
:style="imageStyle"
|
||||
class="max-h-full max-w-full object-contain duration-100 ease-in-out transform select-none"
|
||||
@click.stop
|
||||
@dblclick.stop="onDoubleClickZoomImage"
|
||||
@wheel.prevent.stop="onWheelImageZoom"
|
||||
@mousemove="onMouseMove"
|
||||
@mouseleave="onMouseLeave"
|
||||
<Thumbnail
|
||||
v-if="senderDetails.avatar"
|
||||
:username="senderDetails.name"
|
||||
:src="senderDetails.avatar"
|
||||
class="flex-shrink-0"
|
||||
/>
|
||||
<div class="flex flex-col ml-2 rtl:ml-0 rtl:mr-2 overflow-hidden">
|
||||
<h3 class="text-base leading-5 m-0 font-medium">
|
||||
<span
|
||||
class="overflow-hidden text-n-slate-12 whitespace-nowrap text-ellipsis"
|
||||
>
|
||||
{{ senderDetails.name }}
|
||||
</span>
|
||||
</h3>
|
||||
<span
|
||||
class="text-xs text-n-slate-11 whitespace-nowrap text-ellipsis"
|
||||
>
|
||||
{{ readableTime }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="flex-1 mx-2 px-2 truncate text-sm font-medium text-center text-n-slate-12"
|
||||
>
|
||||
<span v-dompurify-html="fileNameFromDataUrl" class="truncate" />
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-2 ml-2 shrink-0">
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-zoom-in"
|
||||
slate
|
||||
ghost
|
||||
@click="onZoom(0.1)"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-zoom-out"
|
||||
slate
|
||||
ghost
|
||||
@click="onZoom(-0.1)"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-rotate-ccw"
|
||||
slate
|
||||
ghost
|
||||
@click="onRotate('counter-clockwise')"
|
||||
/>
|
||||
<NextButton
|
||||
v-if="isImage"
|
||||
icon="i-lucide-rotate-cw"
|
||||
slate
|
||||
ghost
|
||||
@click="onRotate('clockwise')"
|
||||
/>
|
||||
<NextButton
|
||||
icon="i-lucide-download"
|
||||
slate
|
||||
ghost
|
||||
:is-loading="isDownloading"
|
||||
:disabled="isDownloading"
|
||||
@click="onClickDownload"
|
||||
/>
|
||||
<NextButton icon="i-lucide-x" slate ghost @click="onClose" />
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main class="flex items-stretch flex-1 h-full overflow-hidden">
|
||||
<div class="flex items-center justify-center w-16 shrink-0">
|
||||
<NextButton
|
||||
v-if="hasMoreThanOneAttachment"
|
||||
icon="i-lucide-chevron-left"
|
||||
class="z-10"
|
||||
blue
|
||||
faded
|
||||
lg
|
||||
:disabled="activeImageIndex === 0"
|
||||
@click.stop="
|
||||
onClickChangeAttachment(
|
||||
allAttachments[activeImageIndex - 1],
|
||||
activeImageIndex - 1
|
||||
)
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<video
|
||||
v-if="isVideo"
|
||||
:key="activeAttachment.message_id"
|
||||
:src="activeAttachment.data_url"
|
||||
controls
|
||||
playsInline
|
||||
class="max-h-full max-w-full object-contain"
|
||||
@click.stop
|
||||
/>
|
||||
<div class="flex-1 flex items-center justify-center overflow-hidden">
|
||||
<div
|
||||
v-if="isImage"
|
||||
:style="imageWrapperStyle"
|
||||
class="flex items-center justify-center origin-center"
|
||||
:class="{
|
||||
// Adjust dimensions when rotated 90/270 degrees to maintain visibility
|
||||
// and prevent image from overflowing container in different aspect ratios
|
||||
'w-[calc(100dvh-8rem)] h-[calc(100dvw-7rem)]':
|
||||
activeImageRotation % 180 !== 0,
|
||||
'size-full': activeImageRotation % 180 === 0,
|
||||
}"
|
||||
>
|
||||
<img
|
||||
ref="imageRef"
|
||||
:key="activeAttachment.message_id"
|
||||
:src="activeAttachment.data_url"
|
||||
:style="imageStyle"
|
||||
class="max-h-full max-w-full object-contain duration-100 ease-in-out transform select-none"
|
||||
@click.stop
|
||||
@dblclick.stop="onDoubleClickZoomImage"
|
||||
@wheel.prevent.stop="onWheelImageZoom"
|
||||
@mousemove="onMouseMove"
|
||||
@mouseleave="onMouseLeave"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<audio
|
||||
v-if="isAudio"
|
||||
:key="activeAttachment.message_id"
|
||||
controls
|
||||
class="w-full max-w-md"
|
||||
@click.stop
|
||||
>
|
||||
<source :src="`${activeAttachment.data_url}?t=${Date.now()}`" />
|
||||
</audio>
|
||||
</div>
|
||||
<video
|
||||
v-if="isVideo"
|
||||
:key="activeAttachment.message_id"
|
||||
:src="activeAttachment.data_url"
|
||||
controls
|
||||
playsInline
|
||||
class="max-h-full max-w-full object-contain"
|
||||
@click.stop
|
||||
/>
|
||||
|
||||
<div class="flex items-center justify-center w-16 shrink-0">
|
||||
<NextButton
|
||||
v-if="hasMoreThanOneAttachment"
|
||||
icon="i-lucide-chevron-right"
|
||||
class="z-10"
|
||||
blue
|
||||
faded
|
||||
lg
|
||||
:disabled="activeImageIndex === allAttachments.length - 1"
|
||||
@click.stop="
|
||||
onClickChangeAttachment(
|
||||
allAttachments[activeImageIndex + 1],
|
||||
activeImageIndex + 1
|
||||
)
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
<audio
|
||||
v-if="isAudio"
|
||||
:key="activeAttachment.message_id"
|
||||
controls
|
||||
class="w-full max-w-md"
|
||||
@click.stop
|
||||
>
|
||||
<source :src="`${activeAttachment.data_url}?t=${Date.now()}`" />
|
||||
</audio>
|
||||
</div>
|
||||
|
||||
<footer
|
||||
class="z-10 flex items-center justify-center h-12 border-t border-n-weak"
|
||||
>
|
||||
<div
|
||||
class="rounded-md flex items-center justify-center px-3 py-1 bg-n-slate-3 text-n-slate-12 text-sm font-medium"
|
||||
<div class="flex items-center justify-center w-16 shrink-0">
|
||||
<NextButton
|
||||
v-if="hasMoreThanOneAttachment"
|
||||
icon="i-lucide-chevron-right"
|
||||
class="z-10"
|
||||
blue
|
||||
faded
|
||||
lg
|
||||
:disabled="activeImageIndex === allAttachments.length - 1"
|
||||
@click.stop="
|
||||
onClickChangeAttachment(
|
||||
allAttachments[activeImageIndex + 1],
|
||||
activeImageIndex + 1
|
||||
)
|
||||
"
|
||||
/>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer
|
||||
class="z-10 flex items-center justify-center h-12 border-t border-n-weak"
|
||||
>
|
||||
{{ `${activeImageIndex + 1} / ${allAttachments.length}` }}
|
||||
</div>
|
||||
</footer>
|
||||
</div>
|
||||
</woot-modal>
|
||||
<div
|
||||
class="rounded-md flex items-center justify-center px-3 py-1 bg-n-slate-3 text-n-slate-12 text-sm font-medium"
|
||||
>
|
||||
{{ `${activeImageIndex + 1} / ${allAttachments.length}` }}
|
||||
</div>
|
||||
</footer>
|
||||
</div>
|
||||
</woot-modal>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
@@ -128,6 +128,14 @@ class Account < ApplicationRecord
|
||||
}
|
||||
end
|
||||
|
||||
def locale_english_name
|
||||
# the locale can also be something like pt_BR, en_US, fr_FR, etc.
|
||||
# the format is `<locale_code>_<country_code>`
|
||||
# we need to extract the language code from the locale
|
||||
account_locale = locale&.split('_')&.first
|
||||
ISO_639.find(account_locale)&.english_name&.downcase || 'english'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notify_creation
|
||||
|
||||
@@ -48,4 +48,8 @@ module AssignmentHandler
|
||||
create_assignee_change_activity(user_name)
|
||||
end
|
||||
end
|
||||
|
||||
def self_assign?(assignee_id)
|
||||
assignee_id.present? && Current.user&.id == assignee_id
|
||||
end
|
||||
end
|
||||
|
||||
@@ -125,6 +125,10 @@ class Conversation < ApplicationRecord
|
||||
last_message_in_messaging_window?(messaging_window)
|
||||
end
|
||||
|
||||
def language
|
||||
additional_attributes&.dig('conversation_language')
|
||||
end
|
||||
|
||||
def last_activity_at
|
||||
self[:last_activity_at] || created_at
|
||||
end
|
||||
@@ -257,10 +261,6 @@ class Conversation < ApplicationRecord
|
||||
)
|
||||
end
|
||||
|
||||
def self_assign?(assignee_id)
|
||||
assignee_id.present? && Current.user&.id == assignee_id
|
||||
end
|
||||
|
||||
def load_attributes_created_by_db_triggers
|
||||
# Display id is set via a trigger in the database
|
||||
# So we need to specifically fetch it after the record is created
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class SearchService
|
||||
pattr_initialize [:current_user!, :current_account!, :params!, :search_type!]
|
||||
pattr_initialize [:current_user!, :current_account!, :current_account_user!, :params!, :search_type!]
|
||||
|
||||
def perform
|
||||
case search_type
|
||||
@@ -25,13 +25,16 @@ class SearchService
|
||||
end
|
||||
|
||||
def filter_conversations
|
||||
@conversations = current_account.conversations.where(inbox_id: accessable_inbox_ids)
|
||||
.joins('INNER JOIN contacts ON conversations.contact_id = contacts.id')
|
||||
.where("cast(conversations.display_id as text) ILIKE :search OR contacts.name ILIKE :search OR contacts.email
|
||||
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{search_query}%")
|
||||
.order('conversations.created_at DESC')
|
||||
.page(params[:page])
|
||||
.per(15)
|
||||
query = current_account.conversations
|
||||
# admins can see all conversations anyway, so no need to add the heavy IN clause
|
||||
query = query.where(inbox_id: accessable_inbox_ids) unless @current_account_user.administrator?
|
||||
|
||||
@conversations = query.joins('INNER JOIN contacts ON conversations.contact_id = contacts.id')
|
||||
.where("cast(conversations.display_id as text) ILIKE :search OR contacts.name ILIKE :search OR contacts.email
|
||||
ILIKE :search OR contacts.phone_number ILIKE :search OR contacts.identifier ILIKE :search", search: "%#{search_query}%")
|
||||
.order('conversations.created_at DESC')
|
||||
.page(params[:page])
|
||||
.per(15)
|
||||
end
|
||||
|
||||
def filter_messages
|
||||
@@ -47,16 +50,7 @@ class SearchService
|
||||
|
||||
if search_query.present?
|
||||
# Use the @@ operator with to_tsquery for better GIN index utilization
|
||||
# Convert search query to tsquery format with prefix matching
|
||||
|
||||
# Use this if we wanna match splitting the words
|
||||
# split_query = search_query.split.map { |term| "#{term} | #{term}:*" }.join(' & ')
|
||||
|
||||
# This will do entire sentence matching using phrase distance operator
|
||||
tsquery = search_query.split.join(' <-> ')
|
||||
|
||||
# Apply the text search using the GIN index
|
||||
base_query.where('content @@ to_tsquery(?)', tsquery)
|
||||
base_query.where('content @@ to_tsquery(?)', search_query)
|
||||
.reorder('created_at DESC')
|
||||
.page(params[:page])
|
||||
.per(15)
|
||||
@@ -76,12 +70,14 @@ class SearchService
|
||||
end
|
||||
|
||||
def message_base_query
|
||||
current_account.messages.where(inbox_id: accessable_inbox_ids)
|
||||
.where('created_at >= ?', 3.months.ago)
|
||||
query = current_account.messages
|
||||
# admins can see all conversations anyway, so no need to add the heavy IN clause
|
||||
query = query.where(inbox_id: accessable_inbox_ids) unless @current_account_user.administrator?
|
||||
query.where('created_at >= ?', 3.months.ago)
|
||||
end
|
||||
|
||||
def use_gin_search
|
||||
current_account.feature_enabled?('search_with_gin')
|
||||
search_query.split.size == 1
|
||||
end
|
||||
|
||||
def filter_contacts
|
||||
|
||||
@@ -20,7 +20,8 @@ module Enterprise::Api::V1::Accounts::ConversationsController
|
||||
response = Captain::Copilot::ChatService.new(
|
||||
assistant,
|
||||
previous_messages: copilot_params[:previous_messages],
|
||||
conversation_history: @conversation.to_llm_text
|
||||
conversation_history: @conversation.to_llm_text,
|
||||
language: @conversation.account.locale_english_name
|
||||
).generate_response(copilot_params[:message])
|
||||
|
||||
render json: { message: response['response'] }
|
||||
|
||||
@@ -9,6 +9,7 @@ class Captain::Copilot::ChatService < Llm::BaseOpenAiService
|
||||
@assistant = assistant
|
||||
@conversation_history = config[:conversation_history]
|
||||
@previous_messages = config[:previous_messages] || []
|
||||
@language = config[:language] || 'english'
|
||||
@messages = [system_message, conversation_history_context] + @previous_messages
|
||||
@response = ''
|
||||
end
|
||||
@@ -27,7 +28,7 @@ class Captain::Copilot::ChatService < Llm::BaseOpenAiService
|
||||
def system_message
|
||||
{
|
||||
role: 'system',
|
||||
content: Captain::Llm::SystemPromptsService.copilot_response_generator(@assistant.config['product_name'])
|
||||
content: Captain::Llm::SystemPromptsService.copilot_response_generator(@assistant.config['product_name'], @language)
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -26,7 +26,9 @@ class Captain::Llm::ContactNotesService < Llm::BaseOpenAiService
|
||||
end
|
||||
|
||||
def chat_parameters
|
||||
prompt = Captain::Llm::SystemPromptsService.notes_generator
|
||||
account_language = @conversation.account.locale_english_name
|
||||
prompt = Captain::Llm::SystemPromptsService.notes_generator(account_language)
|
||||
|
||||
{
|
||||
model: @model,
|
||||
response_format: { type: 'json_object' },
|
||||
|
||||
@@ -89,7 +89,9 @@ class Captain::Llm::ConversationFaqService < Llm::BaseOpenAiService
|
||||
end
|
||||
|
||||
def chat_parameters
|
||||
prompt = Captain::Llm::SystemPromptsService.conversation_faq_generator
|
||||
account_language = @conversation.account.locale_english_name
|
||||
prompt = Captain::Llm::SystemPromptsService.conversation_faq_generator(account_language)
|
||||
|
||||
{
|
||||
model: @model,
|
||||
response_format: { type: 'json_object' },
|
||||
|
||||
@@ -56,7 +56,7 @@ class Captain::Llm::SystemPromptsService
|
||||
SYSTEM_PROMPT_MESSAGE
|
||||
end
|
||||
|
||||
def copilot_response_generator(product_name)
|
||||
def copilot_response_generator(product_name, language)
|
||||
<<~SYSTEM_PROMPT_MESSAGE
|
||||
[Identity]
|
||||
You are Captain, a helpful and friendly copilot assistant for support agents using the product #{product_name}. Your primary role is to assist support agents by retrieving information, compiling accurate responses, and guiding them through customer interactions.
|
||||
@@ -67,6 +67,7 @@ class Captain::Llm::SystemPromptsService
|
||||
|
||||
[Response Guidelines]
|
||||
- Use natural, polite, and conversational language that is clear and easy to follow. Keep sentences short and use simple words.
|
||||
- Reply in the language the agent is using, if you're not able to detect the language, reply in #{language}.
|
||||
- Provide brief and relevant responses—typically one or two sentences unless a more detailed explanation is necessary.
|
||||
- Do not use your own training data or assumptions to answer queries. Base responses strictly on the provided information.
|
||||
- If the query is unclear, ask concise clarifying questions instead of making assumptions.
|
||||
|
||||
@@ -2,17 +2,71 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe Captain::Copilot::ChatService do
|
||||
let(:account) { create(:account, custom_attributes: { plan_name: 'startups' }) }
|
||||
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) }
|
||||
|
||||
let(:mock_captain_agent) { instance_double(Captain::Agent) }
|
||||
let(:mock_captain_tool) { instance_double(Captain::Tool) }
|
||||
let(:mock_openai_client) { instance_double(OpenAI::Client) }
|
||||
let(:inbox) { create(:inbox, account: account) }
|
||||
let(:assistant) { create(:captain_assistant, account: account) }
|
||||
|
||||
before do
|
||||
create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key')
|
||||
end
|
||||
|
||||
describe '#initialize' do
|
||||
it 'sets default language to english when not specified' do
|
||||
service = described_class.new(assistant, { previous_messages: [], conversation_history: '' })
|
||||
expect(service.instance_variable_get(:@language)).to eq('english')
|
||||
end
|
||||
|
||||
it 'uses the specified language when provided' do
|
||||
service = described_class.new(assistant, {
|
||||
previous_messages: [],
|
||||
conversation_history: '',
|
||||
language: 'spanish'
|
||||
})
|
||||
expect(service.instance_variable_get(:@language)).to eq('spanish')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#generate_response' do
|
||||
before do
|
||||
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
||||
allow(mock_openai_client).to receive(:chat).and_return({ choices: [{ message: { content: '{ "result": "Hey" }' } }] }.with_indifferent_access)
|
||||
|
||||
allow(Captain::Agent).to receive(:new).and_return(mock_captain_agent)
|
||||
allow(mock_captain_agent).to receive(:execute).and_return(true)
|
||||
allow(mock_captain_agent).to receive(:register_tool).and_return(true)
|
||||
|
||||
allow(Captain::Tool).to receive(:new).and_return(mock_captain_tool)
|
||||
allow(mock_captain_tool).to receive(:register_method).and_return(true)
|
||||
|
||||
allow(account).to receive(:increment_response_usage).and_return(true)
|
||||
end
|
||||
|
||||
it 'increments usage' do
|
||||
described_class.new(assistant, { previous_messages: ['Hello'], conversation_history: 'Hi' }).generate_response('Hey')
|
||||
expect(account).to have_received(:increment_response_usage).once
|
||||
end
|
||||
|
||||
it 'includes language in system message' do
|
||||
service = described_class.new(assistant, {
|
||||
previous_messages: [],
|
||||
conversation_history: '',
|
||||
language: 'spanish'
|
||||
})
|
||||
|
||||
allow(Captain::Llm::SystemPromptsService).to receive(:copilot_response_generator)
|
||||
.with(assistant.config['product_name'], 'spanish')
|
||||
.and_return('Spanish system prompt')
|
||||
|
||||
system_message = service.send(:system_message)
|
||||
expect(system_message[:content]).to eq('Spanish system prompt')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#execute' do
|
||||
before do
|
||||
create(:installation_config) { create(:installation_config, name: 'CAPTAIN_OPEN_AI_API_KEY', value: 'test-key') }
|
||||
allow(OpenAI::Client).to receive(:new).and_return(mock_openai_client)
|
||||
allow(mock_openai_client).to receive(:chat).and_return({ choices: [{ message: { content: '{ "result": "Hey" }' } }] }.with_indifferent_access)
|
||||
|
||||
|
||||
@@ -145,5 +145,25 @@ RSpec.describe Captain::Llm::ConversationFaqService do
|
||||
{ role: 'user', content: conversation.to_llm_text }
|
||||
)
|
||||
end
|
||||
|
||||
context 'when conversation has different language' do
|
||||
let(:account) { create(:account, locale: 'fr') }
|
||||
let(:conversation) do
|
||||
create(:conversation, account: account,
|
||||
first_reply_created_at: Time.zone.now)
|
||||
end
|
||||
|
||||
it 'includes system prompt with correct language' do
|
||||
allow(Captain::Llm::SystemPromptsService).to receive(:conversation_faq_generator)
|
||||
.with('french')
|
||||
.and_return('system prompt in french')
|
||||
|
||||
params = service.send(:chat_parameters)
|
||||
|
||||
expect(params[:messages]).to include(
|
||||
{ role: 'system', content: 'system prompt in french' }
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -108,4 +108,30 @@ RSpec.describe Account do
|
||||
expect(ActiveRecord::Base.connection.execute(query).count).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'locale' do
|
||||
it 'returns correct language if the value is set' do
|
||||
account = create(:account, locale: 'fr')
|
||||
expect(account.locale).to eq('fr')
|
||||
expect(account.locale_english_name).to eq('french')
|
||||
end
|
||||
|
||||
it 'returns english if the value is not set' do
|
||||
account = create(:account, locale: nil)
|
||||
expect(account.locale).to be_nil
|
||||
expect(account.locale_english_name).to eq('english')
|
||||
end
|
||||
|
||||
it 'returns english if the value is empty string' do
|
||||
account = create(:account, locale: '')
|
||||
expect(account.locale).to be_nil
|
||||
expect(account.locale_english_name).to eq('english')
|
||||
end
|
||||
|
||||
it 'returns correct language if the value has country code' do
|
||||
account = create(:account, locale: 'pt_BR')
|
||||
expect(account.locale).to eq('pt_BR')
|
||||
expect(account.locale_english_name).to eq('portuguese')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe SearchService do
|
||||
subject(:search) { described_class.new(current_user: user, current_account: account, params: params, search_type: search_type) }
|
||||
subject(:search) do
|
||||
described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
end
|
||||
|
||||
let(:search_type) { 'all' }
|
||||
let!(:account) { create(:account) }
|
||||
@@ -26,25 +29,33 @@ describe SearchService do
|
||||
|
||||
it 'returns all for all' do
|
||||
search_type = 'all'
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
expect(search.perform.keys).to match_array(%i[contacts messages conversations])
|
||||
end
|
||||
|
||||
it 'returns contacts for contacts' do
|
||||
search_type = 'Contact'
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
expect(search.perform.keys).to match_array(%i[contacts])
|
||||
end
|
||||
|
||||
it 'returns messages for messages' do
|
||||
search_type = 'Message'
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
expect(search.perform.keys).to match_array(%i[messages])
|
||||
end
|
||||
|
||||
it 'returns conversations for conversations' do
|
||||
search_type = 'Conversation'
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
expect(search.perform.keys).to match_array(%i[conversations])
|
||||
end
|
||||
end
|
||||
@@ -61,7 +72,8 @@ describe SearchService do
|
||||
harry4 = create(:contact, identifier: 'Potter1235', account_id: account.id, last_activity_at: 2.minutes.ago)
|
||||
|
||||
params = { q: 'Potter ' }
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Contact')
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Contact')
|
||||
expect(search.perform[:contacts].map(&:id)).to eq([harry4.id, harry3.id, harry2.id, harry.id])
|
||||
end
|
||||
end
|
||||
@@ -75,17 +87,19 @@ describe SearchService do
|
||||
# random messsage in inbox with out access
|
||||
create(:message, account: account, inbox: create(:inbox, account: account), content: 'Harry Potter is a wizard')
|
||||
params = { q: 'Harry' }
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Message')
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Message')
|
||||
expect(search.perform[:messages].map(&:id)).to eq([message2.id, message.id])
|
||||
end
|
||||
|
||||
context 'with feature flag for search type' do
|
||||
let(:params) { { q: 'Harry' } }
|
||||
context 'with different search methods' do
|
||||
let(:search_type) { 'Message' }
|
||||
|
||||
it 'uses LIKE search when search_with_gin feature is disabled' do
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
||||
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
it 'uses LIKE search for multi-word queries' do
|
||||
params = { q: 'Harry Potter' }
|
||||
search_service = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
|
||||
expect(search_service).to receive(:filter_messages_with_like).and_call_original
|
||||
expect(search_service).not_to receive(:filter_messages_with_gin)
|
||||
@@ -93,9 +107,11 @@ describe SearchService do
|
||||
search_service.perform
|
||||
end
|
||||
|
||||
it 'uses GIN search when search_with_gin feature is enabled' do
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||
search_service = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
it 'uses GIN search for single-word queries' do
|
||||
params = { q: 'Harry' }
|
||||
search_service = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
|
||||
expect(search_service).to receive(:filter_messages_with_gin).and_call_original
|
||||
expect(search_service).not_to receive(:filter_messages_with_like)
|
||||
@@ -103,24 +119,39 @@ describe SearchService do
|
||||
search_service.perform
|
||||
end
|
||||
|
||||
it 'returns same results regardless of search type' do
|
||||
it 'returns same results for single word regardless of search method' do
|
||||
# Create test messages
|
||||
message3 = create(:message, account: account, inbox: inbox, content: 'Harry is a wizard apprentice')
|
||||
params = { q: 'Harry' }
|
||||
|
||||
# Test with GIN search
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||
gin_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
# Test with GIN search (single word)
|
||||
gin_search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
allow(gin_search).to receive(:use_gin_search).and_return(true)
|
||||
gin_results = gin_search.perform[:messages].map(&:id)
|
||||
|
||||
# Test with LIKE search
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
||||
like_search = described_class.new(current_user: user, current_account: account, params: params, search_type: search_type)
|
||||
# Test with LIKE search (forcing LIKE for comparison)
|
||||
like_search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
allow(like_search).to receive(:use_gin_search).and_return(false)
|
||||
like_results = like_search.perform[:messages].map(&:id)
|
||||
|
||||
# Both search types should return the same messages
|
||||
expect(gin_results).to match_array(like_results)
|
||||
expect(gin_results).to include(message.id, message2.id, message3.id)
|
||||
end
|
||||
|
||||
it 'handles empty search queries' do
|
||||
params = { q: '' }
|
||||
search_service = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: search_type)
|
||||
|
||||
results = search_service.perform[:messages]
|
||||
expect(results).to be_a(ActiveRecord::Relation)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -131,7 +162,8 @@ describe SearchService do
|
||||
create(:conversation, contact: random, inbox: inbox, account: account)
|
||||
conv2 = create(:conversation, contact: harry, inbox: inbox, account: account)
|
||||
params = { q: 'Harry' }
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Conversation')
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Conversation')
|
||||
expect(search.perform[:conversations].map(&:id)).to eq([conv2.id, conversation.id])
|
||||
end
|
||||
|
||||
@@ -139,28 +171,86 @@ describe SearchService do
|
||||
random = create(:contact, account_id: account.id, name: 'random', email: 'random@random.test', identifier: 'random')
|
||||
new_converstion = create(:conversation, contact: random, inbox: inbox, account: account)
|
||||
params = { q: new_converstion.display_id }
|
||||
search = described_class.new(current_user: user, current_account: account, params: params, search_type: 'Conversation')
|
||||
search = described_class.new(current_user: user, current_account: account,
|
||||
current_account_user: user.account_users.find_by(account: account), params: params, search_type: 'Conversation')
|
||||
expect(search.perform[:conversations].map(&:id)).to include new_converstion.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#use_gin_search' do
|
||||
let(:params) { { q: 'test' } }
|
||||
|
||||
it 'checks if the account has the search_with_gin feature enabled' do
|
||||
expect(account).to receive(:feature_enabled?).with('search_with_gin')
|
||||
search.send(:use_gin_search)
|
||||
end
|
||||
|
||||
it 'returns true when search_with_gin feature is enabled' do
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(true)
|
||||
it 'returns true for single-word queries' do
|
||||
params = { q: 'test' }
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: 'Message')
|
||||
expect(search.send(:use_gin_search)).to be true
|
||||
end
|
||||
|
||||
it 'returns false when search_with_gin feature is disabled' do
|
||||
allow(account).to receive(:feature_enabled?).with('search_with_gin').and_return(false)
|
||||
it 'returns false for multi-word queries' do
|
||||
params = { q: 'test query' }
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: 'Message')
|
||||
expect(search.send(:use_gin_search)).to be false
|
||||
end
|
||||
|
||||
it 'returns false for empty queries' do
|
||||
params = { q: '' }
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: 'Message')
|
||||
expect(search.send(:use_gin_search)).to be false
|
||||
end
|
||||
|
||||
it 'returns false for nil queries' do
|
||||
params = { q: nil }
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: params, search_type: 'Message')
|
||||
expect(search.send(:use_gin_search)).to be false
|
||||
end
|
||||
end
|
||||
|
||||
describe '#accessable_inbox_ids' do
|
||||
it 'returns inbox ids assigned to the user' do
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: {}, search_type: 'Message')
|
||||
expect(search.send(:accessable_inbox_ids)).to eq([inbox.id])
|
||||
end
|
||||
end
|
||||
|
||||
describe '#message_base_query' do
|
||||
it 'returns messages from the last 3 months' do
|
||||
old_message = create(:message, account: account, inbox: inbox, content: 'old message', created_at: 4.months.ago)
|
||||
recent_message = create(:message, account: account, inbox: inbox, content: 'recent message', created_at: 2.months.ago)
|
||||
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: {}, search_type: 'Message')
|
||||
base_query = search.send(:message_base_query)
|
||||
|
||||
expect(base_query).to include(recent_message)
|
||||
expect(base_query).not_to include(old_message)
|
||||
end
|
||||
|
||||
it 'filters by inbox_id for non-administrators' do
|
||||
other_inbox = create(:inbox, account: account)
|
||||
other_inbox_message = create(:message, account: account, inbox: other_inbox, content: 'other inbox message')
|
||||
|
||||
search = described_class.new(current_user: user, current_account: account, current_account_user: user.account_users.find_by(account: account),
|
||||
params: {}, search_type: 'Message')
|
||||
base_query = search.send(:message_base_query)
|
||||
|
||||
expect(base_query).not_to include(other_inbox_message)
|
||||
end
|
||||
|
||||
it 'does not filter by inbox_id for administrators' do
|
||||
# Create a new user with administrator role
|
||||
admin_user = create(:user, account: account, role: :administrator)
|
||||
other_inbox = create(:inbox, account: account)
|
||||
other_inbox_message = create(:message, account: account, inbox: other_inbox, content: 'other inbox message')
|
||||
|
||||
search = described_class.new(current_user: admin_user, current_account: account,
|
||||
current_account_user: admin_user.account_users.find_by(account: account), params: {}, search_type: 'Message')
|
||||
base_query = search.send(:message_base_query)
|
||||
|
||||
expect(base_query).to include(other_inbox_message)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user