实现会话记录项目隔离
Build Push and Deploy / docker-image (push) Successful in 1m13s
Build Push and Deploy / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-05-22 11:20:06 +08:00
parent 4bf99e8069
commit 54fbf15be8
5 changed files with 138 additions and 43 deletions
+54 -18
View File
@@ -42,6 +42,39 @@ describe("chatStorage backend-only persistence", () => {
expect(loaded.title).toBe("已存在会话");
});
it("loads the active remote session from the current project's storage key", async () => {
window.localStorage.setItem(
"tjwater_agent_active_session_id_v2:project-a",
"chat-project-a",
);
window.localStorage.setItem(
"tjwater_agent_active_session_id_v2:project-b",
"chat-project-b",
);
apiFetch.mockImplementation(async (url: string) => {
if (url.endsWith("/api/v1/agent/chat/session/chat-project-b")) {
return {
ok: true,
json: async () => ({
id: "chat-project-b",
title: "项目 B 会话",
is_title_manually_edited: false,
session_id: "chat-project-b",
messages: [],
branch_groups: [],
}),
} as Response;
}
throw new Error(`Unexpected request ${url}`);
});
const loaded = await loadActiveChatState("project-b");
expect(loaded.storageSessionId).toBe("chat-project-b");
expect(loaded.title).toBe("项目 B 会话");
});
it("creates a backend conversation when saving the first non-empty state", async () => {
apiFetch.mockImplementation(async (url: string, init?: RequestInit) => {
if (url.endsWith("/api/v1/agent/chat/session")) {
@@ -67,26 +100,29 @@ describe("chatStorage backend-only persistence", () => {
throw new Error(`Unexpected request ${url}`);
});
const savedSessionId = await saveActiveChatState({
storageSessionId: undefined,
title: "新对话",
isTitleManuallyEdited: false,
messages: [
{
id: "message-2",
role: "user",
content: "第一条消息",
branchRootId: "message-2",
},
],
sessionId: undefined,
branchGroups: [],
});
const savedSessionId = await saveActiveChatState(
{
storageSessionId: undefined,
title: "新对话",
isTitleManuallyEdited: false,
messages: [
{
id: "message-2",
role: "user",
content: "第一条消息",
branchRootId: "message-2",
},
],
sessionId: undefined,
branchGroups: [],
},
"project-a",
);
expect(savedSessionId).toBe("chat-new-1");
expect(window.localStorage.getItem("tjwater_agent_active_session_id_v2")).toBe(
"chat-new-1",
);
expect(
window.localStorage.getItem("tjwater_agent_active_session_id_v2:project-a"),
).toBe("chat-new-1");
});
it("does not persist a blank new session before there is chat content", async () => {