aboutsummaryrefslogtreecommitdiff
path: root/llama.cpp/tools/server/webui/src/lib/components/app/models/ModelsSelector.svelte
blob: efc9cd4e2f8085bd65ce86707f509fdfebd369e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
295
296
297
298
299
300
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<script lang="ts">
	import { onMount, tick } from 'svelte';
	import { ChevronDown, EyeOff, Loader2, MicOff, Package, Power } from '@lucide/svelte';
	import * as Tooltip from '$lib/components/ui/tooltip';
	import * as Popover from '$lib/components/ui/popover';
	import { cn } from '$lib/components/ui/utils';
	import {
		modelsStore,
		modelOptions,
		modelsLoading,
		modelsUpdating,
		selectedModelId,
		routerModels,
		propsCacheVersion,
		singleModelName
	} from '$lib/stores/models.svelte';
	import { usedModalities, conversationsStore } from '$lib/stores/conversations.svelte';
	import { ServerModelStatus } from '$lib/enums';
	import { isRouterMode } from '$lib/stores/server.svelte';
	import { DialogModelInformation, SearchInput } from '$lib/components/app';
	import type { ModelOption } from '$lib/types/models';

	interface Props {
		class?: string;
		currentModel?: string | null;
		/** Callback when model changes. Return false to keep menu open (e.g., for validation failures) */
		onModelChange?: (modelId: string, modelName: string) => Promise<boolean> | boolean | void;
		disabled?: boolean;
		forceForegroundText?: boolean;
		/** When true, user's global selection takes priority over currentModel (for form selector) */
		useGlobalSelection?: boolean;
		/**
		 * When provided, only consider modalities from messages BEFORE this message.
		 * Used for regeneration - allows selecting models that don't support modalities
		 * used in later messages.
		 */
		upToMessageId?: string;
	}

	let {
		class: className = '',
		currentModel = null,
		onModelChange,
		disabled = false,
		forceForegroundText = false,
		useGlobalSelection = false,
		upToMessageId
	}: Props = $props();

	let options = $derived(modelOptions());
	let loading = $derived(modelsLoading());
	let updating = $derived(modelsUpdating());
	let activeId = $derived(selectedModelId());
	let isRouter = $derived(isRouterMode());
	let serverModel = $derived(singleModelName());

	// Reactive router models state - needed for proper reactivity of status checks
	let currentRouterModels = $derived(routerModels());

	let requiredModalities = $derived(
		upToMessageId ? conversationsStore.getModalitiesUpToMessage(upToMessageId) : usedModalities()
	);

	function getModelStatus(modelId: string): ServerModelStatus | null {
		const model = currentRouterModels.find((m) => m.id === modelId);
		return (model?.status?.value as ServerModelStatus) ?? null;
	}

	/**
	 * Checks if a model supports all modalities used in the conversation.
	 * Returns true if the model can be selected, false if it should be disabled.
	 */
	function isModelCompatible(option: ModelOption): boolean {
		void propsCacheVersion();

		const modelModalities = modelsStore.getModelModalities(option.model);

		if (!modelModalities) {
			const status = getModelStatus(option.model);

			if (status === ServerModelStatus.LOADED) {
				if (requiredModalities.vision || requiredModalities.audio) return false;
			}

			return true;
		}

		if (requiredModalities.vision && !modelModalities.vision) return false;
		if (requiredModalities.audio && !modelModalities.audio) return false;

		return true;
	}

	/**
	 * Gets missing modalities for a model.
	 * Returns object with vision/audio booleans indicating what's missing.
	 */
	function getMissingModalities(option: ModelOption): { vision: boolean; audio: boolean } | null {
		void propsCacheVersion();

		const modelModalities = modelsStore.getModelModalities(option.model);

		if (!modelModalities) {
			const status = getModelStatus(option.model);

			if (status === ServerModelStatus.LOADED) {
				const missing = {
					vision: requiredModalities.vision,
					audio: requiredModalities.audio
				};

				if (missing.vision || missing.audio) return missing;
			}

			return null;
		}

		const missing = {
			vision: requiredModalities.vision && !modelModalities.vision,
			audio: requiredModalities.audio && !modelModalities.audio
		};

		if (!missing.vision && !missing.audio) return null;

		return missing;
	}

	let isHighlightedCurrentModelActive = $derived(
		!isRouter || !currentModel
			? false
			: (() => {
					const currentOption = options.find((option) => option.model === currentModel);

					return currentOption ? currentOption.id === activeId : false;
				})()
	);

	let isCurrentModelInCache = $derived(() => {
		if (!isRouter || !currentModel) return true;

		return options.some((option) => option.model === currentModel);
	});

	let searchTerm = $state('');
	let searchInputRef = $state<HTMLInputElement | null>(null);
	let highlightedIndex = $state<number>(-1);

	let filteredOptions: ModelOption[] = $derived(
		(() => {
			const term = searchTerm.trim().toLowerCase();
			if (!term) return options;

			return options.filter(
				(option) =>
					option.model.toLowerCase().includes(term) || option.name?.toLowerCase().includes(term)
			);
		})()
	);

	// Get indices of compatible options for keyboard navigation
	let compatibleIndices = $derived(
		filteredOptions
			.map((option, index) => (isModelCompatible(option) ? index : -1))
			.filter((i) => i !== -1)
	);

	// Reset highlighted index when search term changes
	$effect(() => {
		void searchTerm;
		highlightedIndex = -1;
	});

	let isOpen = $state(false);
	let showModelDialog = $state(false);

	onMount(() => {
		modelsStore.fetch().catch((error) => {
			console.error('Unable to load models:', error);
		});
	});

	// Handle changes to the model selector pop-down or the model dialog, depending on if the server is in
	// router mode or not.
	function handleOpenChange(open: boolean) {
		if (loading || updating) return;

		if (isRouter) {
			if (open) {
				isOpen = true;
				searchTerm = '';
				highlightedIndex = -1;

				// Focus search input after popover opens
				tick().then(() => {
					requestAnimationFrame(() => searchInputRef?.focus());
				});

				modelsStore.fetchRouterModels().then(() => {
					modelsStore.fetchModalitiesForLoadedModels();
				});
			} else {
				isOpen = false;
				searchTerm = '';
				highlightedIndex = -1;
			}
		} else {
			showModelDialog = open;
		}
	}

	export function open() {
		handleOpenChange(true);
	}

	function handleSearchKeyDown(event: KeyboardEvent) {
		if (event.isComposing) return;

		if (event.key === 'ArrowDown') {
			event.preventDefault();
			if (compatibleIndices.length === 0) return;

			const currentPos = compatibleIndices.indexOf(highlightedIndex);
			if (currentPos === -1 || currentPos === compatibleIndices.length - 1) {
				highlightedIndex = compatibleIndices[0];
			} else {
				highlightedIndex = compatibleIndices[currentPos + 1];
			}
		} else if (event.key === 'ArrowUp') {
			event.preventDefault();
			if (compatibleIndices.length === 0) return;

			const currentPos = compatibleIndices.indexOf(highlightedIndex);
			if (currentPos === -1 || currentPos === 0) {
				highlightedIndex = compatibleIndices[compatibleIndices.length - 1];
			} else {
				highlightedIndex = compatibleIndices[currentPos - 1];
			}
		} else if (event.key === 'Enter') {
			event.preventDefault();
			if (highlightedIndex >= 0 && highlightedIndex < filteredOptions.length) {
				const option = filteredOptions[highlightedIndex];
				if (isModelCompatible(option)) {
					handleSelect(option.id);
				}
			} else if (compatibleIndices.length > 0) {
				// No selection - highlight first compatible option
				highlightedIndex = compatibleIndices[0];
			}
		}
	}

	async function handleSelect(modelId: string) {
		const option = options.find((opt) => opt.id === modelId);
		if (!option) return;

		let shouldCloseMenu = true;

		if (onModelChange) {
			// If callback provided, use it (for regenerate functionality)
			const result = await onModelChange(option.id, option.model);

			// If callback returns false, keep menu open (validation failed)
			if (result === false) {
				shouldCloseMenu = false;
			}
		} else {
			// Update global selection
			await modelsStore.selectModelById(option.id);

			// Load the model if not already loaded (router mode)
			if (isRouter && getModelStatus(option.model) !== ServerModelStatus.LOADED) {
				try {
					await modelsStore.loadModel(option.model);
				} catch (error) {
					console.error('Failed to load model:', error);
				}
			}
		}

		if (shouldCloseMenu) {
			handleOpenChange(false);

			// Focus the chat textarea after model selection
			requestAnimationFrame(() => {
				const textarea = document.querySelector<HTMLTextAreaElement>(
					'[data-slot="chat-form"] textarea'
				);
				textarea?.focus();
			});
		}
	}

	function getDisplayOption(): ModelOption | undefined {
		if (!isRouter) {
			if (serverModel) {
				return {
					id: 'current',
					model: serverModel,
					name: serverModel.split('/').pop() || serverModel,
					capabilities: [] // Empty array for single model mode
				};
			}

			return undefined;
		}

		// When useGlobalSelection is true (form selector), prioritize user selection
		// Otherwise (message display), prioritize currentModel
		if (useGlobalSelection && activeId) {
			const selected = options.find((option) => option.id === activeId);
			if (selected) return selected;
		}

		// Show currentModel (from message payload or conversation)
		if (currentModel) {
			if (!isCurrentModelInCache()) {
				return {
					id: 'not-in-cache',
					model: currentModel,
					name: currentModel.split('/').pop() || currentModel,
					capabilities: []
				};
			}

			return options.find((option) => option.model === currentModel);
		}

		// Fallback to user selection (for new chats before first message)
		if (activeId) {
			return options.find((option) => option.id === activeId);
		}

		// No selection - return undefined to show "Select model"
		return undefined;
	}
