3 ChatMessageActionIcons,
7 } from '$lib/components/app/chat';
8 import { getMessageEditContext } from '$lib/contexts';
9 import { ChatMessageStatisticsMode, MessageRole } from '$lib/enums';
10 import { useProcessingState } from '$lib/hooks/use-processing-state.svelte';
11 import { isLoading } from '$lib/stores/chat.svelte';
12 import { config } from '$lib/stores/settings.svelte';
16 message: DatabaseMessage;
17 siblingInfo?: ChatMessageSiblingInfo | null;
21 assistantMessages: number;
22 messageTypes: string[];
24 isLastUserMessage?: boolean;
25 nextAssistantMessage?: DatabaseMessage | null;
26 showDeleteDialog: boolean;
29 onConfirmDelete: () => void;
30 onForkConversation?: (options: { name: string; includeAttachments: boolean }) => void;
31 onShowDeleteDialogChange: (show: boolean) => void;
32 onNavigateToSibling?: (siblingId: string) => void;
37 class: className = '',
39 isLastUserMessage = false,
41 nextAssistantMessage = null,
48 onShowDeleteDialogChange,
54 const editCtx = getMessageEditContext();
55 const processingState = useProcessingState();
57 const currentConfig = $derived(config());
58 const isActivelyProcessing = $derived(isLastUserMessage && isLoading());
60 // For agentic turns, prefer the cumulative agentic.llm totals over per-call timings.
61 let storedReadingStats = $derived.by(() => {
62 const timings = nextAssistantMessage?.timings;
64 if (!timings?.prompt_n || !timings?.prompt_ms) return null;
66 const agentic = timings.agentic;
69 promptMs: agentic ? agentic.llm.prompt_ms : timings.prompt_ms,
70 promptTokens: agentic ? agentic.llm.prompt_n : timings.prompt_n
74 let showStoredReadingStats = $derived(
75 Boolean(currentConfig.showMessageStats) && storedReadingStats !== null
78 let showLiveReadingStats = $derived(
79 Boolean(currentConfig.showMessageStats) && isActivelyProcessing && storedReadingStats === null
83 if (showLiveReadingStats) {
84 processingState.startMonitoring();
90 aria-label="User message with actions"
91 class="chat-message-user group flex flex-col items-end gap-3 md:gap-2 {className}"
94 {#if editCtx.isEditing}
95 <ChatMessageEditForm />
97 <ChatMessageUserBubble
98 content={message.content}
99 attachments={message.extra}
100 renderMarkdown={true}
103 {#if showStoredReadingStats}
104 <!-- Reading stats sourced from the assistant message that followed this turn -->
105 <div class="info my-2 grid w-full justify-items-end gap-4 tabular-nums">
107 class="inline-flex flex-wrap items-start justify-end gap-2 text-xs text-muted-foreground"
109 <ChatMessageStatistics
110 mode={ChatMessageStatisticsMode.READING}
111 promptTokens={storedReadingStats!.promptTokens}
112 promptMs={storedReadingStats!.promptMs}
116 {:else if showLiveReadingStats}
117 {@const liveStats = processingState.getLiveProcessingStats()}
119 <div class="info my-2 grid w-full justify-items-end gap-4 tabular-nums">
121 class="inline-flex flex-wrap items-start justify-end gap-2 text-xs text-muted-foreground"
123 <ChatMessageStatistics
124 mode={ChatMessageStatisticsMode.READING}
126 promptTokens={liveStats.tokensProcessed}
127 promptMs={liveStats.timeMs}
134 {#if message.timestamp}
135 <div class="max-w-[80%]">
136 <ChatMessageActionIcons
137 actionsPosition="right"
145 {onNavigateToSibling}
146 {onShowDeleteDialogChange}
149 role={MessageRole.USER}