Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
22.5 KB
Raw Blame
/**
 * Chronological FlashList chat with initial bottom positioning and MVCP.
 * Follow new messages only while the user is near the bottom; preserve reading
 * position through asynchronous Markdown and image resizing.
 *
 * Render all normalized message roles and failure styling. Attachments not used
 * inline appear as standalone cards. Long-press actions highlight the target
 * without adding a border that would shift the layout.
 */
import { useMemo, useState } from "react";
import { ActivityIndicator, Pressable, View } from "react-native";
import { FlashList } from "@shopify/flash-list";
import { Ionicons } from "@expo/vector-icons";
import { useQuery } from "@tanstack/react-query";
import type {
  Agent,
  ChatMessage,
  ChatPendingTask,
  ChatQuickAction,
  TaskMessagePayload,
} from "@multica/core/types";
import type { AgentAvailability } from "@multica/core/agents";
import { continuousCorners } from "@/lib/radius";
import { THEME } from "@/lib/theme";
import { useColorScheme } from "@/lib/use-color-scheme";
import { taskMessagesOptions } from "@/data/queries/chat";
import { Text } from "@/components/ui/text";
import { Markdown } from "@/lib/markdown";
import { ImageSequenceProvider } from "@/lib/markdown/image-sequence";
import { failureReasonKey } from "@/lib/failure-reason-label";
import type { QueuedChatMessage } from "@/lib/chat-queued-messages";
import { formatElapsedMs, formatMessageTime } from "@/lib/format-elapsed";
import { cn } from "@/lib/utils";
import { useChatSelectStore } from "@/data/chat-select-store";
import { useChatMessageLongPress } from "./message-long-press";
import { ChatEmptyState } from "./chat-empty-state";
import { ChatTimeline } from "./chat-timeline";
// Reuse the comment thread's standalone attachment list — same design web
// reuses in chat (AttachmentList). Renders any bound attachment not already
// referenced inline in the message content, with same-file dedup.
import { CommentAttachmentList } from "@/components/issue/comment-attachment-list";
import { StatusPill } from "./status-pill";
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from "@/components/ui/collapsible";
import { useLocale, useT } from "@/lib/i18n";

interface Props {
  messages: ChatMessage[];
  loading: boolean;
  /** Has the workspace ever started a chat? Drives empty-state copy. */
  hasSessions: boolean;
  /** Currently picked / inherited agent. */
  agent: Agent | null;
  /** Receive a conversation-starter tap. Caller writes into the draft store
   *  (or focuses the composer with the text) — empty state stays neutral
   *  about send vs. preview. */
  onPickPrompt: (text: string) => void;
  /** Send a persisted assistant follow-up without first copying it into draft. */
  onQuickAction?: (action: ChatQuickAction) => void | Promise<unknown>;
  quickActionsDisabled?: boolean;
  /** Server-authoritative pending-task snapshot for the active session.
   *  Used to render the live timeline + status line as the last item in
   *  the message stream, mirroring web's
   *  `packages/views/chat/components/chat-message-list.tsx` placement. */
  pendingTask?: ChatPendingTask | null;
  /** Live timeline rows for the in-flight task. Already fetched by the
   *  parent so this list doesn't have to manage its own subscription. */
  liveTaskMessages?: TaskMessagePayload[];
  /** Follow-ups waiting for their turn, rendered after the live reply. */
  queuedMessages?: QueuedChatMessage[];
  /** Resolved availability — drives the StatusPill's "Offline" /
   *  "Reconnecting" stages. Pass `undefined` while loading. */
  availability?: AgentAvailability;
}

