Skip to content

Commit 9642080

Browse files
authored
Fix follow up message on shared conversation (#1963)
replace state and update conv id
1 parent a3b2e79 commit 9642080

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/routes/conversation/[id]/+page.svelte

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { isAborted } from "$lib/stores/isAborted";
55
import { onMount } from "svelte";
66
import { page } from "$app/state";
7-
import { beforeNavigate, goto, invalidateAll } from "$app/navigation";
7+
import { beforeNavigate, invalidateAll, replaceState } from "$app/navigation";
88
import { base } from "$app/paths";
99
import { ERROR_MESSAGES, error } from "$lib/stores/errors";
1010
import { findCurrentModel } from "$lib/utils/models";
@@ -37,6 +37,7 @@
3737
3838
let files: File[] = $state([]);
3939
40+
let conversationId = page.params.id;
4041
let conversations = $state(data.conversations);
4142
$effect(() => {
4243
conversations = data.conversations;
@@ -241,7 +242,7 @@
241242
const messageUpdatesAbortController = new AbortController();
242243
243244
const messageUpdatesIterator = await fetchMessageUpdates(
244-
page.params.id,
245+
conversationId,
245246
{
246247
base,
247248
inputs: prompt,
@@ -302,13 +303,13 @@
302303
$error = update.message ?? "An error has occurred";
303304
}
304305
} else if (update.type === MessageUpdateType.Title) {
305-
const convInData = conversations.find(({ id }) => id === page.params.id);
306+
const convInData = conversations.find(({ id }) => id === conversationId);
306307
if (convInData) {
307308
convInData.title = update.title;
308309
309310
$titleUpdate = {
310311
title: update.title,
311-
convId: page.params.id,
312+
convId: conversationId,
312313
};
313314
}
314315
} else if (update.type === MessageUpdateType.File) {
@@ -343,7 +344,7 @@
343344
}
344345
345346
async function stopGeneration() {
346-
await fetch(`${base}/conversation/${page.params.id}/stop-generating`, {
347+
await fetch(`${base}/conversation/${conversationId}/stop-generating`, {
347348
method: "POST",
348349
}).then((r) => {
349350
if (r.ok) {
@@ -367,6 +368,7 @@
367368
}
368369
369370
onMount(async () => {
371+
conversationId = page.params.id;
370372
if ($pendingMessage) {
371373
files = $pendingMessage.files;
372374
await writeMessage({ prompt: $pendingMessage.content });
@@ -375,7 +377,7 @@
375377
376378
const streaming = isConversationStreaming(messages);
377379
if (streaming) {
378-
addBackgroundGeneration({ id: page.params.id, startedAt: Date.now() });
380+
addBackgroundGeneration({ id: conversationId, startedAt: Date.now() });
379381
$loading = true;
380382
}
381383
});
@@ -384,12 +386,13 @@
384386
if (!data.shared) {
385387
await writeMessage({ prompt: content });
386388
} 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+
}
393396
}
394397
}
395398
@@ -406,19 +409,17 @@
406409
isRetry: true,
407410
});
408411
} 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+
}
422423
}
423424
}
424425
@@ -456,7 +457,7 @@
456457
}
457458
458459
if (!streaming && browser) {
459-
removeBackgroundGeneration(page.params.id);
460+
removeBackgroundGeneration(conversationId);
460461
}
461462
});
462463
@@ -471,21 +472,21 @@
471472
});
472473
473474
beforeNavigate((navigation) => {
474-
if (!page.params.id) return;
475+
if (!conversationId) return;
475476
476477
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;
478479
479480
if (loading && navigatingAway) {
480-
addBackgroundGeneration({ id: page.params.id, startedAt: Date.now() });
481+
addBackgroundGeneration({ id: conversationId, startedAt: Date.now() });
481482
}
482483
483484
$isAborted = true;
484485
$loading = false;
485486
});
486487
487488
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;
489490
return rawTitle ? rawTitle.charAt(0).toUpperCase() + rawTitle.slice(1) : rawTitle;
490491
});
491492
</script>

0 commit comments

Comments
 (0)