重构会话标题编辑和删除确认逻辑;重构历史会话时间记录

This commit is contained in:
2026-05-19 17:54:09 +08:00
parent 2fbfba118f
commit 4f54da64d0
6 changed files with 329 additions and 136 deletions
@@ -48,6 +48,16 @@ type PromptRunOptions = {
assistantMessage?: Message;
};
const createPersistedStateKey = (state: LoadedChatState) =>
JSON.stringify({
storageSessionId: state.storageSessionId ?? null,
title: state.title ?? null,
isTitleManuallyEdited: state.isTitleManuallyEdited ?? false,
sessionId: state.sessionId ?? null,
messages: state.messages,
branchGroups: state.branchGroups,
});
const upsertProgress = (
progress: ChatProgress[] | undefined,
event: StreamEvent & { type: "progress" },
@@ -158,6 +168,16 @@ export const useAgentChatSession = ({
const isSessionTitleManuallyEditedRef = useRef(false);
const cancelPromiseRef = useRef<Promise<void> | null>(null);
const titleUpdateNonceRef = useRef(0);
const lastPersistedStateKeyRef = useRef(
createPersistedStateKey({
storageSessionId: undefined,
title: undefined,
isTitleManuallyEdited: false,
messages: [],
sessionId: undefined,
branchGroups: [],
}),
);
useEffect(() => {
sessionIdRef.current = sessionId;
@@ -180,6 +200,7 @@ export const useAgentChatSession = ({
storageSessionIdRef.current = loadedState.storageSessionId;
sessionIdRef.current = loadedState.sessionId;
lastPersistedStateKeyRef.current = createPersistedStateKey(loadedState);
hydrationCompletedRef.current = true;
hydrationNonceRef.current += 1;
titleUpdateNonceRef.current += 1;
@@ -219,11 +240,19 @@ export const useAgentChatSession = ({
sessionId,
branchGroups,
};
const currentStateKey = createPersistedStateKey(state);
if (currentStateKey === lastPersistedStateKeyRef.current) {
return;
}
void saveActiveChatState(state)
.then((storageSessionId) => {
if (hydrationNonceRef.current !== currentHydrationNonce) return;
storageSessionIdRef.current = storageSessionId;
lastPersistedStateKeyRef.current = createPersistedStateKey({
...state,
storageSessionId,
});
return listChatSessions();
})
.then((sessions) => {
@@ -503,6 +532,14 @@ export const useAgentChatSession = ({
setSessionId(undefined);
sessionIdRef.current = undefined;
storageSessionIdRef.current = undefined;
lastPersistedStateKeyRef.current = createPersistedStateKey({
storageSessionId: undefined,
title: undefined,
isTitleManuallyEdited: false,
messages: [],
sessionId: undefined,
branchGroups: [],
});
titleUpdateNonceRef.current += 1;
setIsStreaming(false);
}, []);
@@ -521,6 +558,7 @@ export const useAgentChatSession = ({
titleUpdateNonceRef.current += 1;
storageSessionIdRef.current = newState.storageSessionId;
sessionIdRef.current = newState.sessionId;
lastPersistedStateKeyRef.current = createPersistedStateKey(newState);
setMessages(newState.messages);
setSessionTitle(newState.title);
setIsSessionTitleManuallyEdited(newState.isTitleManuallyEdited ?? false);
@@ -547,6 +585,7 @@ export const useAgentChatSession = ({
titleUpdateNonceRef.current += 1;
storageSessionIdRef.current = nextState.storageSessionId;
sessionIdRef.current = nextState.sessionId;
lastPersistedStateKeyRef.current = createPersistedStateKey(nextState);
setBranchTransition(null);
setMessages(nextState.messages);
setSessionTitle(nextState.title);
@@ -581,6 +620,14 @@ export const useAgentChatSession = ({
titleUpdateNonceRef.current += 1;
storageSessionIdRef.current = undefined;
sessionIdRef.current = undefined;
lastPersistedStateKeyRef.current = createPersistedStateKey({
storageSessionId: undefined,
title: undefined,
isTitleManuallyEdited: false,
messages: [],
sessionId: undefined,
branchGroups: [],
});
setBranchTransition(null);
setMessages([]);
setSessionTitle(undefined);
@@ -599,6 +646,7 @@ export const useAgentChatSession = ({
titleUpdateNonceRef.current += 1;
storageSessionIdRef.current = nextState.storageSessionId;
sessionIdRef.current = nextState.sessionId;
lastPersistedStateKeyRef.current = createPersistedStateKey(nextState);
setBranchTransition(null);
setMessages(nextState.messages);
setSessionTitle(nextState.title);
@@ -637,12 +685,20 @@ export const useAgentChatSession = ({
if (storageSessionIdRef.current === targetStorageSessionId) {
setSessionTitle(normalizedTitle);
setIsSessionTitleManuallyEdited(true);
lastPersistedStateKeyRef.current = createPersistedStateKey({
storageSessionId: targetStorageSessionId,
title: normalizedTitle,
isTitleManuallyEdited: true,
messages,
sessionId: sessionIdRef.current,
branchGroups,
});
}
} catch (error) {
console.error("[GlobalChatbox] Failed to rename chat session:", error);
}
},
[isHydrating],
[branchGroups, isHydrating, messages],
);
const regenerate = useCallback(async () => {