</script>

<div class={cn('relative inline-flex flex-col items-end gap-1', className)}>
	{#if loading && options.length === 0 && isRouter}
		<div class="flex items-center gap-2 text-xs text-muted-foreground">
			<Loader2 class="h-3.5 w-3.5 animate-spin" />
			Loading models…
		</div>
	{:else if options.length === 0 && isRouter}
		<p class="text-xs text-muted-foreground">No models available.</p>
	{:else}
		{@const selectedOption = getDisplayOption()}

		{#if isRouter}
			<Popover.Root bind:open={isOpen} onOpenChange={handleOpenChange}>
				<Popover.Trigger
					class={cn(
						`inline-flex cursor-pointer items-center gap-1.5 rounded-sm bg-muted-foreground/10 px-1.5 py-1 text-xs transition hover:text-foreground focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-60`,
						!isCurrentModelInCache()
							? 'bg-red-400/10 !text-red-400 hover:bg-red-400/20 hover:text-red-400'
							: forceForegroundText
								? 'text-foreground'
								: isHighlightedCurrentModelActive
									? 'text-foreground'
									: 'text-muted-foreground',
						isOpen ? 'text-foreground' : ''
					)}
					style="max-width: min(calc(100cqw - 6.5rem), 32rem)"
					disabled={disabled || updating}
				>
					<Package class="h-3.5 w-3.5" />

					<span class="truncate font-medium">
						{selectedOption?.model || 'Select model'}
					</span>

					{#if updating}
						<Loader2 class="h-3 w-3.5 animate-spin" />
					{:else}
						<ChevronDown class="h-3 w-3.5" />
					{/if}
				</Popover.Trigger>

				<Popover.Content
					class="group/popover-content w-96 max-w-[calc(100vw-2rem)] p-0"
					align="end"
					sideOffset={8}
					collisionPadding={16}
				>
					<div class="flex max-h-[50dvh] flex-col overflow-hidden">
						<div
							class="order-1 shrink-0 border-b p-4 group-data-[side=top]/popover-content:order-2 group-data-[side=top]/popover-content:border-t group-data-[side=top]/popover-content:border-b-0"
						>
							<SearchInput
								id="model-search"
								placeholder="Search models..."
								bind:value={searchTerm}
								bind:ref={searchInputRef}
								onClose={() => handleOpenChange(false)}
								onKeyDown={handleSearchKeyDown}
							/>
						</div>
						<div
							class="models-list order-2 min-h-0 flex-1 overflow-y-auto group-data-[side=top]/popover-content:order-1"
						>
							{#if !isCurrentModelInCache() && currentModel}
								<!-- Show unavailable model as first option (disabled) -->
								<button
									type="button"
									class="flex w-full cursor-not-allowed items-center bg-red-400/10 px-4 py-2 text-left text-sm text-red-400"
									role="option"
									aria-selected="true"
									aria-disabled="true"
									disabled
								>
									<span class="truncate">{selectedOption?.name || currentModel}</span>
									<span class="ml-2 text-xs whitespace-nowrap opacity-70">(not available)</span>
								</button>
								<div class="my-1 h-px bg-border"></div>
							{/if}
							{#if filteredOptions.length === 0}
								<p class="px-4 py-3 text-sm text-muted-foreground">No models found.</p>
							{/if}
							{#each filteredOptions as option, index (option.id)}
								{@const status = getModelStatus(option.model)}
								{@const isLoaded = status === ServerModelStatus.LOADED}
								{@const isLoading = status === ServerModelStatus.LOADING}
								{@const isSelected = currentModel === option.model || activeId === option.id}
								{@const isCompatible = isModelCompatible(option)}
								{@const isHighlighted = index === highlightedIndex}
								{@const missingModalities = getMissingModalities(option)}

								<div
									class={cn(
										'group flex w-full items-center gap-2 px-4 py-2 text-left text-sm transition focus:outline-none',
										isCompatible
											? 'cursor-pointer hover:bg-muted focus:bg-muted'
											: 'cursor-not-allowed opacity-50',
										isSelected || isHighlighted
											? 'bg-accent text-accent-foreground'
											: isCompatible
												? 'hover:bg-accent hover:text-accent-foreground'
												: '',
										isLoaded ? 'text-popover-foreground' : 'text-muted-foreground'
									)}
									role="option"
									aria-selected={isSelected || isHighlighted}
									aria-disabled={!isCompatible}
									tabindex={isCompatible ? 0 : -1}
									onclick={() => isCompatible && handleSelect(option.id)}
									onmouseenter={() => (highlightedIndex = index)}
									onkeydown={(e) => {
										if (isCompatible && (e.key === 'Enter' || e.key === ' ')) {
											e.preventDefault();
											handleSelect(option.id);
										}
									}}
								>
									<span class="min-w-0 flex-1 truncate">{option.model}</span>

									{#if missingModalities}
										<span class="flex shrink-0 items-center gap-1 text-muted-foreground/70">
											{#if missingModalities.vision}
												<Tooltip.Root>
													<Tooltip.Trigger>
														<EyeOff class="h-3.5 w-3.5" />
													</Tooltip.Trigger>
													<Tooltip.Content class="z-[9999]">
														<p>No vision support</p>
													</Tooltip.Content>
												</Tooltip.Root>
											{/if}
											{#if missingModalities.audio}
												<Tooltip.Root>
													<Tooltip.Trigger>
														<MicOff class="h-3.5 w-3.5" />
													</Tooltip.Trigger>
													<Tooltip.Content class="z-[9999]">
														<p>No audio support</p>
													</Tooltip.Content>
												</Tooltip.Root>
											{/if}
										</span>
									{/if}

									{#if isLoading}
										<Tooltip.Root>
											<Tooltip.Trigger>
												<Loader2 class="h-4 w-4 shrink-0 animate-spin text-muted-foreground" />
											</Tooltip.Trigger>
											<Tooltip.Content class="z-[9999]">
												<p>Loading model...</p>
											</Tooltip.Content>
										</Tooltip.Root>
									{:else if isLoaded}
										<Tooltip.Root>
											<Tooltip.Trigger>
												<button
													type="button"
													class="relative ml-2 flex h-4 w-4 shrink-0 items-center justify-center"
													onclick={(e) => {
														e.stopPropagation();
														modelsStore.unloadModel(option.model);
													}}
												>
													<span
														class="mr-2 h-2 w-2 rounded-full bg-green-500 transition-opacity group-hover:opacity-0"
													></span>
													<Power
														class="absolute mr-2 h-4 w-4 text-red-500 opacity-0 transition-opacity group-hover:opacity-100 hover:text-red-600"
													/>
												</button>
											</Tooltip.Trigger>
											<Tooltip.Content class="z-[9999]">
												<p>Unload model</p>
											</Tooltip.Content>
										</Tooltip.Root>
									{:else}
										<span class="mx-2 h-2 w-2 rounded-full bg-muted-foreground/50"></span>
									{/if}
								</div>
							{/each}
						</div>
					</div>
				</Popover.Content>
			</Popover.Root>
		{:else}
			<button
				class={cn(
					`inline-flex cursor-pointer items-center gap-1.5 rounded-sm bg-muted-foreground/10 px-1.5 py-1 text-xs transition hover:text-foreground focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-60`,
					!isCurrentModelInCache()
						? 'bg-red-400/10 !text-red-400 hover:bg-red-400/20 hover:text-red-400'
						: forceForegroundText
							? 'text-foreground'
							: isHighlightedCurrentModelActive
								? 'text-foreground'
								: 'text-muted-foreground',
					isOpen ? 'text-foreground' : ''
				)}
				style="max-width: min(calc(100cqw - 6.5rem), 32rem)"
				onclick={() => handleOpenChange(true)}
				disabled={disabled || updating}
			>
				<Package class="h-3.5 w-3.5" />

				<span class="truncate font-medium">
					{selectedOption?.model}
				</span>

				{#if updating}
					<Loader2 class="h-3 w-3.5 animate-spin" />
				{/if}
			</button>
		{/if}
	{/if}
</div>

{#if showModelDialog && !isRouter}
	<DialogModelInformation bind:open={showModelDialog} />
{/if}