mirror of
https://github.com/yangjian102621/geekai.git
synced 2026-05-10 11:44:28 +08:00
optimize regenerate for mobile chat page
This commit is contained in:
@@ -42,16 +42,15 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
import { processContent } from '@/utils/libs'
|
||||||
import { showImagePreview } from 'vant'
|
import Clipboard from 'clipboard'
|
||||||
import Thinking from '../Thinking.vue'
|
|
||||||
import hl from 'highlight.js'
|
import hl from 'highlight.js'
|
||||||
import MarkdownIt from 'markdown-it'
|
import MarkdownIt from 'markdown-it'
|
||||||
import emoji from 'markdown-it-emoji'
|
import emoji from 'markdown-it-emoji'
|
||||||
import mathjaxPlugin from 'markdown-it-mathjax3'
|
import mathjaxPlugin from 'markdown-it-mathjax3'
|
||||||
import { processContent } from '@/utils/libs'
|
import { showImagePreview, showNotify } from 'vant'
|
||||||
import Clipboard from 'clipboard'
|
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||||
import { showNotify } from 'vant'
|
import Thinking from '../Thinking.vue'
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
content: {
|
content: {
|
||||||
@@ -85,6 +84,10 @@ const props = defineProps({
|
|||||||
type: String,
|
type: String,
|
||||||
default: '',
|
default: '',
|
||||||
},
|
},
|
||||||
|
messageIndex: {
|
||||||
|
type: Number,
|
||||||
|
default: -1,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const emits = defineEmits(['regenerate'])
|
const emits = defineEmits(['regenerate'])
|
||||||
@@ -125,7 +128,7 @@ const contentRef = ref(null)
|
|||||||
|
|
||||||
// 处理重新生成
|
// 处理重新生成
|
||||||
const handleRegenerate = () => {
|
const handleRegenerate = () => {
|
||||||
emits('regenerate', props.messageId)
|
emits('regenerate', props.messageIndex)
|
||||||
}
|
}
|
||||||
|
|
||||||
const clipboard = ref(null)
|
const clipboard = ref(null)
|
||||||
|
|||||||
@@ -1178,7 +1178,7 @@ const reGenerate = function (messageIndex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (messageIndex === -1 || isNaN(messageIndex)) {
|
if (messageIndex === -1 || isNaN(messageIndex)) {
|
||||||
ElMessage.error('找不到要编辑的消息')
|
ElMessage.error('找不到要重新生成消息')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,11 +28,17 @@
|
|||||||
error-text="请求失败,点击重新加载"
|
error-text="请求失败,点击重新加载"
|
||||||
@load="onLoad"
|
@load="onLoad"
|
||||||
>
|
>
|
||||||
<van-cell v-for="item in chatData" :key="item" :border="false" class="message-line">
|
<van-cell
|
||||||
|
v-for="(item, index) in chatData"
|
||||||
|
:key="item"
|
||||||
|
:border="false"
|
||||||
|
class="message-line"
|
||||||
|
>
|
||||||
<chat-prompt
|
<chat-prompt
|
||||||
v-if="item.type === 'prompt'"
|
v-if="item.type === 'prompt'"
|
||||||
:content="item.content"
|
:content="item.content"
|
||||||
:icon="item.icon"
|
:icon="item.icon"
|
||||||
|
:message-index="index"
|
||||||
/>
|
/>
|
||||||
<chat-reply
|
<chat-reply
|
||||||
v-else-if="item.type === 'reply'"
|
v-else-if="item.type === 'reply'"
|
||||||
@@ -43,6 +49,7 @@
|
|||||||
:is-generating="isGenerating"
|
:is-generating="isGenerating"
|
||||||
:show-actions="item.showAction"
|
:show-actions="item.showAction"
|
||||||
:error="item.error"
|
:error="item.error"
|
||||||
|
:message-index="index"
|
||||||
@regenerate="handleRegenerate"
|
@regenerate="handleRegenerate"
|
||||||
/>
|
/>
|
||||||
</van-cell>
|
</van-cell>
|
||||||
@@ -62,7 +69,7 @@
|
|||||||
rows="1"
|
rows="1"
|
||||||
:autosize="{ maxHeight: 100, minHeight: 20 }"
|
:autosize="{ maxHeight: 100, minHeight: 20 }"
|
||||||
show-word-limit
|
show-word-limit
|
||||||
@keyup.enter="sendMessage"
|
@keyup.enter="sendMessage(0)"
|
||||||
>
|
>
|
||||||
<template #left-icon>
|
<template #left-icon>
|
||||||
<van-uploader
|
<van-uploader
|
||||||
@@ -76,7 +83,7 @@
|
|||||||
</van-uploader>
|
</van-uploader>
|
||||||
</template>
|
</template>
|
||||||
<template #button>
|
<template #button>
|
||||||
<van-button size="small" type="primary" v-if="!isGenerating" @click="sendMessage"
|
<van-button size="small" type="primary" v-if="!isGenerating" @click="sendMessage(0)"
|
||||||
>发送</van-button
|
>发送</van-button
|
||||||
>
|
>
|
||||||
</template>
|
</template>
|
||||||
@@ -115,20 +122,18 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import ChatPrompt from '@/components/mobile/ChatPrompt.vue'
|
import ChatPrompt from '@/components/mobile/ChatPrompt.vue'
|
||||||
import ChatReply from '@/components/mobile/ChatReply.vue'
|
import ChatReply from '@/components/mobile/ChatReply.vue'
|
||||||
import { checkSession } from '@/store/cache'
|
import MobileFileList from '@/components/mobile/MobileFileList.vue'
|
||||||
|
import { checkSession, getClientId } from '@/store/cache'
|
||||||
import { getUserToken } from '@/store/session'
|
import { getUserToken } from '@/store/session'
|
||||||
import { useSharedStore } from '@/store/sharedata'
|
import { useSharedStore } from '@/store/sharedata'
|
||||||
import { showMessageError, showLoading, closeLoading } from '@/utils/dialog'
|
import { closeLoading, showLoading, showMessageError } from '@/utils/dialog'
|
||||||
import { httpGet, httpPost } from '@/utils/http'
|
import { httpGet, httpPost } from '@/utils/http'
|
||||||
import MobileFileList from '@/components/mobile/MobileFileList.vue'
|
import { randString, renderInputText, UUID } from '@/utils/libs'
|
||||||
import { processContent, randString, renderInputText, UUID } from '@/utils/libs'
|
|
||||||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||||||
import hl from 'highlight.js'
|
|
||||||
import 'highlight.js/styles/a11y-dark.css'
|
import 'highlight.js/styles/a11y-dark.css'
|
||||||
import { showImagePreview, showNotify, showToast } from 'vant'
|
import { showImagePreview, showNotify, showToast } from 'vant'
|
||||||
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
|
import { nextTick, onMounted, ref, watch } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { getClientId } from '@/store/cache'
|
|
||||||
|
|
||||||
const winHeight = ref(0)
|
const winHeight = ref(0)
|
||||||
const navBarRef = ref(null)
|
const navBarRef = ref(null)
|
||||||
@@ -434,7 +439,7 @@ const sendSSERequest = async (message) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 发送消息
|
// 发送消息
|
||||||
const sendMessage = () => {
|
const sendMessage = (messageId = 0) => {
|
||||||
if (isGenerating.value) {
|
if (isGenerating.value) {
|
||||||
showToast('AI 正在作答中,请稍后...')
|
showToast('AI 正在作答中,请稍后...')
|
||||||
return
|
return
|
||||||
@@ -480,6 +485,7 @@ const sendMessage = () => {
|
|||||||
prompt: prompt.value,
|
prompt: prompt.value,
|
||||||
stream: stream.value,
|
stream: stream.value,
|
||||||
files: files.value,
|
files: files.value,
|
||||||
|
last_msg_id: messageId || 0,
|
||||||
})
|
})
|
||||||
|
|
||||||
previousText.value = prompt.value
|
previousText.value = prompt.value
|
||||||
@@ -504,47 +510,26 @@ const stopGenerate = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 处理从ChatReply组件触发的重新生成
|
// 处理从ChatReply组件触发的重新生成
|
||||||
const handleRegenerate = (messageId) => {
|
const handleRegenerate = (messageIndex) => {
|
||||||
if (isGenerating.value) {
|
if (isGenerating.value) {
|
||||||
showToast('AI 正在作答中,请稍后...')
|
showToast('AI 正在作答中,请稍后...')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('messageId', messageId)
|
if (messageIndex === -1 || isNaN(messageIndex)) {
|
||||||
console.log('chatData.value', chatData.value)
|
showToast('找不到要重新生成消息')
|
||||||
|
|
||||||
// 判断 messageId 是整数
|
|
||||||
if (messageId !== '' && isNaN(messageId)) {
|
|
||||||
showToast('消息 ID 不合法,无法重新生成')
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
chatData.value = chatData.value.filter((item) => item.id < messageId && !item.isHello)
|
// 找到该消息的ID
|
||||||
|
const messageId = chatData.value[messageIndex].id
|
||||||
|
// 移除该消息之后的所有消息
|
||||||
|
chatData.value = chatData.value.slice(0, messageIndex)
|
||||||
const userPrompt = chatData.value.pop()
|
const userPrompt = chatData.value.pop()
|
||||||
|
prompt.value = userPrompt.content.text
|
||||||
|
|
||||||
// 添加空回复消息
|
nextTick(() => {
|
||||||
const _role = getRoleById(roleId.value)
|
sendMessage(messageId)
|
||||||
chatData.value.push({
|
|
||||||
chat_id: chatId,
|
|
||||||
role_id: roleId.value,
|
|
||||||
type: 'reply',
|
|
||||||
id: randString(32),
|
|
||||||
icon: _role['icon'],
|
|
||||||
content: {
|
|
||||||
text: '',
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// 发送 SSE 请求
|
|
||||||
sendSSERequest({
|
|
||||||
user_id: loginUser.value.id,
|
|
||||||
role_id: roleId.value,
|
|
||||||
model_id: modelId.value,
|
|
||||||
chat_id: chatId.value,
|
|
||||||
last_msg_id: messageId,
|
|
||||||
prompt: userPrompt.content.text,
|
|
||||||
stream: stream.value,
|
|
||||||
files: [],
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user