File size: 27,185 Bytes
9b906ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import React from "react";
import { useNavigate } from "react-router";
import { useTracking } from "#/hooks/use-tracking";
import { useTranslation } from "react-i18next";
import { isAcpAuthErrorCode } from "#/utils/acp-error-codes";
import { convertImageToBase64 } from "#/utils/convert-image-to-base-64";
import { createChatMessage } from "#/services/chat-service";
import { BtwMessages } from "./btw-messages";
import { GoalStatusBanner } from "./goal-status-banner";
import { ModelMessages } from "./model-messages";
import { useModelStore } from "#/stores/model-store";
import { useGoalStore } from "#/stores/goal-store";
import { InteractiveChatBox } from "./interactive-chat-box";
import { AgentState } from "#/types/agent-state";
import { useFilteredEvents } from "#/hooks/use-filtered-events";
import { useScrollToBottom } from "#/hooks/use-scroll-to-bottom";
import { useLoadOlderEvents } from "#/hooks/use-load-older-events";
import { useAutoRefreshFilesOnEdit } from "#/hooks/use-auto-refresh-files-on-edit";
import { WorkspaceFilesForChatProvider } from "./chat-markdown-path-code";
import { TypingIndicator } from "./typing-indicator";
import { ChatSuggestions } from "./chat-suggestions";
import { ScrollProvider } from "#/context/scroll-context";
import { useInitialQueryStore } from "#/stores/initial-query-store";
import { useSendMessage } from "#/hooks/use-send-message";
import { useAgentState, usePlanningAgentState } from "#/hooks/use-agent-state";
import { useIsArchivedConversation } from "#/hooks/use-is-archived-conversation";
import { useHandleBuildPlanClick } from "#/hooks/use-handle-build-plan-click";

import { ScrollToBottomButton } from "#/components/shared/buttons/scroll-to-bottom-button";
import { LoadingSpinner } from "#/components/shared/loading-spinner";
import { ChatMessagesSkeleton } from "./chat-messages-skeleton";
import { displayErrorToast } from "#/utils/custom-toast-handlers";
import { useErrorMessageStore } from "#/stores/error-message-store";
import { useOptimisticUserMessageStore } from "#/stores/optimistic-user-message-store";
import { SERVER_CONNECTION_ERROR_MESSAGE } from "#/constants/server-connection-error";
import { ErrorMessageBanner } from "./error-message-banner";
import { SkillInstallRestartBanner } from "./skill-install-restart-banner";
import { LlmNotConfiguredBanner } from "#/components/features/home/llm-not-configured-banner";
import { useLlmConfigured } from "#/hooks/use-llm-configured";
import { Messages } from "#/components/conversation-events/chat/messages";
import { PendingUserMessages } from "./pending-user-messages";
import { useUnifiedUploadFiles } from "#/hooks/mutation/use-unified-upload-files";
import { validateFiles } from "#/utils/file-validation";
import { useConversationStore } from "#/stores/conversation-store";
import ConfirmationModeEnabled from "./confirmation-mode-enabled";
import { useTaskPolling } from "#/hooks/query/use-task-polling";
import { matchesPendingConversationId } from "#/utils/pending-task-message-link";
import { useConversationWebSocket } from "#/contexts/conversation-websocket-context";
import ChatStatusIndicator from "./chat-status-indicator";
import { getStatusColor, getStatusText } from "#/utils/utils";
import { useNewConversationCommand } from "#/hooks/mutation/use-new-conversation-command";
import { useOptionalConversationId } from "#/hooks/use-conversation-id";
import { useActiveConversation } from "#/hooks/query/use-active-conversation";
import { I18nKey } from "#/i18n/declaration";
import { hasConversationStarted } from "./components/resolve-picker-kind";

function getEntryPoint(
  hasRepository: boolean | null,
  hasReplayJson: boolean | null,
): string {
  if (hasRepository) return "github";
  if (hasReplayJson) return "replay";
  return "direct";
}

