perf: kael migrated koko

This commit is contained in:
feng 2024-04-25 17:58:15 +08:00
parent 411051f921
commit 6540c0a165
6 changed files with 10663 additions and 11341 deletions

View File

@ -22,5 +22,6 @@ VUE_APP_LOGOUT_PATH = '/core/auth/logout/'
# Dev server for core proxy # Dev server for core proxy
VUE_APP_CORE_HOST = 'http://localhost:8080' VUE_APP_CORE_HOST = 'http://localhost:8080'
VUE_APP_CORE_WS = 'ws://localhost:8080' VUE_APP_CORE_WS = 'ws://localhost:8080'
VUE_APP_KAEL_HOST = 'http://localhost:8083' VUE_APP_KOKO_HOST = 'http://localhost:5000'
VUE_APP_KOKO_WS = 'ws://localhost:5000'
VUE_APP_ENV = 'development' VUE_APP_ENV = 'development'

View File

@ -1,5 +1,13 @@
<template> <template>
<div class="container"> <div class="container">
<div class="chat-action">
<Select2
v-model="select.value"
:disabled="isLoading || isSelectDisabled"
v-bind="select"
@change="onSelectChange"
/>
</div>
<div class="chat-input"> <div class="chat-input">
<el-input <el-input
v-model="inputValue" v-model="inputValue"
@ -18,11 +26,12 @@
<script> <script>
import { mapState } from 'vuex' import { mapState } from 'vuex'
import { useChat } from '../../useChat.js' import { useChat } from '../../useChat.js'
import Select2 from '../../../../Form/FormFields/Select2.vue'
const { setLoading } = useChat() const { setLoading } = useChat()
export default { export default {
components: { }, components: { Select2 },
props: { props: {
expanded: { expanded: {
type: Boolean, type: Boolean,

View File

@ -61,7 +61,7 @@ export default {
return { return {
socket: {}, socket: {},
prompt: '', prompt: '',
currentConversationId: '', conversationId: '',
showIntroduction: false, showIntroduction: false,
introduction: [ introduction: [
] ]
@ -82,19 +82,19 @@ export default {
this.initChatMessage() this.initChatMessage()
}, },
initWebSocket() { initWebSocket() {
const { NODE_ENV, VUE_APP_KAEL_HOST } = process.env || {} const { NODE_ENV, VUE_APP_KOKO_HOST } = process.env || {}
const api = '/kael/chat/system/' const api = '/koko/ws/chat/system/'
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws' const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
const path = `${protocol}://${window.location.host}${api}` const path = `${protocol}://${window.location.host}${api}`
const index = VUE_APP_KAEL_HOST?.indexOf('://') const index = VUE_APP_KOKO_HOST?.indexOf('://')
const localPath = protocol + VUE_APP_KAEL_HOST?.substring(index, VUE_APP_KAEL_HOST?.length) + api const localPath = protocol + VUE_APP_KOKO_HOST?.substring(index, VUE_APP_KOKO_HOST?.length) + api
const url = NODE_ENV === 'development' ? localPath : path const url = NODE_ENV === 'development' ? localPath : path
createWebSocket(url, this.onWebSocketMessage) createWebSocket(url, this.onWebSocketMessage)
}, },
initChatMessage() { initChatMessage() {
this.prompt = '' this.prompt = ''
this.showIntroduction = true this.showIntroduction = true
this.currentConversationId = '' this.conversationId = ''
this.$refs.chatInput.select.value = '' this.$refs.chatInput.select.value = ''
const chat = { const chat = {
message: { message: {
@ -115,10 +115,10 @@ export default {
} }
}, },
onChatMessage(data) { onChatMessage(data) {
if (data.conversation_id) { if (data.id) {
setLoading(true) setLoading(true)
removeLoadingMessageInChat() removeLoadingMessageInChat()
this.currentConversationId = data.conversation_id this.conversationId = data.id
updateChaMessageContentById(data.message.id, data) updateChaMessageContentById(data.message.id, data)
} }
if (data.message?.type === 'finish') { if (data.message?.type === 'finish') {
@ -128,20 +128,18 @@ export default {
}, },
onSystemMessage(data) { onSystemMessage(data) {
data.message = { data.message = {
content: data.system_message, content: data.data,
role: 'assistant', role: 'assistant',
create_time: new Date() create_time: new Date()
} }
removeLoadingMessageInChat() removeLoadingMessageInChat()
addMessageToActiveChat(data) addMessageToActiveChat(data)
this.socketReadyStateSuccess = false
setLoading(true) setLoading(true)
}, },
onSendHandle(value) { onSendHandle(value) {
this.showIntroduction = false this.showIntroduction = false
this.socket = ws || {} this.socket = ws || {}
if (ws?.readyState === 1) { if (ws?.readyState === 1) {
this.socketReadyStateSuccess = true
const chat = { const chat = {
message: { message: {
content: value, content: value,
@ -150,9 +148,9 @@ export default {
} }
} }
const message = { const message = {
content: value, data: value,
prompt: this.prompt, prompt: this.prompt,
conversation_id: this.currentConversationId || '' id: this.conversationId || ''
} }
addChatMessageById(chat) addChatMessageById(chat)
onSend(message) onSend(message)
@ -167,13 +165,12 @@ export default {
type: 'error' type: 'error'
} }
addChatMessageById(chat) addChatMessageById(chat)
this.socketReadyStateSuccess = false
setLoading(true) setLoading(true)
} }
}, },
onSelectPromptHandle(value) { onSelectPromptHandle(value) {
this.prompt = value this.prompt = value
this.currentConversationId = '' this.conversationId = ''
this.showIntroduction = false this.showIntroduction = false
this.onSendHandle(value) this.onSendHandle(value)
}, },
@ -182,13 +179,13 @@ export default {
this.initChatMessage() this.initChatMessage()
}, },
onStopHandle() { onStopHandle() {
this.$axios.post( const message = {
'/kael/interrupt_current_ask/', id: this.conversationId || '',
{ id: this.currentConversationId || '' } interrupt: true
).finally(() => { }
removeLoadingMessageInChat() onSend(message)
setLoading(false) removeLoadingMessageInChat()
}) setLoading(false)
}, },
sendIntroduction(item) { sendIntroduction(item) {
this.showIntroduction = false this.showIntroduction = false

View File

@ -1,12 +1,11 @@
export let ws = null export let ws = null
let lockReconnect = false
const timeout = 10 * 1000
let timeoutObj = null // 心跳心跳倒计时
let serverTimeoutObj = null // 心跳倒计时
let timeoutNum = null // 断开、重连倒计时
let globalCallback = null // 监听服务端消息
let globalUrl = null let globalUrl = null
let timeoutNum = null
let globalCallback = null
let heartbeatInterval = null
const timeout = 20 * 1000
let lockReconnect = false
/** /**
* @param {String} url * @param {String} url
@ -36,60 +35,41 @@ export function onSend(message) {
// 接受服务端消息 // 接受服务端消息
export function onMessage(res) { export function onMessage(res) {
const msgData = res.data const { data: msgData } = res
if (typeof msgData !== 'object' && msgData !== 'Connect success' && msgData !== 'pong') { if (typeof msgData === 'object') {
return
}
try {
let data = msgData.replace(/\ufeff/g, '') let data = msgData.replace(/\ufeff/g, '')
try { data = JSON.parse(data)
data = JSON.parse(data) if (data.type === 'PONG' || data.type === 'PING') {
globalCallback(data) return
} catch (error) {
console.log('返回心跳')
} }
reset() data.message = JSON.parse(data.data)
if (globalCallback) {
globalCallback(data)
}
} catch (error) {
this.$log.error('socket onMessage error', error)
} }
} }
// 连接失败
export function onError() {
reconnect()
}
// 连接关闭
export function onClose() {
}
// 断开关闭
export function closeWebSocket() {
ws?.close()
ws = null
lockReconnect = false
}
// 发送心跳
export function start() { export function start() {
timeoutObj && clearTimeout(timeoutObj) if (heartbeatInterval) clearInterval(heartbeatInterval)
serverTimeoutObj && clearTimeout(serverTimeoutObj)
timeoutObj = setTimeout(function() { heartbeatInterval = setInterval(() => {
if (ws?.readyState === 1) { if (ws?.readyState === WebSocket.OPEN) {
ws.send('ping') ws.send('{"type": "PING"}')
} else {
// reconnect()
} }
serverTimeoutObj = setTimeout(function() {
// 连接超时
// ws.close()
}, timeout)
}, timeout) }, timeout)
} }
// 重置心跳
export function reset() { export function reset() {
clearTimeout(timeoutObj) if (heartbeatInterval) clearInterval(heartbeatInterval)
clearTimeout(serverTimeoutObj)
start() start()
} }
// 重新连接
export function reconnect() { export function reconnect() {
if (lockReconnect) { if (lockReconnect) {
return return
@ -102,3 +82,16 @@ export function reconnect() {
lockReconnect = false lockReconnect = false
}, 10000) }, 10000)
} }
export function onError() {
reconnect()
}
export function onClose() {
}
export function closeWebSocket() {
ws?.close()
ws = null
lockReconnect = false
}

View File

@ -56,7 +56,7 @@ module.exports = {
ws: true ws: true
}, },
'/koko/': { '/koko/': {
target: 'http://127.0.0.1:5000', target: process.env.VUE_APP_KOKO_HOST,
changeOrigin: true, changeOrigin: true,
ws: true ws: true
}, },
@ -74,11 +74,6 @@ module.exports = {
target: 'http://127.0.0.1:4200', target: 'http://127.0.0.1:4200',
changeOrigin: true changeOrigin: true
}, },
'/kael/': {
target: process.env.VUE_APP_KAEL_HOST,
changeOrigin: true,
ws: true
},
'^/(core|static|media)/': { '^/(core|static|media)/': {
target: process.env.VUE_APP_CORE_HOST, target: process.env.VUE_APP_CORE_HOST,
changeOrigin: true changeOrigin: true

21857
yarn.lock

File diff suppressed because it is too large Load Diff