mirror of
https://github.com/jumpserver/lina.git
synced 2025-04-27 11:10:51 +00:00
perf: kael migrated koko
This commit is contained in:
parent
411051f921
commit
6540c0a165
@ -22,5 +22,6 @@ VUE_APP_LOGOUT_PATH = '/core/auth/logout/'
|
||||
# Dev server for core proxy
|
||||
VUE_APP_CORE_HOST = 'http://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'
|
||||
|
@ -1,5 +1,13 @@
|
||||
<template>
|
||||
<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">
|
||||
<el-input
|
||||
v-model="inputValue"
|
||||
@ -18,11 +26,12 @@
|
||||
<script>
|
||||
import { mapState } from 'vuex'
|
||||
import { useChat } from '../../useChat.js'
|
||||
import Select2 from '../../../../Form/FormFields/Select2.vue'
|
||||
|
||||
const { setLoading } = useChat()
|
||||
|
||||
export default {
|
||||
components: { },
|
||||
components: { Select2 },
|
||||
props: {
|
||||
expanded: {
|
||||
type: Boolean,
|
||||
|
@ -61,7 +61,7 @@ export default {
|
||||
return {
|
||||
socket: {},
|
||||
prompt: '',
|
||||
currentConversationId: '',
|
||||
conversationId: '',
|
||||
showIntroduction: false,
|
||||
introduction: [
|
||||
]
|
||||
@ -82,19 +82,19 @@ export default {
|
||||
this.initChatMessage()
|
||||
},
|
||||
initWebSocket() {
|
||||
const { NODE_ENV, VUE_APP_KAEL_HOST } = process.env || {}
|
||||
const api = '/kael/chat/system/'
|
||||
const { NODE_ENV, VUE_APP_KOKO_HOST } = process.env || {}
|
||||
const api = '/koko/ws/chat/system/'
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||
const path = `${protocol}://${window.location.host}${api}`
|
||||
const index = VUE_APP_KAEL_HOST?.indexOf('://')
|
||||
const localPath = protocol + VUE_APP_KAEL_HOST?.substring(index, VUE_APP_KAEL_HOST?.length) + api
|
||||
const index = VUE_APP_KOKO_HOST?.indexOf('://')
|
||||
const localPath = protocol + VUE_APP_KOKO_HOST?.substring(index, VUE_APP_KOKO_HOST?.length) + api
|
||||
const url = NODE_ENV === 'development' ? localPath : path
|
||||
createWebSocket(url, this.onWebSocketMessage)
|
||||
},
|
||||
initChatMessage() {
|
||||
this.prompt = ''
|
||||
this.showIntroduction = true
|
||||
this.currentConversationId = ''
|
||||
this.conversationId = ''
|
||||
this.$refs.chatInput.select.value = ''
|
||||
const chat = {
|
||||
message: {
|
||||
@ -115,10 +115,10 @@ export default {
|
||||
}
|
||||
},
|
||||
onChatMessage(data) {
|
||||
if (data.conversation_id) {
|
||||
if (data.id) {
|
||||
setLoading(true)
|
||||
removeLoadingMessageInChat()
|
||||
this.currentConversationId = data.conversation_id
|
||||
this.conversationId = data.id
|
||||
updateChaMessageContentById(data.message.id, data)
|
||||
}
|
||||
if (data.message?.type === 'finish') {
|
||||
@ -128,20 +128,18 @@ export default {
|
||||
},
|
||||
onSystemMessage(data) {
|
||||
data.message = {
|
||||
content: data.system_message,
|
||||
content: data.data,
|
||||
role: 'assistant',
|
||||
create_time: new Date()
|
||||
}
|
||||
removeLoadingMessageInChat()
|
||||
addMessageToActiveChat(data)
|
||||
this.socketReadyStateSuccess = false
|
||||
setLoading(true)
|
||||
},
|
||||
onSendHandle(value) {
|
||||
this.showIntroduction = false
|
||||
this.socket = ws || {}
|
||||
if (ws?.readyState === 1) {
|
||||
this.socketReadyStateSuccess = true
|
||||
const chat = {
|
||||
message: {
|
||||
content: value,
|
||||
@ -150,9 +148,9 @@ export default {
|
||||
}
|
||||
}
|
||||
const message = {
|
||||
content: value,
|
||||
data: value,
|
||||
prompt: this.prompt,
|
||||
conversation_id: this.currentConversationId || ''
|
||||
id: this.conversationId || ''
|
||||
}
|
||||
addChatMessageById(chat)
|
||||
onSend(message)
|
||||
@ -167,13 +165,12 @@ export default {
|
||||
type: 'error'
|
||||
}
|
||||
addChatMessageById(chat)
|
||||
this.socketReadyStateSuccess = false
|
||||
setLoading(true)
|
||||
}
|
||||
},
|
||||
onSelectPromptHandle(value) {
|
||||
this.prompt = value
|
||||
this.currentConversationId = ''
|
||||
this.conversationId = ''
|
||||
this.showIntroduction = false
|
||||
this.onSendHandle(value)
|
||||
},
|
||||
@ -182,13 +179,13 @@ export default {
|
||||
this.initChatMessage()
|
||||
},
|
||||
onStopHandle() {
|
||||
this.$axios.post(
|
||||
'/kael/interrupt_current_ask/',
|
||||
{ id: this.currentConversationId || '' }
|
||||
).finally(() => {
|
||||
removeLoadingMessageInChat()
|
||||
setLoading(false)
|
||||
})
|
||||
const message = {
|
||||
id: this.conversationId || '',
|
||||
interrupt: true
|
||||
}
|
||||
onSend(message)
|
||||
removeLoadingMessageInChat()
|
||||
setLoading(false)
|
||||
},
|
||||
sendIntroduction(item) {
|
||||
this.showIntroduction = false
|
||||
|
@ -1,12 +1,11 @@
|
||||
|
||||
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 timeoutNum = null
|
||||
let globalCallback = null
|
||||
let heartbeatInterval = null
|
||||
const timeout = 20 * 1000
|
||||
let lockReconnect = false
|
||||
|
||||
/**
|
||||
* @param {String} url
|
||||
@ -36,60 +35,41 @@ export function onSend(message) {
|
||||
|
||||
// 接受服务端消息
|
||||
export function onMessage(res) {
|
||||
const msgData = res.data
|
||||
if (typeof msgData !== 'object' && msgData !== 'Connect success' && msgData !== 'pong') {
|
||||
const { data: msgData } = res
|
||||
if (typeof msgData === 'object') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
let data = msgData.replace(/\ufeff/g, '')
|
||||
try {
|
||||
data = JSON.parse(data)
|
||||
globalCallback(data)
|
||||
} catch (error) {
|
||||
console.log('返回心跳')
|
||||
data = JSON.parse(data)
|
||||
if (data.type === 'PONG' || data.type === 'PING') {
|
||||
return
|
||||
}
|
||||
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() {
|
||||
timeoutObj && clearTimeout(timeoutObj)
|
||||
serverTimeoutObj && clearTimeout(serverTimeoutObj)
|
||||
timeoutObj = setTimeout(function() {
|
||||
if (ws?.readyState === 1) {
|
||||
ws.send('ping')
|
||||
} else {
|
||||
// reconnect()
|
||||
if (heartbeatInterval) clearInterval(heartbeatInterval)
|
||||
|
||||
heartbeatInterval = setInterval(() => {
|
||||
if (ws?.readyState === WebSocket.OPEN) {
|
||||
ws.send('{"type": "PING"}')
|
||||
}
|
||||
serverTimeoutObj = setTimeout(function() {
|
||||
// 连接超时
|
||||
// ws.close()
|
||||
}, timeout)
|
||||
}, timeout)
|
||||
}
|
||||
|
||||
// 重置心跳
|
||||
export function reset() {
|
||||
clearTimeout(timeoutObj)
|
||||
clearTimeout(serverTimeoutObj)
|
||||
if (heartbeatInterval) clearInterval(heartbeatInterval)
|
||||
start()
|
||||
}
|
||||
|
||||
// 重新连接
|
||||
export function reconnect() {
|
||||
if (lockReconnect) {
|
||||
return
|
||||
@ -102,3 +82,16 @@ export function reconnect() {
|
||||
lockReconnect = false
|
||||
}, 10000)
|
||||
}
|
||||
|
||||
export function onError() {
|
||||
reconnect()
|
||||
}
|
||||
|
||||
export function onClose() {
|
||||
}
|
||||
|
||||
export function closeWebSocket() {
|
||||
ws?.close()
|
||||
ws = null
|
||||
lockReconnect = false
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ module.exports = {
|
||||
ws: true
|
||||
},
|
||||
'/koko/': {
|
||||
target: 'http://127.0.0.1:5000',
|
||||
target: process.env.VUE_APP_KOKO_HOST,
|
||||
changeOrigin: true,
|
||||
ws: true
|
||||
},
|
||||
@ -74,11 +74,6 @@ module.exports = {
|
||||
target: 'http://127.0.0.1:4200',
|
||||
changeOrigin: true
|
||||
},
|
||||
'/kael/': {
|
||||
target: process.env.VUE_APP_KAEL_HOST,
|
||||
changeOrigin: true,
|
||||
ws: true
|
||||
},
|
||||
'^/(core|static|media)/': {
|
||||
target: process.env.VUE_APP_CORE_HOST,
|
||||
changeOrigin: true
|
||||
|
Loading…
Reference in New Issue
Block a user