Refactor(AI):支持短时间内多个会话

This commit is contained in:
lifizer
2025-04-23 17:34:19 +08:00
parent 58b716c0ec
commit 74b02ed292
3 changed files with 40 additions and 34 deletions
+36 -32
View File
@@ -1,6 +1,6 @@
import { import {
getSseConfigV1, getSseConfigV1,
getCompletions, getCompletionsV2,
deleteChatItemByConversationId deleteChatItemByConversationId
} from '@/api/chat/index' } from '@/api/chat/index'
import { VUE_APP_API_URL } from '@/config' import { VUE_APP_API_URL } from '@/config'
@@ -184,39 +184,43 @@ export const chatMixins = {
type: -2 type: -2
}) })
_this.loading = true _this.loading = true
_this.initWebSocket(() => {
_this.showCursor = false _this.showCursor = false
getSseConfigV1({ getSseConfigV1({
'chatNumber': _this.chatNumber, 'chatNumber': _this.chatNumber,
prompt prompt
}).then(resConfig => { }).then(resConfig => {
const conversationId = resConfig.data.conversationId const conversationId = resConfig.data.conversationId
_this.chatNumber = resConfig.data.chatNumber || '' _this.chatNumber = resConfig.data.chatNumber || ''
_this.chatMessageList[_this.chatMessageListLength - 1].showCursor = true _this.chatMessageList[_this.chatMessageListLength - 1].showCursor = true
_this.chatMessageList[_this.chatMessageListLength - 1].conversationId = conversationId _this.chatMessageList[_this.chatMessageListLength - 1].conversationId = conversationId
getCompletions({ if (conversationId) {
conversationId, _this.initWebSocket(conversationId, () => {
ws: true, getCompletionsV2({
isNoNeedPublicErrorNotification: 1 conversationId,
}).then(sseRes => { ws: true,
console.log(sseRes) isNoNeedPublicErrorNotification: 1
// _this.closeWsFn() }).then(sseRes => {
}).catch(err => { console.log(sseRes)
console.log(err) // _this.closeWsFn()
_this.chatMessageList[_this.chatMessageListLength - 1].isError = true }).catch(err => {
_this.chatMessageList[_this.chatMessageListLength - 1].content = _this.errorMsg console.log(err)
_this.chatMessageList[_this.chatMessageListLength - 1].nodes = _this.errorMsg _this.chatErrorSetContent()
_this.closeWsFn() })
}) })
}).catch(err => { } else {
console.log(err) _this.chatErrorSetContent()
_this.chatMessageList[_this.chatMessageListLength - 1].isError = true }
_this.chatMessageList[_this.chatMessageListLength - 1].content = _this.errorMsg }).catch(err => {
_this.chatMessageList[_this.chatMessageListLength - 1].nodes = _this.errorMsg console.log(err)
_this.closeWsFn() _this.chatErrorSetContent()
})
}) })
}, },
chatErrorSetContent() {
this.chatMessageList[this.chatMessageListLength - 1].isError = true
this.chatMessageList[this.chatMessageListLength - 1].content = this.errorMsg
this.chatMessageList[this.chatMessageListLength - 1].nodes = this.errorMsg
this.closeWsFn()
},
closeWsFn() { closeWsFn() {
this.loading = false this.loading = false
if (this.chatMessageListLength) { if (this.chatMessageListLength) {
@@ -231,11 +235,11 @@ export const chatMixins = {
this.scrollToBottomHandle() this.scrollToBottomHandle()
}) })
}, },
initWebSocket(cb) { initWebSocket(conversationId, cb) {
const _this = this const _this = this
console.log('------------------创建连接') console.log('------------------创建连接')
uni.connectSocket({ uni.connectSocket({
url: VUE_APP_API_URL.replace('https', 'wss') + settings.sysPrefix + `v1/chat/websocket/${_this.userInfo.unionId}`, url: VUE_APP_API_URL.replace('https', 'wss') + settings.sysPrefix + `v1/chat/websocket/${_this.userInfo.unionId}-${conversationId}`,
header: { header: {
'content-type': 'application/json' 'content-type': 'application/json'
}, },
-2
View File
@@ -296,7 +296,6 @@ export default {
this.chatMessageList = [] this.chatMessageList = []
}, },
clickBackHandle() { clickBackHandle() {
console.log(1)
if (!this.chatNumber) { if (!this.chatNumber) {
if (getCurrentPages().length > 1) { if (getCurrentPages().length > 1) {
uni.navigateBack() uni.navigateBack()
@@ -310,7 +309,6 @@ export default {
} }
}, },
pageContainerBeforeleave() { pageContainerBeforeleave() {
console.log(22222)
if (!this.chatNumber) { if (!this.chatNumber) {
if (getCurrentPages().length > 1) { if (getCurrentPages().length > 1) {
uni.navigateBack() uni.navigateBack()
+4
View File
@@ -8,6 +8,10 @@ export function getCompletions(params) {
return request.get('/v1/chat/sse/completions', params, { login: true }) return request.get('/v1/chat/sse/completions', params, { login: true })
} }
export function getCompletionsV2(params) {
return request.get('/v1/chat/sse/completions2', params, { login: true })
}
export function deleteChatItemByConversationId(id) { export function deleteChatItemByConversationId(id) {
return request.delete('/v1/chat/message/' + id, {}, { login: true }) return request.delete('/v1/chat/message/' + id, {}, { login: true })
} }