3 ChatMessageAgenticContent,
4 ChatMessageActionIcons,
5 ChatMessageAssistantModel,
6 ChatMessageAssistantProcessingInfo,
7 ChatMessageAssistantRawOutput,
8 ChatMessageAssistantStatistics,
10 } from '$lib/components/app';
11 import { getMessageEditContext } from '$lib/contexts';
12 import { useProcessingState } from '$lib/hooks/use-processing-state.svelte';
13 import { chatStore, isLoading, isChatStreaming } from '$lib/stores/chat.svelte';
14 import { modelLoadProgressText } from '$lib/utils';
15 import { MessageRole } from '$lib/enums';
16 import { config } from '$lib/stores/settings.svelte';
17 import { isRouterMode } from '$lib/stores/server.svelte';
18 import { modelsStore } from '$lib/stores/models.svelte';
20 import { hasAgenticContent } from '$lib/utils';
27 assistantMessages: number;
28 messageTypes: string[];
30 isLastAssistantMessage?: boolean;
31 message: DatabaseMessage;
32 toolMessages?: DatabaseMessage[];
34 onConfirmDelete: () => void;
35 onContinue?: () => void;
38 onForkConversation?: (options: { name: string; includeAttachments: boolean }) => void;
39 onNavigateToSibling?: (siblingId: string) => void;
40 onRegenerate: (modelOverride?: string) => void;
41 onShowDeleteDialogChange: (show: boolean) => void;
42 showDeleteDialog: boolean;
43 siblingInfo?: ChatMessageSiblingInfo | null;
44 textareaElement?: HTMLTextAreaElement;
48 class: className = '',
50 isLastAssistantMessage = false,
61 onShowDeleteDialogChange,
64 textareaElement = $bindable()
68 const editCtx = getMessageEditContext();
70 const isAgentic = $derived(hasAgenticContent(message, toolMessages));
71 const processingState = useProcessingState();
73 let currentConfig = $derived(config());
74 let isRouter = $derived(isRouterMode());
76 let showRawOutput = $state(false);
78 let displayedModel = $derived(message.model ?? null);
80 let isCurrentlyLoading = $derived(isLoading());
81 let isStreaming = $derived(isChatStreaming());
82 let hasNoContent = $derived(!message?.content?.trim());
83 let isActivelyProcessing = $derived(isCurrentlyLoading || isStreaming);
85 // during a router auto-load the message has no model yet: target the model frozen in the
86 // persisted stream state (survives a reload), then fall back to the dropdown selection
87 let loadTargetModel = $derived(
88 message.model ?? chatStore.getResumeModel(message.convId) ?? modelsStore.selectedModelName
90 let modelLoadProgress = $derived(
91 isRouter && loadTargetModel ? modelsStore.getLoadProgress(loadTargetModel) : null
93 let modelLoadingText = $derived(modelLoadProgressText(modelLoadProgress));
95 let showProcessingInfoTop = $derived(
96 message?.role === MessageRole.ASSISTANT &&
97 isActivelyProcessing &&
100 isLastAssistantMessage
103 let showProcessingInfoBottom = $derived(
104 message?.role === MessageRole.ASSISTANT &&
105 isActivelyProcessing &&
106 (!hasNoContent || isAgentic) &&
107 isLastAssistantMessage
110 let assistantEl: HTMLDivElement | undefined = $state();
111 let lastUserMessageHeight = $state(0);
112 let assistantMarginTop = $state(0);
115 if (!assistantEl) return;
117 assistantMarginTop = Math.round(parseFloat(getComputedStyle(assistantEl).marginTop));
119 const chatMessageEl = assistantEl.closest('.chat-message');
120 const previousChatMessage = chatMessageEl?.previousElementSibling;
121 const userMessageEl = previousChatMessage?.querySelector(
123 ) as HTMLElement | null;
125 if (!userMessageEl) {
126 lastUserMessageHeight = 0;
130 const updateHeight = () => {
131 const rect = userMessageEl.getBoundingClientRect();
132 const marginTop = Math.round(parseFloat(getComputedStyle(userMessageEl).marginTop));
133 lastUserMessageHeight = Math.round(rect.height + marginTop);
138 const resizeObserver = new ResizeObserver(updateHeight);
139 resizeObserver.observe(userMessageEl);
142 resizeObserver.disconnect();
147 if (showProcessingInfoTop || showProcessingInfoBottom) {
148 processingState.startMonitoring();
154 bind:this={assistantEl}
155 class="chat-message-assistant text-md group w-full leading-7.5 {className}"
156 style:--last-user-message-height={lastUserMessageHeight > 0
157 ? `${lastUserMessageHeight}px`
159 style:--assistant-margin-top={assistantMarginTop > 0 ? `${assistantMarginTop}px` : undefined}
161 aria-label="Assistant message with actions"
163 {#if showProcessingInfoTop}
164 <ChatMessageAssistantProcessingInfo {modelLoadingText} {processingState} position="top" />
167 {#if editCtx.isEditing}
168 <ChatMessageEditForm />
171 <ChatMessageAssistantRawOutput {message} {toolMessages} />
173 <ChatMessageAgenticContent
176 isStreaming={isChatStreaming()}
177 {isLastAssistantMessage}
182 {#if showProcessingInfoBottom}
183 <ChatMessageAssistantProcessingInfo {modelLoadingText} {processingState} position="bottom" />
187 <div class="info my-6 grid gap-4 tabular-nums">
188 <div class="inline-flex flex-wrap items-start gap-2 text-xs text-muted-foreground">
189 <ChatMessageAssistantModel
191 isLoading={isLoading()}
196 <ChatMessageAssistantStatistics
198 isLoading={isLoading()}
200 showMessageStats={currentConfig.showMessageStats}
206 {#if message.timestamp && !editCtx.isEditing}
207 <ChatMessageActionIcons
208 role={MessageRole.ASSISTANT}
210 actionsPosition="left"
217 onContinue={currentConfig.enableContinueGeneration ? onContinue : undefined}
221 {onNavigateToSibling}
222 {onShowDeleteDialogChange}
223 showRawOutputSwitch={currentConfig.showRawOutputSwitch}
224 rawOutputEnabled={showRawOutput}
225 onRawOutputToggle={(enabled) => (showRawOutput = enabled)}
231 :global(.chat-message):last-child .chat-message-assistant {
232 --assistant-min-height-offset: calc(
233 var(--last-user-message-height, 19rem) + var(--chat-form-height, 6rem) +
234 var(--chat-form-bottom-position, 0.5rem) + var(--chat-form-padding-top, 6rem) +
235 var(--assistant-margin-top, 3rem)
237 min-height: calc(100dvh - var(--assistant-min-height-offset));
239 @media (width > 768px) {
240 --assistant-min-height-offset: calc(
241 var(--last-user-message-height, 18rem) + var(--chat-form-height, 6rem) +
242 var(--chat-form-bottom-position, 1rem) + var(--chat-form-padding-top, 6rem) +
243 var(--assistant-margin-top, 3rem)