Messages.svelte 10.1 KB
Newer Older
1
2
3
<script lang="ts">
	import { v4 as uuidv4 } from 'uuid';

Timothy J. Baek's avatar
Timothy J. Baek committed
4
	import { chats, config, modelfiles, settings, user } from '$lib/stores';
5
6
7
	import { tick } from 'svelte';

	import toast from 'svelte-french-toast';
Timothy J. Baek's avatar
Timothy J. Baek committed
8
	import { getChatList, updateChatById } from '$lib/apis/chats';
9

10
11
12
	import UserMessage from './Messages/UserMessage.svelte';
	import ResponseMessage from './Messages/ResponseMessage.svelte';
	import Placeholder from './Messages/Placeholder.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
13
	import Spinner from '../common/Spinner.svelte';
14

15
	export let chatId = '';
16
	export let sendPrompt: Function;
Timothy J. Baek's avatar
Timothy J. Baek committed
17
	export let continueGeneration: Function;
18
19
	export let regenerateResponse: Function;

Timothy J. Baek's avatar
Timothy J. Baek committed
20
	export let processing = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
21
	export let bottomPadding = false;
22
	export let autoScroll;
23
	export let selectedModels;
24
25
26
	export let history = {};
	export let messages = [];

27
	export let selectedModelfiles = [];
28

Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
	$: if (autoScroll && bottomPadding) {
		(async () => {
			await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
32
			scrollToBottom();
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
35
		})();
	}

Timothy J. Baek's avatar
Timothy J. Baek committed
36
37
38
39
40
	const scrollToBottom = () => {
		const element = document.getElementById('messages-container');
		element.scrollTop = element.scrollHeight;
	};

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	const copyToClipboard = (text) => {
		if (!navigator.clipboard) {
			var textArea = document.createElement('textarea');
			textArea.value = text;

			// Avoid scrolling to bottom
			textArea.style.top = '0';
			textArea.style.left = '0';
			textArea.style.position = 'fixed';

			document.body.appendChild(textArea);
			textArea.focus();
			textArea.select();

			try {
				var successful = document.execCommand('copy');
				var msg = successful ? 'successful' : 'unsuccessful';
				console.log('Fallback: Copying text command was ' + msg);
			} catch (err) {
				console.error('Fallback: Oops, unable to copy', err);
			}

			document.body.removeChild(textArea);
			return;
		}
		navigator.clipboard.writeText(text).then(
			function () {
				console.log('Async: Copying to clipboard was successful!');
				toast.success('Copying to clipboard was successful!');
			},
			function (err) {
				console.error('Async: Could not copy text: ', err);
			}
		);
	};

77
78
	const confirmEditMessage = async (messageId, content) => {
		let userPrompt = content;
79
80
81
82
83
84
85
		let userMessageId = uuidv4();

		let userMessage = {
			id: userMessageId,
			parentId: history.messages[messageId].parentId,
			childrenIds: [],
			role: 'user',
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
			content: userPrompt,
			...(history.messages[messageId].files && { files: history.messages[messageId].files })
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
		};

		let messageParentId = history.messages[messageId].parentId;

		if (messageParentId !== null) {
			history.messages[messageParentId].childrenIds = [
				...history.messages[messageParentId].childrenIds,
				userMessageId
			];
		}

		history.messages[userMessageId] = userMessage;
		history.currentId = userMessageId;

		await tick();
103
		await sendPrompt(userPrompt, userMessageId, chatId);
104
105
	};

106
	const confirmEditResponseMessage = async (messageId, content) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
107
		history.messages[messageId].originalContent = history.messages[messageId].content;
108
		history.messages[messageId].content = content;
Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
112
113
114
115
116
117

		await tick();

		await updateChatById(localStorage.token, chatId, {
			messages: messages,
			history: history
		});

		await chats.set(await getChatList(localStorage.token));
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
	};

