fix(frontend): stabilize thread id when sending messages
This commit is contained in:
parent
f92444c722
commit
ce02c40b87
|
|
@ -200,9 +200,9 @@ export default function ChatPage() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setHasSubmitted(true);
|
setHasSubmitted(true);
|
||||||
void sendMessage(threadId, message);
|
void sendMessage(safeThreadId, message);
|
||||||
},
|
},
|
||||||
[isSelectedSkillBootstrapping, sendMessage, threadId],
|
[isSelectedSkillBootstrapping, safeThreadId, sendMessage],
|
||||||
);
|
);
|
||||||
const handleStop = useCallback(async () => {
|
const handleStop = useCallback(async () => {
|
||||||
await thread.stop();
|
await thread.stop();
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,15 @@ function getStreamErrorMessage(error: unknown): string {
|
||||||
return "Request failed.";
|
return "Request failed.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeThreadId(
|
||||||
|
value: string | null | undefined,
|
||||||
|
): string | undefined {
|
||||||
|
if (!value) return undefined;
|
||||||
|
const normalized = value.trim();
|
||||||
|
if (!normalized || normalized === "new") return undefined;
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
export function useThreadStreamLegacy({
|
export function useThreadStreamLegacy({
|
||||||
threadId,
|
threadId,
|
||||||
isNewThread,
|
isNewThread,
|
||||||
|
|
@ -155,12 +164,14 @@ export function useThreadStream({
|
||||||
}, [onStart, onFinish, onToolEnd]);
|
}, [onStart, onFinish, onToolEnd]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const normalizedThreadId = threadId ?? null;
|
const normalizedThreadId = normalizeThreadId(threadId) ?? null;
|
||||||
if (!normalizedThreadId) {
|
if (!normalizedThreadId) {
|
||||||
// Just reset for new thread creation when threadId becomes null/undefined
|
// Just reset for new thread creation when threadId becomes null/undefined
|
||||||
startedRef.current = false;
|
startedRef.current = false;
|
||||||
setOnStreamThreadId(normalizedThreadId);
|
|
||||||
}
|
}
|
||||||
|
setOnStreamThreadId((prev) =>
|
||||||
|
prev === normalizedThreadId ? prev : normalizedThreadId,
|
||||||
|
);
|
||||||
threadIdRef.current = normalizedThreadId;
|
threadIdRef.current = normalizedThreadId;
|
||||||
}, [threadId]);
|
}, [threadId]);
|
||||||
|
|
||||||
|
|
@ -288,7 +299,9 @@ export function useThreadStream({
|
||||||
|
|
||||||
const text = message.text.trim();
|
const text = message.text.trim();
|
||||||
const resolvedThreadId =
|
const resolvedThreadId =
|
||||||
threadId ?? threadIdRef.current ?? undefined;
|
normalizeThreadId(threadId) ??
|
||||||
|
normalizeThreadId(threadIdRef.current) ??
|
||||||
|
undefined;
|
||||||
if (resolvedThreadId === "new") {
|
if (resolvedThreadId === "new") {
|
||||||
toast.error("Invalid thread id 'new'. Please refresh and retry.");
|
toast.error("Invalid thread id 'new'. Please refresh and retry.");
|
||||||
sendInFlightRef.current = false;
|
sendInFlightRef.current = false;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue