Spaces:
Sleeping
Sleeping
Update src/routes/conversation/[id]/+page.svelte
Browse files
src/routes/conversation/[id]/+page.svelte
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
<script lang="ts">
|
| 2 |
import ChatWindow from "$lib/components/chat/ChatWindow.svelte";
|
| 3 |
import { pendingMessage } from "$lib/stores/pendingMessage";
|
|
|
|
| 4 |
import { onMount } from "svelte";
|
| 5 |
import { page } from "$app/stores";
|
| 6 |
-
import {
|
| 7 |
import { base } from "$app/paths";
|
| 8 |
import { shareConversation } from "$lib/shareConversation";
|
| 9 |
import { UrlDependency } from "$lib/types/UrlDependency";
|
|
@@ -33,35 +34,6 @@
|
|
| 33 |
let pending = false;
|
| 34 |
let loginRequired = false;
|
| 35 |
|
| 36 |
-
async function convFromShared() {
|
| 37 |
-
try {
|
| 38 |
-
loading = true;
|
| 39 |
-
const res = await fetch(`${base}/conversation`, {
|
| 40 |
-
method: "POST",
|
| 41 |
-
headers: {
|
| 42 |
-
"Content-Type": "application/json",
|
| 43 |
-
},
|
| 44 |
-
body: JSON.stringify({
|
| 45 |
-
fromShare: $page.params.id,
|
| 46 |
-
model: data.model,
|
| 47 |
-
}),
|
| 48 |
-
});
|
| 49 |
-
|
| 50 |
-
if (!res.ok) {
|
| 51 |
-
error.set("Error while creating conversation, try again.");
|
| 52 |
-
console.error("Error while creating conversation: " + (await res.text()));
|
| 53 |
-
return;
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
const { conversationId } = await res.json();
|
| 57 |
-
|
| 58 |
-
return conversationId;
|
| 59 |
-
} catch (err) {
|
| 60 |
-
error.set(ERROR_MESSAGES.default);
|
| 61 |
-
console.error(String(err));
|
| 62 |
-
throw err;
|
| 63 |
-
}
|
| 64 |
-
}
|
| 65 |
// this function is used to send new message to the backends
|
| 66 |
async function writeMessage(message: string, messageId = randomUUID()) {
|
| 67 |
if (!message.trim()) return;
|
|
@@ -97,6 +69,7 @@
|
|
| 97 |
response_id: responseId,
|
| 98 |
is_retry: isRetry,
|
| 99 |
web_search: $webSearchParameters.useSearch,
|
|
|
|
| 100 |
}),
|
| 101 |
});
|
| 102 |
|
|
@@ -104,10 +77,6 @@
|
|
| 104 |
throw new Error("Body not defined");
|
| 105 |
}
|
| 106 |
|
| 107 |
-
if (!response.ok) {
|
| 108 |
-
error.set((await response.json())?.message);
|
| 109 |
-
return;
|
| 110 |
-
}
|
| 111 |
// eslint-disable-next-line no-undef
|
| 112 |
const encoder = new TextDecoderStream();
|
| 113 |
const reader = response?.body?.pipeThrough(encoder).getReader();
|
|
@@ -116,7 +85,7 @@
|
|
| 116 |
// this is a bit ugly
|
| 117 |
// we read the stream until we get the final answer
|
| 118 |
while (finalAnswer === "") {
|
| 119 |
-
await new Promise((r) => setTimeout(r, 25));
|
| 120 |
|
| 121 |
// check for abort
|
| 122 |
if (isAborted) {
|
|
@@ -143,7 +112,6 @@
|
|
| 143 |
let update = JSON.parse(el) as MessageUpdate;
|
| 144 |
if (update.type === "finalAnswer") {
|
| 145 |
finalAnswer = update.text;
|
| 146 |
-
reader.cancel();
|
| 147 |
invalidate(UrlDependency.Conversation);
|
| 148 |
} else if (update.type === "stream") {
|
| 149 |
pending = false;
|
|
@@ -218,38 +186,15 @@
|
|
| 218 |
}
|
| 219 |
|
| 220 |
onMount(async () => {
|
| 221 |
-
// only used in case of creating new conversations (from the parent POST endpoint)
|
| 222 |
if ($pendingMessage) {
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
|
|
|
| 226 |
|
| 227 |
-
|
| 228 |
-
if (!data.shared) {
|
| 229 |
-
writeMessage(event.detail);
|
| 230 |
-
} else {
|
| 231 |
-
convFromShared()
|
| 232 |
-
.then(async (convId) => {
|
| 233 |
-
await goto(`${base}/conversation/${convId}`, { invalidateAll: true });
|
| 234 |
-
})
|
| 235 |
-
.then(() => writeMessage(event.detail))
|
| 236 |
-
.finally(() => (loading = false));
|
| 237 |
}
|
| 238 |
-
}
|
| 239 |
-
|
| 240 |
-
async function onRetry(event: CustomEvent<{ id: Message["id"]; content: string }>) {
|
| 241 |
-
if (!data.shared) {
|
| 242 |
-
writeMessage(event.detail.content, event.detail.id);
|
| 243 |
-
} else {
|
| 244 |
-
convFromShared()
|
| 245 |
-
.then(async (convId) => {
|
| 246 |
-
await goto(`${base}/conversation/${convId}`, { invalidateAll: true });
|
| 247 |
-
})
|
| 248 |
-
.then(() => writeMessage(event.detail.content, event.detail.id))
|
| 249 |
-
.finally(() => (loading = false));
|
| 250 |
-
}
|
| 251 |
-
}
|
| 252 |
-
|
| 253 |
$: $page.params.id, (isAborted = true);
|
| 254 |
$: title = data.conversations.find((conv) => conv.id === $page.params.id)?.title ?? data.title;
|
| 255 |
|
|
@@ -274,11 +219,9 @@
|
|
| 274 |
{loading}
|
| 275 |
{pending}
|
| 276 |
{messages}
|
| 277 |
-
shared={data.shared}
|
| 278 |
-
preprompt={data.preprompt}
|
| 279 |
bind:webSearchMessages
|
| 280 |
-
on:message={
|
| 281 |
-
on:retry={
|
| 282 |
on:vote={(event) => voteMessage(event.detail.score, event.detail.id)}
|
| 283 |
on:share={() => shareConversation($page.params.id, data.title)}
|
| 284 |
on:stop={() => (isAborted = true)}
|
|
@@ -286,4 +229,4 @@
|
|
| 286 |
currentModel={findCurrentModel([...data.models, ...data.oldModels], data.model)}
|
| 287 |
settings={data.settings}
|
| 288 |
{loginRequired}
|
| 289 |
-
/>
|
|
|
|
| 1 |
<script lang="ts">
|
| 2 |
import ChatWindow from "$lib/components/chat/ChatWindow.svelte";
|
| 3 |
import { pendingMessage } from "$lib/stores/pendingMessage";
|
| 4 |
+
import { pendingMessageIdToRetry } from "$lib/stores/pendingMessageIdToRetry";
|
| 5 |
import { onMount } from "svelte";
|
| 6 |
import { page } from "$app/stores";
|
| 7 |
+
import { invalidate } from "$app/navigation";
|
| 8 |
import { base } from "$app/paths";
|
| 9 |
import { shareConversation } from "$lib/shareConversation";
|
| 10 |
import { UrlDependency } from "$lib/types/UrlDependency";
|
|
|
|
| 34 |
let pending = false;
|
| 35 |
let loginRequired = false;
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
// this function is used to send new message to the backends
|
| 38 |
async function writeMessage(message: string, messageId = randomUUID()) {
|
| 39 |
if (!message.trim()) return;
|
|
|
|
| 69 |
response_id: responseId,
|
| 70 |
is_retry: isRetry,
|
| 71 |
web_search: $webSearchParameters.useSearch,
|
| 72 |
+
domainFilters: $webSearchParameters.domainFilters,
|
| 73 |
}),
|
| 74 |
});
|
| 75 |
|
|
|
|
| 77 |
throw new Error("Body not defined");
|
| 78 |
}
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
// eslint-disable-next-line no-undef
|
| 81 |
const encoder = new TextDecoderStream();
|
| 82 |
const reader = response?.body?.pipeThrough(encoder).getReader();
|
|
|
|
| 85 |
// this is a bit ugly
|
| 86 |
// we read the stream until we get the final answer
|
| 87 |
while (finalAnswer === "") {
|
| 88 |
+
// await new Promise((r) => setTimeout(r, 25));
|
| 89 |
|
| 90 |
// check for abort
|
| 91 |
if (isAborted) {
|
|
|
|
| 112 |
let update = JSON.parse(el) as MessageUpdate;
|
| 113 |
if (update.type === "finalAnswer") {
|
| 114 |
finalAnswer = update.text;
|
|
|
|
| 115 |
invalidate(UrlDependency.Conversation);
|
| 116 |
} else if (update.type === "stream") {
|
| 117 |
pending = false;
|
|
|
|
| 186 |
}
|
| 187 |
|
| 188 |
onMount(async () => {
|
|
|
|
| 189 |
if ($pendingMessage) {
|
| 190 |
+
const val = $pendingMessage;
|
| 191 |
+
const messageId = $pendingMessageIdToRetry || undefined;
|
| 192 |
+
$pendingMessage = "";
|
| 193 |
+
$pendingMessageIdToRetry = null;
|
| 194 |
|
| 195 |
+
writeMessage(val, messageId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
}
|
| 197 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
$: $page.params.id, (isAborted = true);
|
| 199 |
$: title = data.conversations.find((conv) => conv.id === $page.params.id)?.title ?? data.title;
|
| 200 |
|
|
|
|
| 219 |
{loading}
|
| 220 |
{pending}
|
| 221 |
{messages}
|
|
|
|
|
|
|
| 222 |
bind:webSearchMessages
|
| 223 |
+
on:message={(event) => writeMessage(event.detail)}
|
| 224 |
+
on:retry={(event) => writeMessage(event.detail.content, event.detail.id)}
|
| 225 |
on:vote={(event) => voteMessage(event.detail.score, event.detail.id)}
|
| 226 |
on:share={() => shareConversation($page.params.id, data.title)}
|
| 227 |
on:stop={() => (isAborted = true)}
|
|
|
|
| 229 |
currentModel={findCurrentModel([...data.models, ...data.oldModels], data.model)}
|
| 230 |
settings={data.settings}
|
| 231 |
{loginRequired}
|
| 232 |
+
/>
|