120
121
122
	const rateMessage = async (messageId, rating) => {
		history.messages[messageId].rating = rating;
		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
123
		await updateChatById(localStorage.token, chatId, {
124
125
126
			messages: messages,
			history: history
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
127
128

		await chats.set(await getChatList(localStorage.token));
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	};

	const showPreviousMessage = async (message) => {
		if (message.parentId !== null) {
			let messageId =
				history.messages[message.parentId].childrenIds[
					Math.max(history.messages[message.parentId].childrenIds.indexOf(message.id) - 1, 0)
				];

			if (message.id !== messageId) {
				let messageChildrenIds = history.messages[messageId].childrenIds;

				while (messageChildrenIds.length !== 0) {
					messageId = messageChildrenIds.at(-1);
					messageChildrenIds = history.messages[messageId].childrenIds;
				}

				history.currentId = messageId;
			}
		} else {
			let childrenIds = Object.values(history.messages)
				.filter((message) => message.parentId === null)
				.map((message) => message.id);
			let messageId = childrenIds[Math.max(childrenIds.indexOf(message.id) - 1, 0)];

			if (message.id !== messageId) {
				let messageChildrenIds = history.messages[messageId].childrenIds;

				while (messageChildrenIds.length !== 0) {
					messageId = messageChildrenIds.at(-1);
					messageChildrenIds = history.messages[messageId].childrenIds;
				}

				history.currentId = messageId;
			}
		}

		await tick();

Timothy J. Baek's avatar
Timothy J. Baek committed
168
		const element = document.getElementById('messages-container');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
169
		autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
170
171

		setTimeout(() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
172
			scrollToBottom();
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
		}, 100);
	};

	const showNextMessage = async (message) => {
		if (message.parentId !== null) {
			let messageId =
				history.messages[message.parentId].childrenIds[
					Math.min(
						history.messages[message.parentId].childrenIds.indexOf(message.id) + 1,
						history.messages[message.parentId].childrenIds.length - 1
					)
				];

			if (message.id !== messageId) {
				let messageChildrenIds = history.messages[messageId].childrenIds;

				while (messageChildrenIds.length !== 0) {
					messageId = messageChildrenIds.at(-1);
					messageChildrenIds = history.messages[messageId].childrenIds;
				}

				history.currentId = messageId;
			}
		} else {
			let childrenIds = Object.values(history.messages)
				.filter((message) => message.parentId === null)
				.map((message) => message.id);
			let messageId =
				childrenIds[Math.min(childrenIds.indexOf(message.id) + 1, childrenIds.length - 1)];

			if (message.id !== messageId) {
				let messageChildrenIds = history.messages[messageId].childrenIds;

				while (messageChildrenIds.length !== 0) {
					messageId = messageChildrenIds.at(-1);
					messageChildrenIds = history.messages[messageId].childrenIds;
				}

				history.currentId = messageId;
			}
		}

		await tick();

Timothy J. Baek's avatar
Timothy J. Baek committed
217
		const element = document.getElementById('messages-container');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
218
		autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
Timothy J. Baek's avatar
Timothy J. Baek committed
219

220
		setTimeout(() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
221
			scrollToBottom();
222
223
		}, 100);
	};
224

Timothy J. Baek's avatar
Timothy J. Baek committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	// TODO: change delete behaviour
	// const deleteMessageAndDescendants = async (messageId: string) => {
	// 	if (history.messages[messageId]) {
	// 		history.messages[messageId].deleted = true;

	// 		for (const childId of history.messages[messageId].childrenIds) {
	// 			await deleteMessageAndDescendants(childId);
	// 		}
	// 	}
	// };

	// const triggerDeleteMessageRecursive = async (messageId: string) => {
	// 	await deleteMessageAndDescendants(messageId);
	// 	await updateChatById(localStorage.token, chatId, { history });
	// 	await chats.set(await getChatList(localStorage.token));
	// };

	const messageDeleteHandler = async (messageId) => {
243
244
		if (history.messages[messageId]) {
			history.messages[messageId].deleted = true;
245

246
			for (const childId of history.messages[messageId].childrenIds) {
Timothy J. Baek's avatar
Timothy J. Baek committed
247
				history.messages[childId].deleted = true;
248
			}
249
		}
250
		await updateChatById(localStorage.token, chatId, { history });
251
	};