export function ChatMessageList({
  messages: allMessages,
  loading,
  hasSessions,
  agent,
  onPickPrompt,
  onQuickAction,
  quickActionsDisabled = false,
  pendingTask,
  liveTaskMessages,
  queuedMessages = [],
  availability,
}: Props) {
  // Top-level selection subscription gates the outer "tap-outside-to-dismiss"
  // Pressable below. When null, the Pressable stays disabled and every tap
  // passes through to the list cells / bubble long-press wrappers normally.
  const selectingId = useChatSelectStore((s) => s.selectingId);
  // A wakeup turn's input is the runtime's, not a message the person wrote;
  // its content is withheld, so it would render as an empty bubble.
  const messages = useMemo(
    () => allMessages.filter((m) => m.message_kind !== "wakeup"),
    [allMessages],
  );

  // Every image in this session, in message order (MUL-5752), so tapping one
  // opens the lightbox at its position and a swipe walks the rest.
  //
  // Above the loading / empty early returns because hooks must run on every
  // render — an empty `messages` just yields an empty block list.
  //
  // Persisted messages only — same boundary web draws: a task transcript's
  // images live behind a separate cache and inside a folded section, so they
  // keep opening on their own rather than joining a sequence the reader
  // cannot see the rest of.
  const imageBlocks = useMemo(
    () =>
      messages.map((message) => ({
        content: message.content,
        attachments: message.attachments,
      })),
    [messages],
  );

  if (loading && messages.length === 0) {
    return (
      <View className="flex-1 items-center justify-center">
        <ActivityIndicator />
      </View>
    );
  }

  if (messages.length === 0) {
    // Empty new-chat state. Lives here (rather than the parent screen) so
    // the empty state and the rendered list share spacing/layout rules.
    return (
      <ChatEmptyState
        hasSessions={hasSessions}
        agent={agent}
        onPickPrompt={onPickPrompt}
      />
    );
  }

  // Show the live trace + status line until the persisted assistant
  // message lands. Once chat:done writes the assistant row, AssistantRow's
  // own timeline (read from the same cache entry) owns the render — no
  // double-rendering.
  const pendingTaskId = pendingTask?.task_id ?? null;
  const pendingAlreadyPersisted =
    !!pendingTaskId &&
    messages.some(
      (m) => m.role === "assistant" && m.task_id === pendingTaskId,
    );
  const showLiveSection = !!pendingTaskId && !pendingAlreadyPersisted;
  const showLiveTimeline =
    showLiveSection && (liveTaskMessages?.length ?? 0) > 0;

  return (
    // Outer Pressable owns the "tap anywhere outside the selected bubble
    // to exit text-selection mode" gesture. Disabled when no message is
    // selected, so it's a layout-only wrapper and every tap passes straight
    // through to the FlashList cells. Active state captures any tap that
    // didn't fire an inner Pressable — bubble cells in selecting mode
    // render their body without a Pressable wrapper (see `MessageRow`'s
    // `if (isSelecting) return body;`), so taps on the selected bubble
    // also dismiss, matching iOS Notes / iMessage behaviour. Scroll
    // gestures are unaffected (Pressable only intercepts non-drag taps).
    <ImageSequenceProvider blocks={imageBlocks}>
    <Pressable
      onPress={
        selectingId
          ? () => useChatSelectStore.getState().clear()
          : undefined
      }
      disabled={!selectingId}
      style={{ flex: 1 }}
    >
    {/* `key` on first message id forces remount on session switch so
        `startRenderingFromBottom` re-fires and we land at the new
        session's bottom (instead of inheriting the previous session's
        scroll position). Cheap because sessions are switched, not
        re-rendered every keystroke. */}
    <FlashList
      key={messages[0]?.id ?? "empty"}
      data={messages}
      keyExtractor={(m) => m.id}
      renderItem={({ item }) => (
        <MessageRow
          message={item}
          onQuickAction={onQuickAction}
          quickActionsDisabled={quickActionsDisabled}
        />
      )}
      ItemSeparatorComponent={MessageSeparator}
      ListFooterComponent={
        showLiveSection || queuedMessages.length > 0 ? (
          <View style={{ paddingTop: 12 }} className="gap-2">
            {showLiveTimeline ? (
              <ChatTimeline items={liveTaskMessages ?? []} isStreaming />
            ) : null}
            {showLiveSection ? (
              <StatusPill
                pendingTask={pendingTask}
                taskMessages={liveTaskMessages}
                availability={availability}
              />
            ) : null}
            {queuedMessages.map((row) => (
              <QueuedMessageRow key={row.task_id} message={row} />
            ))}
          </View>
        ) : null
      }
      // Outer padding mirrors web's max-w-4xl px-5 py-4 container at
      // mobile scale. Vertical gap between bubbles handled by
      // ItemSeparatorComponent (FlashList doesn't honour `gap-*` on
      // contentContainer the way FlatList's gap-via-NativeWind did).
      contentContainerStyle={{
        paddingHorizontal: 16,
        paddingTop: 12,
        paddingBottom: 16,
      }}
      // Chat behavior: initial render at the bottom; when new messages
      // arrive AND the user is within 20% of the bottom, auto-scroll.
      // Reading history (further than 20% up) is preserved. This single
      // prop replaces the entire FlatList-era guard ref dance.
      maintainVisibleContentPosition={{
        autoscrollToBottomThreshold: 0.2,
        startRenderingFromBottom: true,
      }}
      // Any user-initiated scroll exits message text-selection mode —
      // matches iMessage's behavior where scrolling implicitly commits /
      // dismisses the selection caret. Hooks both drag-start and the
      // momentum kick after a flick so a fast scroll can't escape.
      onScrollBeginDrag={() => useChatSelectStore.getState().clear()}
      onMomentumScrollBegin={() => useChatSelectStore.getState().clear()}
      // iMessage-style keyboard dismissal: dragging the list pulls the
      // keyboard down with the finger (iOS); tapping empty space between
      // bubbles dismisses it. `handled` keeps Pressables inside bubbles
      // (long-press action sheet etc.) firing normally.
      keyboardDismissMode="interactive"
      keyboardShouldPersistTaps="handled"
    />
    </Pressable>
    </ImageSequenceProvider>
  );
}

