实现会话记录项目隔离
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
+35 -15
View File
@@ -60,19 +60,32 @@ const toMillis = (value: string | number | undefined) =>
const normalizeTitle = (value?: string) => value?.trim() || "新对话";
const getStoredActiveSessionId = () => {
const getActiveSessionStorageKey = (projectId?: string | null) => {
const normalizedProjectId = projectId?.trim();
return normalizedProjectId
? `${ACTIVE_SESSION_STORAGE_KEY}:${encodeURIComponent(normalizedProjectId)}`
: ACTIVE_SESSION_STORAGE_KEY;
};
const getStoredActiveSessionId = (projectId?: string | null) => {
if (typeof window === "undefined") return undefined;
const stored = window.localStorage.getItem(ACTIVE_SESSION_STORAGE_KEY)?.trim();
const stored = window.localStorage
.getItem(getActiveSessionStorageKey(projectId))
?.trim();
return stored || undefined;
};
const setStoredActiveSessionId = (sessionId?: string) => {
const setStoredActiveSessionId = (
sessionId?: string,
projectId?: string | null,
) => {
if (typeof window === "undefined") return;
const storageKey = getActiveSessionStorageKey(projectId);
if (sessionId) {
window.localStorage.setItem(ACTIVE_SESSION_STORAGE_KEY, sessionId);
window.localStorage.setItem(storageKey, sessionId);
return;
}
window.localStorage.removeItem(ACTIVE_SESSION_STORAGE_KEY);
window.localStorage.removeItem(storageKey);
};
const fetchRemoteChatSessions = async (): Promise<ChatSessionSummary[]> => {
@@ -233,16 +246,18 @@ const deleteRemoteChatSession = async (sessionId: string) => {
}
};
export const loadActiveChatState = async (): Promise<LoadedChatState> => {
export const loadActiveChatState = async (
projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
const activeSessionId = getStoredActiveSessionId();
const activeSessionId = getStoredActiveSessionId(projectId);
if (activeSessionId) {
const activeSession = await fetchRemoteChatSession(activeSessionId);
if (activeSession.storageSessionId) {
return activeSession;
}
setStoredActiveSessionId(undefined);
setStoredActiveSessionId(undefined, projectId);
}
const sessions = await fetchRemoteChatSessions();
@@ -250,17 +265,18 @@ export const loadActiveChatState = async (): Promise<LoadedChatState> => {
if (!latestSession) {
return emptyLoadedChatState();
}
setStoredActiveSessionId(latestSession.id);
setStoredActiveSessionId(latestSession.id, projectId);
return await fetchRemoteChatSession(latestSession.id);
};
export const saveActiveChatState = async (
state: LoadedChatState,
projectId?: string | null,
): Promise<string | undefined> => {
if (typeof window === "undefined") return state.storageSessionId;
if (!hasChatContent(state)) {
setStoredActiveSessionId(undefined);
setStoredActiveSessionId(undefined, projectId);
return undefined;
}
@@ -274,7 +290,7 @@ export const saveActiveChatState = async (
storageSessionId: remoteSessionId,
sessionId: remoteSessionId,
});
setStoredActiveSessionId(savedSessionId);
setStoredActiveSessionId(savedSessionId, projectId);
return savedSessionId;
};
@@ -301,10 +317,12 @@ export const updateChatSessionTitle = async (
);
};
export const createEmptyChatSession = async (): Promise<LoadedChatState> => {
export const createEmptyChatSession = async (
projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
setStoredActiveSessionId(undefined);
setStoredActiveSessionId(undefined, projectId);
return {
...emptyLoadedChatState(),
title: "新对话",
@@ -313,23 +331,25 @@ export const createEmptyChatSession = async (): Promise<LoadedChatState> => {
export const loadChatSessionById = async (
sessionId: string,
projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
const loaded = await fetchRemoteChatSession(sessionId);
if (loaded.storageSessionId) {
setStoredActiveSessionId(sessionId);
setStoredActiveSessionId(sessionId, projectId);
}
return loaded;
};
export const deleteChatSession = async (
sessionId: string,
projectId?: string | null,
): Promise<string | undefined> => {
if (typeof window === "undefined") return undefined;
await deleteRemoteChatSession(sessionId);
const nextActiveSession = (await listChatSessions())[0];
setStoredActiveSessionId(nextActiveSession?.id);
setStoredActiveSessionId(nextActiveSession?.id, projectId);
return nextActiveSession?.id;
};