2 import { Check, X } from '@lucide/svelte';
3 import { ChatMessageActionIcons, MarkdownContent } from '$lib/components/app';
4 import { Button } from '$lib/components/ui/button';
5 import { Card } from '$lib/components/ui/card';
6 import { INPUT_CLASSES } from '$lib/constants';
7 import { getMessageEditContext } from '$lib/contexts';
8 import { KeyboardKey, MessageRole } from '$lib/enums';
9 import { config } from '$lib/stores/settings.svelte';
10 import { autoResizeTextarea, isIMEComposing } from '$lib/utils';
14 message: DatabaseMessage;
15 siblingInfo?: ChatMessageSiblingInfo | null;
16 showDeleteDialog: boolean;
20 assistantMessages: number;
21 messageTypes: string[];
26 onConfirmDelete: () => void;
27 onNavigateToSibling?: (siblingId: string) => void;
28 onShowDeleteDialogChange: (show: boolean) => void;
29 textareaElement?: HTMLTextAreaElement;
33 class: className = '',
43 onShowDeleteDialogChange,
44 textareaElement = $bindable()
47 const editCtx = getMessageEditContext();
49 function handleEditKeydown(event: KeyboardEvent) {
50 if (event.key === KeyboardKey.ENTER && !event.shiftKey && !isIMEComposing(event)) {
51 event.preventDefault();
54 } else if (event.key === KeyboardKey.ESCAPE) {
55 event.preventDefault();
61 let isMultiline = $state(false);
62 let messageElement: HTMLElement | undefined = $state();
63 let isExpanded = $state(false);
64 let contentHeight = $state(0);
66 const MAX_HEIGHT = 200; // pixels
67 const currentConfig = config();
69 let showExpandButton = $derived(contentHeight > MAX_HEIGHT);
72 if (!messageElement || !message.content.trim()) return;
74 if (message.content.includes('\n')) {
78 const resizeObserver = new ResizeObserver((entries) => {
79 for (const entry of entries) {
80 const element = entry.target as HTMLElement;
81 const estimatedSingleLineHeight = 24;
83 isMultiline = element.offsetHeight > estimatedSingleLineHeight * 1.5;
84 contentHeight = element.scrollHeight;
88 resizeObserver.observe(messageElement);
91 resizeObserver.disconnect();
95 if (editCtx.isEditing && textareaElement) {
96 autoResizeTextarea(textareaElement);
100 function toggleExpand() {
101 isExpanded = !isExpanded;
106 aria-label="System message with actions"
107 class="group flex flex-col items-end gap-3 md:gap-2 {className}"
110 {#if editCtx.isEditing}
111 <div class="w-full max-w-[80%]">
113 style="max-height: var(--max-message-height);"
114 bind:this={textareaElement}
115 value={editCtx.editedContent}
116 class="min-h-[60px] w-full resize-none rounded-2xl px-3 py-2 text-sm {INPUT_CLASSES}"
117 onkeydown={handleEditKeydown}
119 autoResizeTextarea(e.currentTarget);
120 editCtx.setContent(e.currentTarget.value);
122 placeholder="Edit system message..."
125 <div class="mt-2 flex justify-end gap-2">
126 <Button class="h-8 px-3" onclick={editCtx.cancel} size="sm" variant="outline">
127 <X class="mr-1 h-3 w-3" />
134 onclick={editCtx.save}
135 disabled={!editCtx.editedContent.trim()}
138 <Check class="mr-1 h-3 w-3" />
145 {#if message.content.trim()}
146 <div class="relative max-w-[80%]">
148 class="group/expand w-full text-left {!isExpanded && showExpandButton
151 onclick={showExpandButton && !isExpanded ? toggleExpand : undefined}
155 class="overflow-y-auto rounded-[1.125rem] !border-2 !border-dashed !border-border/50 bg-muted px-3.75 py-1.5 data-[multiline]:py-2.5"
156 data-multiline={isMultiline ? '' : undefined}
157 style="border: 2px dashed hsl(var(--border)); max-height: var(--max-message-height); overflow-wrap: anywhere; word-break: break-word;"
160 class="relative transition-all duration-300 {isExpanded
161 ? 'cursor-text select-text'
163 style={!isExpanded && showExpandButton
164 ? `max-height: ${MAX_HEIGHT}px;`
165 : 'max-height: none;'}
167 {#if !currentConfig.renderContentAsRawText}
168 <div bind:this={messageElement} class={isExpanded ? 'cursor-text' : ''}>
169 <MarkdownContent class="markdown-system-content" content={message.content} />
173 bind:this={messageElement}
174 class="text-md whitespace-pre-wrap {isExpanded ? 'cursor-text' : ''}"
180 {#if !isExpanded && showExpandButton}
182 class="pointer-events-none absolute right-0 bottom-0 left-0 h-48 bg-gradient-to-t from-muted to-transparent"
186 class="pointer-events-none absolute right-0 bottom-4 left-0 flex justify-center opacity-0 transition-opacity group-hover/expand:opacity-100"
189 class="rounded-full px-4 py-1.5 text-xs shadow-md"
193 Show full system message
199 {#if isExpanded && showExpandButton}
200 <div class="mb-2 flex justify-center">
202 class="rounded-full px-4 py-1.5 text-xs"
210 Collapse System Message
219 {#if message.timestamp}
220 <div class="max-w-[80%]">
221 <ChatMessageActionIcons
222 actionsPosition="right"
229 {onNavigateToSibling}
230 {onShowDeleteDialogChange}
233 role={MessageRole.USER}