fix: 对话创建和消息发送时同步到backend
This commit is contained in:
123
www/app.js
123
www/app.js
@@ -2823,7 +2823,7 @@ function showAgentHistoryPage() {
|
||||
}
|
||||
|
||||
// 打开智能体对话
|
||||
function openAgent(agentId) {
|
||||
async function openAgent(agentId) {
|
||||
currentAgent = systemAgents.find(a => a.id === agentId);
|
||||
if (!currentAgent) return;
|
||||
|
||||
@@ -2844,9 +2844,31 @@ function openAgent(agentId) {
|
||||
setAgentUsed(agentId);
|
||||
}
|
||||
|
||||
// 如果用户已登录,先在 backend 创建对话
|
||||
let backendId = null;
|
||||
if (currentUser && currentUser.id) {
|
||||
try {
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: currentAgent.name,
|
||||
messages: [],
|
||||
agentId: agentId
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success && data.id) {
|
||||
backendId = data.id;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('创建智能体对话失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新对话并设置智能体
|
||||
const newConv = {
|
||||
id: Date.now().toString(),
|
||||
id: backendId || Date.now().toString(),
|
||||
title: currentAgent.name,
|
||||
messages: [],
|
||||
agentId: agentId,
|
||||
@@ -3297,15 +3319,33 @@ function togglePinConversation(convId) {
|
||||
}
|
||||
|
||||
// 创建新对话
|
||||
function createNewConversation() {
|
||||
async function createNewConversation() {
|
||||
// 检查未登录用户的对话限制
|
||||
if (!currentUser && !canCreateNewChat()) {
|
||||
showLimitDialog('chat_session');
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果用户已登录,先在 backend 创建对话
|
||||
let backendId = null;
|
||||
if (currentUser && currentUser.id) {
|
||||
try {
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ title: '新对话', messages: [] })
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success && data.id) {
|
||||
backendId = data.id;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('创建对话失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
const newConv = {
|
||||
id: Date.now().toString(),
|
||||
id: backendId || Date.now().toString(), // 使用 backend ID 或本地 ID
|
||||
title: '新对话',
|
||||
messages: [],
|
||||
createdAt: Date.now(),
|
||||
@@ -3622,6 +3662,9 @@ async function sendMessage() {
|
||||
currentConversation.updatedAt = Date.now();
|
||||
saveConversations();
|
||||
|
||||
// 同步当前对话到 backend(用户已登录)
|
||||
syncConversationToBackend(currentConversation);
|
||||
|
||||
// 增加消息计数(未登录用户)
|
||||
if (!currentUser) {
|
||||
if (currentConversation.agentId) {
|
||||
@@ -3742,6 +3785,7 @@ async function streamGenerate(userMsgIndex) {
|
||||
contentEl.innerHTML = renderMarkdown(currentConversation.messages[aiMessageIndex].content);
|
||||
currentConversation.updatedAt = Date.now();
|
||||
saveConversations();
|
||||
syncConversationToBackend(currentConversation);
|
||||
renderMessages();
|
||||
};
|
||||
}
|
||||
@@ -3818,6 +3862,7 @@ async function streamGenerate(userMsgIndex) {
|
||||
hideStopGenerateBtn();
|
||||
currentConversation.updatedAt = Date.now();
|
||||
saveConversations();
|
||||
syncConversationToBackend(currentConversation);
|
||||
renderMessages();
|
||||
|
||||
// 自动总结标题:第一次对话和每隔5次对话
|
||||
@@ -4226,52 +4271,46 @@ function setupScrollListener() {
|
||||
function saveConversations() {
|
||||
// 保存到本地存储(离线可用)
|
||||
localStorage.setItem('conversations', JSON.stringify(conversations));
|
||||
|
||||
// 如果用户已登录,同步到 backend
|
||||
if (currentUser && currentUser.id) {
|
||||
syncConversationsToBackend();
|
||||
}
|
||||
}
|
||||
|
||||
// 同步对话数据到 backend
|
||||
async function syncConversationsToBackend() {
|
||||
// 同步单个对话到 backend(新创建或更新)
|
||||
async function syncConversationToBackend(conv) {
|
||||
if (!currentUser || !currentUser.id) return;
|
||||
|
||||
const convId = parseInt(conv.id);
|
||||
|
||||
try {
|
||||
// 批量同步所有对话
|
||||
for (const conv of conversations) {
|
||||
const convId = parseInt(conv.id);
|
||||
if (convId > 0) {
|
||||
// 更新现有对话
|
||||
await fetch(`/api/user/${currentUser.id}/conversations/${convId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// 新对话,需要创建
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success && data.id) {
|
||||
// 更新本地ID为backend ID
|
||||
conv.id = data.id;
|
||||
}
|
||||
if (convId > 0 && convId < 1e12) {
|
||||
// 已有 backend ID,更新对话
|
||||
await fetch(`/api/user/${currentUser.id}/conversations/${convId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// 新对话(本地 ID 是时间戳),创建
|
||||
const res = await fetch(`/api/user/${currentUser.id}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title: conv.title,
|
||||
messages: conv.messages,
|
||||
agentId: conv.agentId
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.success && data.id) {
|
||||
// 更新本地 ID 为 backend ID
|
||||
conv.id = data.id;
|
||||
// 更新 localStorage
|
||||
localStorage.setItem('conversations', JSON.stringify(conversations));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// 同步失败,静默处理(下次登录时会重新加载)
|
||||
console.error('同步对话失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user