1
1
"use client" ;
2
2
3
- import { redirect , useRouter , useSearchParams } from "next/navigation" ;
3
+ import {
4
+ redirect ,
5
+ usePathname ,
6
+ useRouter ,
7
+ useSearchParams ,
8
+ } from "next/navigation" ;
4
9
import {
5
10
BackendChatSession ,
6
11
BackendMessage ,
@@ -130,6 +135,7 @@ import {
130
135
} from "@/lib/browserUtilities" ;
131
136
import { Button } from "@/components/ui/button" ;
132
137
import { ConfirmEntityModal } from "@/components/modals/ConfirmEntityModal" ;
138
+ import { MessageChannel } from "node:worker_threads" ;
133
139
134
140
const TEMP_USER_MESSAGE_ID = - 1 ;
135
141
const TEMP_ASSISTANT_MESSAGE_ID = - 2 ;
@@ -1145,6 +1151,7 @@ export function ChatPage({
1145
1151
regenerationRequest ?: RegenerationRequest | null ;
1146
1152
overrideFileDescriptors ?: FileDescriptor [ ] ;
1147
1153
} = { } ) => {
1154
+ navigatingAway . current = false ;
1148
1155
let frozenSessionId = currentSessionId ( ) ;
1149
1156
updateCanContinue ( false , frozenSessionId ) ;
1150
1157
@@ -1267,7 +1274,6 @@ export function ChatPage({
1267
1274
let stackTrace : string | null = null ;
1268
1275
1269
1276
let sub_questions : SubQuestionDetail [ ] = [ ] ;
1270
- let second_level_sub_questions : SubQuestionDetail [ ] = [ ] ;
1271
1277
let is_generating : boolean = false ;
1272
1278
let second_level_generating : boolean = false ;
1273
1279
let finalMessage : BackendMessage | null = null ;
@@ -1291,7 +1297,7 @@ export function ChatPage({
1291
1297
1292
1298
const stack = new CurrentMessageFIFO ( ) ;
1293
1299
updateCurrentMessageFIFO ( stack , {
1294
- signal : controller . signal , // Add this line
1300
+ signal : controller . signal ,
1295
1301
message : currMessage ,
1296
1302
alternateAssistantId : currentAssistantId ,
1297
1303
fileDescriptors : overrideFileDescriptors || currentMessageFiles ,
@@ -1712,7 +1718,10 @@ export function ChatPage({
1712
1718
const newUrl = buildChatUrl ( searchParams , currChatSessionId , null ) ;
1713
1719
// newUrl is like /chat?chatId=10
1714
1720
// current page is like /chat
1715
- router . push ( newUrl , { scroll : false } ) ;
1721
+
1722
+ if ( pathname == "/chat" && ! navigatingAway . current ) {
1723
+ router . push ( newUrl , { scroll : false } ) ;
1724
+ }
1716
1725
}
1717
1726
}
1718
1727
if (
@@ -2086,6 +2095,31 @@ export function ChatPage({
2086
2095
llmOverrideManager . updateImageFilesPresent ( imageFileInMessageHistory ) ;
2087
2096
} , [ imageFileInMessageHistory ] ) ;
2088
2097
2098
+ const pathname = usePathname ( ) ;
2099
+ useEffect ( ( ) => {
2100
+ return ( ) => {
2101
+ // Cleanup which only runs when the component unmounts (i.e. when you navigate away).
2102
+ const currentSession = currentSessionId ( ) ;
2103
+ const controller = abortControllersRef . current . get ( currentSession ) ;
2104
+ if ( controller ) {
2105
+ controller . abort ( ) ;
2106
+ navigatingAway . current = true ;
2107
+ setAbortControllers ( ( prev ) => {
2108
+ const newControllers = new Map ( prev ) ;
2109
+ newControllers . delete ( currentSession ) ;
2110
+ return newControllers ;
2111
+ } ) ;
2112
+ }
2113
+ } ;
2114
+ } , [ pathname ] ) ;
2115
+
2116
+ const navigatingAway = useRef ( false ) ;
2117
+ // Keep a ref to abortControllers to ensure we always have the latest value
2118
+ const abortControllersRef = useRef ( abortControllers ) ;
2119
+ useEffect ( ( ) => {
2120
+ abortControllersRef . current = abortControllers ;
2121
+ } , [ abortControllers ] ) ;
2122
+
2089
2123
useSidebarShortcut ( router , toggleSidebar ) ;
2090
2124
2091
2125
const [ sharedChatSession , setSharedChatSession ] =
@@ -2300,7 +2334,7 @@ export function ChatPage({
2300
2334
fixed
2301
2335
left-0
2302
2336
z-40
2303
- bg-background-100
2337
+ bg-neutral-200
2304
2338
h-screen
2305
2339
transition-all
2306
2340
bg-opacity-80
@@ -2557,12 +2591,21 @@ export function ChatPage({
2557
2591
) {
2558
2592
return < > </ > ;
2559
2593
}
2594
+ const nextMessage =
2595
+ messageHistory . length > i + 1
2596
+ ? messageHistory [ i + 1 ]
2597
+ : null ;
2560
2598
return (
2561
2599
< div
2562
2600
id = { `message-${ message . messageId } ` }
2563
2601
key = { messageReactComponentKey }
2564
2602
>
2565
2603
< HumanMessage
2604
+ disableSwitchingForStreaming = {
2605
+ ( nextMessage &&
2606
+ nextMessage . is_generating ) ||
2607
+ false
2608
+ }
2566
2609
stopGenerating = { stopGenerating }
2567
2610
content = { message . message }
2568
2611
files = { message . files }
0 commit comments