feat: 对话的调整

This commit is contained in:
zoujing
2026-03-06 14:41:59 +08:00
parent ed04eea481
commit 3fc26d6996
5 changed files with 42 additions and 12 deletions

View File

@@ -86,7 +86,7 @@ export const getSessionMessages = async (params: SessionMessagesRequest) => {
/// PATCH /api/sessions/{session_id} 的请求参数和响应数据结构 /// /api/sessions/{session_id} 的请求参数和响应数据结构
export interface UpdateSessionRequest { export interface UpdateSessionRequest {
session_id: string session_id: string
title: string title: string
@@ -98,7 +98,7 @@ export interface UpdateSessionResponse {
// 更新会话信息 的函数实现 // 更新会话信息 的函数实现
export const updateSession = async (params: UpdateSessionRequest) => { 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, title: params.title,
}) })
return res.data as UpdateSessionResponse 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 { export interface DeleteSessionRequest {
session_id: string session_id: string
} }
@@ -117,6 +117,6 @@ export interface DeleteSessionResponse {
// 删除会话 的函数实现 // 删除会话 的函数实现
export const deleteSession = async (params: DeleteSessionRequest) => { 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 return res.data as DeleteSessionResponse
} }

View File

@@ -110,8 +110,8 @@ watch(isGuidePage, (v) => {
emit('update:guide', v); emit('update:guide', v);
if (v) { if (v) {
// 当切换到引导页时,重置/清理会话状态 // 当切换到引导页时,重置/清理会话状态
conversationId.value = '';
resetConversation(); resetConversation();
createConversationRequest();
} }
}); });
@@ -129,11 +129,15 @@ const isSendingMessage = ref(false);
const agentId = ref("1953462165250859011"); const agentId = ref("1953462165250859011");
/// 会话ID 历史数据接口中获取 /// 会话ID 历史数据接口中获取
const conversationId = ref(props.conversationId); const conversationId = ref(props.conversationId);
// 标记 conversationId 是否来自历史消息(由 props.conversationId 提供)
const conversationIdFromHistory = ref(!!props.conversationId);
// 监听 conversationId prop 变化,只有当有值时(选择历史消息)才请求消息列表 // 监听 conversationId prop 变化,只有当有值时(选择历史消息)才请求消息列表
watch(() => props.conversationId, (newId) => { watch(() => props.conversationId, (newId) => {
if (newId) { if (newId) {
conversationId.value = newId; conversationId.value = newId;
console.log("外部 conversationId 变化,加载对应消息:", newId);
conversationIdFromHistory.value = true;
loadConversationMessages(newId); loadConversationMessages(newId);
} }
}); });
@@ -278,7 +282,6 @@ const initHandler = async () => {
console.log("initHandler检查 token 并初始化数据"); console.log("initHandler检查 token 并初始化数据");
const token = getAccessToken(); const token = getAccessToken();
if (!token) return; if (!token) return;
await createConversationRequest();
await initWebSocket(); await initWebSocket();
}; };
@@ -299,6 +302,8 @@ const createConversationRequest = async (): Promise<string | null> => {
const res = await createSession({}); const res = await createSession({});
if (res && res.session_id) { if (res && res.session_id) {
conversationId.value = res.session_id; conversationId.value = res.session_id;
// 新创建的 session 不是来源于历史
conversationIdFromHistory.value = false;
console.log("创建新会话ID:", conversationId.value); console.log("创建新会话ID:", conversationId.value);
return res.session_id; return res.session_id;
} else { } else {
@@ -538,6 +543,16 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
await checkToken(); await checkToken();
// 如果没有 conversationId且非历史来源在发送时按需创建会话
if (!conversationId.value) {
const sid = await createConversationRequest();
if (!sid) {
ElMessage({ message: '创建会话失败,请稍后重试', type: 'error' });
console.error('createConversationRequest failed before send');
return;
}
}
// 检查WebSocket连接状态如果未连接尝试重新连接 // 检查WebSocket连接状态如果未连接尝试重新连接
if (!isWsConnected()) { if (!isWsConnected()) {
console.log("WebSocket未连接尝试重新连接..."); console.log("WebSocket未连接尝试重新连接...");
@@ -575,6 +590,7 @@ const sendMessage = async (message: string, isInstruct: boolean = false) => {
messageId: IdUtils.generateMessageId(), messageId: IdUtils.generateMessageId(),
messageRole: MessageRole.ME, messageRole: MessageRole.ME,
messageContent: message, messageContent: message,
messageContentList: [],
timestamp: Date.now(), timestamp: Date.now(),
}; };
chatMsgList.value.push(newMsg); chatMsgList.value.push(newMsg);
@@ -709,6 +725,8 @@ const sendChat = async (message: string, isInstruct = false) => {
messageId: currentSessionMessageId, messageId: currentSessionMessageId,
messageRole: MessageRole.AI, messageRole: MessageRole.AI,
messageContent: "加载中", messageContent: "加载中",
messageContentList: [],
timestamp: Date.now(),
isLoading: true, isLoading: true,
}; };
@@ -846,6 +864,10 @@ const resetConversation = () => {
pendingTimeouts.clear(); pendingTimeouts.clear();
pendingMap.clear(); pendingMap.clear();
// 如果 conversationId 不是来自历史,重置 conversationId
if (!conversationIdFromHistory.value) {
conversationId.value = '';
}
// 清理消息与状态 // 清理消息与状态
chatMsgList.value = []; chatMsgList.value = [];
inputMessage.value = ''; inputMessage.value = '';

View File

@@ -157,10 +157,11 @@ onMounted(() => {
const getHistoryConversationList = async () => { const getHistoryConversationList = async () => {
const list = await getSessionList({ limit: 50, offset: 0 }) const list = await getSessionList({ limit: 50, offset: 0 })
if (!list || !list.sessions) return; 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, conversationId: item.session_id,
conversationTitle: item.title conversationTitle: item.title
}))) }))
} }
</script> </script>

View File

@@ -1,13 +1,14 @@
<template> <template>
<div class="max-w-[75%] flex flex-col"> <div class="max-w-[75%] flex flex-col">
<slot name="header"></slot> <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> <div v-html="compiledMarkdown"></div>
<ChatLoading v-if="msg.isLoading" /> <ChatLoading v-if="msg.isLoading" />
</div> </div>
<div v-else class="flex flex-col p-2 mb-2 text-sm text-gray-700 bg-[#f7f9fc] rounded-md" <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"> v-for="(_, index) in msg.messageContentList" :key="index">
<div v-html="compiledAt(index)"></div> <div v-html="compiledAt(index)"></div>
</div> </div>

View File

@@ -1,7 +1,7 @@
<template> <template>
<layout> <layout>
<div class="flex h-full w-full flex-col md:flex-row"> <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"> <div class="flex-1 mr-2 overflow-hidden bg-white rounded-xl">
<ChatBox v-model:guide="guide" :conversationId="selectedConversationId" /> <ChatBox v-model:guide="guide" :conversationId="selectedConversationId" />
</div> </div>
@@ -20,6 +20,12 @@ const guide = ref(true)
/// 选择的历史会话ID /// 选择的历史会话ID
const selectedConversationId = ref('') const selectedConversationId = ref('')
/// 处理新对话事件切换到引导页并清空选中的历史会话ID
const handleNewChat = () => {
guide.value = true;
selectedConversationId.value = '';
};
/// 选择历史会话 /// 选择历史会话
const handleSelectChat = (conversationId: string) => { const handleSelectChat = (conversationId: string) => {
guide.value = false; guide.value = false;