Interface.svelte 11.5 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';
4
	import { config, models, settings, user } from '$lib/stores';
5
	import { createEventDispatcher, onMount, getContext } from 'svelte';
Jannik Streidl's avatar
Jannik Streidl committed
6
	import { toast } from 'svelte-sonner';
7
	import Tooltip from '$lib/components/common/Tooltip.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
	const dispatch = createEventDispatcher();

10
11
	const i18n = getContext('i18n');

Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
14
15
16
17
	export let saveSettings: Function;

	// Addons
	let titleAutoGenerate = true;
	let responseAutoCopy = false;
	let titleAutoGenerateModel = '';
18
	let titleAutoGenerateModelExternal = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
19
	let fullScreenMode = false;
20
	let titleGenerationPrompt = '';
21
	let splitLargeChunks = false;
Timothy J. Baek's avatar
Timothy J. Baek committed
22

Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
	// Interface
	let promptSuggestions = [];
25
26
	let showUsername = false;

27
28
29
30
31
	const toggleSplitLargeChunks = async () => {
		splitLargeChunks = !splitLargeChunks;
		saveSettings({ splitLargeChunks: splitLargeChunks });
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34
35
36
	const toggleFullScreenMode = async () => {
		fullScreenMode = !fullScreenMode;
		saveSettings({ fullScreenMode: fullScreenMode });
	};

37
38
39
40
41
	const toggleShowUsername = async () => {
		showUsername = !showUsername;
		saveSettings({ showUsername: showUsername });
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
42
43
	const toggleTitleAutoGenerate = async () => {
		titleAutoGenerate = !titleAutoGenerate;
44
45
46
47
48
49
		saveSettings({
			title: {
				...$settings.title,
				auto: titleAutoGenerate
			}
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	};

	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
74
	const updateInterfaceHandler = async () => {
75
76
77
78
79
80
		if ($user.role === 'admin') {
			promptSuggestions = await setDefaultPromptSuggestions(localStorage.token, promptSuggestions);
			await config.set(await getBackendConfig());
		}

		saveSettings({
81
82
83
84
85
86
87
			title: {
				...$settings.title,
				model: titleAutoGenerateModel !== '' ? titleAutoGenerateModel : undefined,
				modelExternal:
					titleAutoGenerateModelExternal !== '' ? titleAutoGenerateModelExternal : undefined,
				prompt: titleGenerationPrompt ? titleGenerationPrompt : undefined
			}
88
		});
Timothy J. Baek's avatar
Timothy J. Baek committed
89
90
91
92
93
94
	};

	onMount(async () => {
		if ($user.role === 'admin') {
			promptSuggestions = $config?.default_prompt_suggestions;
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
95
96
97

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

98
99
100
		titleAutoGenerate = settings?.title?.auto ?? true;
		titleAutoGenerateModel = settings?.title?.model ?? '';
		titleAutoGenerateModelExternal = settings?.title?.modelExternal ?? '';
101
		titleGenerationPrompt =
102
			settings?.title?.prompt ??
Ased Mammad's avatar
Ased Mammad committed
103
104
105
			$i18n.t(
				"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}}';
106
107
108
109

		responseAutoCopy = settings.responseAutoCopy ?? false;
		showUsername = settings.showUsername ?? false;
		fullScreenMode = settings.fullScreenMode ?? false;
110
		splitLargeChunks = settings.splitLargeChunks ?? false;
Timothy J. Baek's avatar
Timothy J. Baek committed
111
112
113
114
115
116
117
118
119
120
	});
</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
121
	<div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
Timothy J. Baek's avatar
Timothy J. Baek committed
122
		<div>
123
			<div class=" mb-1 text-sm font-medium">{$i18n.t('WebUI Add-ons')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
126

			<div>
				<div class=" py-0.5 flex w-full justify-between">
127
					<div class=" self-center text-xs font-medium">{$i18n.t('Title Auto-Generation')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
128
129
130
131
132
133
134
135
136

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleTitleAutoGenerate();
						}}
						type="button"
					>
						{#if titleAutoGenerate === true}
137
							<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
138
						{:else}
139
							<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
140
141
142
143
144
145
146
						{/if}
					</button>
				</div>
			</div>

			<div>
				<div class=" py-0.5 flex w-full justify-between">
147
148
149
					<div class=" self-center text-xs font-medium">
						{$i18n.t('Response AutoCopy to Clipboard')}
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
150
151
152
153
154
155
156
157
158

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

Timothy J. Baek's avatar
Timothy J. Baek committed
167
168
			<div>
				<div class=" py-0.5 flex w-full justify-between">
169
					<div class=" self-center text-xs font-medium">{$i18n.t('Full Screen Mode')}</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
170
171
172
173
174
175
176
177
178

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleFullScreenMode();
						}}
						type="button"
					>
						{#if fullScreenMode === true}
179
							<span class="ml-2 self-center">{$i18n.t('On')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
180
						{:else}
181
							<span class="ml-2 self-center">{$i18n.t('Off')}</span>
Timothy J. Baek's avatar
Timothy J. Baek committed
182
183
184
185
186
						{/if}
					</button>
				</div>
			</div>

187
188
			<div>
				<div class=" py-0.5 flex w-full justify-between">
Timothy J. Baek's avatar
Timothy J. Baek committed
189
					<div class=" self-center text-xs font-medium">
190
						{$i18n.t('Display the username instead of You in the Chat')}
Timothy J. Baek's avatar
Timothy J. Baek committed
191
					</div>
192
193
194
195
196
197
198
199
200

					<button
						class="p-1 px-3 text-xs flex rounded transition"
						on:click={() => {
							toggleShowUsername();
						}}
						type="button"
					>
						{#if showUsername === true}
201
							<span class="ml-2 self-center">{$i18n.t('On')}</span>
202
						{:else}
203
							<span class="ml-2 self-center">{$i18n.t('Off')}</span>
204
205
206
207
						{/if}
					</button>
				</div>
			</div>
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229

			<div>
				<div class=" py-0.5 flex w-full justify-between">
					<div class=" self-center text-xs font-medium">
						{$i18n.t('Fluidly stream large external response chunks')}
					</div>

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

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

Timothy J. Baek's avatar
Timothy J. Baek committed
234
		<div>
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
			<div class=" mb-2.5 text-sm font-medium flex">
				<div class=" mr-1">{$i18n.t('Set Task Model')}</div>
				<Tooltip
					content={$i18n.t('A task model is used when performing tasks such as generating titles for chats and web search queries')}>
					<svg
						xmlns="http://www.w3.org/2000/svg"
						fill="none"
						viewBox="0 0 24 24"
						stroke-width="1.5"
						stroke="currentColor"
						class="w-5 h-5"
					>
						<path
							stroke-linecap="round"
							stroke-linejoin="round"
							d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"
						/>
					</svg>
				</Tooltip>
			</div>
255
256
257
			<div class="flex w-full gap-2 pr-2">
				<div class="flex-1">
					<div class=" text-xs mb-1">Local Models</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
258
					<select
Timothy J. Baek's avatar
Timothy J. Baek committed
259
						class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
260
						bind:value={titleAutoGenerateModel}
261
						placeholder={$i18n.t('Select a model')}
Timothy J. Baek's avatar
Timothy J. Baek committed
262
					>
263
						<option value="" selected>{$i18n.t('Current Model')}</option>
264
265
266
267
268
269
						{#each $models as model}
							{#if model.size != null}
								<option value={model.name} class="bg-gray-100 dark:bg-gray-700">
									{model.name + ' (' + (model.size / 1024 ** 3).toFixed(1) + ' GB)'}
								</option>
							{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
270
271
272
						{/each}
					</select>
				</div>
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

				<div class="flex-1">
					<div class=" text-xs mb-1">External Models</div>
					<select
						class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
						bind:value={titleAutoGenerateModelExternal}
						placeholder={$i18n.t('Select a model')}
					>
						<option value="" selected>{$i18n.t('Current Model')}</option>
						{#each $models as model}
							{#if model.name !== 'hr'}
								<option value={model.name} class="bg-gray-100 dark:bg-gray-700">
									{model.name}
								</option>
							{/if}
						{/each}
					</select>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
291
			</div>
292

Timothy J. Baek's avatar
Timothy J. Baek committed
293
			<div class="mt-3 mr-2">
294
				<div class=" mb-2.5 text-sm font-medium">{$i18n.t('Title Generation Prompt')}</div>
295
296
				<textarea
					bind:value={titleGenerationPrompt}
Timothy J. Baek's avatar
Timothy J. Baek committed
297
					class="w-full rounded-lg p-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none resize-none"
298
299
300
					rows="3"
				/>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
301
302
303
304
305
306
307
		</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">
308
309
310
					<div class=" self-center text-sm font-semibold">
						{$i18n.t('Default Prompt Suggestions')}
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
311
312

					<button
Timothy J. Baek's avatar
Timothy J. Baek committed
313
						class="p-1 px-3 text-xs flex rounded transition"
Timothy J. Baek's avatar
Timothy J. Baek committed
314
315
						type="button"
						on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
316
317
318
							if (promptSuggestions.length === 0 || promptSuggestions.at(-1).content !== '') {
								promptSuggestions = [...promptSuggestions, { content: '', title: ['', ''] }];
							}
Timothy J. Baek's avatar
Timothy J. Baek committed
319
320
321
322
323
324
325
326
327
						}}
					>
						<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
328
								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
329
330
331
332
							/>
						</svg>
					</button>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
333
334
335
336
337
338
339
				<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"
340
										placeholder={$i18n.t('Title (e.g. Tell me a fun fact)')}
Timothy J. Baek's avatar
Timothy J. Baek committed
341
342
343
344
345
										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"
346
										placeholder={$i18n.t('Subtitle (e.g. about the Roman Empire)')}
Timothy J. Baek's avatar
Timothy J. Baek committed
347
348
349
350
351
352
										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"
353
									placeholder={$i18n.t('Prompt (e.g. Tell me a fun fact about the Roman Empire)')}
Timothy J. Baek's avatar
Timothy J. Baek committed
354
355
356
									bind:value={prompt.content}
								/>
							</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
357

Timothy J. Baek's avatar
Timothy J. Baek committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
							<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">
Jannik Streidl's avatar
Jannik Streidl committed
383
						{$i18n.t('Adjusting these settings will apply changes universally to all users.')}
Timothy J. Baek's avatar
Timothy J. Baek committed
384
385
					</div>
				{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
386
387
388
389
			</div>
		{/if}
	</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
390
	<div class="flex justify-end text-sm font-medium">
Timothy J. Baek's avatar
Timothy J. Baek committed
391
		<button
Timothy J. Baek's avatar
Timothy J. Baek committed
392
			class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
Timothy J. Baek's avatar
Timothy J. Baek committed
393
394
			type="submit"
		>
Jannik Streidl's avatar
Jannik Streidl committed
395
			{$i18n.t('Save')}
Timothy J. Baek's avatar
Timothy J. Baek committed
396
397
398
		</button>
	</div>
</form>