function MessageSeparator() {
  return <View style={{ height: 12 }} />;
}

/**
 * A follow-up waiting for its turn. Sits after the live reply, where it will
 * run, dimmed and captioned so it does not read as part of the current
 * reply. Read-only on mobile: removing or editing queued messages stays on
 * web and desktop. Once its turn starts the server row returns to the
 * transcript in this position.
 */
function QueuedMessageRow({ message }: { message: QueuedChatMessage }) {
  const { t } = useT("chat");
  const { colorScheme } = useColorScheme();
  return (
    <View className="self-end max-w-[80%] items-end gap-1 opacity-60">
      <View
        className="gap-1.5 rounded-xl border-2 border-transparent bg-muted px-3.5 py-2"
        style={continuousCorners}
      >
        {message.content.trim() ? (
          <Markdown
            content={message.content}
            attachments={message.attachments}
            compact
          />
        ) : null}
        <CommentAttachmentList
          attachments={message.attachments}
          content={message.content}
        />
      </View>
      <View className="flex-row items-center gap-1 px-1">
        <Ionicons
          name="time-outline"
          size={12}
          color={THEME[colorScheme].mutedForeground}
        />
        <Text className="text-xs text-muted-foreground">
          {t("status.queued")}
        </Text>
      </View>
    </View>
  );
}

function MessageRow({
  message,
  onQuickAction,
  quickActionsDisabled,
}: {
  message: ChatMessage;
  onQuickAction?: (action: ChatQuickAction) => void | Promise<unknown>;
  quickActionsDisabled: boolean;
}) {
  const isUser = message.role === "user";
  const { t } = useT("chat");
  const isFailure = !!message.failure_reason;
  const isSelecting = useChatSelectStore(
    (s) => s.selectingId === message.id,
  );
  const longPress = useChatMessageLongPress(message);

  if (isFailure) {
    return (
      <FailureBubble
        reasonLabel={t(failureReasonKey(message.failure_reason))}
        rawError={message.content}
        elapsedMs={message.elapsed_ms ?? null}
        createdAt={message.created_at}
        isSelecting={isSelecting}
        longPress={longPress}
      />
    );
  }

  if (isUser) {
    // User bubble: same Markdown pipeline as assistant — `@mention`
    // serialisation `[MUL-1](mention://issue/<id>)`, inline links, and
    // inline code resolve identically to web's
    // `packages/views/chat/components/chat-message-list.tsx` user branch.
    // Width is capped at 80% so the bubble keeps the iMessage-style
    // trailing alignment instead of stretching across the column.
    const body = (
      <View
        className={cn(
          "self-end max-w-[80%] gap-1.5 rounded-xl border-2 px-3.5 py-2 transition-colors",
          isSelecting
            ? "bg-primary/5 border-primary/30"
            : longPress.isPressed
              ? "bg-muted border-primary/30"
              : "bg-muted border-transparent",
        )}
        style={continuousCorners}
      >
        <Markdown
          content={message.content}
          attachments={message.attachments}
          selectable={isSelecting}
          compact
        />
        <CommentAttachmentList
          attachments={message.attachments}
          content={message.content}
        />
      </View>
    );
    if (isSelecting) return body;
    return (
      <Pressable
        onLongPress={longPress.onLongPress}
        delayLongPress={500}
      >
        {body}
      </Pressable>
    );
  }

  // Assistant: timeline fold + markdown + elapsed caption. See
  // AssistantRow for why timeline is lifted into its own component.
  return (
    <AssistantRow
      message={message}
      isSelecting={isSelecting}
      longPress={longPress}
      onQuickAction={onQuickAction}
      quickActionsDisabled={quickActionsDisabled}
    />
  );
}

/**
 * Persisted assistant message. Renders:
 *
 *   - Process-steps fold (from `task-messages` cache; same cache fed by
 *     the live timeline above, so completed runs keep their trace).
 *   - Markdown content (the model's final answer).
 *   - "Replied in Ns · 14:32" caption: duration from `elapsed_ms` when
 *     stamped, plus the wall-clock reply time.
 *
 * Web's equivalent is `AssistantMessage` in packages/views/chat/components/
 * chat-message-list.tsx — same shape, simplified for RN (no inner Tooltip
 * / Copy button — long-press already exposes Copy via the native action
 * sheet, and selection mode owns the highlight, so a hover-only Copy
 * affordance would be redundant on mobile).
 */
function AssistantRow({
  message,
  isSelecting,
  longPress,
  onQuickAction,
  quickActionsDisabled,
}: {
  message: ChatMessage;
  isSelecting: boolean;
  longPress: ReturnType<typeof useChatMessageLongPress>;
  onQuickAction?: (action: ChatQuickAction) => void | Promise<unknown>;
  quickActionsDisabled: boolean;
}) {
  // Read the cached timeline if any. `enabled` (in taskMessagesOptions) is
  // gated on isTaskMessageTaskId — optimistic id prefixes never fetch, so
  // freshly-sent messages don't spam the API while waiting for the real
  // task_id to land. Cached cells (after live timeline finished) return
  // synchronously with no network roundtrip.
  const { data: timeline = [] } = useQuery(
    taskMessagesOptions(message.task_id),
  );
  // no_response (MUL-4351, mirrors packages/views AssistantMessage): the agent
  // completed this turn without text. Keep the tool timeline and show a notice
  // instead of an empty Markdown block; caption reads "Finished in" not
  // "Replied in".
  const isNoResponse = message.message_kind === "no_response";
  const { t } = useT("chat");
  const body = (
    <View className="gap-1.5">
      {timeline.length > 0 ? (
        <ChatTimeline items={timeline} />
      ) : null}
      {isNoResponse ? (
        <Text className="text-sm italic text-muted-foreground">
          {t("no_response")}
        </Text>
      ) : (
        <Markdown
          content={message.content}
          attachments={message.attachments}
          selectable={isSelecting}
        />
      )}
      {/* Standalone attachment cards for anything not referenced inline. An
          image-only reply is a real ('message') outcome with empty content, so
          it flows through the else-branch above (renders nothing) and the cards
          here ARE the reply. */}
      <CommentAttachmentList
        attachments={message.attachments}
        content={message.content}
      />
      <ReplyTimingCaption
        variant={isNoResponse ? "finished" : "replied"}
        elapsedMs={message.elapsed_ms ?? null}
        createdAt={message.created_at}
      />
    </View>
  );
  const messageBody = isSelecting ? body : (
    <Pressable onLongPress={longPress.onLongPress} delayLongPress={500}>
      {body}
    </Pressable>
  );
  if (!onQuickAction || (message.quick_actions?.length ?? 0) === 0) {
    return messageBody;
  }
  return (
    <View className="gap-2">
      {messageBody}
      <QuickActions
        actions={message.quick_actions ?? []}
        disabled={quickActionsDisabled}
        onSelect={onQuickAction}
      />
    </View>
  );
}

function QuickActions({
  actions,
  disabled,
  onSelect,
}: {
  actions: ChatQuickAction[];
  disabled: boolean;
  onSelect: (action: ChatQuickAction) => void | Promise<unknown>;
}) {
  const [submitting, setSubmitting] = useState(false);
  const { t } = useT("chat");
  const blocked = disabled || submitting;

  const handleSelect = async (action: ChatQuickAction) => {
    if (blocked) return;
    setSubmitting(true);
    try {
      await onSelect(action);
    } catch {
      // The send path rolls back its optimistic message. Keep the action usable
      // so a transient request failure can be retried.
    } finally {
      setSubmitting(false);
    }
  };

  return (
    <View
      className="flex-row flex-wrap gap-2 pt-0.5"
      accessibilityLabel={t("a11y.suggested_followups")}
    >
      {actions.slice(0, 3).map((action, index) => (
        <Pressable
          key={`${action.label}-${index}`}
          accessibilityRole="button"
          accessibilityState={{ disabled: blocked }}
          disabled={blocked}
          onPress={() => void handleSelect(action)}
          className={cn(
            "min-h-10 max-w-full flex-row items-center gap-1 rounded-full border px-3 active:opacity-70",
            action.primary
              ? "border-primary/30 bg-primary/10"
              : "border-border bg-background",
            blocked && "opacity-50",
          )}
        >
          <Text
            numberOfLines={1}
            className={cn(
              "shrink text-sm font-medium",
              action.primary ? "text-primary" : "text-foreground",
            )}
          >
            {action.label}
          </Text>
          {action.primary ? (
            <Text className="text-sm font-medium text-primary">↗</Text>
          ) : null}
        </Pressable>
      ))}
    </View>
  );
}