252
253
254
</script>

{#if messages.length == 0}
255
	<Placeholder models={selectedModels} modelfiles={selectedModelfiles} />
256
{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
257
258
259
	<div class=" pb-10">
		{#key chatId}
			{#each messages as message, messageIdx}
260
261
262
263
264
265
266
267
268
				{#if !message.deleted}
					<div class=" w-full">
						<div
							class="flex flex-col justify-between px-5 mb-3 {$settings?.fullScreenMode ?? null
								? 'max-w-full'
								: 'max-w-3xl'} mx-auto rounded-lg group"
						>
							{#if message.role === 'user'}
								<UserMessage
Timothy J. Baek's avatar
Timothy J. Baek committed
269
									on:delete={() => messageDeleteHandler(message.id)}
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
									user={$user}
									{message}
									isFirstMessage={messageIdx === 0}
									siblings={message.parentId !== null
										? history.messages[message.parentId]?.childrenIds ?? []
										: Object.values(history.messages)
												.filter((message) => message.parentId === null)
												.map((message) => message.id) ?? []}
									{confirmEditMessage}
									{showPreviousMessage}
									{showNextMessage}
									{copyToClipboard}
								/>

								{#if messages.length - 1 === messageIdx && processing !== ''}
									<div class="flex my-2.5 ml-12 items-center w-fit space-x-2.5">
										<div class=" dark:text-blue-100">
											<svg
												class=" w-4 h-4 translate-y-[0.5px]"
												fill="currentColor"
												viewBox="0 0 24 24"
												xmlns="http://www.w3.org/2000/svg"
												><style>
													.spinner_qM83 {
														animation: spinner_8HQG 1.05s infinite;
Timothy J. Baek's avatar
Timothy J. Baek committed
295
													}
296
297
													.spinner_oXPr {
														animation-delay: 0.1s;
Timothy J. Baek's avatar
Timothy J. Baek committed
298
													}
299
300
													.spinner_ZTLf {
														animation-delay: 0.2s;
Timothy J. Baek's avatar
Timothy J. Baek committed
301
													}
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
													@keyframes spinner_8HQG {
														0%,
														57.14% {
															animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
															transform: translate(0);
														}
														28.57% {
															animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
															transform: translateY(-6px);
														}
														100% {
															transform: translate(0);
														}
													}
												</style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
													class="spinner_qM83 spinner_oXPr"
													cx="12"
													cy="12"
													r="2.5"
												/><circle class="spinner_qM83 spinner_ZTLf" cx="20" cy="12" r="2.5" /></svg
											>
										</div>
										<div class=" text-sm font-medium">
											{processing}
										</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
327
									</div>
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
								{/if}
							{:else}
								<ResponseMessage
									{message}
									modelfiles={selectedModelfiles}
									siblings={history.messages[message.parentId]?.childrenIds ?? []}
									isLastMessage={messageIdx + 1 === messages.length}
									{confirmEditResponseMessage}
									{showPreviousMessage}
									{showNextMessage}
									{rateMessage}
									{copyToClipboard}
									{continueGeneration}
									{regenerateResponse}
								/>
Timothy J. Baek's avatar
Timothy J. Baek committed
343
							{/if}
344
						</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
345
					</div>
346
				{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
347
			{/each}
Timothy J. Baek's avatar
Timothy J. Baek committed
348

Timothy J. Baek's avatar
Timothy J. Baek committed
349
350
351
352
353
			{#if bottomPadding}
				<div class=" mb-10" />
			{/if}
		{/key}
	</div>
354
{/if}