fix:修复了thread_id会读到new的错误
This commit is contained in:
parent
ce4b0dcd4d
commit
081adb34b3
|
|
@ -1,7 +1,7 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { FilesIcon, ListTodoIcon, XIcon } from "lucide-react";
|
import { FilesIcon, ListTodoIcon, XIcon } from "lucide-react";
|
||||||
import { useSearchParams } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
|
|
||||||
import { ConversationEmptyState } from "@/components/ai-elements/conversation";
|
import { ConversationEmptyState } from "@/components/ai-elements/conversation";
|
||||||
|
|
@ -44,6 +44,7 @@ export default function ChatPage() {
|
||||||
useSpecificChatMode();
|
useSpecificChatMode();
|
||||||
const [settings, setSettings] = useLocalSettings();
|
const [settings, setSettings] = useLocalSettings();
|
||||||
const { setOpen: setSidebarOpen } = useSidebar();
|
const { setOpen: setSidebarOpen } = useSidebar();
|
||||||
|
const router = useRouter();
|
||||||
const {
|
const {
|
||||||
artifacts,
|
artifacts,
|
||||||
open: artifactsOpen,
|
open: artifactsOpen,
|
||||||
|
|
@ -51,9 +52,12 @@ export default function ChatPage() {
|
||||||
setArtifacts,
|
setArtifacts,
|
||||||
select: selectArtifact,
|
select: selectArtifact,
|
||||||
selectedArtifact,
|
selectedArtifact,
|
||||||
|
deselect: deselectArtifact,
|
||||||
|
setFullscreen: setArtifactsFullscreen,
|
||||||
fullscreen,
|
fullscreen,
|
||||||
} = useArtifacts();
|
} = useArtifacts();
|
||||||
const { threadId, isNewThread, setIsNewThread, isMock } = useThreadChat();
|
const { threadId, isNewThread, setIsNewThread, isMock } = useThreadChat();
|
||||||
|
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
// History render rules:
|
// History render rules:
|
||||||
// - /workspace/chats/{thread_id}: always render history
|
// - /workspace/chats/{thread_id}: always render history
|
||||||
|
|
@ -63,8 +67,9 @@ export default function ChatPage() {
|
||||||
searchParams.get("xclaw_used")?.trim().toLowerCase() === "true";
|
searchParams.get("xclaw_used")?.trim().toLowerCase() === "true";
|
||||||
|
|
||||||
// Submission strategy:
|
// Submission strategy:
|
||||||
|
// - isnew=false + thread_id: reuse existing thread (explicit request from URL)
|
||||||
// - xclaw_used=true: follow `isnew` (isnew=false => reuse existing thread)
|
// - xclaw_used=true: follow `isnew` (isnew=false => reuse existing thread)
|
||||||
// - xclaw_used!=true: always create/start a new session (no history)
|
// - otherwise: create/start a new session (no history)
|
||||||
const createNewSession = useMemo(() => {
|
const createNewSession = useMemo(() => {
|
||||||
if (!isNewThread) {
|
if (!isNewThread) {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -99,7 +104,8 @@ export default function ChatPage() {
|
||||||
isMock,
|
isMock,
|
||||||
onStart: (currentThreadId) => {
|
onStart: (currentThreadId) => {
|
||||||
setIsNewThread(false);
|
setIsNewThread(false);
|
||||||
history.replaceState(null, "", pathOfThread(currentThreadId));
|
// Keep /new in history so router.back() can return to it.
|
||||||
|
history.pushState(null, "", pathOfThread(currentThreadId));
|
||||||
},
|
},
|
||||||
onFinish: (state) => {
|
onFinish: (state) => {
|
||||||
if (document.hidden || !document.hasFocus()) {
|
if (document.hidden || !document.hasFocus()) {
|
||||||
|
|
@ -205,6 +211,22 @@ export default function ChatPage() {
|
||||||
await thread.stop();
|
await thread.stop();
|
||||||
}, [thread]);
|
}, [thread]);
|
||||||
|
|
||||||
|
const resetNewSessionState = useCallback(() => {
|
||||||
|
setIsNewThread(true);
|
||||||
|
setHasSubmitted(false);
|
||||||
|
setHistoryCutoff(null);
|
||||||
|
setArtifacts([]);
|
||||||
|
deselectArtifact();
|
||||||
|
setArtifactsOpen(false);
|
||||||
|
setArtifactsFullscreen(false);
|
||||||
|
}, [
|
||||||
|
deselectArtifact,
|
||||||
|
setArtifacts,
|
||||||
|
setArtifactsFullscreen,
|
||||||
|
setArtifactsOpen,
|
||||||
|
setIsNewThread,
|
||||||
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThreadContext.Provider value={{ thread }}>
|
<ThreadContext.Provider value={{ thread }}>
|
||||||
<div
|
<div
|
||||||
|
|
@ -474,8 +496,15 @@ export default function ChatPage() {
|
||||||
type: POST_MESSAGE_TYPES.XCLAW_USED,
|
type: POST_MESSAGE_TYPES.XCLAW_USED,
|
||||||
XClawUsed: false,
|
XClawUsed: false,
|
||||||
});
|
});
|
||||||
// 使用完整页面刷新确保组件重新挂载,isNewThread 为 true
|
resetNewSessionState();
|
||||||
window.location.reload();
|
// 因为threadId可能为undefined,所以这里不直接导航到 /workspace/chats/new,而是通过 replace 的方式更新 URL 参数,保持在当前页面,触发 useThreadChat 重新计算状态。
|
||||||
|
const nextQuery = new URLSearchParams();
|
||||||
|
nextQuery.set("isnew", "false");
|
||||||
|
nextQuery.set("xclaw_used", "false");
|
||||||
|
if (threadId && threadId !== "new") {
|
||||||
|
nextQuery.set("thread_id", threadId);
|
||||||
|
}
|
||||||
|
router.replace(`/workspace/chats/new?${nextQuery.toString()}`);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
确定
|
确定
|
||||||
|
|
@ -513,7 +542,7 @@ export default function ChatPage() {
|
||||||
</DevDialog>
|
</DevDialog>
|
||||||
|
|
||||||
{/* MARK: 开发测试:iframe 通信功能测试面板 */}
|
{/* MARK: 开发测试:iframe 通信功能测试面板 */}
|
||||||
{process.env.NODE_ENV !== "production" && <IframeTestPanel />}
|
{/* {process.env.NODE_ENV !== "production" && <IframeTestPanel />} */}
|
||||||
</div>
|
</div>
|
||||||
</ThreadContext.Provider>
|
</ThreadContext.Provider>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -4,24 +4,64 @@ import { useParams, usePathname, useRouter, useSearchParams } from "next/navigat
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
export function useThreadChat() {
|
export function useThreadChat() {
|
||||||
const { thread_id: threadIdFromPath } = useParams<{ thread_id: string }>();
|
|
||||||
const pathname = usePathname();
|
const pathname = usePathname();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const params = useParams<{ thread_id?: string }>();
|
||||||
|
const threadIdFromPathname = (() => {
|
||||||
|
const parts = pathname.split("?")[0]?.split("/") ?? [];
|
||||||
|
const idx = parts.lastIndexOf("chats");
|
||||||
|
if (idx >= 0 && parts.length > idx + 1) {
|
||||||
|
return parts[idx + 1];
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
})();
|
||||||
|
const threadIdFromPath = params?.thread_id !== 'new' ? params?.thread_id : threadIdFromPathname;
|
||||||
|
console.log("[useThreadChat] pathname", pathname);
|
||||||
|
console.log("[useThreadChat] params.thread_id", params?.thread_id);
|
||||||
|
console.log("[useThreadChat] threadIdFromPathname", threadIdFromPathname);
|
||||||
|
console.log("[useThreadChat] threadIdFromPath", threadIdFromPath);
|
||||||
|
const readStoredThreadId = () => {
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const stored = window.sessionStorage.getItem("workspace.thread_id");
|
||||||
|
return stored && stored !== "new" ? stored : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
|
const readQueryThreadId = () => {
|
||||||
|
const fromHook = searchParams.get("thread_id")?.trim();
|
||||||
|
if (fromHook && fromHook !== "new") {
|
||||||
|
return fromHook;
|
||||||
|
}
|
||||||
|
if (typeof window === "undefined") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const fromLocation = new URLSearchParams(window.location.search).get(
|
||||||
|
"thread_id",
|
||||||
|
);
|
||||||
|
if (fromLocation && fromLocation !== "new") {
|
||||||
|
return fromLocation.trim();
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
const queryThreadIdFromParams = readQueryThreadId();
|
||||||
|
console.log("[useThreadChat] query.thread_id", queryThreadIdFromParams);
|
||||||
|
const normalizeThreadId = (value?: string | null) => {
|
||||||
|
if (!value) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return value === "new" ? queryThreadIdFromParams : value;
|
||||||
|
};
|
||||||
const xClawUsedFromQuery = searchParams.get("xclaw_used");
|
const xClawUsedFromQuery = searchParams.get("xclaw_used");
|
||||||
const isNewFromQuery =
|
const isNewFromQuery =
|
||||||
searchParams.get("isnew")?.trim().toLowerCase() === "false";
|
searchParams.get("isnew")?.trim().toLowerCase() === "false";
|
||||||
const queryThreadIdFromParams = searchParams.get("thread_id")?.trim();
|
const effectiveThreadIdFromPath =
|
||||||
const shouldUseQueryThreadId =
|
normalizeThreadId(threadIdFromPath) ?? readStoredThreadId();
|
||||||
pathname.startsWith("/workspace/chats/") &&
|
console.log("[useThreadChat] effectiveThreadIdFromPath", effectiveThreadIdFromPath);
|
||||||
!!queryThreadIdFromParams &&
|
|
||||||
(xClawUsedFromQuery === "true" || isNewFromQuery);
|
|
||||||
const [threadId, setThreadId] = useState(() => {
|
const [threadId, setThreadId] = useState(() => {
|
||||||
if (threadIdFromPath === "new") {
|
return effectiveThreadIdFromPath ?? undefined;
|
||||||
return shouldUseQueryThreadId ? queryThreadIdFromParams : undefined;
|
|
||||||
}
|
|
||||||
return threadIdFromPath;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const [isNewThread, setIsNewThread] = useState(
|
const [isNewThread, setIsNewThread] = useState(
|
||||||
|
|
@ -29,25 +69,22 @@ export function useThreadChat() {
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (threadId && threadId !== "new" && typeof window !== "undefined") {
|
||||||
|
window.sessionStorage.setItem("workspace.thread_id", threadId);
|
||||||
|
}
|
||||||
if (pathname.endsWith("/new")) {
|
if (pathname.endsWith("/new")) {
|
||||||
setIsNewThread(true);
|
setIsNewThread(true);
|
||||||
const nextQueryThreadId = searchParams.get("thread_id")?.trim();
|
const nextQueryThreadId = readQueryThreadId();
|
||||||
const nextIsNewFromQuery =
|
const nextIsNewFromQuery =
|
||||||
searchParams.get("isnew")?.trim().toLowerCase() === "false";
|
searchParams.get("isnew")?.trim().toLowerCase() === "false";
|
||||||
const nextXClawUsed = searchParams.get("xclaw_used");
|
const nextXClawUsed = searchParams.get("xclaw_used");
|
||||||
const nextShouldUseQueryThreadId =
|
setThreadId(nextQueryThreadId ?? undefined);
|
||||||
pathname.startsWith("/workspace/chats/") &&
|
|
||||||
!!nextQueryThreadId &&
|
|
||||||
(nextXClawUsed === "true" || nextIsNewFromQuery);
|
|
||||||
if (nextShouldUseQueryThreadId && nextQueryThreadId) {
|
|
||||||
router.replace(`/workspace/chats/${nextQueryThreadId}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setThreadId(nextShouldUseQueryThreadId ? nextQueryThreadId : undefined);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsNewThread(false);
|
setIsNewThread(false);
|
||||||
setThreadId(threadIdFromPath);
|
console.log("threadIdFromPath", threadIdFromPath, "normalized", normalizeThreadId(threadIdFromPath));
|
||||||
|
|
||||||
|
setThreadId(normalizeThreadId(threadIdFromPath));
|
||||||
}, [pathname, router, searchParams, threadIdFromPath]);
|
}, [pathname, router, searchParams, threadIdFromPath]);
|
||||||
const isMock = searchParams.get("mock") === "true";
|
const isMock = searchParams.get("mock") === "true";
|
||||||
return { threadId, isNewThread, setIsNewThread, isMock };
|
return { threadId, isNewThread, setIsNewThread, isMock };
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue