feat: 对话sessionId
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// 每一个存储的模块,命名规则use开头,store结尾
|
||||
import { defineStore } from 'pinia'
|
||||
import MyEvent from '@/utils/myEvent'
|
||||
MyEvent.add('clear-generate-state', () => useGenerateStore().clearGenerateData())
|
||||
MyEvent.add('clear-generate-state', () => useGenerateStore().clearGenerateData())
|
||||
|
||||
export const useGenerateStore = defineStore({
|
||||
id: 'generate', // 必须指明唯一的pinia仓库的id
|
||||
@@ -11,7 +11,7 @@ export const useGenerateStore = defineStore({
|
||||
id: '',
|
||||
oldId: '' //表示从生成页面返回回来,需要调整的样式id
|
||||
},
|
||||
styleList: [{},{},{},{}],
|
||||
styleList: [{}, {}, {}, {}],
|
||||
model: {
|
||||
id: ''
|
||||
},
|
||||
@@ -32,9 +32,9 @@ export const useGenerateStore = defineStore({
|
||||
/** AI魔改信息 */
|
||||
customizeInfo: {
|
||||
inputText: '',
|
||||
count: 0,
|
||||
oldInputText: '',
|
||||
oldTryOnId: '',
|
||||
count: 0,
|
||||
oldInputText: '',
|
||||
oldTryOnId: '',
|
||||
|
||||
tryOnId: '',
|
||||
tryOnUrl: '',
|
||||
@@ -45,7 +45,8 @@ export const useGenerateStore = defineStore({
|
||||
customerInfo: {
|
||||
customerId: '',
|
||||
visitRecordId: ''
|
||||
}
|
||||
},
|
||||
sessionId: '' //会话id 秒级时间戳
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
@@ -87,7 +88,7 @@ export const useGenerateStore = defineStore({
|
||||
this.isGenerate = isGenerate
|
||||
},
|
||||
clearProductData() {
|
||||
this.styleList = [{},{},{},{}]
|
||||
this.styleList = [{}, {}, {}, {}]
|
||||
this.style = {
|
||||
id: '',
|
||||
oldId: ''
|
||||
@@ -111,8 +112,8 @@ export const useGenerateStore = defineStore({
|
||||
/** 清空 AI魔改信息 */
|
||||
clearCustomizeInfo() {
|
||||
this.customizeInfo.inputText = ''
|
||||
this.customizeInfo.count = 0
|
||||
this.customizeInfo.oldInputText = ''
|
||||
this.customizeInfo.count = 0
|
||||
this.customizeInfo.oldInputText = ''
|
||||
this.customizeInfo.oldTryOnId = ''
|
||||
this.customizeInfo.tryOnId = ''
|
||||
this.customizeInfo.tryOnUrl = ''
|
||||
@@ -120,12 +121,12 @@ export const useGenerateStore = defineStore({
|
||||
this.customizeInfo.isRegenerated = ''
|
||||
this.customizeInfo.isFavorite = false
|
||||
},
|
||||
uploadCustomizeInfo(data: object){
|
||||
for (const key in data) {
|
||||
this.customizeInfo[key] = data[key]
|
||||
}
|
||||
},
|
||||
clearCustomerInfo(){
|
||||
uploadCustomizeInfo(data: object) {
|
||||
for (const key in data) {
|
||||
this.customizeInfo[key] = data[key]
|
||||
}
|
||||
},
|
||||
clearCustomerInfo() {
|
||||
this.customerInfo = {
|
||||
customerId: '',
|
||||
visitRecordId: ''
|
||||
@@ -137,9 +138,13 @@ export const useGenerateStore = defineStore({
|
||||
this.updatePhotoInfo({})
|
||||
this.clearCustomizeInfo()
|
||||
this.clearCustomerInfo()
|
||||
this.setSessionId('')
|
||||
},
|
||||
setCustomerInfo(data: any) {
|
||||
this.customerInfo = data
|
||||
}
|
||||
},
|
||||
setSessionId(data: string) {
|
||||
this.sessionId = data
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -29,7 +29,7 @@ import HeaderTitle from '@/components/HeaderTitle.vue'
|
||||
import NoticeList from './components/NoticeList.vue'
|
||||
import InputArea from './components/InputArea.vue'
|
||||
import GenerateLoading from './components/GenerateLoading.vue'
|
||||
import { ref, onUnmounted, onActivated } from 'vue'
|
||||
import { ref, onMounted, onUnmounted, onActivated } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { useUserInfoStore, useGenerateStore } from '@/stores'
|
||||
import { streamChatAddress } from '@/api/workshop'
|
||||
@@ -66,6 +66,12 @@ const messageList = ref<ChatMessage[]>([])
|
||||
// 流式消息相关状态
|
||||
const isStreaming = ref<boolean>(false)
|
||||
const currentStreamingMessage = ref<ChatMessage | null>(null)
|
||||
const sessionId = ref<string>('')
|
||||
|
||||
onMounted(() => {
|
||||
sessionId.value = Math.floor(Date.now() / 1000).toString()
|
||||
generateStore.setSessionId(sessionId.value)
|
||||
})
|
||||
|
||||
onActivated(() => {
|
||||
noticeListRef.value?.scrollToBottom()
|
||||
@@ -84,7 +90,7 @@ const handleSendMessage = (message: string): void => {
|
||||
type: 'text',
|
||||
content: message,
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: Math.floor(Date.now() / 1000).toString(),
|
||||
sessionId: sessionId.value,
|
||||
self: true
|
||||
}
|
||||
messageList.value.push(userMessage)
|
||||
@@ -95,11 +101,9 @@ const handleSendMessage = (message: string): void => {
|
||||
|
||||
const abort = new AbortController()
|
||||
const handleFetchMessage = (message: string) => {
|
||||
const sessionId = Math.floor(Date.now() / 1000).toString()
|
||||
|
||||
const params = {
|
||||
message: message,
|
||||
sessionId: sessionId,
|
||||
sessionId: sessionId.value,
|
||||
gender: 'male'
|
||||
}
|
||||
|
||||
@@ -109,7 +113,7 @@ const handleFetchMessage = (message: string) => {
|
||||
type: 'text',
|
||||
content: '',
|
||||
timestamp: new Date().toISOString(),
|
||||
sessionId: sessionId
|
||||
sessionId: sessionId.value
|
||||
}
|
||||
|
||||
// 添加到消息列表
|
||||
@@ -140,7 +144,8 @@ const handleFetchMessage = (message: string) => {
|
||||
.then(async (response) => {
|
||||
// 检查响应内容类型,判断是否为流式响应
|
||||
const contentType = response.headers.get('content-type') || ''
|
||||
const isStreamResponse = contentType.includes('text/event-stream') || contentType.includes('stream')
|
||||
const isStreamResponse =
|
||||
contentType.includes('text/event-stream') || contentType.includes('stream')
|
||||
|
||||
if (!response.ok) {
|
||||
// 非流式错误响应,使用 text() 读取错误信息
|
||||
@@ -157,7 +162,7 @@ const handleFetchMessage = (message: string) => {
|
||||
// 不是流式响应,使用 text()读取错误信息
|
||||
if (!isStreamResponse) {
|
||||
const text = await response.text()
|
||||
|
||||
|
||||
try {
|
||||
const errorData = JSON.parse(text)
|
||||
if (errorData.message || errorData.error) {
|
||||
@@ -176,7 +181,7 @@ const handleFetchMessage = (message: string) => {
|
||||
})
|
||||
throw new Error(text || '请求失败')
|
||||
}
|
||||
|
||||
|
||||
isStreaming.value = false
|
||||
currentStreamingMessage.value = null
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user