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