Interface.svelte 10.4 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
<script lang="ts">
	import { getBackendConfig } from '$lib/apis';
	import { setDefaultPromptSuggestions } from '$lib/apis/configs';
Jannik Streidl's avatar
Jannik Streidl committed
4
	import { config, models, user, showWhatsChanged } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
5
	import { createEventDispatcher, onMount } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
	import toast from 'svelte-french-toast';
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
	const dispatch = createEventDispatcher();

Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
12
13
14
	export let saveSettings: Function;

	// Addons
	let titleAutoGenerate = true;
	let responseAutoCopy = false;
	let titleAutoGenerateModel = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	let fullScreenMode = false;
16
	let titleGenerationPrompt = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
17

Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
	// Interface
	let promptSuggestions = [];
20
	let showUsername = false;
Jannik Streidl's avatar
Jannik Streidl committed
21
	let enableWhatsChanged = true;
22

Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
	const toggleFullScreenMode = async () => {
		fullScreenMode = !fullScreenMode;
		saveSettings({ fullScreenMode: fullScreenMode });
	};

28
29
30
31
32
	const toggleShowUsername = async () => {
		showUsername = !showUsername;
		saveSettings({ showUsername: showUsername });
	};

Jannik Streidl's avatar
Jannik Streidl committed
33
34
35
36
37
	const toggleenableWhatsChanged = async () => {
		enableWhatsChanged = !enableWhatsChanged;
		saveSettings({ enableWhatsChanged: enableWhatsChanged });
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
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
	const toggleTitleAutoGenerate = async () => {
		titleAutoGenerate = !titleAutoGenerate;
		saveSettings({ titleAutoGenerate: titleAutoGenerate });
	};

	const toggleResponseAutoCopy = async () => {
		const permission = await navigator.clipboard
			.readText()
			.then(() => {
				return 'granted';
			})
			.catch(() => {
				return '';
			});

		console.log(permission);

		if (permission === 'granted') {
			responseAutoCopy = !responseAutoCopy;
			saveSettings({ responseAutoCopy: responseAutoCopy });
		} else {
			toast.error(
				'Clipboard write permission denied. Please check your browser settings to grant the necessary access.'
			);
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
65
	const updateInterfaceHandler = async () => {
66
67
68
69
70
71
72
73
		if ($user.role === 'admin') {
			promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
			await config.set(await getBackendConfig());
		}

		saveSettings({
			titleGenerationPrompt: titleGenerationPrompt ? titleGenerationPrompt : undefined
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
77
78
79
	};

	onMount(async () => {
		if ($user.role === 'admin') {
			promptSuggestions = $config?.default_prompt_suggestions;
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
80
81
82
83
84

		let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');

		titleAutoGenerate = settings.titleAutoGenerate ?? true;
		responseAutoCopy = settings.responseAutoCopy ?? false;
85
		showUsername = settings.showUsername ?? false;
Jannik Streidl's avatar
Jannik Streidl committed
86
		enableWhatsChanged = settings.enableWhatsChanged ?? true;
Timothy J. Baek's avatar
Timothy J. Baek committed
87
		fullScreenMode = settings.fullScreenMode ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
88
		titleAutoGenerateModel = settings.titleAutoGenerateModel ?? '';
89
90
91
		titleGenerationPrompt =
			settings.titleGenerationPrompt ??
			`Create a concise, 3-5 word phrase as a header for the following query, strictly adhering to the 3-5 word limit and avoiding the use of the word 'title': {{prompt}}`;
Timothy J. Baek's avatar
Timothy J. Baek committed
92
93
94
95
96
97
98
99
100
101
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
		updateInterfaceHandler();
		dispatch('save');
	}}
>
Timothy J. Baek's avatar
Timothy J. Baek committed
102
103
104
105
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
	<div class=" space-y-3 pr-1.5 overflow-y-scroll h-80">
		<div>
			<div class=" mb-1 text-sm font-medium">WebUI Add-ons</div>

			<div>
				<div class=" py-0.5 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">Title Auto-Generation</div>

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleTitleAutoGenerate();
						}}
						type="button"
					>
						{#if titleAutoGenerate === true}
							<span class="ml-2 self-center">On</span>
						{:else}
							<span class="ml-2 self-center">Off</span>
						{/if}
					</button>
				</div>
			</div>

			<div>
				<div class=" py-0.5 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">Response AutoCopy to Clipboard</div>

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleResponseAutoCopy();
						}}
						type="button"
					>
						{#if responseAutoCopy === true}
							<span class="ml-2 self-center">On</span>
						{:else}
							<span class="ml-2 self-center">Off</span>
						{/if}
					</button>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
145

Timothy J. Baek's avatar
Timothy J. Baek committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
			<div>
				<div class=" py-0.5 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">Full Screen Mode</div>

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleFullScreenMode();
						}}
						type="button"
					>
						{#if fullScreenMode === true}
							<span class="ml-2 self-center">On</span>
						{:else}
							<span class="ml-2 self-center">Off</span>
						{/if}
					</button>
				</div>
			</div>

166
167
			<div>
				<div class=" py-0.5 flex w-full justify-between">
Timothy J. Baek's avatar
Timothy J. Baek committed
168
169
170
					<div class=" self-center text-xs font-medium">
						Display the username instead of "You" in the Chat
					</div>
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleShowUsername();
						}}
						type="button"
					>
						{#if showUsername === true}
							<span class="ml-2 self-center">On</span>
						{:else}
							<span class="ml-2 self-center">Off</span>
						{/if}
					</button>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
187
188
		</div>

Jannik Streidl's avatar
Jannik Streidl committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
		<div>
			<div class=" py-0.5 flex w-full justify-between">
				<div class=" self-center text-xs font-medium">Show "WhatsChanged" Modal on Startup</div>

				<button
					class="p-1 px-3 text-xs flex rounded transition"
					on:click={() => {
						toggleenableWhatsChanged();
					}}
					type="button"
				>
					{#if enableWhatsChanged === true}
						<span class="ml-2 self-center">On</span>
					{:else}
						<span class="ml-2 self-center">Off</span>
					{/if}
				</button>
			</div>
		</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
209
		<hr class=" dark:border-gray-700" />
Timothy J. Baek's avatar
Timothy J. Baek committed
210

Timothy J. Baek's avatar
Timothy J. Baek committed
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
238
239
240
241
242
243
244
245
246
247
		<div>
			<div class=" mb-2.5 text-sm font-medium">Set Title Auto-Generation Model</div>
			<div class="flex w-full">
				<div class="flex-1 mr-2">
					<select
						class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
						bind:value={titleAutoGenerateModel}
						placeholder="Select a model"
					>
						<option value="" selected>Current Model</option>
						{#each $models.filter((m) => m.size != null) as model}
							<option value={model.name} class="bg-gray-100 dark:bg-gray-700"
								>{model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}</option
							>
						{/each}
					</select>
				</div>
				<button
					class="px-3 bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-800 dark:text-gray-100 rounded transition"
					on:click={() => {
						saveSettings({
							titleAutoGenerateModel:
								titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined
						});
					}}
					type="button"
				>
					<svg
						xmlns="http://www.w3.org/2000/svg"
						viewBox="0 0 16 16"
						fill="currentColor"
						class="w-3.5 h-3.5"
					>
						<path
							fill-rule="evenodd"
							d="M13.836 2.477a.75.75 0 0 1 .75.75v3.182a.75.75 0 0 1-.75.75h-3.182a.75.75 0 0 1 0-1.5h1.37l-.84-.841a4.5 4.5 0 0 0-7.08.932.75.75 0 0 1-1.3-.75 6 6 0 0 1 9.44-1.242l.842.84V3.227a.75.75 0 0 1 .75-.75Zm-.911 7.5A.75.75 0 0 1 13.199 11a6 6 0 0 1-9.44 1.241l-.84-.84v1.371a.75.75 0 0 1-1.5 0V9.591a.75.75 0 0 1 .75-.75H5.35a.75.75 0 0 1 0 1.5H3.98l.841.841a4.5 4.5 0 0 0 7.08-.932.75.75 0 0 1 1.025-.273Z"
							clip-rule="evenodd"
Timothy J. Baek's avatar
Timothy J. Baek committed
248
						/>
Timothy J. Baek's avatar
Timothy J. Baek committed
249
250
251
					</svg>
				</button>
			</div>
252
253
254
255
256
257
258
259
			<div class="mt-3">
				<div class=" mb-2.5 text-sm font-medium">Title Generation Prompt</div>
				<textarea
					bind:value={titleGenerationPrompt}
					class="w-full rounded p-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none resize-none"
					rows="3"
				/>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
260
261
262
263
264
265
266
267
		</div>

		{#if $user.role === 'admin'}
			<hr class=" dark:border-gray-700" />

			<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
				<div class="flex w-full justify-between mb-2">
					<div class=" self-center text-sm font-semibold">Default Prompt Suggestions</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
268
269

					<button
Timothy J. Baek's avatar
Timothy J. Baek committed
270
						class="p-1 px-3 text-xs flex rounded transition"
Timothy J. Baek's avatar
Timothy J. Baek committed
271
272
						type="button"
						on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
273
274
275
							if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
								promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
							}
Timothy J. Baek's avatar
Timothy J. Baek committed
276
277
278
279
280
281
282
283
284
						}}
					>
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 20 20"
							fill="currentColor"
							class="w-4 h-4"
						>
							<path
Timothy J. Baek's avatar
Timothy J. Baek committed
285
								d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
Timothy J. Baek's avatar
Timothy J. Baek committed
286
287
288
289
							/>
						</svg>
					</button>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
				<div class="flex flex-col space-y-1">
					{#each promptSuggestions as prompt, promptIdx}
						<div class=" flex border dark:border-gray-600 rounded-lg">
							<div class="flex flex-col flex-1">
								<div class="flex border-b dark:border-gray-600 w-full">
									<input
										class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
										placeholder="Title (e.g. Tell me a fun fact)"
										bind:value={prompt.title[0]}
									/>

									<input
										class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
										placeholder="Subtitle (e.g. about the Roman Empire)"
										bind:value={prompt.title[1]}
									/>
								</div>

								<input
									class="px-3 py-1.5 text-xs w-full bg-transparent outline-none border-r dark:border-gray-600"
									placeholder="Prompt (e.g. Tell me a fun fact about the Roman Empire)"
									bind:value={prompt.content}
								/>
							</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
314

Timothy J. Baek's avatar
Timothy J. Baek committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
							<button
								class="px-2"
								type="button"
								on:click={() => {
									promptSuggestions.splice(promptIdx, 1);
									promptSuggestions = promptSuggestions;
								}}
							>
								<svg
									xmlns="http://www.w3.org/2000/svg"
									viewBox="0 0 20 20"
									fill="currentColor"
									class="w-4 h-4"
								>
									<path
										d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
									/>
								</svg>
							</button>
						</div>
					{/each}
				</div>

				{#if promptSuggestions.length > 0}
					<div class="text-xs text-left w-full mt-2">
						Adjusting these settings will apply changes universally to all users.
					</div>
				{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
343
344
345
346
347
348
349
350
351
352
353
354
355
			</div>
		{/if}
	</div>

	<div class="flex justify-end pt-3 text-sm font-medium">
		<button
			class=" px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-gray-100 transition rounded"
			type="submit"
		>
			Save
		</button>
	</div>
</form>