export function ChatInterface() {
  useAutoRefreshFilesOnEdit();

  const { trackInitialQuerySubmitted, trackUserMessageSent } = useTracking();
  const { setMessageToSend, conversationMode, planContent } =
    useConversationStore();
  const {
    errorMessage,
    errorCode,
    errorClassification,
    removeErrorMessage,
    setErrorMessage,
  } = useErrorMessageStore();
  const navigate = useNavigate();
  const { isTask, taskStatus, taskDetail } = useTaskPolling();
  // Hide empty-state chrome for the entire `/conversations/task-{uuid}` route,
  // including the brief READY window before redirect completes.
  const isProvisioningTask = isTask;
  const conversationWebSocket = useConversationWebSocket();
  const { send } = useSendMessage();
  const {
    renderableEvents,
    allConversationEvents,
    totalEvents,
    hasSubstantiveAgentActions,
    userEventsExist,
  } = useFilteredEvents();
  const enqueuePendingMessage = useOptimisticUserMessageStore(
    (state) => state.enqueuePendingMessage,
  );
  const markPendingMessageError = useOptimisticUserMessageStore(
    (state) => state.markPendingMessageError,
  );
  const pendingMessages = useOptimisticUserMessageStore(
    (state) => state.pendingMessages,
  );
  const { t } = useTranslation("openhands");
  const scrollRef = React.useRef<HTMLDivElement>(null);
  const {
    scrollDomToBottom,
    onChatBodyScroll,
    hitBottom,
    autoScroll,
    setAutoScroll,
    setHitBottom,
  } = useScrollToBottom(scrollRef);
  const {
    mutate: newConversationCommand,
    isPending: isNewConversationPending,
  } = useNewConversationCommand();

  const { curAgentState } = useAgentState();
  const { isPlanningAgentRunning } = usePlanningAgentState();
  const { handleBuildPlanClick } = useHandleBuildPlanClick();

  // Cloud conversations whose sandbox is MISSING or ERROR are read-only:
  // the sandbox is gone and cannot be resumed, so we hide the chat input
  // and show an explanatory banner. For local backends sandbox_status is
  // always null, so this is effectively a no-op for non-cloud use.
  const { data: activeConversation } = useActiveConversation();
  const sandboxStatus = activeConversation?.sandbox_status ?? null;
  const isArchivedConversation = useIsArchivedConversation();

  // Block sending in a resumed conversation that has no usable LLM, and show
  // the same setup banner as the home screen so the dead end is explained.
  const { isConfigured: isLlmConfigured, isLoading: isLlmConfigLoading } =
    useLlmConfigured();
  const llmBlocked = !isLlmConfigLoading && !isLlmConfigured;

  // Disable Build button while agent is running (streaming)
  const isAgentRunning =
    curAgentState === AgentState.RUNNING ||
    curAgentState === AgentState.LOADING;

  // Global keyboard shortcut for Build button (Cmd+Enter / Ctrl+Enter)
  // This is placed here instead of PlanPreview to avoid duplicate listeners
  // when multiple PlanPreview components exist in the chat.
  // Gated on the same conditions as the Build button (ConversationTabs'
  // `isBuildDisabled`) so it cannot fire outside the plan flow.
  React.useEffect(() => {
    if (isAgentRunning || conversationMode !== "plan" || !planContent) {
      return undefined;
    }

    const handleKeyDown = (event: KeyboardEvent) => {
      // Check for Cmd+Enter (Mac) or Ctrl+Enter (Windows/Linux)
      if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
        event.preventDefault();
        event.stopPropagation();
        handleBuildPlanClick(event);
        scrollDomToBottom();
      }
    };

    document.addEventListener("keydown", handleKeyDown);

    return () => {
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, [
    isAgentRunning,
    conversationMode,
    planContent,
    handleBuildPlanClick,
    scrollDomToBottom,
  ]);

  const { selectedRepository, replayJson } = useInitialQueryStore();
  const { conversationId } = useOptionalConversationId();

  // The live goal banner renders in the scroll stream but advances via store
  // updates (in-progress goal events are filtered out of `renderableEvents`),
  // so the bottom-following effect would not react to it. This key changes as
  // an active loop appears and advances each round; feeding it into that effect
  // keeps the banner in view when the user is pinned to the bottom.
  const activeGoalScrollKey = useGoalStore((s) => {
    const goal = conversationId
      ? s.statusByConversation[conversationId]
      : undefined;
    return goal?.active ? `${goal.iteration}:${goal.status}` : null;
  });
  const { mutateAsync: uploadFiles } = useUnifiedUploadFiles();

  // Lazy "scroll up to load older events" backfill. Initial REST fetch only
  // returns the most recent page; this hook paginates older events into the
  // store on demand so the chat doesn't load (potentially) thousands of
  // events on first render.
  const {
    isLoading: isLoadingOlderEvents,
    hasMore: hasMoreOlderEvents,
    loadOlder,
  } = useLoadOlderEvents(conversationId);

  // Trigger `loadOlder` and preserve the visual scroll position once the
  // older page is merged in (otherwise prepending events would jump the
  // chat far down). We fire from three places to cover the cases the
  // browser's scroll event misses:
  //
  //   - `onScroll`: normal "user scrolled near the top" path.
  //   - `onWheel`:  user is already pinned at scrollTop=0 and tries to
  //                 wheel further up — no scroll event fires past 0.
  //   - effect:     content is shorter than the viewport (no scrollbar
  //                 at all), so the user has nothing to scroll. Re-runs
  //                 as more pages arrive until there's overflow or the
  //                 server runs out of older events.
  const SCROLL_TOP_THRESHOLD_PX = 80;
  const preserveScrollPosition = React.useRef<{
    scrollHeight: number;
    scrollTop: number;
  } | null>(null);
  const maybeLoadOlder = React.useCallback(
    (target: HTMLElement) => {
      if (isProvisioningTask || isLoadingOlderEvents || !hasMoreOlderEvents) {
        return;
      }

      const atTop = target.scrollTop <= SCROLL_TOP_THRESHOLD_PX;
      const noOverflow =
        target.scrollHeight <= target.clientHeight + SCROLL_TOP_THRESHOLD_PX;
      if (!atTop && !noOverflow) return;

      preserveScrollPosition.current = {
        scrollHeight: target.scrollHeight,
        scrollTop: target.scrollTop,
      };
      loadOlder().catch((error) => {
        preserveScrollPosition.current = null;
        const message =
          error instanceof Error && error.message
            ? error.message
            : t(I18nKey.ERROR$GENERIC);
        setErrorMessage(message);
      });
    },
    [
      hasMoreOlderEvents,
      isLoadingOlderEvents,
      isProvisioningTask,
      loadOlder,
      setErrorMessage,
      t,
    ],
  );

  const handleWheelForPagination = React.useCallback(
    (e: React.WheelEvent<HTMLDivElement>) => {
      // Browsers don't dispatch a scroll event when scrollTop is already
      // 0 and the user wheels upward, so onScroll alone misses this case.
      if (e.deltaY < 0 && e.currentTarget.scrollTop <= 0) {
        maybeLoadOlder(e.currentTarget);
      }
    },
    [maybeLoadOlder],
  );

  const hasPendingUserMessages = React.useMemo(
    () =>
      conversationId
        ? pendingMessages.some((message) =>
            matchesPendingConversationId(
              conversationId,
              message.conversationId,
            ),
          )
        : false,
    [pendingMessages, conversationId],
  );

  const hasModelEntries = useModelStore((s) =>
    conversationId
      ? (s.entriesByConversation[conversationId]?.length ?? 0) > 0
      : false,
  );
  const hasStartedConversation = hasConversationStarted({
    isLoadingHistory: conversationWebSocket?.isLoadingHistory === true,
    hasUserEvents: userEventsExist,
    hasPendingUserMessages,
    hasSubstantiveAgentActions,
    hasModelEntries,
  });

  // Show V1 messages immediately if events exist in store (e.g., remount),
  // if the user already has a locally-tracked pending bubble (home-page cloud
  // submit while history/WS catch up), or once loading completes. This
  // replaces the old transition-observation pattern (useState + useEffect
  // watching loading→loaded) which always showed skeleton on remount because
  // local state initialized to false.
  const showConversationMessages =
    allConversationEvents.length > 0 ||
    hasPendingUserMessages ||
    !conversationWebSocket?.isLoadingHistory;

  const isReturningToConversation = !!conversationId;
  // Only show loading skeleton when genuinely loading AND no events in store yet.
  // If events exist (e.g., remount after data was already fetched), skip skeleton.
  const isHistoryLoading = !showConversationMessages;
  const isChatLoading = isHistoryLoading && !isTask;

  const handleSendMessage = async (
    content: string,
    originalImages: File[],
    originalFiles: File[],
  ) => {
    // Handle /new command for V1 conversations
    if (content.trim() === "/new") {
      if (!conversationId) {
        displayErrorToast(t(I18nKey.CONVERSATION$CLEAR_NO_ID));
        return;
      }
      if (totalEvents === 0) {
        displayErrorToast(t(I18nKey.CONVERSATION$CLEAR_EMPTY));
        return;
      }
      if (isNewConversationPending) {
        return;
      }
      newConversationCommand();
      return;
    }

    // Create mutable copies of the arrays
    const images = [...originalImages];
    const files = [...originalFiles];
    if (totalEvents === 0) {
      trackInitialQuerySubmitted({
        entryPoint: getEntryPoint(
          selectedRepository !== null,
          replayJson !== null,
        ),
        queryCharacterLength: content.length,
        replayJsonSize: replayJson?.length,
      });
    } else {
      trackUserMessageSent({
        sessionMessageCount: totalEvents,
        currentMessageLength: content.length,
      });
    }

    // Validate file sizes before any processing
    const allFiles = [...images, ...files];
    const validation = validateFiles(allFiles);

    if (!validation.isValid) {
      displayErrorToast(`Error: ${validation.errorMessage}`);
      return; // Stop processing if validation fails
    }

    const promises = images.map((image) => convertImageToBase64(image));
    const imageUrls = await Promise.all(promises);

    const timestamp = new Date().toISOString();

    const { skipped_files: skippedFiles, uploaded_files: uploadedFiles } =
      files.length > 0
        ? await uploadFiles({ conversationId: conversationId!, files })
        : { skipped_files: [], uploaded_files: [] };

    skippedFiles.forEach((f) => displayErrorToast(f.reason));

    const filePrompt = `${t(I18nKey.CHAT_INTERFACE$AUGMENTED_PROMPT_FILES_TITLE)}: ${uploadedFiles.join("\n\n")}`;
    const prompt =
      uploadedFiles.length > 0 ? `${content}\n\n${filePrompt}` : content;

    // Enqueue the message into the local pending queue with status "sending"
    // so the user immediately sees it in the chat with a faded treatment. The
    // entry is removed when the WebSocket echoes back the corresponding
    // `UserMessageEvent`. If the API call to send the message fails, the entry
    // is flipped to "error" with a retry link.
    const pendingId = enqueuePendingMessage({
      conversationId: conversationId!,
      // `text` is what the user sees in the bubble; `content` is what we
      // actually hand to the server (the prompt may include an appended
      // "Files uploaded: …" block) and is what the echo will be matched
      // against. They're different when there are file attachments.
      text: content,
      content: prompt,
      imageUrls,
      fileUrls: uploadedFiles,
      timestamp,
    });
    // Submitting a new prompt should always pull the chat back to the
    // latest message even if the user had scrolled up. This also re-arms
    // autoScroll so the streamed agent reply auto-follows.
    scrollDomToBottom();
    setMessageToSend("");

    try {
      await send(
        createChatMessage(prompt, imageUrls, uploadedFiles, timestamp),
      );
    } catch (sendError) {
      const sendErrorMessage =
        sendError instanceof Error
          ? sendError.message
          : t(I18nKey.CHAT_INTERFACE$FAILED_TO_SEND_MESSAGE);
      markPendingMessageError(pendingId, sendErrorMessage);
    }
  };

  // Auto-scroll to bottom when new messages arrive — but only if the user is
  // already pinned to the bottom. Scrolling up to load older events also
  // grows `renderableEvents`, and we don't want to yank the user back to the
  // bottom in that case.
  React.useEffect(() => {
    // If a "load older" was just triggered, restore the scroll position so
    // the conversation appears to extend upward instead of jumping.
    if (preserveScrollPosition.current && scrollRef.current) {
      const { scrollHeight: prevHeight, scrollTop: prevTop } =
        preserveScrollPosition.current;
      const dom = scrollRef.current;
      const delta = dom.scrollHeight - prevHeight;
      if (delta > 0) {
        dom.scrollTop = prevTop + delta;
      }
      preserveScrollPosition.current = null;
      return;
    }

    if (autoScroll) {
      scrollDomToBottom();
    }
    // Note: We intentionally exclude autoScroll from deps because we only want
    // to scroll when message content changes, not when autoScroll state changes.
  }, [
    renderableEvents.length,
    hasPendingUserMessages,
    activeGoalScrollKey,
    scrollDomToBottom,
  ]);

  // Auto-load older events when the chat content doesn't overflow the
  // scroll area (no scrollbar to drag, no wheel events past 0). We
  // re-run only when the rendered list grows or `hasMore` flips, NOT
  // when `maybeLoadOlder` re-creates: the underlying hook's `loadOlder`
  // ref changes whenever its internal `isLoading` toggles, so depending
  // on `maybeLoadOlder` would re-fire the effect on every failed page
  // and tight-loop until the server recovered. Driving off
  // `renderableEvents.length` instead means a successful page (events
  // grow) chains the next request, while a failed page (events
  // unchanged) waits for the user to retry.
  const maybeLoadOlderRef = React.useRef(maybeLoadOlder);
  React.useEffect(() => {
    maybeLoadOlderRef.current = maybeLoadOlder;
  });
  React.useEffect(() => {
    const target = scrollRef.current;
    if (!target) return;
    maybeLoadOlderRef.current(target);
  }, [renderableEvents.length, hasMoreOlderEvents]);

  // Create a ScrollProvider with the scroll hook values
  const scrollProviderValue = {
    scrollRef,
    autoScroll,
    setAutoScroll,
    scrollDomToBottom,
    hitBottom,
    setHitBottom,
    onChatBodyScroll,
  };

  // Get server status indicator props
  const isStartingStatus =
    curAgentState === AgentState.LOADING || curAgentState === AgentState.INIT;
  const isStopStatus = curAgentState === AgentState.STOPPED;
  const isPausing = curAgentState === AgentState.PAUSED;
  const serverStatusColor = getStatusColor({
    isPausing,
    isTask,
    taskStatus,
    isStartingStatus,
    isStopStatus,
    curAgentState,
  });
  const serverStatusText = getStatusText({
    isPausing,
    isTask,
    taskStatus,
    taskDetail,
    isStartingStatus,
    isStopStatus,
    curAgentState,
    errorMessage,
    t,
  });

  return (
    <WorkspaceFilesForChatProvider>
      <ScrollProvider value={scrollProviderValue}>
        <div
          className="relative flex h-full flex-col justify-between px-4"
          data-testid="chat-interface"
        >
          {!hasSubstantiveAgentActions &&
            !hasPendingUserMessages &&
            !userEventsExist &&
            !hasModelEntries &&
            !isChatLoading &&
            !isProvisioningTask &&
            totalEvents === 0 &&
            !isArchivedConversation &&
            // With no usable LLM the suggestions can't be acted on (the input is
            // disabled). They're also a `pointer-events-auto` overlay that would
            // sit over the LlmNotConfiguredBanner below and swallow clicks on its
            // setup button — so hide them and let the banner be the lone CTA.
            !llmBlocked && (
              <ChatSuggestions
                onSuggestionsClick={(message) => setMessageToSend(message)}
              />
            )}
          {/* Note: We only hide chat suggestions when there's a user message */}

          <div
            ref={scrollRef}
            data-testid="chat-scroll-container"
            onScroll={(e) => {
              onChatBodyScroll(e.currentTarget);
              maybeLoadOlder(e.currentTarget);
            }}
            onWheel={handleWheelForPagination}
            className="custom-scrollbar-always flex min-h-0 grow flex-col gap-2 overflow-x-hidden overflow-y-auto px-0 pt-4 pb-8 md:px-4"
          >
            {isChatLoading && isReturningToConversation && (
              <ChatMessagesSkeleton />
            )}

            {isChatLoading && !isReturningToConversation && (
              <div
                className="flex justify-center"
                data-testid="loading-spinner"
              >
                <LoadingSpinner size="small" />
              </div>
            )}

            {isLoadingOlderEvents && (
              <div
                className="flex items-center justify-center gap-2 py-3 text-sm text-neutral-400"
                data-testid="loading-older-events"
              >
                <LoadingSpinner size="small" />
                <span>{t(I18nKey.CHAT_INTERFACE$FETCHING_OLDER_MESSAGES)}</span>
              </div>
            )}

            {/*
             * Render whenever there's anything to display. Previously this
             * was gated on `conversationUserEventsExist`, but with the lazy
             * "50 most recent" REST fetch the initial window may not include
             * any `source: "user"` events (long agent runs between user
             * turns). That left the chat blank, leaving the user nothing to
             * scroll — which is why "scroll up to load older" appeared
             * broken. The empty-state ChatSuggestions block above still
             * keeps its own gate (`!userEventsExist && !hasSubstantiveAgentActions`)
             * so brand-new conversations show suggestions, not an empty chat.
             */}
            {/* /model entries created before any event is rendered are
              anchored to `null` and live above the message list. */}
            <ModelMessages
              conversationId={conversationId}
              anchorEventId={null}
            />

            {showConversationMessages && renderableEvents.length > 0 && (
              <Messages
                messages={renderableEvents}
                allEvents={allConversationEvents}
              />
            )}

            {/*
            Render the local pending-message queue independently so messages
            the user just submitted show up immediately (with a faded "sending"
            treatment) even before any real conversation event has come back
            from the server. Entries drain (FIFO) when the matching
            UserMessageEvent echoes back over the WebSocket, so this never
            double-renders alongside the real event list.
          */}
            <PendingUserMessages />

            {/* Goal-loop status sits at the end of the message flow — above the
              composer and its typing indicator — so progress stays in view. */}
            <GoalStatusBanner conversationId={conversationId} />
          </div>

          <div className="flex shrink-0 flex-col gap-[6px] pb-4">
            <SkillInstallRestartBanner conversationId={conversationId} />
            <BtwMessages conversationId={conversationId} />
            {errorMessage && (
              <ErrorMessageBanner
                message={errorMessage}
                code={errorCode}
                classification={errorClassification}
                onDismiss={removeErrorMessage}
                onRetry={
                  errorMessage === SERVER_CONNECTION_ERROR_MESSAGE
                    ? () => conversationWebSocket?.reconnect()
                    : undefined
                }
                onReauth={
                  isAcpAuthErrorCode(errorCode)
                    ? () => navigate("/settings/agents")
                    : undefined
                }
              />
            )}

            {llmBlocked && !isArchivedConversation && (
              <LlmNotConfiguredBanner />
            )}

            {isArchivedConversation ? (
              // Archived / sandbox-error: show a read-only notice in place of
              // the chat input. The conversation history above is still visible.
              <div
                data-testid="archived-conversation-banner"
                className="mx-1 px-4 py-3 rounded-lg bg-[var(--oh-surface)] border border-[var(--oh-border-subtle)]"
              >
                <p className="text-xs font-semibold text-[var(--oh-foreground)]">
                  {sandboxStatus === "ERROR"
                    ? t(I18nKey.CHAT_INTERFACE$ERROR_SANDBOX_TITLE)
                    : t(I18nKey.CHAT_INTERFACE$ARCHIVED_SANDBOX_TITLE)}
                </p>
                <p className="text-xs text-[var(--oh-muted)] mt-0.5">
                  {sandboxStatus === "ERROR"
                    ? t(I18nKey.CHAT_INTERFACE$ERROR_SANDBOX_DESCRIPTION)
                    : t(I18nKey.CHAT_INTERFACE$ARCHIVED_SANDBOX_DESCRIPTION)}
                </p>
              </div>
            ) : (
              <div className="relative">
                <div className="pointer-events-none absolute inset-x-0 bottom-full mb-1 z-20">
                  <div className="flex justify-between relative">
                    <div className="flex items-end gap-1 pointer-events-auto">
                      <ConfirmationModeEnabled />
                      {isStartingStatus && (
                        <ChatStatusIndicator
                          statusColor={serverStatusColor}
                          status={serverStatusText}
                        />
                      )}
                    </div>

                    {!hitBottom ? (
                      <div className="absolute left-1/2 transform -translate-x-1/2 bottom-0 pointer-events-auto">
                        <ScrollToBottomButton onClick={scrollDomToBottom} />
                      </div>
                    ) : (
                      (curAgentState === AgentState.RUNNING ||
                        isPlanningAgentRunning) && (
                        <div className="pointer-events-none absolute inset-x-9 bottom-0 flex justify-center">
                          <TypingIndicator events={allConversationEvents} />
                        </div>
                      )
                    )}
                  </div>
                </div>

                <InteractiveChatBox
                  onSubmit={handleSendMessage}
                  disabled={isNewConversationPending || llmBlocked}
                  hasStartedConversation={hasStartedConversation}
                />
              </div>
            )}
          </div>
        </div>
      </ScrollProvider>
    </WorkspaceFilesForChatProvider>
  );
}