增加会话标题重命名功能,优化历史面板交互
This commit is contained in:
@@ -146,6 +146,7 @@ export const useAgentChatSession = ({
|
||||
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [sessionTitle, setSessionTitle] = useState<string | undefined>(undefined);
|
||||
const [isSessionTitleManuallyEdited, setIsSessionTitleManuallyEdited] = useState(false);
|
||||
const [sessionId, setSessionId] = useState<string | undefined>(undefined);
|
||||
const [branchGroups, setBranchGroups] = useState<BranchGroup[]>([]);
|
||||
const [chatSessions, setChatSessions] = useState<ChatSessionSummary[]>([]);
|
||||
@@ -154,6 +155,7 @@ export const useAgentChatSession = ({
|
||||
const [isHydrating, setIsHydrating] = useState(true);
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
const sessionIdRef = useRef<string | undefined>(undefined);
|
||||
const isSessionTitleManuallyEditedRef = useRef(false);
|
||||
const cancelPromiseRef = useRef<Promise<void> | null>(null);
|
||||
const titleUpdateNonceRef = useRef(0);
|
||||
|
||||
@@ -161,6 +163,10 @@ export const useAgentChatSession = ({
|
||||
sessionIdRef.current = sessionId;
|
||||
}, [sessionId]);
|
||||
|
||||
useEffect(() => {
|
||||
isSessionTitleManuallyEditedRef.current = isSessionTitleManuallyEdited;
|
||||
}, [isSessionTitleManuallyEdited]);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
||||
@@ -180,6 +186,7 @@ export const useAgentChatSession = ({
|
||||
|
||||
setMessages(loadedState.messages);
|
||||
setSessionTitle(loadedState.title);
|
||||
setIsSessionTitleManuallyEdited(loadedState.isTitleManuallyEdited ?? false);
|
||||
setSessionId(loadedState.sessionId);
|
||||
setBranchGroups(loadedState.branchGroups);
|
||||
setChatSessions(sessions);
|
||||
@@ -207,6 +214,7 @@ export const useAgentChatSession = ({
|
||||
const state: LoadedChatState = {
|
||||
storageSessionId: storageSessionIdRef.current,
|
||||
title: sessionTitle,
|
||||
isTitleManuallyEdited: isSessionTitleManuallyEdited,
|
||||
messages,
|
||||
sessionId,
|
||||
branchGroups,
|
||||
@@ -230,7 +238,7 @@ export const useAgentChatSession = ({
|
||||
return () => {
|
||||
window.clearTimeout(persistTimer);
|
||||
};
|
||||
}, [branchGroups, isHydrating, messages, sessionId, sessionTitle]);
|
||||
}, [branchGroups, isHydrating, isSessionTitleManuallyEdited, messages, sessionId, sessionTitle]);
|
||||
|
||||
useEffect(() => {
|
||||
setBranchGroups((prev) => {
|
||||
@@ -354,12 +362,14 @@ export const useAgentChatSession = ({
|
||||
});
|
||||
} else if (event.type === "session_title") {
|
||||
const nextTitle = event.title.trim();
|
||||
if (nextTitle) {
|
||||
if (nextTitle && !isSessionTitleManuallyEditedRef.current) {
|
||||
setSessionTitle(nextTitle);
|
||||
const currentStorageSessionId = storageSessionIdRef.current;
|
||||
if (currentStorageSessionId) {
|
||||
const currentNonce = ++titleUpdateNonceRef.current;
|
||||
void updateChatSessionTitle(currentStorageSessionId, nextTitle)
|
||||
void updateChatSessionTitle(currentStorageSessionId, nextTitle, {
|
||||
isTitleManuallyEdited: false,
|
||||
})
|
||||
.then(() => listChatSessions())
|
||||
.then((sessions) => {
|
||||
if (titleUpdateNonceRef.current !== currentNonce) return;
|
||||
@@ -487,6 +497,7 @@ export const useAgentChatSession = ({
|
||||
}
|
||||
setMessages([]);
|
||||
setSessionTitle(undefined);
|
||||
setIsSessionTitleManuallyEdited(false);
|
||||
setBranchGroups([]);
|
||||
setBranchTransition(null);
|
||||
setSessionId(undefined);
|
||||
@@ -512,6 +523,7 @@ export const useAgentChatSession = ({
|
||||
sessionIdRef.current = newState.sessionId;
|
||||
setMessages(newState.messages);
|
||||
setSessionTitle(newState.title);
|
||||
setIsSessionTitleManuallyEdited(newState.isTitleManuallyEdited ?? false);
|
||||
setSessionId(newState.sessionId);
|
||||
setBranchGroups(newState.branchGroups);
|
||||
setChatSessions(sessions);
|
||||
@@ -538,6 +550,7 @@ export const useAgentChatSession = ({
|
||||
setBranchTransition(null);
|
||||
setMessages(nextState.messages);
|
||||
setSessionTitle(nextState.title);
|
||||
setIsSessionTitleManuallyEdited(nextState.isTitleManuallyEdited ?? false);
|
||||
setSessionId(nextState.sessionId);
|
||||
setBranchGroups(nextState.branchGroups);
|
||||
setChatSessions(sessions);
|
||||
@@ -571,6 +584,7 @@ export const useAgentChatSession = ({
|
||||
setBranchTransition(null);
|
||||
setMessages([]);
|
||||
setSessionTitle(undefined);
|
||||
setIsSessionTitleManuallyEdited(false);
|
||||
setSessionId(undefined);
|
||||
setBranchGroups([]);
|
||||
return;
|
||||
@@ -588,6 +602,7 @@ export const useAgentChatSession = ({
|
||||
setBranchTransition(null);
|
||||
setMessages(nextState.messages);
|
||||
setSessionTitle(nextState.title);
|
||||
setIsSessionTitleManuallyEdited(nextState.isTitleManuallyEdited ?? false);
|
||||
setSessionId(nextState.sessionId);
|
||||
setBranchGroups(nextState.branchGroups);
|
||||
setChatSessions(sessionsAfterDelete);
|
||||
@@ -607,6 +622,29 @@ export const useAgentChatSession = ({
|
||||
[runPrompt],
|
||||
);
|
||||
|
||||
const renameSession = useCallback(
|
||||
async (targetStorageSessionId: string, nextTitle: string) => {
|
||||
const normalizedTitle = nextTitle.trim();
|
||||
if (!normalizedTitle || isHydrating) return;
|
||||
|
||||
try {
|
||||
await updateChatSessionTitle(targetStorageSessionId, normalizedTitle, {
|
||||
isTitleManuallyEdited: true,
|
||||
});
|
||||
const sessions = await listChatSessions();
|
||||
setChatSessions(sessions);
|
||||
|
||||
if (storageSessionIdRef.current === targetStorageSessionId) {
|
||||
setSessionTitle(normalizedTitle);
|
||||
setIsSessionTitleManuallyEdited(true);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("[GlobalChatbox] Failed to rename chat session:", error);
|
||||
}
|
||||
},
|
||||
[isHydrating],
|
||||
);
|
||||
|
||||
const regenerate = useCallback(async () => {
|
||||
if (isHydrating || isStreaming || messages.length === 0) return;
|
||||
|
||||
@@ -776,6 +814,7 @@ export const useAgentChatSession = ({
|
||||
abort,
|
||||
createSession,
|
||||
reset,
|
||||
renameSession,
|
||||
removeSession,
|
||||
switchSession,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user