]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/blob
c533acc750050ced8c9878048fd3411a6e318fb1
[pkg/ggml/sources/llama.cpp] /
1 <script lang="ts">
2 import { ICON_CLASS_DEFAULT } from '$lib/constants/css-classes';
3 import type { Snippet } from 'svelte';
4 import * as Tooltip from '$lib/components/ui/tooltip';
5 import * as Sheet from '$lib/components/ui/sheet';
6 import * as Collapsible from '$lib/components/ui/collapsible';
7 import { File, MessageSquare, Zap, FolderOpen } from '@lucide/svelte';
8 import { Switch } from '$lib/components/ui/switch';
9 import { Checkbox } from '$lib/components/ui/checkbox';
10 import { TOOLTIP_DELAY_DURATION } from '$lib/constants';
11 import { ATTACHMENT_FILE_ITEMS } from '$lib/constants/attachment-menu';
12 import { useAttachmentMenu } from '$lib/hooks/use-attachment-menu.svelte';
13 import { useToolsPanel } from '$lib/hooks/use-tools-panel.svelte';
14 import { useReasoningMenu } from '$lib/hooks/use-reasoning-menu.svelte';
15 import { conversationsStore } from '$lib/stores/conversations.svelte';
16 import { mcpStore } from '$lib/stores/mcp.svelte';
17 import { McpLogo } from '$lib/components/app';
18 import {
19 PencilRuler,
20 ChevronDown,
21 ChevronRight,
22 Lightbulb,
23 LightbulbOff,
24 Check
25 } from '@lucide/svelte';
26 import { HealthCheckStatus } from '$lib/enums';
27 import { AttachmentAction } from '$lib/enums/attachment.enums';
28
29 interface Props {
30 class?: string;
31 disabled?: boolean;
32 hasAudioModality?: boolean;
33 hasVideoModality?: boolean;
34 hasVisionModality?: boolean;
35 hasMcpPromptsSupport?: boolean;
36 hasMcpResourcesSupport?: boolean;
37 onFileUpload?: () => void;
38 onSystemPromptClick?: () => void;
39 onMcpPromptClick?: () => void;
40 onMcpResourcesClick?: () => void;
41 trigger: Snippet<[{ disabled: boolean; onclick?: () => void }]>;
42 }
43
44 let {
45 class: className = '',
46 disabled = false,
47 hasAudioModality = false,
48 hasVisionModality = false,
49 hasVideoModality = false,
50 hasMcpPromptsSupport = false,
51 hasMcpResourcesSupport = false,
52 onFileUpload,
53 onSystemPromptClick,
54 onMcpPromptClick,
55 onMcpResourcesClick,
56 trigger
57 }: Props = $props();
58
59 let sheetOpen = $state(false);
60 let reasoningExpanded = $state(false);
61 let filesExpanded = $state(true);
62 let toolsExpanded = $state(false);
63 let mcpExpanded = $state(false);
64
65 const attachmentMenu = useAttachmentMenu(
66 () => ({
67 hasVisionModality,
68 hasAudioModality,
69 hasVideoModality,
70 hasMcpPromptsSupport,
71 hasMcpResourcesSupport
72 }),
73 () => ({ onFileUpload, onSystemPromptClick, onMcpPromptClick, onMcpResourcesClick }),
74 () => {
75 sheetOpen = false;
76 }
77 );
78
79 const toolsPanel = useToolsPanel();
80 const reasoning = useReasoningMenu();
81
82 const sheetItemClass =
83 'flex w-full items-center gap-3 rounded-md px-3 py-2.5 text-left text-sm transition-colors hover:bg-accent active:bg-accent disabled:cursor-not-allowed disabled:opacity-50';
84
85 const sheetItemRowClass =
86 'flex w-full items-center justify-between gap-2 rounded-md px-3 py-2 text-left text-sm transition-colors hover:bg-accent';
87
88 let mcpServers = $derived(mcpStore.getServers());
89 </script>
90
91 <div class="flex items-center gap-1 {className}">
92 <Sheet.Root bind:open={sheetOpen}>
93 {@render trigger({ disabled, onclick: () => (sheetOpen = true) })}
94
95 <Sheet.Content side="bottom" class="max-h-[85vh] gap-0 overflow-y-auto">
96 <Sheet.Header>
97 <Sheet.Title>Add to chat</Sheet.Title>
98
99 <Sheet.Description class="sr-only">
100 Add files, system prompt or configure MCP servers
101 </Sheet.Description>
102 </Sheet.Header>
103
104 <div class="flex flex-col gap-1 px-1.5 pb-2">
105 {#if reasoning.modelSupportsThinking}
106 <Collapsible.Root
107 open={reasoningExpanded}
108 onOpenChange={(open) => (reasoningExpanded = open)}
109 >
110 <Collapsible.Trigger class={sheetItemClass}>
111 {#if reasoningExpanded}
112 <ChevronDown class="{ICON_CLASS_DEFAULT} shrink-0" />
113 {:else}
114 <ChevronRight class="{ICON_CLASS_DEFAULT} shrink-0" />
115 {/if}
116
117 {#if reasoning.thinkingEnabled}
118 <Lightbulb class="{ICON_CLASS_DEFAULT} shrink-0 text-amber-400" />
119 {:else}
120 <LightbulbOff class="{ICON_CLASS_DEFAULT} shrink-0 text-muted-foreground" />
121 {/if}
122
123 <span class="flex-1">Reasoning</span>
124
125 <span class="text-xs capitalize text-muted-foreground">
126 {reasoning.thinkingEnabled ? reasoning.currentEffort : 'off'}
127 </span>
128 </Collapsible.Trigger>
129
130 <Collapsible.Content>
131 <div class="flex flex-col gap-0.5 pl-4">
132 {#each reasoning.levels as level (level.value)}
133 {@const tokenLabel = reasoning.tokenLabel(level)}
134 <button
135 type="button"
136 class={sheetItemRowClass}
137 class:bg-accent={reasoning.isSelected(level)}
138 onclick={() => reasoning.select(level)}
139 >
140 <div class="flex min-w-0 items-center gap-3">
141 {#if reasoning.isSelected(level)}
142 <Check class="{ICON_CLASS_DEFAULT} shrink-0 text-foreground" />
143 {:else}
144 <div class="{ICON_CLASS_DEFAULT} shrink-0"></div>
145 {/if}
146
147 <span class="text-sm">{level.label}</span>
148 </div>
149
150 {#if tokenLabel}
151 <span class="shrink-0 text-[11px] text-muted-foreground opacity-60">
152 {tokenLabel}
153 </span>
154 {/if}
155 </button>
156 {/each}
157 </div>
158 </Collapsible.Content>
159 </Collapsible.Root>
160 {/if}
161
162 <Collapsible.Root open={filesExpanded} onOpenChange={(open) => (filesExpanded = open)}>
163 <Collapsible.Trigger class={sheetItemClass}>
164 {#if filesExpanded}
165 <ChevronDown class="{ICON_CLASS_DEFAULT} shrink-0" />
166 {:else}
167 <ChevronRight class="{ICON_CLASS_DEFAULT} shrink-0" />
168 {/if}
169
170 <File class="{ICON_CLASS_DEFAULT} shrink-0" />
171
172 <span class="flex-1">Add files</span>
173 </Collapsible.Trigger>
174
175 <Collapsible.Content>
176 <div class="flex flex-col gap-0.5 pl-4">
177 {#each ATTACHMENT_FILE_ITEMS as item (item.id)}
178 {@const enabled = attachmentMenu.isItemEnabled(item.enabledWhen)}
179 {#if enabled}
180 <button
181 type="button"
182 class={sheetItemClass}
183 onclick={() => attachmentMenu.callbacks[item.action]()}
184 >
185 <item.icon class="{ICON_CLASS_DEFAULT} shrink-0" />
186
187 <span>{item.label}</span>
188 </button>
189 {:else if item.disabledTooltip}
190 <Tooltip.Root delayDuration={TOOLTIP_DELAY_DURATION}>
191 <Tooltip.Trigger>
192 <button type="button" class={sheetItemClass} disabled>
193 <item.icon class="{ICON_CLASS_DEFAULT} shrink-0" />
194
195 <span>{item.label}</span>
196 </button>
197 </Tooltip.Trigger>
198
199 <Tooltip.Content side="right">
200 <p>{item.disabledTooltip}</p>
201 </Tooltip.Content>
202 </Tooltip.Root>
203 {/if}
204 {/each}
205 </div>
206 </Collapsible.Content>
207 </Collapsible.Root>
208
209 <Collapsible.Root open={mcpExpanded} onOpenChange={(open) => (mcpExpanded = open)}>
210 <Collapsible.Trigger class={sheetItemClass}>
211 {#if mcpExpanded}
212 <ChevronDown class="{ICON_CLASS_DEFAULT} shrink-0" />
213 {:else}
214 <ChevronRight class="{ICON_CLASS_DEFAULT} shrink-0" />
215 {/if}
216
217 <McpLogo class="inline {ICON_CLASS_DEFAULT} shrink-0" />
218
219 <span class="flex-1">MCP Servers</span>
220
221 <span class="text-xs text-muted-foreground">
222 {mcpServers.length} server{mcpServers.length !== 1 ? 's' : ''}
223 </span>
224 </Collapsible.Trigger>
225
226 <Collapsible.Content>
227 <div class="flex flex-col gap-0.5 pl-4">
228 {#each mcpServers as server (server.id)}
229 {@const healthState = mcpStore.getHealthCheckState(server.id)}
230 {@const hasError = healthState.status === HealthCheckStatus.ERROR}
231 {@const displayName = mcpStore.getServerLabel(server)}
232 {@const faviconUrl = mcpStore.getServerFavicon(server.id)}
233 {@const isEnabled = conversationsStore.isMcpServerEnabledForChat(server.id)}
234
235 <button
236 type="button"
237 class={sheetItemRowClass}
238 onclick={() => !hasError && conversationsStore.toggleMcpServerForChat(server.id)}
239 disabled={hasError}
240 >
241 <div class="flex min-w-0 flex-1 items-center gap-2">
242 {#if faviconUrl}
243 <img
244 src={faviconUrl}
245 alt=""
246 class="{ICON_CLASS_DEFAULT} shrink-0 rounded-sm"
247 onerror={(e) => {
248 (e.currentTarget as HTMLImageElement).style.display = 'none';
249 }}
250 />
251 {/if}
252
253 <span class="min-w-0 truncate text-sm">{displayName}</span>
254 </div>
255
256 {#if hasError}
257 <span
258 class="shrink-0 rounded bg-destructive/15 px-1.5 py-0.5 text-xs text-destructive"
259 >
260 Error
261 </span>
262 {:else}
263 <Switch
264 checked={isEnabled}
265 onCheckedChange={() => conversationsStore.toggleMcpServerForChat(server.id)}
266 />
267 {/if}
268 </button>
269 {/each}
270
271 {#if mcpServers.length === 0}
272 <div class="px-3 py-2 text-center text-sm text-muted-foreground">
273 No MCP servers configured
274 </div>
275 {/if}
276 </div>
277 </Collapsible.Content>
278 </Collapsible.Root>
279
280 {#if toolsPanel.totalToolCount > 0}
281 <Collapsible.Root open={toolsExpanded} onOpenChange={(open) => (toolsExpanded = open)}>
282 <Collapsible.Trigger class={sheetItemClass}>
283 {#if toolsExpanded}
284 <ChevronDown class="{ICON_CLASS_DEFAULT} shrink-0" />
285 {:else}
286 <ChevronRight class="{ICON_CLASS_DEFAULT} shrink-0" />
287 {/if}
288
289 <PencilRuler class="inline {ICON_CLASS_DEFAULT} shrink-0" />
290
291 <span class="flex-1">Tools</span>
292
293 <span class="text-xs text-muted-foreground">
294 {toolsPanel.totalToolCount} tool{toolsPanel.totalToolCount !== 1 ? 's' : ''}
295 </span>
296 </Collapsible.Trigger>
297
298 <Collapsible.Content>
299 <div class="flex flex-col gap-0.5 pl-4">
300 {#each toolsPanel.activeGroups as group (group.label)}
301 {@const checked = toolsPanel.isGroupChecked(group)}
302 {@const enabledCount = toolsPanel.getEnabledToolCount(group)}
303 {@const favicon = toolsPanel.getFavicon(group)}
304
305 <button
306 type="button"
307 class={sheetItemRowClass}
308 onclick={() => toolsPanel.toggleGroupByLabel(group.label)}
309 >
310 {#if favicon}
311 <img
312 src={favicon}
313 alt=""
314 class="{ICON_CLASS_DEFAULT} shrink-0 rounded-sm"
315 onerror={(e) => {
316 (e.currentTarget as HTMLImageElement).style.display = 'none';
317 }}
318 />
319 {/if}
320
321 <span class="min-w-0 flex-1 truncate text-sm font-medium">{group.label}</span>
322
323 <span class="shrink-0 text-xs text-muted-foreground">
324 {enabledCount}/{group.tools.length}
325 </span>
326
327 <Checkbox
328 {checked}
329 class="{ICON_CLASS_DEFAULT} shrink-0"
330 onclick={(e) => e.stopPropagation()}
331 onCheckedChange={() => toolsPanel.toggleGroupByLabel(group.label)}
332 />
333 </button>
334 {/each}
335 </div>
336 </Collapsible.Content>
337 </Collapsible.Root>
338 {/if}
339
340 <button
341 type="button"
342 class={sheetItemClass}
343 onclick={() => attachmentMenu.callbacks[AttachmentAction.SYSTEM_PROMPT_CLICK]()}
344 >
345 <MessageSquare class="{ICON_CLASS_DEFAULT} shrink-0" />
346
347 <span>System Message</span>
348 </button>
349
350 {#if hasMcpPromptsSupport}
351 <button
352 type="button"
353 class={sheetItemClass}
354 onclick={() => attachmentMenu.callbacks[AttachmentAction.MCP_PROMPT_CLICK]()}
355 >
356 <Zap class="{ICON_CLASS_DEFAULT} shrink-0" />
357
358 <span>MCP Prompt</span>
359 </button>
360 {/if}
361
362 {#if hasMcpResourcesSupport}
363 <button
364 type="button"
365 class={sheetItemClass}
366 onclick={() => attachmentMenu.callbacks[AttachmentAction.MCP_RESOURCES_CLICK]()}
367 >
368 <FolderOpen class="{ICON_CLASS_DEFAULT} shrink-0" />
369
370 <span>MCP Resources</span>
371 </button>
372 {/if}
373 </div>
374 </Sheet.Content>
375 </Sheet.Root>
376 </div>