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';
22 hasAudioModality?: boolean;
23 hasVideoModality?: boolean;
24 hasVisionModality?: boolean;
25 hasMcpPromptsSupport?: boolean;
26 hasMcpResourcesSupport?: boolean;
27 onFileUpload?: () => void;
28 onSystemPromptClick?: () => void;
29 onMcpPromptClick?: () => void;
30 onMcpResourcesClick?: () => void;
31 trigger: Snippet<[{ disabled: boolean; onclick?: () => void }]>;
35 class: className = '',
37 hasAudioModality = false,
38 hasVisionModality = false,
39 hasVideoModality = false,
40 hasMcpPromptsSupport = false,
41 hasMcpResourcesSupport = false,
49 let sheetOpen = $state(false);
50 let filesExpanded = $state(true);
51 let toolsExpanded = $state(false);
52 let mcpExpanded = $state(false);
54 const attachmentMenu = useAttachmentMenu(
60 hasMcpResourcesSupport
62 () => ({ onFileUpload, onSystemPromptClick, onMcpPromptClick, onMcpResourcesClick }),
68 const toolsPanel = useToolsPanel();
70 const sheetItemClass =
71 '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 const sheetItemRowClass =
74 '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 function getEnabledMcpServers() {
77 return mcpStore.getServersSorted().filter((s) => s.enabled);
81 <div class="flex items-center gap-1 {className}">
82 <Sheet.Root bind:open={sheetOpen}>
83 {@render trigger({ disabled, onclick: () => (sheetOpen = true) })}
85 <Sheet.Content side="bottom" class="max-h-[85vh] gap-0 overflow-y-auto">
87 <Sheet.Title>Add to chat</Sheet.Title>
89 <Sheet.Description class="sr-only">
90 Add files, system prompt or configure MCP servers
94 <div class="flex flex-col gap-1 px-1.5 pb-2">
95 <Collapsible.Root open={filesExpanded} onOpenChange={(open) => (filesExpanded = open)}>
96 <Collapsible.Trigger class={sheetItemClass}>
98 <ChevronDown class="h-4 w-4 shrink-0" />
100 <ChevronRight class="h-4 w-4 shrink-0" />
103 <File class="h-4 w-4 shrink-0" />
105 <span class="flex-1">Add files</span>
106 </Collapsible.Trigger>
108 <Collapsible.Content>
109 <div class="flex flex-col gap-0.5 pl-4">
110 {#each ATTACHMENT_FILE_ITEMS as item (item.id)}
111 {@const enabled = attachmentMenu.isItemEnabled(item.enabledWhen)}
115 class={sheetItemClass}
116 onclick={() => attachmentMenu.callbacks[item.action]()}
118 <item.icon class="h-4 w-4 shrink-0" />
120 <span>{item.label}</span>
122 {:else if item.disabledTooltip}
123 <Tooltip.Root delayDuration={TOOLTIP_DELAY_DURATION}>
125 <button type="button" class={sheetItemClass} disabled>
126 <item.icon class="h-4 w-4 shrink-0" />
128 <span>{item.label}</span>
132 <Tooltip.Content side="right">
133 <p>{item.disabledTooltip}</p>
139 </Collapsible.Content>
142 <Collapsible.Root open={mcpExpanded} onOpenChange={(open) => (mcpExpanded = open)}>
143 <Collapsible.Trigger class={sheetItemClass}>
145 <ChevronDown class="h-4 w-4 shrink-0" />
147 <ChevronRight class="h-4 w-4 shrink-0" />
150 <McpLogo class="inline h-4 w-4 shrink-0" />
152 <span class="flex-1">MCP Servers</span>
154 <span class="text-xs text-muted-foreground">
155 {getEnabledMcpServers().length} server{getEnabledMcpServers().length !== 1 ? 's' : ''}
157 </Collapsible.Trigger>
159 <Collapsible.Content>
160 <div class="flex flex-col gap-0.5 pl-4">
161 {#each getEnabledMcpServers() as server (server.id)}
162 {@const healthState = mcpStore.getHealthCheckState(server.id)}
163 {@const hasError = healthState.status === HealthCheckStatus.ERROR}
164 {@const displayName = mcpStore.getServerLabel(server)}
165 {@const faviconUrl = mcpStore.getServerFavicon(server.id)}
166 {@const isEnabled = conversationsStore.isMcpServerEnabledForChat(server.id)}
170 class={sheetItemRowClass}
171 onclick={() => !hasError && conversationsStore.toggleMcpServerForChat(server.id)}
174 <div class="flex min-w-0 flex-1 items-center gap-2">
179 class="h-4 w-4 shrink-0 rounded-sm"
181 (e.currentTarget as HTMLImageElement).style.display = 'none';
186 <span class="min-w-0 truncate text-sm">{displayName}</span>
191 class="shrink-0 rounded bg-destructive/15 px-1.5 py-0.5 text-xs text-destructive"
198 onCheckedChange={() => conversationsStore.toggleMcpServerForChat(server.id)}
204 {#if getEnabledMcpServers().length === 0}
205 <div class="px-3 py-2 text-center text-sm text-muted-foreground">
206 No MCP servers configured
210 </Collapsible.Content>
213 {#if toolsPanel.totalToolCount > 0}
214 <Collapsible.Root open={toolsExpanded} onOpenChange={(open) => (toolsExpanded = open)}>
215 <Collapsible.Trigger class={sheetItemClass}>
217 <ChevronDown class="h-4 w-4 shrink-0" />
219 <ChevronRight class="h-4 w-4 shrink-0" />
222 <PencilRuler class="inline h-4 w-4 shrink-0" />
224 <span class="flex-1">Tools</span>
226 <span class="text-xs text-muted-foreground">
227 {toolsPanel.totalToolCount} tool{toolsPanel.totalToolCount !== 1 ? 's' : ''}
229 </Collapsible.Trigger>
231 <Collapsible.Content>
232 <div class="flex flex-col gap-0.5 pl-4">
233 {#each toolsPanel.activeGroups as group (group.label)}
234 {@const { checked, indeterminate } = toolsPanel.getGroupCheckedState(group)}
235 {@const enabledCount = toolsPanel.getEnabledToolCount(group)}
236 {@const favicon = toolsPanel.getFavicon(group)}
240 class={sheetItemRowClass}
241 onclick={() => toolsPanel.toggleGroupByLabel(group.label)}
247 class="h-4 w-4 shrink-0 rounded-sm"
249 (e.currentTarget as HTMLImageElement).style.display = 'none';
254 <span class="min-w-0 flex-1 truncate text-sm font-medium">{group.label}</span>
256 <span class="shrink-0 text-xs text-muted-foreground">
257 {enabledCount}/{group.tools.length}
263 class="h-4 w-4 shrink-0"
264 onclick={(e) => e.stopPropagation()}
265 onCheckedChange={() => toolsPanel.toggleGroupByLabel(group.label)}
270 </Collapsible.Content>
274 <button type="button" class={sheetItemClass} onclick={onSystemPromptClick}>
275 <MessageSquare class="h-4 w-4 shrink-0" />
277 <span>System Message</span>
280 {#if hasMcpPromptsSupport}
281 <button type="button" class={sheetItemClass} onclick={onMcpPromptClick}>
282 <Zap class="h-4 w-4 shrink-0" />
284 <span>MCP Prompt</span>
288 {#if hasMcpResourcesSupport}
289 <button type="button" class={sheetItemClass} onclick={onMcpResourcesClick}>
290 <FolderOpen class="h-4 w-4 shrink-0" />
292 <span>MCP Resources</span>