3 ChatMessageActionIcons,
4 ChatMessageAgenticContent,
5 ChatMessageAssistantModel,
6 ChatMessageAssistantProcessingInfo,
7 ChatMessageAssistantRawOutput,
8 ChatMessageAssistantStatistics,
10 } from '$lib/components/app';
11 import { getMessageEditContext } from '$lib/contexts';
12 import { MessageRole } from '$lib/enums';
13 import { useProcessingState } from '$lib/hooks/use-processing-state.svelte';
14 import { chatStore, modelsStore, serverStore, settingsStore } from '$lib/stores';
15 import { modelLoadProgressText } from '$lib/utils';
16 import { hasAgenticContent } from '$lib/utils';
23 assistantMessages: number;
24 messageTypes: string[];
26 isLastAssistantMessage?: boolean;
27 message: DatabaseMessage;
28 toolMessages?: DatabaseMessage[];
30 onConfirmDelete: () => void;
31 onContinue?: () => void;
34 onForkConversation?: (options: { name: string; includeAttachments: boolean }) => void;
35 onNavigateToSibling?: (siblingId: string) => void;
36 onRegenerate: (modelOverride?: string) => void;
37 onShowDeleteDialogChange: (show: boolean) => void;
38 showDeleteDialog: boolean;
39 siblingInfo?: ChatMessageSiblingInfo | null;
40 textareaElement?: HTMLTextAreaElement;
44 class: className = '',
46 isLastAssistantMessage = false,
56 onShowDeleteDialogChange,
59 textareaElement = $bindable(),
64 const editCtx = getMessageEditContext();
66 const isAgentic = $derived(hasAgenticContent(message, toolMessages));
67 const processingState = useProcessingState();
69 let currentConfig = $derived(settingsStore.config);
70 let isRouter = $derived(serverStore.isRouterMode);
72 let showRawOutput = $state(false);
74 let displayedModel = $derived(message.model ?? null);
76 let isCurrentlyLoading = $derived(chatStore.isLoading);
77 let isStreaming = $derived(chatStore.isStreaming());
78 let hasNoContent = $derived(!message?.content?.trim());
79 let isActivelyProcessing = $derived(isCurrentlyLoading || isStreaming);
81 // during a router auto-load the message has no model yet: target the model frozen in the
82 // persisted stream state (survives a reload), then fall back to the dropdown selection
83 let loadTargetModel = $derived(
84 message.model ?? chatStore.getResumeModel(message.convId) ?? modelsStore.selectedModelName
86 let modelLoadProgress = $derived(
87 isRouter && loadTargetModel ? modelsStore.getLoadProgress(loadTargetModel) : null
89 let modelLoadingText = $derived(modelLoadProgressText(modelLoadProgress));
91 let showProcessingInfoTop = $derived(
92 message?.role === MessageRole.ASSISTANT &&
93 isActivelyProcessing &&
96 isLastAssistantMessage
99 let showProcessingInfoBottom = $derived(
100 message?.role === MessageRole.ASSISTANT &&
101 isActivelyProcessing &&
102 (!hasNoContent || isAgentic) &&
103 isLastAssistantMessage
106 let assistantEl: HTMLDivElement | undefined = $state();
107 let lastUserMessageHeight = $state(0);
108 let assistantMarginTop = $state(0);
111 if (!assistantEl) return;
113 assistantMarginTop = Math.round(parseFloat(getComputedStyle(assistantEl).marginTop));
115 const chatMessageEl = assistantEl.closest('.chat-message');
116 const previousChatMessage = chatMessageEl?.previousElementSibling;
117 const userMessageEl = previousChatMessage?.querySelector(
119 ) as HTMLElement | null;
121 if (!userMessageEl) {
122 lastUserMessageHeight = 0;
127 const updateHeight = () => {
128 const rect = userMessageEl.getBoundingClientRect();
129 const marginTop = Math.round(parseFloat(getComputedStyle(userMessageEl).marginTop));
131 lastUserMessageHeight = Math.round(rect.height + marginTop);
136 const resizeObserver = new ResizeObserver(updateHeight);
138 resizeObserver.observe(userMessageEl);
141 resizeObserver.disconnect();
146 if (showProcessingInfoTop || showProcessingInfoBottom) {
147 processingState.startMonitoring();
153 bind:this={assistantEl}
154 class="chat-message-assistant text-md group w-full leading-7.5 {className}"
155 style:--last-user-message-height={lastUserMessageHeight > 0
156 ? `${lastUserMessageHeight}px`
158 style:--assistant-margin-top={assistantMarginTop > 0 ? `${assistantMarginTop}px` : undefined}
160 aria-label="Assistant message with actions"
162 {#if showProcessingInfoTop}
163 <ChatMessageAssistantProcessingInfo {modelLoadingText} {processingState} position="top" />
166 {#if editCtx.isEditing}
167 <ChatMessageEditForm />
170 <ChatMessageAssistantRawOutput {message} {toolMessages} />
172 <ChatMessageAgenticContent
175 isStreaming={chatStore.isStreaming()}
176 {isLastAssistantMessage}
181 {#if showProcessingInfoBottom}
182 <ChatMessageAssistantProcessingInfo {modelLoadingText} {processingState} position="bottom" />
186 <div class="info my-6 grid gap-4 tabular-nums">
187 <div class="inline-flex flex-wrap items-start gap-2 text-xs text-muted-foreground">
188 <ChatMessageAssistantModel
190 isLoading={chatStore.isLoading}
195 <ChatMessageAssistantStatistics
197 isLoading={chatStore.isLoading}
199 showMessageStats={currentConfig.showMessageStats}
205 {#if message.timestamp && !editCtx.isEditing}
206 <ChatMessageActionIcons
207 role={MessageRole.ASSISTANT}
209 actionsPosition="left"
216 onContinue={currentConfig.enableContinueGeneration ? onContinue : undefined}
220 {onNavigateToSibling}
221 {onShowDeleteDialogChange}
222 showRawOutputSwitch={currentConfig.showRawOutputSwitch}
223 rawOutputEnabled={showRawOutput}
224 onRawOutputToggle={(enabled) => (showRawOutput = enabled)}
230 :global(.chat-message):last-child .chat-message-assistant {
231 --assistant-min-height-offset: calc(
232 var(--last-user-message-height, 19rem) + var(--chat-form-height, 6rem) +
233 var(--chat-form-bottom-position, 0.5rem) + var(--chat-form-padding-top, 6rem) +
234 var(--assistant-margin-top, 3rem)
236 min-height: calc(100dvh - var(--assistant-min-height-offset));
238 @media (width > 768px) {
239 --assistant-min-height-offset: calc(
240 var(--last-user-message-height, 18rem) + var(--chat-form-height, 6rem) +
241 var(--chat-form-bottom-position, 1rem) + var(--chat-form-padding-top, 6rem) +
242 var(--assistant-margin-top, 3rem)