"vscode:/vscode.git/clone" did not exist on "12d7ae96b9ad6084890e123e5537bd6d0d1f3368"
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
	import { tick, getContext } from 'svelte';
6

Jannik Streidl's avatar
Jannik Streidl committed
7
	import { toast } from 'svelte-sonner';
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
	import { imageGenerations } from '$lib/apis/images';
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	import { copyToClipboard } from '$lib/utils';
16

17
18
	const i18n = getContext('i18n');

19
	export let chatId = '';
20
	export let readOnly = false;
21
	export let sendPrompt: Function;
Timothy J. Baek's avatar
Timothy J. Baek committed
22
	export let continueGeneration: Function;
23
24
	export let regenerateResponse: Function;

Timothy J. Baek's avatar
Timothy J. Baek committed
25
	export let processing = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
26
	export let bottomPadding = false;
27
	export let autoScroll;
28
	export let selectedModels;
29
30
31
	export let history = {};
	export let messages = [];

32
	export let selectedModelfiles = [];
33

Timothy J. Baek's avatar
Timothy J. Baek committed
34
35
36
	$: if (autoScroll && bottomPadding) {
		(async () => {
			await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
37
			scrollToBottom();
Timothy J. Baek's avatar
Timothy J. Baek committed
38
39
40
		})();
	}

Timothy J. Baek's avatar
Timothy J. Baek committed
41
42
43
44
45
	const scrollToBottom = () => {
		const element = document.getElementById('messages-container');
		element.scrollTop = element.scrollHeight;
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
46
47
48
49
	const copyToClipboardWithToast = async (text) => {
		const res = await copyToClipboard(text);
		if (res) {
			toast.success($i18n.t('Copying to clipboard was successful!'));
50
51
52
		}
	};

53
54
	const confirmEditMessage = async (messageId, content) => {
		let userPrompt = content;
55
56
57
58
59
60
61
		let userMessageId = uuidv4();

		let userMessage = {
			id: userMessageId,
			parentId: history.messages[messageId].parentId,
			childrenIds: [],
			role: 'user',
Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
			content: userPrompt,
			...(history.messages[messageId].files && { files: history.messages[messageId].files })
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		};

		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();
79
		await sendPrompt(userPrompt, userMessageId, chatId);
80
81
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
82
	const updateChatMessages = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
85
86
87
88
89
		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
90
91
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
92
93
94
95
96
97
98
	const confirmEditResponseMessage = async (messageId, content) => {
		history.messages[messageId].originalContent = history.messages[messageId].content;
		history.messages[messageId].content = content;

		await updateChatMessages();
	};

99
	const rateMessage = async (messageId, rating) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
100
101
102
103
		history.messages[messageId].annotation = {
			...history.messages[messageId].annotation,
			rating: rating
		};
Timothy J. Baek's avatar
Timothy J. Baek committed
104

Timothy J. Baek's avatar
Timothy J. Baek committed
105
		await updateChatMessages();
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	};

	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
145
		const element = document.getElementById('messages-container');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
146
		autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
147
148

		setTimeout(() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
149
			scrollToBottom();
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		}, 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
194
		const element = document.getElementById('messages-container');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
195
		autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
Timothy J. Baek's avatar
Timothy J. Baek committed
196

197
		setTimeout(() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
198
			scrollToBottom();
199
200
		}, 100);
	};
201

Timothy J. Baek's avatar
Timothy J. Baek committed
202
	const messageDeleteHandler = async (messageId) => {
Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
203
204
205
206
207
		const messageToDelete = history.messages[messageId];
		const messageParentId = messageToDelete.parentId;
		const messageChildrenIds = messageToDelete.childrenIds ?? [];
		const hasSibling = messageChildrenIds.some(
			(childId) => history.messages[childId]?.childrenIds?.length > 0
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
208
		);
Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
		messageChildrenIds.forEach((childId) => {
			const child = history.messages[childId];
			if (child && child.childrenIds) {
				if (child.childrenIds.length === 0 && !hasSibling) {
					// if last prompt/response pair
					history.messages[messageParentId].childrenIds = [];
					history.currentId = messageParentId;
				} else {
					child.childrenIds.forEach((grandChildId) => {
						if (history.messages[grandChildId]) {
							history.messages[grandChildId].parentId = messageParentId;
							history.messages[messageParentId].childrenIds.push(grandChildId);
						}
					});
				}
			}
			// remove response
			history.messages[messageParentId].childrenIds = history.messages[
				messageParentId
			].childrenIds.filter((id) => id !== childId);
		});
		// remove prompt
		history.messages[messageParentId].childrenIds = history.messages[
			messageParentId
		].childrenIds.filter((id) => id !== messageId);
		await updateChatById(localStorage.token, chatId, {
			messages: messages,
			history: history
		});
238
	};
Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
239
240
241
242
243
244

	// const messageDeleteHandler = async (messageId) => {
	// 	const message = history.messages[messageId];
	// 	const parentId = message.parentId;
	// 	const childrenIds = message.childrenIds ?? [];
	// 	const grandchildrenIds = [];
