Messages.svelte 7.14 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, db, 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
13
	import UserMessage from './Messages/UserMessage.svelte';
	import ResponseMessage from './Messages/ResponseMessage.svelte';
	import Placeholder from './Messages/Placeholder.svelte';

14
	export let chatId = '';
15
16
17
	export let sendPrompt: Function;
	export let regenerateResponse: Function;

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

24
	export let selectedModelfiles = [];
25

Timothy J. Baek's avatar
Timothy J. Baek committed
26
27
28
29
30
31
32
	$: if (autoScroll && bottomPadding) {
		(async () => {
			await tick();
			window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
		})();
	}

33
34
35
36
37
38
39
40
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
	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);
			}
		);
	};

69
70
	const confirmEditMessage = async (messageId, content) => {
		let userPrompt = content;
71
72
73
74
75
76
77
		let userMessageId = uuidv4();

		let userMessage = {
			id: userMessageId,
			parentId: history.messages[messageId].parentId,
			childrenIds: [],
			role: 'user',
Timothy J. Baek's avatar
Timothy J. Baek committed
78
79
			content: userPrompt,
			...(history.messages[messageId].files && { files: history.messages[messageId].files })
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
		};

		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();
95
		await sendPrompt(userPrompt, userMessageId, chatId);
96
97
	};

98
	const confirmEditResponseMessage = async (messageId, content) => {
Timothy J. Baek's avatar
Timothy J. Baek committed
99
		history.messages[messageId].originalContent = history.messages[messageId].content;
100
		history.messages[messageId].content = content;
Timothy J. Baek's avatar
Timothy J. Baek committed
101
102
103
104
105
106
107
108
109

		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
110
111
	};

112
113
114
	const rateMessage = async (messageId, rating) => {
		history.messages[messageId].rating = rating;
		await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
115
		await updateChatById(localStorage.token, chatId, {
116
117
118
			messages: messages,
			history: history
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
119
120

		await chats.set(await getChatList(localStorage.token));
121
122
123
124
125
126
127
128
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
	};

	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();

		autoScroll = window.innerHeight + window.scrollY >= document.body.offsetHeight - 40;

		setTimeout(() => {
			window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
		}, 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();

		autoScroll = window.innerHeight + window.scrollY >= document.body.offsetHeight - 40;
		setTimeout(() => {
			window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
		}, 100);
	};
</script>

{#if messages.length == 0}
216
	<Placeholder models={selectedModels} modelfiles={selectedModelfiles} />
217
{:else}
Timothy J. Baek's avatar
Timothy J. Baek committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	{#key chatId}
		{#each messages as message, messageIdx}
			<div class=" w-full">
				<div class="flex justify-between px-5 mb-3 max-w-3xl mx-auto rounded-lg group">
					{#if message.role === 'user'}
						<UserMessage
							user={$user}
							{message}
							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}
						/>
					{:else}
						<ResponseMessage
							{message}
							modelfiles={selectedModelfiles}
							siblings={history.messages[message.parentId]?.childrenIds ?? []}
							isLastMessage={messageIdx + 1 === messages.length}
							{confirmEditResponseMessage}
							{showPreviousMessage}
							{showNextMessage}
							{rateMessage}
							{copyToClipboard}
							{regenerateResponse}
						/>
					{/if}
				</div>
251
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
252
		{/each}
Timothy J. Baek's avatar
Timothy J. Baek committed
253

Timothy J. Baek's avatar
Timothy J. Baek committed
254
255
256
257
		{#if bottomPadding}
			<div class=" mb-10" />
		{/if}
	{/key}
258
{/if}