// Persistent caption rendered under the assistant bubble / failure bubble
// once the turn settles: duration from the server-stamped `elapsed_ms` plus
// the wall-clock reply time, e.g. "Replied in 38s · 14:32". The server
// computes elapsed once at task completion, so the caption is identical
// across reloads and clients; legacy rows without `elapsed_ms` still show
// the time. Mirrors web's MessageFooter caption in packages/views/chat/
// components/chat-message-list.tsx.
function ReplyTimingCaption({
  variant,
  elapsedMs,
  createdAt,
}: {
  variant: "replied" | "failed" | "finished";
  elapsedMs: number | null;
  createdAt: string;
}) {
  const { t } = useT("chat");
  const locale = useLocale();
  const timeLabel = formatMessageTime(createdAt, locale);
  const parts: string[] = [];
  if (elapsedMs != null) {
    parts.push(
      variant === "replied"
        ? t("elapsed.replied", { time: formatElapsedMs(elapsedMs) })
        : variant === "finished"
          ? t("elapsed.finished", { time: formatElapsedMs(elapsedMs) })
          : t("elapsed.failed", { time: formatElapsedMs(elapsedMs) }),
    );
  }
  if (timeLabel) parts.push(timeLabel);
  if (parts.length === 0) return null;
  return (
    <Text className="text-xs text-muted-foreground/80 mt-1">
      {parts.join(" · ")}
    </Text>
  );
}

function FailureBubble({
  reasonLabel,
  rawError,
  elapsedMs,
  createdAt,
  isSelecting,
  longPress,
}: {
  reasonLabel: string;
  rawError: string;
  elapsedMs: number | null;
  createdAt: string;
  isSelecting: boolean;
  longPress: ReturnType<typeof useChatMessageLongPress>;
}) {
  const hasRawError = rawError.trim().length > 0;
  const { t } = useT("chat");

  // B6: pass `selectable={isSelecting}` rather than hard-coding
  // `selectable` — otherwise UIKit's text-selection gesture pre-empts
  // our long-press handler and the action sheet never fires. Select-mode
  // cue is the border-tint to primary; bg stays destructive so the
  // failure signal is never lost.
  const body = (
    <View className="self-start max-w-[80%]">
      <View
        className={cn(
          "rounded-xl border-2 bg-destructive/10 px-3.5 py-2 transition-colors",
          isSelecting || longPress.isPressed
            ? "border-primary/30"
            : "border-destructive/30",
        )}
        style={continuousCorners}
      >
        <Text className="text-xs font-semibold text-destructive">
          {reasonLabel}
        </Text>
        {hasRawError ? (
          <Collapsible>
            <CollapsibleTrigger asChild>
              <View
                accessibilityRole="button"
                accessibilityLabel={t("a11y.show_error_details")}
                className="mt-1 flex-row items-center gap-1 active:opacity-70"
              >
                <Ionicons
                  name="chevron-forward"
                  size={12}
                  color="#71717a"
                />
                <Text className="text-xs text-muted-foreground">
                  {t("details.show")}
                </Text>
              </View>
            </CollapsibleTrigger>
            <CollapsibleContent>
              <View className="mt-1 rounded-xs bg-muted/40 px-2 py-1.5">
                <Text
                  className="text-xs text-muted-foreground"
                  selectable={isSelecting}
                >
                  {rawError}
                </Text>
              </View>
            </CollapsibleContent>
          </Collapsible>
        ) : null}
      </View>
      <ReplyTimingCaption
        variant="failed"
        elapsedMs={elapsedMs}
        createdAt={createdAt}
      />
    </View>
  );
  if (isSelecting) return body;
  return (
    <Pressable onLongPress={longPress.onLongPress} delayLongPress={500}>
      {body}
    </Pressable>
  );
}