Timothy J. Baek's avatar
Timothy J. Baek committed
245

Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
246
247
248
249
250
251
252
253
	// 	// Iterate through childrenIds to find grandchildrenIds
	// 	for (const childId of childrenIds) {
	// 		const childMessage = history.messages[childId];
	// 		const grandChildrenIds = childMessage.childrenIds ?? [];

	// 		for (const grandchildId of grandchildrenIds) {
	// 			const childMessage = history.messages[grandchildId];
	// 			childMessage.parentId = parentId;
Timothy J. Baek's avatar
Timothy J. Baek committed
254
	// 		}
Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
255
	// 		grandchildrenIds.push(...grandChildrenIds);
Timothy J. Baek's avatar
Timothy J. Baek committed
256
257
	// 	}

Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	// 	history.messages[parentId].childrenIds.push(...grandchildrenIds);
	// 	history.messages[parentId].childrenIds = history.messages[parentId].childrenIds.filter(
	// 		(id) => id !== messageId
	// 	);

	// 	// Select latest message
	// 	let currentMessageId = grandchildrenIds.at(-1);
	// 	if (currentMessageId) {
	// 		let messageChildrenIds = history.messages[currentMessageId].childrenIds;
	// 		while (messageChildrenIds.length !== 0) {
	// 			currentMessageId = messageChildrenIds.at(-1);
	// 			messageChildrenIds = history.messages[currentMessageId].childrenIds;
	// 		}
	// 		history.currentId = currentMessageId;
	// 	}
273

Timothy J. Baek's avatar
revert  
Timothy J. Baek committed
274
275
	// 	await updateChatById(localStorage.token, chatId, { messages, history });
	// };
276
277
278
</script>

{#if messages.length == 0}
279
	<Placeholder models={selectedModels} modelfiles={selectedModelfiles} />
280
{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
281
282
283
	<div class=" pb-10">
		{#key chatId}
			{#each messages as message, messageIdx}
Danny Liu's avatar
Danny Liu committed
284
285
286
287
288
289
290
291
292
293
				<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
								on:delete={() => messageDeleteHandler(message.id)}
								user={$user}
294
								{readOnly}
Danny Liu's avatar
Danny Liu committed
295
296
297
298
299
300
301
302
303
304
								{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}
Timothy J. Baek's avatar
Timothy J. Baek committed
305
								copyToClipboard={copyToClipboardWithToast}
Danny Liu's avatar
Danny Liu committed
306
307
308
309
310
311
312
							/>
						{:else}
							<ResponseMessage
								{message}
								modelfiles={selectedModelfiles}
								siblings={history.messages[message.parentId]?.childrenIds ?? []}
								isLastMessage={messageIdx + 1 === messages.length}
313
								{readOnly}
Timothy J. Baek's avatar
Timothy J. Baek committed
314
								{updateChatMessages}
Danny Liu's avatar
Danny Liu committed
315
316
317
318
								{confirmEditResponseMessage}
								{showPreviousMessage}
								{showNextMessage}
								{rateMessage}
Timothy J. Baek's avatar
Timothy J. Baek committed
319
								copyToClipboard={copyToClipboardWithToast}
Danny Liu's avatar
Danny Liu committed
320
321
322
323
324
325
326
327
328
329
330
331
332
333
								{continueGeneration}
								{regenerateResponse}
								on:save={async (e) => {
									console.log('save', e);

									const message = e.detail;
									history.messages[message.id] = message;
									await updateChatById(localStorage.token, chatId, {
										messages: messages,
										history: history
									});
								}}
							/>
						{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
334
					</div>
Danny Liu's avatar
Danny Liu committed
335
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
336
			{/each}
Timothy J. Baek's avatar
Timothy J. Baek committed
337

Timothy J. Baek's avatar
Timothy J. Baek committed
338
339
340
341
342
			{#if bottomPadding}
				<div class=" mb-10" />
			{/if}
		{/key}
	</div>
343
{/if}