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