feat: 添加产品助手对话功能

This commit is contained in:
huangzhe
2025-07-29 10:26:24 +08:00
parent dced4d6ca5
commit b873e378d2
12 changed files with 225 additions and 198 deletions

View File

@@ -1,62 +1,62 @@
<template>
<div>
<div class="new-session">
<button @click="startNewConversation" class="active">
<svg-icon icon-class="add" class-name="chat-icon" style="color: #000;"></svg-icon>
开启新会话
</button>
</div>
<div v-if="isVoiceMode && newMessage" class="isVoiceModeText">
<textarea class="textarea" placeholder="请输入内容" v-model="newMessage"></textarea>
</div>
<footer class="chat-footer">
<!-- 输入框 or 按住说话提示 -->
<div class="input-wrapper ml10">
<input v-if="!isVoiceMode" type="text" v-model="newMessage" placeholder="请简短描述您的问题"
@keyup.enter="sendMessage" />
<div v-else class="voice-hint-container" :class="{ disabled: messageStatus === 'send' }"
@mousedown="startRecording" @selectstart="() => false" @mouseup="stopRecording" @mouseleave="stopRecording"
@touchend="stopRecording" @touchstart="startRecording">
<div class="waveform" :class="{ active: isRecording }">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<!-- <div class="hint-text" v-if='!isRecording'>按住说话</div>-->
</div>
<div class="chat-container">
<div class="mb15">
<div v-if="isVoiceMode && newMessage" class="isVoiceModeText">
<textarea class="textarea" placeholder="请输入内容" v-model="newMessage"></textarea>
</div>
<!-- 语音按钮按住说话 -->
<button @click="isVoiceMode = !isVoiceMode" class="mic-button ml10 mr10">
<!-- <svg-icon v-if="!isVoiceMode" icon-class="voice" class-name="chat-icon ml10 wh25"></svg-icon>
<div class="new-session ">
<button @click="startNewConversation">
<svg-icon icon-class="add" class-name="chat-icon" style="color: #000;"></svg-icon>
开启新会话
</button>
</div>
<footer class="chat-footer pt10 pb10">
<!-- 输入框 or 按住说话提示 -->
<div class="input-wrapper">
<input v-if="!isVoiceMode" type="text" v-model="newMessage" placeholder="你有什么想知道的?"
@keyup.enter="sendMessage" />
<div v-else class="voice-hint-container" :class="{ disabled: messageStatus === 'send' }"
@mousedown="startRecording" @selectstart="() => false" @mouseup="stopRecording" @mouseleave="stopRecording"
@touchend="stopRecording" @touchstart="startRecording">
<div class="waveform" :class="{ active: isRecording }">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
<!-- <div class="hint-text" v-if='!isRecording'>按住说话</div>-->
</div>
<!-- 发送按钮 -->
<button @click="sendMessage" :disabled="messageStatus === 'send'"
:class="{ disabled: messageStatus === 'send' }" class="active send">
发送
</button>
</div>
<!-- 语音按钮按住说话 -->
<button @click="isVoiceMode = !isVoiceMode" class="mic-button ml10 mr10" v-if="false">
<!-- <svg-icon v-if="!isVoiceMode" icon-class="voice" class-name="chat-icon ml10 wh25"></svg-icon>
<span v-else class="ml15 mr5 input-icon"></span> -->
</button>
</button>
</footer>
<!-- <section class="section pb10" > -->
<!-- <button @click="deepInternet" :class="{ active: isDeep }">
<svg-icon icon-class="think" class-name="chat-icon"></svg-icon>
深度思考
</button> -->
<!-- 发送按钮 -->
<button @click="sendMessage" :disabled="messageStatus === 'send'" :class="{ disabled: messageStatus === 'send' }"
class="mr10 fs16">发送</button>
</footer>
<section class="section pb10">
<!-- <button @click="searchInternet" :class="{ active: isSearching }" class="ml10">
<svg-icon icon-class="earth" class-name="chat-icon"></svg-icon>
联网搜索
</button> -->
<button @click="deepInternet" :class="{ active: isDeep }">
<svg-icon icon-class="think" class-name="chat-icon"></svg-icon>
深度思考
</button>
<!-- <button @click="startNewConversation" class="active">
<svg-icon icon-class="add" class-name="chat-icon"></svg-icon>
新建会话
</button> -->
</section>
<!-- <button @click="sendMessage" :disabled="messageStatus === 'send'"
:class="{ disabled: messageStatus === 'send' }" class="mr10 active send fs16">
发送
</button> -->
<!-- </section> -->
</div>
</div>
</template>
<script>
import SvgIcon from '@/components/svg-icon/index.vue'
import { audioToText, chat, chatProduct } from '@/api/generatedApi'
import { audioToText, gwcsChat, chatProduct } from '@/api/generatedApi'
export default {
components: {
@@ -98,6 +98,7 @@ export default {
},
data() {
return {
requestSingle: undefined,
isThink: false,
newMessage: '',
isRecording: false,
@@ -205,26 +206,32 @@ export default {
},
axiosGetAiChat() {
const abortController = new AbortController()
this.currentMessage = JSON.parse(
JSON.stringify({
type: 'bot',
text: '',
isThink: this.isDeep,
showThink: true,
think: '',
isLike: false,
isDisLike: false,
})
)
this.messages.push(this.currentMessage)
let params = {
query: this.newMessage,
isDeep: this.isDeep ? 1 : 0,
isOnline: this.isSearching ? 1 : 0,
user: 'chenyuda',
conversationId: this.conversationId,
productName: this.productName,
this.requestSingle = abortController
let message = {
is_complete: false.toString(),
information: this.newMessage,
}
this.currentMessage = JSON.parse(JSON.stringify(message))
let params = {
appType: "gwcsHelper",
conversationId: "",
message: JSON.stringify(message),
user: "gwcs-test",
inputs: {}
}
this.currentMessage = {
...message,
type: 'bot',
text: '',
think: '',
isThink: false,
showThink: false,
isLike: false,
isDisLike: false,
}
this.messages.push(this.currentMessage)
// 如果有自定义参数
if (this.chatData) {
for (let k in this.chatData) {
@@ -234,7 +241,7 @@ export default {
if (this.$route.query.compareId) {
params.compareResult = JSON.parse(sessionStorage.getItem('results'))
}
fetch(chat(), {
fetch(gwcsChat(), {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
signal: abortController.signal,
@@ -278,8 +285,14 @@ export default {
const cleanLine = line.replace(/^data:\s*/, '')
if (!cleanLine) return null
const data = JSON.parse(cleanLine)
// console.log(data)
if (data.answer) {
this.answerMap += data.answer
const match = /<is_complete>([^<]*)(?:<\/is_complete>)?/.exec(data.answer)
if (match && match[1] === 'true') {
this.requestSingle.abort()
this.axiosGetAiChat()
}
}
return this.updateConversationState(data)
@@ -315,12 +328,13 @@ export default {
this.$emit('update:messageStatus', 'stop')
}
if (!this.currentMessage || !answer) return
if (event !== 'message') return
// console.log(parse);
const mode = isThink ? 'think' : 'text'
const chars = {
answer: answer,
isThink: isThink,
}
this.typingQueue.push(chars)
if (!this.isTyping) {
this.startTypingAnimation(mode)
@@ -337,7 +351,11 @@ export default {
// 取出一个完整文本块
const chunk = this.typingQueue.shift()
const chars = chunk.answer.split('')
// console.log(this.messages);
const chars = /* chunk.answer.split('') */ Array.from(chunk.answer)
// console.log(chars);
const isThink = chunk.isThink
// 内部递归函数,用于逐字输出当前块
const outputChar = () => {
@@ -346,8 +364,8 @@ export default {
setTimeout(typeNextChar, 10)
return
}
const char = chars.shift()
this.currentMessage[isThink ? 'think' : 'text'] += char
const char = chars.shift() || ''
this.$set(this.currentMessage, isThink ? 'think' : 'text', this.currentMessage[isThink ? 'think' : 'text'] + char)
const delay = this.getTypingDelay(char)
setTimeout(outputChar, delay)
}
@@ -377,9 +395,8 @@ export default {
this.axiosGetAiChat()
},
cellClick(item) {
console.log(item);
const productName = item.title
this.messages.push({ type: 'user', text: productName })
this.newMessage = item.title
this.messages.push({ type: 'user', text: this.newMessage })
this.$emit('update:messageStatus', 'send')
this.$emit('update:autoScrollEnabled', true)
this.axiosGetAiChat()
@@ -388,10 +405,22 @@ export default {
}
</script>
<style lang="scss" scoped>
$primary-color: #2e5ca9;
$primary-color: #57a6fc;
$primary-text-color: #f6aa21;
$primary-trans-color: rgba(135, 162, 208, 0.5);
.chat-container {
background-color: #fafafa;
display: flex;
&>div {
margin: 0 auto;
background-color: #fff;
width: 90%;
border-radius: 10px;
}
}
.input-icon {
font-size: 20px;
color: #707070;
@@ -409,7 +438,6 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
flex: 1;
max-height: 80px;
resize: none;
background: #fff;
outline: none;
overflow-y: auto;
padding: 10px;
@@ -421,11 +449,13 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
font-size: 13px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 10px 0 10px;
background-color: #fff;
gap: 10px;
button {
transition: all 0.3s ease-in-out;
padding: 4px 8px;
border: none;
background-color: $primary-trans-color;
@@ -435,41 +465,62 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
cursor: pointer;
}
.send {}
.active {
background-color: $primary-color;
color: #fff;
}
}
.new-session {
position: absolute;
color: white;
margin-left: 10px;
margin-top: 10px;
background-color: $primary-color;
border-radius: 10px;
padding: 2px 4px;
width: fit-content;
background-color: transparent;
left: 50%;
transform: translateX(-50%);
bottom: 80px;
button{
height: fit-content;
button {
border-radius: 10px;
border: 1px solid #090909;
margin-bottom: 15px;
padding: 5px 10px;
background-color: transparent;
border: none;
svg {
color: #090909;
}
}
}
.chat-footer {
display: flex;
align-items: center;
padding: 10px 10px 0 10px;
justify-content: space-between;
padding: 5px 5px 0 5px;
//padding-bottom: constant(safe-area-inset-bottom);
//padding-bottom: env(safe-area-inset-bottom);
background-color: #fff;
// background-color: #fff;
.input-wrapper {
flex: 1;
margin-right: 10px;
// display: flex;
// align-items: center;
// justify-content: space-between;
display: grid;
grid-template-columns: 1fr 50px;
&>button {
// width: 50px;
color: $primary-color;
}
input {
width: 100%;
width: 110%;
padding: 10px;
border: none;
background: #f5f5f5;
background: #fff;
border-radius: 5px;
font-size: 14px;
}
@@ -504,11 +555,6 @@ $primary-trans-color: rgba(135, 162, 208, 0.5);
&.disabled {
cursor: not-allowed;
background: red;
}
&.active .bar {
animation: wave-animation 1s infinite ease-in-out;
}
.bar {