|
| 1 | +<script lang="ts"> |
| 2 | + import { onMount } from 'svelte'; |
| 3 | + import { Loader2, RefreshCw } from '@lucide/svelte'; |
| 4 | + import * as Select from '$lib/components/ui/select'; |
| 5 | + import { Button } from '$lib/components/ui/button'; |
| 6 | + import { Badge } from '$lib/components/ui/badge'; |
| 7 | + import { |
| 8 | + fetchModels, |
| 9 | + modelOptions, |
| 10 | + modelsError, |
| 11 | + modelsLoading, |
| 12 | + modelsUpdating, |
| 13 | + selectModel, |
| 14 | + selectedModelId |
| 15 | + } from '$lib/stores/models.svelte'; |
| 16 | + import type { ModelOption } from '$lib/stores/models.svelte'; |
| 17 | + import { toast } from 'svelte-sonner'; |
| 18 | +
|
| 19 | + let options = $derived(modelOptions()); |
| 20 | + let loading = $derived(modelsLoading()); |
| 21 | + let updating = $derived(modelsUpdating()); |
| 22 | + let error = $derived(modelsError()); |
| 23 | + let activeId = $derived(selectedModelId()); |
| 24 | +
|
| 25 | + let isMounted = $state(false); |
| 26 | +
|
| 27 | + onMount(async () => { |
| 28 | + try { |
| 29 | + await fetchModels(); |
| 30 | + } catch (error) { |
| 31 | + reportError('Unable to load models', error); |
| 32 | + } finally { |
| 33 | + isMounted = true; |
| 34 | + } |
| 35 | + }); |
| 36 | +
|
| 37 | + async function handleRefresh() { |
| 38 | + try { |
| 39 | + await fetchModels(true); |
| 40 | + toast.success('Model list refreshed'); |
| 41 | + } catch (error) { |
| 42 | + reportError('Failed to refresh model list', error); |
| 43 | + } |
| 44 | + } |
| 45 | +
|
| 46 | + async function handleSelect(value: string | undefined) { |
| 47 | + if (!value) return; |
| 48 | +
|
| 49 | + const option = options.find((item) => item.id === value); |
| 50 | + if (!option) { |
| 51 | + reportError('Model is no longer available', new Error('Unknown model')); |
| 52 | + return; |
| 53 | + } |
| 54 | +
|
| 55 | + try { |
| 56 | + await selectModel(option.id); |
| 57 | + toast.success(`Switched to ${option.name}`); |
| 58 | + } catch (error) { |
| 59 | + reportError('Failed to switch model', error); |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + function reportError(message: string, error: unknown) { |
| 64 | + const description = error instanceof Error ? error.message : 'Unknown error'; |
| 65 | + toast.error(message, { description }); |
| 66 | + } |
| 67 | +
|
| 68 | + function getDisplayOption(): ModelOption | undefined { |
| 69 | + if (activeId) { |
| 70 | + return options.find((option) => option.id === activeId); |
| 71 | + } |
| 72 | +
|
| 73 | + return options[0]; |
| 74 | + } |
| 75 | +
|
| 76 | + function getCapabilityLabel(capability: string): string { |
| 77 | + switch (capability.toLowerCase()) { |
| 78 | + case 'vision': |
| 79 | + return 'Vision'; |
| 80 | + case 'audio': |
| 81 | + return 'Audio'; |
| 82 | + case 'multimodal': |
| 83 | + return 'Multimodal'; |
| 84 | + case 'completion': |
| 85 | + return 'Text'; |
| 86 | + default: |
| 87 | + return capability; |
| 88 | + } |
| 89 | + } |
| 90 | +</script> |
| 91 | + |
| 92 | +<div class="rounded-lg border border-border/40 bg-background/5 p-3 shadow-sm"> |
| 93 | + <div class="mb-2 flex items-center justify-between"> |
| 94 | + <p class="text-xs font-medium text-muted-foreground">Model selector</p> |
| 95 | + |
| 96 | + <Button |
| 97 | + aria-label="Refresh model list" |
| 98 | + class="h-7 w-7" |
| 99 | + disabled={loading} |
| 100 | + onclick={handleRefresh} |
| 101 | + size="icon" |
| 102 | + variant="ghost" |
| 103 | + > |
| 104 | + {#if loading} |
| 105 | + <Loader2 class="h-4 w-4 animate-spin" /> |
| 106 | + {:else} |
| 107 | + <RefreshCw class="h-4 w-4" /> |
| 108 | + {/if} |
| 109 | + </Button> |
| 110 | + </div> |
| 111 | + |
| 112 | + {#if loading && options.length === 0 && !isMounted} |
| 113 | + <div class="flex items-center gap-2 text-xs text-muted-foreground"> |
| 114 | + <Loader2 class="h-4 w-4 animate-spin" /> |
| 115 | + Loading models… |
| 116 | + </div> |
| 117 | + {:else if options.length === 0} |
| 118 | + <p class="text-xs text-muted-foreground">No models available.</p> |
| 119 | + {:else} |
| 120 | + {@const selectedOption = getDisplayOption()} |
| 121 | + |
| 122 | + <Select.Root |
| 123 | + type="single" |
| 124 | + value={selectedOption?.id ?? ''} |
| 125 | + onValueChange={handleSelect} |
| 126 | + disabled={loading || updating} |
| 127 | + > |
| 128 | + <Select.Trigger class="h-9 w-full justify-between"> |
| 129 | + <span class="truncate text-sm font-medium">{selectedOption?.name || 'Select model'}</span> |
| 130 | + |
| 131 | + {#if updating} |
| 132 | + <Loader2 class="h-4 w-4 animate-spin text-muted-foreground" /> |
| 133 | + {/if} |
| 134 | + </Select.Trigger> |
| 135 | + |
| 136 | + <Select.Content class="z-[100000]"> |
| 137 | + {#each options as option (option.id)} |
| 138 | + <Select.Item value={option.id} label={option.name}> |
| 139 | + <div class="flex flex-col gap-1"> |
| 140 | + <span class="text-sm font-medium">{option.name}</span> |
| 141 | + |
| 142 | + {#if option.description} |
| 143 | + <span class="text-xs text-muted-foreground">{option.description}</span> |
| 144 | + {/if} |
| 145 | + |
| 146 | + {#if option.capabilities.length > 0} |
| 147 | + <div class="flex flex-wrap gap-1"> |
| 148 | + {#each option.capabilities as capability (capability)} |
| 149 | + <Badge variant="secondary" class="text-[10px]"> |
| 150 | + {getCapabilityLabel(capability)} |
| 151 | + </Badge> |
| 152 | + {/each} |
| 153 | + </div> |
| 154 | + {/if} |
| 155 | + </div> |
| 156 | + </Select.Item> |
| 157 | + {/each} |
| 158 | + </Select.Content> |
| 159 | + </Select.Root> |
| 160 | + |
| 161 | + {#if selectedOption?.capabilities.length} |
| 162 | + <div class="mt-3 flex flex-wrap gap-1"> |
| 163 | + {#each selectedOption.capabilities as capability (capability)} |
| 164 | + <Badge variant="outline" class="text-[10px]"> |
| 165 | + {getCapabilityLabel(capability)} |
| 166 | + </Badge> |
| 167 | + {/each} |
| 168 | + </div> |
| 169 | + {/if} |
| 170 | + {/if} |
| 171 | + |
| 172 | + {#if error} |
| 173 | + <p class="mt-2 text-xs text-destructive">{error}</p> |
| 174 | + {/if} |
| 175 | +</div> |
0 commit comments