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