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';
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 }]>;
36 class: className = '',
38 hasAudioModality = false,
39 hasVisionModality = false,
40 hasVideoModality = false,
41 hasMcpPromptsSupport = false,
42 hasMcpResourcesSupport = false,
50 let sheetOpen = $state(false);
51 let filesExpanded = $state(true);
52 let toolsExpanded = $state(false);
53 let mcpExpanded = $state(false);
55 const attachmentMenu = useAttachmentMenu(
61 hasMcpResourcesSupport
63 () => ({ onFileUpload, onSystemPromptClick, onMcpPromptClick, onMcpResourcesClick }),
69 const toolsPanel = useToolsPanel();
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';
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';
77 function getEnabledMcpServers() {
78 return mcpStore.getServersSorted().filter((s) => s.enabled);
82 <div class="flex items-center gap-1 {className}">
83 <Sheet.Root bind:open={sheetOpen}>
84 {@render trigger({ disabled, onclick: () => (sheetOpen = true) })}
86 <Sheet.Content side="bottom" class="max-h-[85vh] gap-0 overflow-y-auto">
88 <Sheet.Title>Add to chat</Sheet.Title>
90 <Sheet.Description class="sr-only">
91 Add files, system prompt or configure MCP servers
95 <div class="flex flex-col gap-1 px-1.5 pb-2">
96 <Collapsible.Root open={filesExpanded} onOpenChange={(open) => (filesExpanded = open)}>
97 <Collapsible.Trigger class={sheetItemClass}>
99 <ChevronDown class="h-4 w-4 shrink-0" />
101 <ChevronRight class="h-4 w-4 shrink-0" />
104 <File class="h-4 w-4 shrink-0" />
106 <span class="flex-1">Add files</span>
107 </Collapsible.Trigger>
109 <Collapsible.Content>
110 <div class="flex flex-col gap-0.5 pl-4">
111 {#each ATTACHMENT_FILE_ITEMS as item (item.id)}
112 {@const enabled = attachmentMenu.isItemEnabled(item.enabledWhen)}
116 class={sheetItemClass}
117 onclick={() => attachmentMenu.callbacks[item.action]()}
119 <item.icon class="h-4 w-4 shrink-0" />
121 <span>{item.label}</span>
123 {:else if item.disabledTooltip}
124 <Tooltip.Root delayDuration={TOOLTIP_DELAY_DURATION}>
126 <button type="button" class={sheetItemClass} disabled>
127 <item.icon class="h-4 w-4 shrink-0" />
129 <span>{item.label}</span>
133 <Tooltip.Content side="right">
134 <p>{item.disabledTooltip}</p>
140 </Collapsible.Content>
143 <Collapsible.Root open={mcpExpanded} onOpenChange={(open) => (mcpExpanded = open)}>
144 <Collapsible.Trigger class={sheetItemClass}>
146 <ChevronDown class="h-4 w-4 shrink-0" />
148 <ChevronRight class="h-4 w-4 shrink-0" />
151 <McpLogo class="inline h-4 w-4 shrink-0" />
153 <span class="flex-1">MCP Servers</span>
155 <span class="text-xs text-muted-foreground">
156 {getEnabledMcpServers().length} server{getEnabledMcpServers().length !== 1 ? 's' : ''}
158 </Collapsible.Trigger>
160 <Collapsible.Content>
161 <div class="flex flex-col gap-0.5 pl-4">
162 {#each getEnabledMcpServers() as server (server.id)}
163 {@const healthState = mcpStore.getHealthCheckState(server.id)}
164 {@const hasError = healthState.status === HealthCheckStatus.ERROR}
165 {@const displayName = mcpStore.getServerLabel(server)}
166 {@const faviconUrl = mcpStore.getServerFavicon(server.id)}
167 {@const isEnabled = conversationsStore.isMcpServerEnabledForChat(server.id)}
171 class={sheetItemRowClass}
172 onclick={() => !hasError && conversationsStore.toggleMcpServerForChat(server.id)}
175 <div class="flex min-w-0 flex-1 items-center gap-2">
180 class="h-4 w-4 shrink-0 rounded-sm"
182 (e.currentTarget as HTMLImageElement).style.display = 'none';
187 <span class="min-w-0 truncate text-sm">{displayName}</span>
192 class="shrink-0 rounded bg-destructive/15 px-1.5 py-0.5 text-xs text-destructive"
199 onCheckedChange={() => conversationsStore.toggleMcpServerForChat(server.id)}
205 {#if getEnabledMcpServers().length === 0}
206 <div class="px-3 py-2 text-center text-sm text-muted-foreground">
207 No MCP servers configured
211 </Collapsible.Content>
214 {#if toolsPanel.totalToolCount > 0}
215 <Collapsible.Root open={toolsExpanded} onOpenChange={(open) => (toolsExpanded = open)}>
216 <Collapsible.Trigger class={sheetItemClass}>
218 <ChevronDown class="h-4 w-4 shrink-0" />
220 <ChevronRight class="h-4 w-4 shrink-0" />
223 <PencilRuler class="inline h-4 w-4 shrink-0" />
225 <span class="flex-1">Tools</span>
227 <span class="text-xs text-muted-foreground">
228 {toolsPanel.totalToolCount} tool{toolsPanel.totalToolCount !== 1 ? 's' : ''}
230 </Collapsible.Trigger>
232 <Collapsible.Content>
233 <div class="flex flex-col gap-0.5 pl-4">
234 {#each toolsPanel.activeGroups as group (group.label)}
235 {@const checked = toolsPanel.isGroupChecked(group)}
236 {@const enabledCount = toolsPanel.getEnabledToolCount(group)}
237 {@const favicon = toolsPanel.getFavicon(group)}
241 class={sheetItemRowClass}
242 onclick={() => toolsPanel.toggleGroupByLabel(group.label)}
248 class="h-4 w-4 shrink-0 rounded-sm"
250 (e.currentTarget as HTMLImageElement).style.display = 'none';
255 <span class="min-w-0 flex-1 truncate text-sm font-medium">{group.label}</span>
257 <span class="shrink-0 text-xs text-muted-foreground">
258 {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>
276 class={sheetItemClass}
277 onclick={() => attachmentMenu.callbacks[AttachmentAction.SYSTEM_PROMPT_CLICK]()}
279 <MessageSquare class="h-4 w-4 shrink-0" />
281 <span>System Message</span>
284 {#if hasMcpPromptsSupport}
287 class={sheetItemClass}
288 onclick={() => attachmentMenu.callbacks[AttachmentAction.MCP_PROMPT_CLICK]()}
290 <Zap class="h-4 w-4 shrink-0" />
292 <span>MCP Prompt</span>
296 {#if hasMcpResourcesSupport}
299 class={sheetItemClass}
300 onclick={() => attachmentMenu.callbacks[AttachmentAction.MCP_RESOURCES_CLICK]()}
302 <FolderOpen class="h-4 w-4 shrink-0" />
304 <span>MCP Resources</span>