|
4 | 4 | import { isAborted } from "$lib/stores/isAborted"; |
5 | 5 | import { onMount } from "svelte"; |
6 | 6 | import { page } from "$app/state"; |
7 | | - import { beforeNavigate, goto, invalidateAll } from "$app/navigation"; |
| 7 | + import { beforeNavigate, invalidateAll, replaceState } from "$app/navigation"; |
8 | 8 | import { base } from "$app/paths"; |
9 | 9 | import { ERROR_MESSAGES, error } from "$lib/stores/errors"; |
10 | 10 | import { findCurrentModel } from "$lib/utils/models"; |
|
37 | 37 |
|
38 | 38 | let files: File[] = $state([]); |
39 | 39 |
|
| 40 | + let conversationId = page.params.id; |
40 | 41 | let conversations = $state(data.conversations); |
41 | 42 | $effect(() => { |
42 | 43 | conversations = data.conversations; |
|
241 | 242 | const messageUpdatesAbortController = new AbortController(); |
242 | 243 |
|
243 | 244 | const messageUpdatesIterator = await fetchMessageUpdates( |
244 | | - page.params.id, |
| 245 | + conversationId, |
245 | 246 | { |
246 | 247 | base, |
247 | 248 | inputs: prompt, |
|
302 | 303 | $error = update.message ?? "An error has occurred"; |
303 | 304 | } |
304 | 305 | } else if (update.type === MessageUpdateType.Title) { |
305 | | - const convInData = conversations.find(({ id }) => id === page.params.id); |
| 306 | + const convInData = conversations.find(({ id }) => id === conversationId); |
306 | 307 | if (convInData) { |
307 | 308 | convInData.title = update.title; |
308 | 309 |
|
309 | 310 | $titleUpdate = { |
310 | 311 | title: update.title, |
311 | | - convId: page.params.id, |
| 312 | + convId: conversationId, |
312 | 313 | }; |
313 | 314 | } |
314 | 315 | } else if (update.type === MessageUpdateType.File) { |
|
343 | 344 | } |
344 | 345 |
|
345 | 346 | async function stopGeneration() { |
346 | | - await fetch(`${base}/conversation/${page.params.id}/stop-generating`, { |
| 347 | + await fetch(`${base}/conversation/${conversationId}/stop-generating`, { |
347 | 348 | method: "POST", |
348 | 349 | }).then((r) => { |
349 | 350 | if (r.ok) { |
|
367 | 368 | } |
368 | 369 |
|
369 | 370 | onMount(async () => { |
| 371 | + conversationId = page.params.id; |
370 | 372 | if ($pendingMessage) { |
371 | 373 | files = $pendingMessage.files; |
372 | 374 | await writeMessage({ prompt: $pendingMessage.content }); |
|
375 | 377 |
|
376 | 378 | const streaming = isConversationStreaming(messages); |
377 | 379 | if (streaming) { |
378 | | - addBackgroundGeneration({ id: page.params.id, startedAt: Date.now() }); |
| 380 | + addBackgroundGeneration({ id: conversationId, startedAt: Date.now() }); |
379 | 381 | $loading = true; |
380 | 382 | } |
381 | 383 | }); |
|
384 | 386 | if (!data.shared) { |
385 | 387 | await writeMessage({ prompt: content }); |
386 | 388 | } else { |
387 | | - await convFromShared() |
388 | | - .then(async (convId) => { |
389 | | - await goto(`${base}/conversation/${convId}`, { invalidateAll: true }); |
390 | | - }) |
391 | | - .then(async () => await writeMessage({ prompt: content })) |
392 | | - .finally(() => ($loading = false)); |
| 389 | + try { |
| 390 | + conversationId = await convFromShared(); |
| 391 | + replaceState(`${base}/conversation/${conversationId}`, page.state); |
| 392 | + await writeMessage({ prompt: content }); |
| 393 | + } finally { |
| 394 | + $loading = false; |
| 395 | + } |
393 | 396 | } |
394 | 397 | } |
395 | 398 |
|
|
406 | 409 | isRetry: true, |
407 | 410 | }); |
408 | 411 | } else { |
409 | | - await convFromShared() |
410 | | - .then(async (convId) => { |
411 | | - await goto(`${base}/conversation/${convId}`, { invalidateAll: true }); |
412 | | - }) |
413 | | - .then( |
414 | | - async () => |
415 | | - await writeMessage({ |
416 | | - prompt: payload.content, |
417 | | - messageId: payload.id, |
418 | | - isRetry: true, |
419 | | - }) |
420 | | - ) |
421 | | - .finally(() => ($loading = false)); |
| 412 | + try { |
| 413 | + const conversationId = await convFromShared(); |
| 414 | + replaceState(`${base}/conversation/${conversationId}`, page.state); |
| 415 | + await writeMessage({ |
| 416 | + prompt: payload.content, |
| 417 | + messageId: payload.id, |
| 418 | + isRetry: true, |
| 419 | + }); |
| 420 | + } finally { |
| 421 | + $loading = false; |
| 422 | + } |
422 | 423 | } |
423 | 424 | } |
424 | 425 |
|
|
456 | 457 | } |
457 | 458 |
|
458 | 459 | if (!streaming && browser) { |
459 | | - removeBackgroundGeneration(page.params.id); |
| 460 | + removeBackgroundGeneration(conversationId); |
460 | 461 | } |
461 | 462 | }); |
462 | 463 |
|
|
471 | 472 | }); |
472 | 473 |
|
473 | 474 | beforeNavigate((navigation) => { |
474 | | - if (!page.params.id) return; |
| 475 | + if (!conversationId) return; |
475 | 476 |
|
476 | 477 | const navigatingAway = |
477 | | - navigation.to?.route.id !== page.route.id || navigation.to?.params?.id !== page.params.id; |
| 478 | + navigation.to?.route.id !== page.route.id || navigation.to?.params?.id !== conversationId; |
478 | 479 |
|
479 | 480 | if (loading && navigatingAway) { |
480 | | - addBackgroundGeneration({ id: page.params.id, startedAt: Date.now() }); |
| 481 | + addBackgroundGeneration({ id: conversationId, startedAt: Date.now() }); |
481 | 482 | } |
482 | 483 |
|
483 | 484 | $isAborted = true; |
484 | 485 | $loading = false; |
485 | 486 | }); |
486 | 487 |
|
487 | 488 | let title = $derived.by(() => { |
488 | | - const rawTitle = conversations.find((conv) => conv.id === page.params.id)?.title ?? data.title; |
| 489 | + const rawTitle = conversations.find((conv) => conv.id === conversationId)?.title ?? data.title; |
489 | 490 | return rawTitle ? rawTitle.charAt(0).toUpperCase() + rawTitle.slice(1) : rawTitle; |
490 | 491 | }); |
491 | 492 | </script> |
|
0 commit comments