修复会话记录可能存储两次的bug;更改会话行为,默认进入新对话
Build Push and Deploy / docker-image (push) Successful in 1m1s
Build Push and Deploy / deploy-fallback-log (push) Has been skipped

This commit is contained in:
2026-05-22 14:19:14 +08:00
parent 54fbf15be8
commit 6b447eb398
5 changed files with 103 additions and 163 deletions
+6 -72
View File
@@ -9,8 +9,6 @@ import type {
} from "./GlobalChatbox.types";
import { cloneBranchGroups, cloneMessages } from "./GlobalChatbox.utils";
const ACTIVE_SESSION_STORAGE_KEY = "tjwater_agent_active_session_id_v2";
type RemoteSessionPayload = {
id?: string;
title?: string;
@@ -60,34 +58,6 @@ const toMillis = (value: string | number | undefined) =>
const normalizeTitle = (value?: string) => value?.trim() || "新对话";
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(getActiveSessionStorageKey(projectId))
?.trim();
return stored || undefined;
};
const setStoredActiveSessionId = (
sessionId?: string,
projectId?: string | null,
) => {
if (typeof window === "undefined") return;
const storageKey = getActiveSessionStorageKey(projectId);
if (sessionId) {
window.localStorage.setItem(storageKey, sessionId);
return;
}
window.localStorage.removeItem(storageKey);
};
const fetchRemoteChatSessions = async (): Promise<ChatSessionSummary[]> => {
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/sessions`, {
method: "GET",
@@ -247,36 +217,18 @@ const deleteRemoteChatSession = async (sessionId: string) => {
};
export const loadActiveChatState = async (
projectId?: string | null,
_projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
const activeSessionId = getStoredActiveSessionId(projectId);
if (activeSessionId) {
const activeSession = await fetchRemoteChatSession(activeSessionId);
if (activeSession.storageSessionId) {
return activeSession;
}
setStoredActiveSessionId(undefined, projectId);
}
const sessions = await fetchRemoteChatSessions();
const latestSession = sessions[0];
if (!latestSession) {
return emptyLoadedChatState();
}
setStoredActiveSessionId(latestSession.id, projectId);
return await fetchRemoteChatSession(latestSession.id);
return emptyLoadedChatState();
};
export const saveActiveChatState = async (
state: LoadedChatState,
projectId?: string | null,
_projectId?: string | null,
): Promise<string | undefined> => {
if (typeof window === "undefined") return state.storageSessionId;
if (!hasChatContent(state)) {
setStoredActiveSessionId(undefined, projectId);
return undefined;
}
@@ -290,7 +242,6 @@ export const saveActiveChatState = async (
storageSessionId: remoteSessionId,
sessionId: remoteSessionId,
});
setStoredActiveSessionId(savedSessionId, projectId);
return savedSessionId;
};
@@ -317,39 +268,22 @@ export const updateChatSessionTitle = async (
);
};
export const createEmptyChatSession = async (
projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
setStoredActiveSessionId(undefined, projectId);
return {
...emptyLoadedChatState(),
title: "新对话",
};
};
export const loadChatSessionById = async (
sessionId: string,
projectId?: string | null,
_projectId?: string | null,
): Promise<LoadedChatState> => {
if (typeof window === "undefined") return emptyLoadedChatState();
const loaded = await fetchRemoteChatSession(sessionId);
if (loaded.storageSessionId) {
setStoredActiveSessionId(sessionId, projectId);
}
return loaded;
return await fetchRemoteChatSession(sessionId);
};
export const deleteChatSession = async (
sessionId: string,
projectId?: string | null,
_projectId?: string | null,
): Promise<string | undefined> => {
if (typeof window === "undefined") return undefined;
await deleteRemoteChatSession(sessionId);
const nextActiveSession = (await listChatSessions())[0];
setStoredActiveSessionId(nextActiveSession?.id, projectId);
return nextActiveSession?.id;
};