feat: 对话的调整
This commit is contained in:
@@ -86,7 +86,7 @@ export const getSessionMessages = async (params: SessionMessagesRequest) => {
|
||||
|
||||
|
||||
|
||||
/// PATCH /api/sessions/{session_id} 的请求参数和响应数据结构
|
||||
/// /api/sessions/{session_id} 的请求参数和响应数据结构
|
||||
export interface UpdateSessionRequest {
|
||||
session_id: string
|
||||
title: string
|
||||
@@ -98,7 +98,7 @@ export interface UpdateSessionResponse {
|
||||
|
||||
// 更新会话信息 的函数实现
|
||||
export const updateSession = async (params: UpdateSessionRequest) => {
|
||||
const res: ResponseModel = await patchRequest(`/nianxx/api/sessions/${params.session_id}`, {
|
||||
const res: ResponseModel = await postRequest(`/nianxx/api/sessions/${params.session_id}/rename`, {
|
||||
title: params.title,
|
||||
})
|
||||
return res.data as UpdateSessionResponse
|
||||
@@ -106,7 +106,7 @@ export const updateSession = async (params: UpdateSessionRequest) => {
|
||||
|
||||
|
||||
|
||||
/// DELETE /api/sessions/{session_id} 的请求参数和响应数据结构
|
||||
/// /api/sessions/{session_id} 的请求参数和响应数据结构
|
||||
export interface DeleteSessionRequest {
|
||||
session_id: string
|
||||
}
|
||||
@@ -117,6 +117,6 @@ export interface DeleteSessionResponse {
|
||||
|
||||
// 删除会话 的函数实现
|
||||
export const deleteSession = async (params: DeleteSessionRequest) => {
|
||||
const res: ResponseModel = await deleteRequest(`/nianxx/api/sessions/${params.session_id}`)
|
||||
const res: ResponseModel = await postRequest(`/nianxx/api/sessions/${params.session_id}/delete`, {})
|
||||
return res.data as DeleteSessionResponse
|
||||
}
|
||||
@@ -110,8 +110,8 @@ watch(isGuidePage, (v) => {
|
||||
emit('update:guide', v);
|
||||
if (v) {
|
||||
// 当切换到引导页时,重置/清理会话状态
|
||||
conversationId.value = '';
|
||||
resetConversation();
|
||||
createConversationRequest();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -129,11 +129,15 @@ const isSendingMessage = ref(false);
|
||||
const agentId = ref("1953462165250859011");
|
||||
/// 会话ID 历史数据接口中获取
|
||||
const conversationId = ref(props.conversationId);
|
||||
// 标记 conversationId 是否来自历史消息(由 props.conversationId 提供)
|
||||
const conversationIdFromHistory = ref(!!props.conversationId);
|
||||
|
||||
// 监听 conversationId prop 变化,只有当有值时(选择历史消息)才请求消息列表
|
||||
watch(() => props.conversationId, (newId) => {
|
||||
if (newId) {
|
||||
conversationId.value = newId;
|
||||
console.log("外部 conversationId 变化,加载对应消息:", newId);
|
||||
conversationIdFromHistory.value = true;
|
||||
loadConversationMessages(newId);
|
||||
}
|
||||
});
|
||||
@@ -278,7 +282,6 @@ const initHandler = async () => {
|
||||
console.log("initHandler:检查 token 并初始化数据");
|
||||
const token = getAccessToken();
|
||||
if (!token) return;
|
||||
await createConversationRequest();
|
||||
await initWebSocket();
|
||||
};
|
||||
|
||||
@@ -299,6 +302,8 @@ const createConversationRequest = async (): Promise<string | null> => {
|
||||
const res = await createSession({});
|
||||
if (res && res.session_id) {
|
||||
conversationId.value = res.session_id;
|
||||
// 新创建的 session 不是来源于历史
|
||||
conversationIdFromHistory.value = false;
|
||||
console.log("创建新会话,ID:", conversationId.value);
|
||||
return res.session_id;
|
||||
} else {
|
||||
@@ -538,6 +543,16 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
|
||||
|
||||
await checkToken();
|
||||
|
||||
// 如果没有 conversationId(且非历史来源),在发送时按需创建会话
|
||||
if (!conversationId.value) {
|
||||
const sid = await createConversationRequest();
|
||||
if (!sid) {
|
||||
ElMessage({ message: '创建会话失败,请稍后重试', type: 'error' });
|
||||
console.error('createConversationRequest failed before send');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查WebSocket连接状态,如果未连接,尝试重新连接
|
||||
if (!isWsConnected()) {
|
||||
console.log("WebSocket未连接,尝试重新连接...");
|
||||
@@ -575,6 +590,7 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
|
||||
messageId: IdUtils.generateMessageId(),
|
||||
messageRole: MessageRole.ME,
|
||||
messageContent: message,
|
||||
messageContentList: [],
|
||||
timestamp: Date.now(),
|
||||
};
|
||||
chatMsgList.value.push(newMsg);
|
||||
@@ -709,6 +725,8 @@ const sendChat = async (message: string, isInstruct = false) => {
|
||||
messageId: currentSessionMessageId,
|
||||
messageRole: MessageRole.AI,
|
||||
messageContent: "加载中",
|
||||
messageContentList: [],
|
||||
timestamp: Date.now(),
|
||||
isLoading: true,
|
||||
};
|
||||
|
||||
@@ -846,6 +864,10 @@ const resetConversation = () => {
|
||||
pendingTimeouts.clear();
|
||||
pendingMap.clear();
|
||||
|
||||
// 如果 conversationId 不是来自历史,重置 conversationId
|
||||
if (!conversationIdFromHistory.value) {
|
||||
conversationId.value = '';
|
||||
}
|
||||
// 清理消息与状态
|
||||
chatMsgList.value = [];
|
||||
inputMessage.value = '';
|
||||
|
||||
@@ -157,10 +157,11 @@ onMounted(() => {
|
||||
const getHistoryConversationList = async () => {
|
||||
const list = await getSessionList({ limit: 50, offset: 0 })
|
||||
if (!list || !list.sessions) return;
|
||||
groups.value.push(...list.sessions.map((item: any) => ({
|
||||
// 使用整体赋值替换 push,避免重复累加
|
||||
groups.value = list.sessions.map((item: any) => ({
|
||||
conversationId: item.session_id,
|
||||
conversationTitle: item.title
|
||||
})))
|
||||
}))
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
<template>
|
||||
<div class="max-w-[75%] flex flex-col">
|
||||
<slot name="header"></slot>
|
||||
<div v-if="!msg.messageContentList" class="flex flex-row text-sm text-gray-700">
|
||||
<div v-if="!msg.messageContentList || msg.messageContentList.length === 0"
|
||||
class="flex flex-row text-sm text-gray-700">
|
||||
<div v-html="compiledMarkdown"></div>
|
||||
<ChatLoading v-if="msg.isLoading" />
|
||||
</div>
|
||||
|
||||
<div v-else class="flex flex-col p-2 mb-2 text-sm text-gray-700 bg-[#f7f9fc] rounded-md"
|
||||
v-for="(_, index) in msg.messageContentList" :key="index">
|
||||
<div v-else class="flex flex-col p-2 mb-2 text-sm text-gray-700 bg-[#f7f9fc] rounded-md"
|
||||
v-for="(_, index) in msg.messageContentList" :key="index">
|
||||
<div v-html="compiledAt(index)"></div>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<layout>
|
||||
<div class="flex h-full w-full flex-col md:flex-row">
|
||||
<ChatHistory class="flex-none w-50" @new-chat="guide = true" @select-chat="handleSelectChat" />
|
||||
<ChatHistory class="flex-none w-50" @new-chat="handleNewChat" @select-chat="handleSelectChat" />
|
||||
<div class="flex-1 mr-2 overflow-hidden bg-white rounded-xl">
|
||||
<ChatBox v-model:guide="guide" :conversationId="selectedConversationId" />
|
||||
</div>
|
||||
@@ -20,6 +20,12 @@ const guide = ref(true)
|
||||
/// 选择的历史会话ID
|
||||
const selectedConversationId = ref('')
|
||||
|
||||
/// 处理新对话事件:切换到引导页并清空选中的历史会话ID
|
||||
const handleNewChat = () => {
|
||||
guide.value = true;
|
||||
selectedConversationId.value = '';
|
||||
};
|
||||
|
||||
/// 选择历史会话
|
||||
const handleSelectChat = (conversationId: string) => {
|
||||
guide.value = false;
|
||||
|
||||
Reference in New Issue
Block a user