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 { chatStore, settingsStore } from '$lib/stores';
15 message: DatabaseMessage;
16 siblingInfo?: ChatMessageSiblingInfo | null;
20 assistantMessages: number;
21 messageTypes: string[];
23 isLastUserMessage?: boolean;
24 nextAssistantMessage?: DatabaseMessage | null;
25 showDeleteDialog: boolean;
28 onConfirmDelete: () => void;
29 onForkConversation?: (options: { name: string; includeAttachments: boolean }) => void;
30 onShowDeleteDialogChange: (show: boolean) => void;
31 onNavigateToSibling?: (siblingId: string) => void;
36 class: className = '',
38 isLastUserMessage = false,
40 nextAssistantMessage = null,
47 onShowDeleteDialogChange,
53 const editCtx = getMessageEditContext();
54 const processingState = useProcessingState();
56 const currentConfig = $derived(settingsStore.config);
57 const isActivelyProcessing = $derived(isLastUserMessage && chatStore.isLoading);
59 // For agentic turns, prefer the cumulative agentic.llm totals over per-call timings.
60 let storedReadingStats = $derived.by(() => {
61 const timings = nextAssistantMessage?.timings;
63 if (!timings?.prompt_n || !timings?.prompt_ms) return null;
65 const agentic = timings.agentic;
68 promptMs: agentic ? agentic.llm.prompt_ms : timings.prompt_ms,
69 promptTokens: agentic ? agentic.llm.prompt_n : timings.prompt_n
73 let showStoredReadingStats = $derived(
74 Boolean(currentConfig.showMessageStats) && storedReadingStats !== null
77 let showLiveReadingStats = $derived(
78 Boolean(currentConfig.showMessageStats) && isActivelyProcessing && storedReadingStats === null
82 if (showLiveReadingStats) {
83 processingState.startMonitoring();
89 aria-label="User message with actions"
90 class="chat-message-user group flex flex-col items-end gap-3 md:gap-2 {className}"
93 {#if editCtx.isEditing}
94 <ChatMessageEditForm />
96 <ChatMessageUserBubble
97 content={message.content}
98 attachments={message.extra}
102 {#if showStoredReadingStats}
103 <!-- Reading stats sourced from the assistant message that followed this turn -->
104 <div class="info my-2 grid w-full justify-items-end gap-4 tabular-nums">
106 class="inline-flex flex-wrap items-start justify-end gap-2 text-xs text-muted-foreground"
108 <ChatMessageStatistics
109 mode={ChatMessageStatisticsMode.READING}
110 promptTokens={storedReadingStats!.promptTokens}
111 promptMs={storedReadingStats!.promptMs}
115 {:else if showLiveReadingStats}
116 {@const liveStats = processingState.getLiveProcessingStats()}
118 <div class="info my-2 grid w-full justify-items-end gap-4 tabular-nums">
120 class="inline-flex flex-wrap items-start justify-end gap-2 text-xs text-muted-foreground"
122 <ChatMessageStatistics
123 mode={ChatMessageStatisticsMode.READING}
125 promptTokens={liveStats.tokensProcessed}
126 promptMs={liveStats.timeMs}
133 {#if message.timestamp}
134 <div class="max-w-[80%]">
135 <ChatMessageActionIcons
136 actionsPosition="right"
144 {onNavigateToSibling}
145 {onShowDeleteDialogChange}
148 role={MessageRole.USER}