|
17 | 17 | let chatId = $derived($page.params.id); |
18 | 18 | let currentChatId: string | undefined = undefined; |
19 | 19 |
|
20 | | - // Navigation guard to handle streaming abortion |
21 | 20 | beforeNavigate(async ({ cancel, to }) => { |
22 | | - // Check if we're currently streaming a response |
23 | 21 | if (isLoading()) { |
24 | 22 | console.log( |
25 | 23 | 'Navigation detected while streaming - aborting stream and saving partial response' |
26 | 24 | ); |
27 | 25 |
|
28 | | - // Cancel navigation temporarily to allow cleanup |
29 | 26 | cancel(); |
30 | 27 |
|
31 | | - // Gracefully stop generation and save partial response |
32 | 28 | await gracefulStop(); |
33 | 29 |
|
34 | | - // Now proceed with navigation |
35 | 30 | if (to?.url) { |
36 | 31 | await goto(to.url.pathname + to.url.search); |
37 | 32 | } |
38 | 33 | } |
39 | 34 | }); |
40 | 35 |
|
41 | | - // Load chat when ID changes |
42 | 36 | $effect(() => { |
43 | 37 | if (chatId && chatId !== currentChatId) { |
44 | | - // If we're switching chats and currently streaming, abort first |
45 | 38 | if (isLoading()) { |
46 | 39 | console.log('Chat switch detected while streaming - aborting stream'); |
47 | 40 | stopGeneration(); |
48 | 41 | } |
49 | 42 |
|
50 | 43 | currentChatId = chatId; |
51 | 44 |
|
52 | | - // Load the chat asynchronously |
53 | 45 | (async () => { |
54 | 46 | const success = await chatStore.loadConversation(chatId); |
| 47 | +
|
55 | 48 | if (!success) { |
56 | | - // Chat not found, redirect to home |
57 | 49 | await goto('/'); |
58 | 50 | } |
59 | 51 | })(); |
60 | 52 | } |
61 | 53 | }); |
62 | 54 |
|
63 | | - // Handle page unload (refresh, close tab, etc.) |
64 | 55 | $effect(() => { |
65 | 56 | if (typeof window !== 'undefined') { |
66 | 57 | const handleBeforeUnload = (event: BeforeUnloadEvent) => { |
67 | 58 | if (isLoading()) { |
68 | 59 | console.log('Page unload detected while streaming - aborting stream'); |
69 | 60 | stopGeneration(); |
70 | | - // Note: We can't wait for async operations in beforeunload |
71 | | - // but stopGeneration() will attempt to save synchronously |
72 | 61 | } |
73 | 62 | }; |
74 | 63 |
|
|
80 | 69 | } |
81 | 70 | }); |
82 | 71 |
|
83 | | - // Cleanup on component destroy |
84 | 72 | onDestroy(() => { |
85 | 73 | if (isLoading()) { |
86 | | - console.log('Component destroying while streaming - aborting stream'); |
87 | 74 | stopGeneration(); |
88 | 75 | } |
89 | 76 | }); |
|
0 commit comments