优化聊天页面代码,刷新页面之后自动加载当前对话的 role_id 和 model_id

This commit is contained in:
RockYang 2024-09-05 14:50:37 +08:00
parent ae7f1c03e2
commit c755befa28
2 changed files with 55 additions and 53 deletions

View File

@ -543,9 +543,9 @@ func (h *ChatHandler) saveChatHistory(
} }
historyUserMsg.CreatedAt = promptCreatedAt historyUserMsg.CreatedAt = promptCreatedAt
historyUserMsg.UpdatedAt = promptCreatedAt historyUserMsg.UpdatedAt = promptCreatedAt
res := h.DB.Save(&historyUserMsg) err = h.DB.Save(&historyUserMsg).Error
if res.Error != nil { if err != nil {
logger.Error("failed to save prompt history message: ", res.Error) logger.Error("failed to save prompt history message: ", err)
} }
// for reply // for reply
@ -565,9 +565,9 @@ func (h *ChatHandler) saveChatHistory(
} }
historyReplyMsg.CreatedAt = replyCreatedAt historyReplyMsg.CreatedAt = replyCreatedAt
historyReplyMsg.UpdatedAt = replyCreatedAt historyReplyMsg.UpdatedAt = replyCreatedAt
res = h.DB.Create(&historyReplyMsg) err = h.DB.Create(&historyReplyMsg).Error
if res.Error != nil { if err != nil {
logger.Error("failed to save reply history message: ", res.Error) logger.Error("failed to save reply history message: ", err)
} }
// 更新用户算力 // 更新用户算力
@ -576,8 +576,8 @@ func (h *ChatHandler) saveChatHistory(
} }
// 保存当前会话 // 保存当前会话
var chatItem model.ChatItem var chatItem model.ChatItem
res = h.DB.Where("chat_id = ?", session.ChatId).First(&chatItem) err = h.DB.Where("chat_id = ?", session.ChatId).First(&chatItem).Error
if res.Error != nil { if err != nil {
chatItem.ChatId = session.ChatId chatItem.ChatId = session.ChatId
chatItem.UserId = userVo.Id chatItem.UserId = userVo.Id
chatItem.RoleId = role.Id chatItem.RoleId = role.Id
@ -588,7 +588,10 @@ func (h *ChatHandler) saveChatHistory(
chatItem.Title = prompt chatItem.Title = prompt
} }
chatItem.Model = req.Model chatItem.Model = req.Model
h.DB.Create(&chatItem) err = h.DB.Create(&chatItem).Error
if err != nil {
logger.Error("failed to save chat item: ", err)
}
} }
} }

View File

@ -272,10 +272,22 @@ const tools = ref([])
const toolSelected = ref([]) const toolSelected = ref([])
const loadHistory = ref(false) const loadHistory = ref(false)
// ID
if (router.currentRoute.value.query.role_id) {
roleId.value = parseInt(router.currentRoute.value.query.role_id)
}
// ChatID // ChatID
chatId.value = router.currentRoute.value.params.id chatId.value = router.currentRoute.value.params.id
if (!chatId.value) { if (!chatId.value) {
chatId.value = UUID() chatId.value = UUID()
}else { //
httpGet("/api/chat/detail", {chat_id: chatId.value}).then(res => {
roleId.value = res.data.role_id
modelID.value = res.data.model_id
}).catch(e => {
console.error("获取对话信息失败:"+e.message)
})
} }
if (isMobile()) { if (isMobile()) {
@ -344,59 +356,46 @@ onUnmounted(() => {
// //
const initData = () => { const initData = () => {
//
checkSession().then((user) => {
loginUser.value = user
isLogin.value = true
// //
httpGet("/api/chat/list").then((res) => { httpGet('/api/model/list').then(res => {
if (res.data) { models.value = res.data
chatList.value = res.data; if (!modelID.value) {
allChats.value = res.data;
}
if (router.currentRoute.value.query.role_id) {
roleId.value = parseInt(router.currentRoute.value.query.role_id)
}
//
httpGet('/api/model/list').then(res => {
models.value = res.data
modelID.value = models.value[0].id
//
httpGet(`/api/role/list`,{id:roleId.value}).then((res) => {
roles.value = res.data;
if (!roleId.value) {
roleId.value = roles.value[0]['id']
}
newChat();
}).catch((e) => {
ElMessage.error('获取聊天角色失败: ' + e.messages)
})
}).catch(e => {
ElMessage.error("加载模型失败: " + e.message)
})
}).catch(() => {
ElMessage.error("加载会话列表失败!")
})
}).catch(() => {
//
httpGet('/api/model/list',{id:roleId.value}).then(res => {
models.value = res.data
modelID.value = models.value[0].id modelID.value = models.value[0].id
}).catch(e => { }
ElMessage.error("加载模型失败: " + e.message)
})
// //
httpGet(`/api/role/list`).then((res) => { httpGet(`/api/role/list`,{id:roleId.value}).then((res) => {
roles.value = res.data; roles.value = res.data;
roleId.value = roles.value[0]['id']; if (!roleId.value) {
roleId.value = roles.value[0]['id']
}
//
checkSession().then((user) => {
loginUser.value = user
isLogin.value = true
newChat();
})
}).catch((e) => { }).catch((e) => {
ElMessage.error('获取聊天角色失败: ' + e.messages) ElMessage.error('获取聊天角色失败: ' + e.messages)
}) })
}).catch(e => {
ElMessage.error("加载模型失败: " + e.message)
}) })
//
httpGet("/api/chat/list").then((res) => {
if (res.data) {
chatList.value = res.data;
allChats.value = res.data;
}
}).catch(() => {
ElMessage.error("加载会话列表失败!")
})
//
inputRef.value.addEventListener('paste', (event) => { inputRef.value.addEventListener('paste', (event) => {
const items = (event.clipboardData || window.clipboardData).items; const items = (event.clipboardData || window.clipboardData).items;
let fileFound = false; let fileFound = false;