UnisKB/ui/src/views/chat/pc/index.vue

589 lines
18 KiB
Vue
Raw Normal View History

2025-06-03 08:08:49 +00:00
<template>
<div
2025-06-15 06:53:18 +00:00
class="chat-pc"
2025-06-03 08:08:49 +00:00
:class="classObj"
2025-07-15 12:46:47 +00:00
v-loading="loading"
2025-06-03 08:08:49 +00:00
:style="{
2025-07-02 09:46:39 +00:00
'--el-color-primary': applicationDetail?.custom_theme?.theme_color,
2025-07-10 14:33:39 +00:00
'--el-color-primary-light-9': hexToRgba(
applicationDetail?.custom_theme?.theme_color || '#3370FF',
0.1,
),
'--el-color-primary-light-6': hexToRgba(
applicationDetail?.custom_theme?.theme_color || '#3370FF',
0.4,
),
2025-07-11 07:19:34 +00:00
'--el-color-primary-light-06': hexToRgba(
applicationDetail?.custom_theme?.theme_color || '#3370FF',
0.04,
),
2025-06-03 08:08:49 +00:00
}"
>
2025-06-24 10:15:26 +00:00
<div class="flex h-full w-full">
<div class="chat-pc__left">
2025-07-08 17:31:21 +00:00
<HistoryPanel
2025-07-08 12:50:19 +00:00
:application-detail="applicationDetail"
:chat-log-data="chatLogData"
:left-loading="left_loading"
:currentChatId="currentChatId"
@new-chat="newChat"
@clickLog="clickListHandle"
@delete-log="deleteLog"
2025-07-15 06:59:40 +00:00
@clear-chat="clearChat"
2025-07-08 12:50:19 +00:00
@refreshFieldTitle="refreshFieldTitle"
:isPcCollapse="isPcCollapse"
2025-07-02 09:46:39 +00:00
>
2025-07-08 12:50:19 +00:00
<div class="user-info p-16 cursor">
<el-avatar
:size="32"
v-if="
!chatUser.chat_profile?.authentication ||
chatUser.chat_profile.authentication_type === 'password'
"
2025-07-02 09:46:39 +00:00
>
2025-07-15 11:57:21 +00:00
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
2025-06-25 11:20:30 +00:00
</el-avatar>
2025-07-08 12:50:19 +00:00
<el-dropdown v-else trigger="click" type="primary" class="w-full">
<div class="flex align-center">
<el-avatar :size="32">
2025-07-15 11:57:21 +00:00
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
2025-07-08 12:50:19 +00:00
</el-avatar>
<span v-show="!isPcCollapse" class="ml-8 color-text-primary">{{
2025-07-15 11:57:21 +00:00
chatUser.chatUserProfile?.nick_name
}}</span>
2025-07-08 12:50:19 +00:00
</div>
2025-06-25 10:57:06 +00:00
2025-07-08 12:50:19 +00:00
<template #dropdown>
2025-07-08 17:31:21 +00:00
<el-dropdown-menu style="min-width: 260px">
<div class="flex align-center p-8">
2025-07-08 12:50:19 +00:00
<div class="mr-8 flex align-center">
<el-avatar :size="40">
2025-07-15 11:57:21 +00:00
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
2025-07-08 12:50:19 +00:00
</el-avatar>
</div>
<div>
<h4 class="medium mb-4">{{ chatUser.chatUserProfile?.nick_name }}</h4>
<div class="color-secondary">
{{ `${t('common.username')}: ${chatUser.chatUserProfile?.username}` }}
</div>
2025-07-02 09:46:39 +00:00
</div>
2025-06-25 08:49:17 +00:00
</div>
2025-07-08 12:50:19 +00:00
<el-dropdown-item
v-if="chatUser.chatUserProfile?.source === 'LOCAL'"
class="border-t"
style="padding-top: 8px; padding-bottom: 8px"
@click="openResetPassword"
>
<el-icon>
2025-07-15 11:57:21 +00:00
<Lock />
</el-icon>
2025-07-08 12:50:19 +00:00
{{ $t('views.login.resetPassword') }}
</el-dropdown-item>
<el-dropdown-item
v-if="chatUser.chatUserProfile?.source === 'LOCAL'"
class="border-t"
style="padding-top: 8px; padding-bottom: 8px"
@click="logout"
>
2025-07-15 11:57:21 +00:00
<AppIcon iconName="app-export" />
2025-07-08 12:50:19 +00:00
{{ $t('layout.logout') }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
2025-07-08 17:31:21 +00:00
</HistoryPanel>
2025-07-02 09:46:39 +00:00
<el-button
v-if="!common.isMobile()"
class="pc-collapse cursor"
circle
@click="isPcCollapse = !isPcCollapse"
>
2025-06-24 10:15:26 +00:00
<el-icon>
2025-07-15 11:57:21 +00:00
<component :is="isPcCollapse ? 'ArrowRightBold' : 'ArrowLeftBold'" />
2025-06-24 10:15:26 +00:00
</el-icon>
</el-button>
2025-06-13 13:56:01 +00:00
</div>
2025-07-03 11:17:13 +00:00
<div
2025-07-03 11:39:24 +00:00
class="chat-pc__right chat-background"
:style="{ backgroundImage: `url(${applicationDetail?.chat_background})` }"
2025-07-03 11:17:13 +00:00
>
<div style="flex: 1">
<div class="p-16-24 flex-between">
<h4 class="ellipsis-1" style="width: 66%">
{{ currentChatName }}
</h4>
2025-06-27 12:27:18 +00:00
<span class="flex align-center" v-if="currentRecordList.length">
2025-07-15 11:57:21 +00:00
<AppIcon
v-if="paginationConfig.total"
iconName="app-chat-record"
class="color-secondary mr-8"
style="font-size: 16px"
></AppIcon>
<span v-if="paginationConfig.total" class="lighter">
{{ paginationConfig.total }} {{ $t('chat.question_count') }}
</span>
<el-dropdown class="ml-8">
2025-06-27 12:27:18 +00:00
<AppIcon
2025-07-15 11:57:21 +00:00
iconName="app-export"
class="cursor"
:title="$t('chat.exportRecords')"
2025-06-27 12:27:18 +00:00
></AppIcon>
2025-07-15 11:57:21 +00:00
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click="exportMarkdown"
>{{ $t('common.export') }} Markdown</el-dropdown-item
2025-07-15 11:57:21 +00:00
>
<el-dropdown-item @click="exportHTML"
>{{ $t('common.export') }} HTML</el-dropdown-item
2025-07-15 11:57:21 +00:00
>
</el-dropdown-menu>
</template>
</el-dropdown>
</span>
2025-07-11 07:44:11 +00:00
</div>
<div class="right-height chat-width">
<AiChat
ref="AiChatRef"
v-model:applicationDetails="applicationDetail"
:available="applicationAvailable"
type="ai-chat"
:appId="applicationDetail?.id"
:record="currentRecordList"
:chatId="currentChatId"
executionIsRightPanel
@refresh="refresh"
@scroll="handleScroll"
@open-execution-detail="openExecutionDetail"
@openParagraph="openKnowledgeSource"
@openParagraphDocument="openParagraphDocument"
>
</AiChat>
</div>
</div>
<div
class="execution-detail-panel"
2025-07-15 11:57:21 +00:00
:style="`width: ${rightPanelSize}px`"
:resizable="false"
collapsible
>
<div class="p-16 flex-between border-b">
<h4 class="medium ellipsis" :title="rightPanelTitle">{{ rightPanelTitle }}</h4>
 
<div class="flex align-center">
2025-07-15 11:57:21 +00:00
<span v-if="rightPanelType === 'paragraphDocument'" class="mr-4">
<a
:href="
getFileUrl(rightPanelDetail?.meta?.source_file_id) ||
rightPanelDetail?.meta?.source_url
"
target="_blank"
class="ellipsis-1"
:title="rightPanelDetail?.document_name?.trim()"
>
2025-07-11 07:46:50 +00:00
<el-button text>
2025-07-15 11:57:21 +00:00
<el-icon> <Download /> </el-icon>
2025-07-11 07:46:50 +00:00
</el-button>
2025-07-15 11:57:21 +00:00
</a>
</span>
<!-- <span v-if="rightPanelType === 'paragraphDocument'">
<el-button text> <app-icon iconName="app-export" size="20" /></el-button>
</span> -->
<span>
2025-07-15 11:57:21 +00:00
<el-button text @click="closeExecutionDetail">
<el-icon size="20"><Close /></el-icon
></el-button>
</span>
2025-06-27 12:27:18 +00:00
</div>
2025-07-11 07:44:11 +00:00
</div>
<div class="execution-detail-content" v-loading="rightPanelLoading">
<ParagraphSourceContent
v-if="rightPanelType === 'knowledgeSource'"
:detail="rightPanelDetail"
/>
<ExecutionDetailContent
v-if="rightPanelType === 'executionDetail'"
:detail="executionDetail"
2025-07-15 12:46:47 +00:00
:appType="applicationDetail?.type"
/>
2025-07-15 11:57:21 +00:00
<ParagraphDocumentContent :detail="rightPanelDetail" v-else />
</div>
</div>
2025-06-03 08:08:49 +00:00
</div>
</div>
2025-06-13 13:56:01 +00:00
2025-07-02 09:46:39 +00:00
<ResetPassword
ref="resetPasswordRef"
emitConfirm
@confirm="handleResetPassword"
></ResetPassword>
2025-06-03 08:08:49 +00:00
</div>
</template>
<script setup lang="ts">
2025-07-15 11:57:21 +00:00
import { ref, onMounted, nextTick, computed, watch } from 'vue'
import { marked } from 'marked'
import { saveAs } from 'file-saver'
2025-06-23 13:28:10 +00:00
import chatAPI from '@/api/chat/chat'
2025-07-08 12:50:19 +00:00
2025-06-03 08:08:49 +00:00
import useStore from '@/stores'
import useResize from '@/layout/hooks/useResize'
2025-07-15 11:57:21 +00:00
import { hexToRgba } from '@/utils/theme'
import { useRouter } from 'vue-router'
2025-06-25 08:49:17 +00:00
import ResetPassword from '@/layout/layout-header/avatar/ResetPassword.vue'
2025-07-15 11:57:21 +00:00
import { t } from '@/locales'
import type { ResetCurrentUserPasswordRequest } from '@/api/type/user'
import ExecutionDetailContent from '@/components/ai-chat/component/knowledge-source-component/ExecutionDetailContent.vue'
import ParagraphSourceContent from '@/components/ai-chat/component/knowledge-source-component/ParagraphSourceContent.vue'
import ParagraphDocumentContent from '@/components/ai-chat/component/knowledge-source-component/ParagraphDocumentContent.vue'
2025-07-08 17:31:21 +00:00
import HistoryPanel from '@/views/chat/component/HistoryPanel.vue'
2025-07-15 11:57:21 +00:00
import { cloneDeep } from 'lodash'
import { getFileUrl } from '@/utils/common'
2025-06-03 08:08:49 +00:00
useResize()
2025-07-15 11:57:21 +00:00
const { common, chatUser } = useStore()
2025-06-25 08:49:17 +00:00
const router = useRouter()
2025-06-03 08:08:49 +00:00
const isCollapse = ref(false)
2025-06-24 10:15:26 +00:00
const isPcCollapse = ref(false)
2025-07-02 09:46:39 +00:00
watch(
() => common.device,
() => {
if (common.isMobile()) {
isPcCollapse.value = false
}
},
)
2025-06-03 08:08:49 +00:00
2025-06-25 08:49:17 +00:00
const logout = () => {
chatUser.logout().then(() => {
2025-07-15 11:57:21 +00:00
router.push({ name: 'login' })
2025-06-25 08:49:17 +00:00
})
}
const resetPasswordRef = ref<InstanceType<typeof ResetPassword>>()
const openResetPassword = () => {
resetPasswordRef.value?.open()
}
const handleResetPassword = (param: ResetCurrentUserPasswordRequest) => {
chatAPI.resetCurrentPassword(param).then(() => {
2025-07-15 11:57:21 +00:00
router.push({ name: 'login' })
2025-06-25 08:49:17 +00:00
})
}
2025-06-03 08:08:49 +00:00
const classObj = computed(() => {
return {
mobile: common.isMobile(),
hideLeft: !isCollapse.value,
2025-06-13 13:56:01 +00:00
openLeft: isCollapse.value,
2025-06-03 08:08:49 +00:00
}
})
const newObj = {
id: 'new',
2025-06-13 13:56:01 +00:00
abstract: t('chat.createChat'),
2025-06-03 08:08:49 +00:00
}
const props = defineProps<{
application_profile: any
applicationAvailable: boolean
}>()
const AiChatRef = ref()
const loading = ref(false)
const left_loading = ref(false)
const applicationDetail = computed({
get: () => {
return props.application_profile
},
2025-07-15 11:57:21 +00:00
set: (v) => {},
2025-06-03 08:08:49 +00:00
})
const chatLogData = ref<any[]>([])
const paginationConfig = ref({
current_page: 1,
page_size: 20,
2025-06-13 13:56:01 +00:00
total: 0,
2025-06-03 08:08:49 +00:00
})
const currentRecordList = ref<any>([])
const currentChatId = ref('new') // 当前历史记录Id 默认为'new'
const currentChatName = ref(t('chat.createChat'))
function refreshFieldTitle(chatId: string, abstract: string) {
const find = chatLogData.value.find((item: any) => item.id == chatId)
if (find) {
find.abstract = abstract
}
}
2025-06-03 08:08:49 +00:00
function deleteLog(row: any) {
2025-07-03 09:38:14 +00:00
chatAPI.deleteChat(row.id, left_loading).then(() => {
2025-06-03 08:08:49 +00:00
if (currentChatId.value === row.id) {
currentChatId.value = 'new'
currentChatName.value = t('chat.createChat')
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
}
getChatLog(applicationDetail.value.id)
})
}
2025-07-15 06:59:40 +00:00
function clearChat() {
2025-07-15 11:57:21 +00:00
chatAPI.clearChat(left_loading).then(() => {
2025-07-15 06:59:40 +00:00
currentChatId.value = 'new'
2025-07-15 11:57:21 +00:00
currentChatName.value = t('chat.createChat')
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
getChatLog(applicationDetail.value.id)
2025-07-15 06:59:40 +00:00
})
}
2025-06-03 08:08:49 +00:00
function handleScroll(event: any) {
if (
currentChatId.value !== 'new' &&
event.scrollTop === 0 &&
paginationConfig.value.total > currentRecordList.value.length
) {
const history_height = event.dialogScrollbar.offsetHeight
paginationConfig.value.current_page += 1
getChatRecord().then(() => {
event.scrollDiv.setScrollTop(event.dialogScrollbar.offsetHeight - history_height)
})
}
}
function newChat() {
if (!chatLogData.value.some((v) => v.id === 'new')) {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
chatLogData.value.unshift(newObj)
} else {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
}
currentChatId.value = 'new'
currentChatName.value = t('chat.createChat')
if (common.isMobile()) {
isCollapse.value = false
}
}
function getChatLog(id: string, refresh?: boolean) {
const page = {
current_page: 1,
2025-06-13 13:56:01 +00:00
page_size: 20,
2025-06-03 08:08:49 +00:00
}
2025-06-23 13:28:10 +00:00
chatAPI.pageChat(page.current_page, page.page_size, left_loading).then((res: any) => {
2025-06-03 08:08:49 +00:00
chatLogData.value = res.data.records
if (refresh) {
currentChatName.value = chatLogData.value?.[0]?.abstract
} else {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
currentChatId.value = chatLogData.value?.[0]?.id || 'new'
currentChatName.value = chatLogData.value?.[0]?.abstract || t('chat.createChat')
if (currentChatId.value !== 'new') {
getChatRecord()
}
}
})
}
function getChatRecord() {
2025-06-24 09:44:59 +00:00
return chatAPI
.pageChatRecord(
2025-06-03 08:08:49 +00:00
currentChatId.value,
2025-06-24 09:44:59 +00:00
paginationConfig.value.current_page,
paginationConfig.value.page_size,
2025-06-03 08:08:49 +00:00
loading,
)
.then((res: any) => {
paginationConfig.value.total = res.data.total
const list = res.data.records
list.map((v: any) => {
v['write_ed'] = true
v['record_id'] = v.id
})
currentRecordList.value = [...list, ...currentRecordList.value].sort((a, b) =>
2025-06-13 13:56:01 +00:00
a.create_time.localeCompare(b.create_time),
2025-06-03 08:08:49 +00:00
)
if (paginationConfig.value.current_page === 1) {
nextTick(() => {
// 将滚动条滚动到最下面
AiChatRef.value.setScrollBottom()
})
}
})
}
const clickListHandle = (item: any) => {
if (item.id !== currentChatId.value) {
paginationConfig.value.current_page = 1
paginationConfig.value.total = 0
currentRecordList.value = []
currentChatId.value = item.id
currentChatName.value = item.abstract
if (currentChatId.value !== 'new') {
getChatRecord()
// 切换对话后,取消暂停的浏览器播放
if (window.speechSynthesis.paused && window.speechSynthesis.speaking) {
window.speechSynthesis.resume()
nextTick(() => {
window.speechSynthesis.cancel()
})
}
}
}
if (common.isMobile()) {
isCollapse.value = false
}
}
function refresh(id: string) {
currentChatId.value = id
2025-07-15 12:46:47 +00:00
getChatLog(applicationDetail.value.id, true)
2025-06-03 08:08:49 +00:00
}
async function exportMarkdown(): Promise<void> {
const suggestedName: string = `${currentChatId.value}.md`
const markdownContent: string = currentRecordList.value
.map((record: any) => `# ${record.problem_text}\n\n${record.answer_text}\n\n`)
.join('\n')
2025-07-15 11:57:21 +00:00
const blob: Blob = new Blob([markdownContent], { type: 'text/markdown;charset=utf-8' })
2025-06-03 08:08:49 +00:00
saveAs(blob, suggestedName)
}
async function exportHTML(): Promise<void> {
const suggestedName: string = `${currentChatId.value}.html`
const markdownContent: string = currentRecordList.value
.map((record: any) => `# ${record.problem_text}\n\n${record.answer_text}\n\n`)
.join('\n')
const htmlContent: any = marked(markdownContent)
2025-07-15 11:57:21 +00:00
const blob: Blob = new Blob([htmlContent], { type: 'text/html;charset=utf-8' })
2025-06-03 08:08:49 +00:00
saveAs(blob, suggestedName)
}
/**
*初始化历史对话记录
*/
const init = () => {
2025-06-24 10:15:26 +00:00
getChatLog(applicationDetail.value?.id)
2025-06-03 08:08:49 +00:00
}
onMounted(() => {
init()
})
2025-06-27 12:27:18 +00:00
const rightPanelSize = ref(0)
2025-06-30 08:05:14 +00:00
const rightPanelTitle = ref('')
const rightPanelType = ref('')
const rightPanelLoading = ref(false)
2025-06-27 12:27:18 +00:00
const executionDetail = ref<any[]>([])
2025-06-30 08:05:14 +00:00
const rightPanelDetail = ref<any>()
2025-06-27 12:27:18 +00:00
async function openExecutionDetail(row: any) {
rightPanelSize.value = 400
2025-06-30 08:05:14 +00:00
rightPanelTitle.value = t('chat.executionDetails.title')
rightPanelType.value = 'executionDetail'
2025-06-27 12:27:18 +00:00
if (row.execution_details) {
executionDetail.value = cloneDeep(row.execution_details)
} else {
2025-06-30 08:05:14 +00:00
const res = await chatAPI.getChatRecord(row.chat_id, row.record_id, rightPanelLoading)
2025-06-27 12:27:18 +00:00
executionDetail.value = cloneDeep(res.data.execution_details)
}
}
2025-06-30 08:05:14 +00:00
2025-07-02 09:46:39 +00:00
async function openKnowledgeSource(row: any) {
2025-06-30 08:05:14 +00:00
rightPanelTitle.value = t('chat.KnowledgeSource.title')
rightPanelType.value = 'knowledgeSource'
// TODO 数据
rightPanelDetail.value = row
rightPanelSize.value = 400
}
2025-07-04 10:21:11 +00:00
function openParagraphDocument(detail: any, row: any) {
rightPanelTitle.value = row.document_name
rightPanelType.value = 'paragraphDocument'
rightPanelSize.value = 400
2025-07-07 14:58:23 +00:00
rightPanelDetail.value = row
2025-07-04 10:21:11 +00:00
}
2025-06-27 12:27:18 +00:00
function closeExecutionDetail() {
rightPanelSize.value = 0
}
2025-06-03 08:08:49 +00:00
</script>
2025-07-08 12:50:19 +00:00
<style lang="scss" scoped>
2025-06-03 08:08:49 +00:00
.chat-pc {
2025-06-24 10:15:26 +00:00
height: 100%;
2025-06-03 08:08:49 +00:00
overflow: hidden;
2025-06-15 06:53:18 +00:00
background: #eef1f4;
2025-06-03 08:08:49 +00:00
&__left {
2025-06-24 10:15:26 +00:00
position: relative;
2025-07-04 03:09:33 +00:00
z-index: 1;
2025-06-24 10:15:26 +00:00
.pc-collapse {
position: absolute;
top: 20px;
2025-07-08 17:31:21 +00:00
right: -13px;
2025-07-08 12:50:19 +00:00
box-shadow: 0px 5px 10px 0px rgba(31, 35, 41, 0.1);
2025-07-04 09:42:12 +00:00
z-index: 1;
2025-07-08 17:31:21 +00:00
width: 24px;
height: 24px;
2025-06-24 10:15:26 +00:00
}
2025-06-03 08:08:49 +00:00
}
&__right {
2025-06-24 10:15:26 +00:00
flex: 1;
2025-06-03 08:08:49 +00:00
overflow: hidden;
position: relative;
box-sizing: border-box;
2025-07-11 07:44:11 +00:00
display: flex;
2025-06-03 08:08:49 +00:00
.right-height {
2025-07-08 07:47:52 +00:00
height: calc(100vh - 60px);
2025-06-03 08:08:49 +00:00
}
2025-06-27 12:27:18 +00:00
2025-07-10 14:33:39 +00:00
:deep(.execution-detail-panel) {
2025-07-11 07:44:11 +00:00
transition: width 0.4s;
2025-06-27 12:27:18 +00:00
background: #ffffff;
height: 100%;
overflow: hidden;
2025-06-27 12:27:18 +00:00
.execution-detail-content {
flex: 1;
overflow: hidden;
2025-07-10 14:33:39 +00:00
height: calc(100% - 63px);
2025-06-27 12:27:18 +00:00
.execution-details {
padding: 16px;
}
}
}
2025-06-03 08:08:49 +00:00
}
}
2025-06-24 10:15:26 +00:00
2025-06-03 08:08:49 +00:00
.chat-width {
max-width: 80%;
margin: 0 auto;
}
2025-06-03 08:08:49 +00:00
@media only screen and (max-width: 1000px) {
.chat-width {
max-width: 100% !important;
margin: 0 auto;
}
}
2025-06-25 10:57:06 +00:00
</style>