Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

19.2 KB
Raw Blame
import {
  memo,
  useMemo,
  useEffect,
  useRef,
  useState,
  type MouseEvent as ReactMouseEvent,
  type ReactNode,
} from "react";
import { useTranslation } from "react-i18next";
import type { UiMessage } from "@pi-desktop/shared";
import { splitComposerDirectoryPrompt, splitInlineContent } from "@pi-desktop/shared";
import { DirectoryReferenceCard } from "../DirectoryReferenceCard";
import { useOpenChatFileRef } from "../../../hooks/use-preview-target";
import { sessionAllows } from "../../../lib/remote-session-safety";
import { useSlotSessionId } from "../../../plugins/renderer-slots/use-slots";
import { useAppStore } from "../../../stores/app-store";
import { Markdown } from "../../../components/Markdown";
import {
  IconChevronLeft,
  IconChevronRight,
  IconPencil,
  IconTrash,
} from "../../../components/icons";
import { TooltipButton } from "../../../components/ui";
import { userMessageMenuItems } from "./menu-items";
import { ActionSlotSide } from "./ActionBarSlots";
import { slotMessage } from "../../../plugins/renderer-slots/slot-message";
import { SessionMessageOrigin } from "./SessionMessageOrigin";
import {
  CopyButton,
  FileRefChip,
  LinkifiedText,
  MessageAttachmentImage,
  MessageTimestamp,
} from "./shared";
import {
  useChatTextActions,
  useTranscriptMenu,
} from "./TranscriptMenu";
import { getExtraMessageAttachments } from "./extra-attachments";
import { directionAttr } from "../../../lib/text-direction";
import { CollapsibleUserText } from "./CollapsibleUserText";

/**
 * The typed form of a Skill or `@agent` invocation (ADR 0346): each validated
 * mention is a chip, and the text around it stays text.
 */
function MentionInvocationText({ message }: { message: UiMessage }) {
  const command = message.command ?? "";
  const mentions = [
    ...(message.skillMentions ?? []).map((mention) => ({ ...mention, sigil: "/", title: mention.id, agent: false })),
    ...(message.agentMentions ?? []).map((mention) => ({ ...mention, sigil: "@", title: `@${mention.name}`, agent: true })),
  ].sort((a, b) => a.start - b.start);
  const parts: ReactNode[] = [];
  let cursor = 0;
  for (const mention of mentions) {
    if (
      !Number.isInteger(mention.start) ||
      !Number.isInteger(mention.end) ||
      mention.start < cursor ||
      mention.end > command.length ||
      !command.slice(mention.start, mention.end).startsWith(mention.sigil)
    ) {
      return <LinkifiedText text={command} attachments={message.attachments} />;
    }
    if (mention.start > cursor) {
      parts.push(<LinkifiedText key={`text-${cursor}`} text={command.slice(cursor, mention.start)} attachments={message.attachments} />);
    }
    parts.push(
      <code
        key={`mention-${mention.start}`}
        className="chat-command-chip"
        title={mention.title}
        {...(mention.agent ? { "data-agent-mention": "" } : {})}
      >
        {command.slice(mention.start, mention.end)}
      </code>,
    );
    cursor = mention.end;
  }
  if (cursor < command.length) {
    parts.push(<LinkifiedText key={`text-${cursor}`} text={command.slice(cursor)} attachments={message.attachments} />);
  }
  return <>{parts}</>;
}

export const MessageRow = memo(function MessageRow({
  message,
  isRunning,
}: {
  message: UiMessage;
  isRunning: boolean;
}) {
  const { t } = useTranslation();
  const openTranscriptMenu = useTranscriptMenu();
  const { copyText, selectText } = useChatTextActions();
  const prepareUserMessageEdit = useAppStore((s) => s.prepareUserMessageEdit);
  const editUserMessage = useAppStore((s) => s.editUserMessage);
  const activateMessageRevision = useAppStore((s) => s.activateMessageRevision);
  const deleteMessage = useAppStore((s) => s.deleteMessage);
  const paneSessionId = useSlotSessionId();
  const session = useAppStore((s) => s.sessions.find((item) => item.id === paneSessionId));
  const canEditMessages = sessionAllows(session ?? (paneSessionId.startsWith("remote:") ? { id: paneSessionId } : undefined), "canEditMessages");
  const isUser = message.role === "user";
  const isSessionMessage = Boolean(message.sessionMessage);
  const editableUserMessage = isUser && !isSessionMessage && canEditMessages;
  const localWorkspaceRoot = useAppStore((s) => s.workspace?.path);
  const workspaceRoot = sessionAllows(session, "canUseLocalTools") ? localWorkspaceRoot : undefined;
  const openFileRef = useOpenChatFileRef();
  // userAction belongs to user cards; other rows keep a plugin-free bar.
  const slotUser = isUser ? slotMessage("user", message) : undefined;
  // Slash prompts are stored expanded; editing works on the typed form so the
  // resent turn re-expands the template (D123).
  const editSeed =
    (editableUserMessage && message.command) || (message.content || "");
  const [editing, setEditing] = useState(false);
  const [loadingEdit, setLoadingEdit] = useState(false);
  const editRequest = useRef<AbortController | null>(null);
  useEffect(() => () => editRequest.current?.abort(), []);
  const [editValue, setEditValue] = useState(editSeed);
  const [retryingEdit, setRetryingEdit] = useState(false);
  const copyLabel = t("chat.copy");
  const editLabel = t("chat.editMessage");
  const deleteLabel = t("chat.deleteMessage");
  // Runtime chunks are already progressive. Rendering that source directly
  // avoids a second per-frame state loop while Markdown memoizes stable blocks.
  const displayed = message.content || "";
  const directories = useMemo(
    () => isUser ? splitComposerDirectoryPrompt(displayed) : { text: displayed, paths: [] },
    [isUser, displayed],
  );
  const folderSessionId = sessionAllows(session ?? { id: paneSessionId }, "canUseLocalTools")
    ? paneSessionId : undefined;
  const hasAnswer = Boolean((message.content || "").trim());
  const revisionCount = message.revisionCount ?? 0;
  const activeRevision = message.activeRevision ?? revisionCount;
  const showRevisionPager = editableUserMessage && revisionCount > 1;
  const extraAttachments = useMemo(() => {
    return getExtraMessageAttachments(
      String(message.content || ""),
      message.attachments,
      workspaceRoot,
    );
  }, [message.attachments, message.content, workspaceRoot]);
  // The user's own order: an image chip the draft named inline renders between
  // the text runs it sat between, exactly where the Composer showed it.
  const userContentParts = useMemo(
    () => splitInlineContent(directories.text, message.attachments ?? []).parts,
    [message.attachments, directories.text],
  );
  // #2388: a Persian/Arabic paragraph opening with an English word must keep
  // its RTL base direction; direction follows the majority strong script, not
  // the first strong character.
  const userTextDirection = useMemo(
    () => directionAttr(directories.text),
    [directories.text],
  );
  const renderContentPart = (
    part: (typeof userContentParts)[number],
    index: number,
    keyPrefix: string,
  ) => part.kind === "text" ? (
    <LinkifiedText
      key={`${keyPrefix}-${index}`}
      text={part.text}
      attachments={message.attachments}
      sourceOffset={part.start}
    />
  ) : (
    <MessageAttachmentImage
      key={`${keyPrefix}-${index}`}
      attachment={part.attachment}
      onOpenFile={openFileRef}
      inline
    />
  );
  // Group the existing inline runs inside actual paragraph boxes. This gives
  // each paragraph its own base direction and alignment without moving chips
  // out of their paragraph or losing LinkifiedText's source offsets.
  const userParagraphs: ReactNode[] = [];
  let paragraphStart = 0;
  for (const paragraph of directories.text.match(/[^\n]*(?:\n+|$)/g) ?? []) {
    if (!paragraph) continue;
    const start = paragraphStart;
    const end = start + paragraph.length;
    paragraphStart = end;
    userParagraphs.push(
      <span key={start} dir={directionAttr(paragraph)} style={{ display: "block", textAlign: "start" }}>
        {userContentParts.map((part, index) => {
          if (part.end <= start || part.start >= end) return null;
          if (part.kind === "attachment") return renderContentPart(part, index, `paragraph-${start}`);
          const sliceStart = Math.max(start, part.start);
          const sliceEnd = Math.min(end, part.end);
          return renderContentPart({
            ...part,
            start: sliceStart,
            end: sliceEnd,
            text: part.text.slice(sliceStart - part.start, sliceEnd - part.start),
          }, index, `paragraph-${start}`);
        })}
      </span>,
    );
  }
  // An attachment the body does not already name inline continues the body
  // text instead of taking a line of its own above it.
  const attachmentChips = extraAttachments.length ? (
    <span
      className="message-attachments"
      role="list"
      aria-label={t("chat.messageAttachments")}
    >
      {extraAttachments.map((attachment) =>
        attachment.kind === "image" ? (
          <MessageAttachmentImage
            key={`${attachment.ref}:${attachment.name}`}
            attachment={attachment}
            onOpenFile={openFileRef}
          />
        ) : (
          <span key={`${attachment.ref}:${attachment.name}`} role="listitem">
            <FileRefChip
              name={attachment.name}
              path={attachment.ref}
              kind={attachment.kind}
              mimeType={attachment.mimeType}
              onOpen={openFileRef}
            />
          </span>
        ),
      )}
    </span>
  ) : null;
  const beginEdit = async () => {
    if (!editableUserMessage || isRunning || loadingEdit) return;
    const request = new AbortController();
    editRequest.current?.abort();
    editRequest.current = request;
    setLoadingEdit(true);
    // Subscribe synchronously: React can batch A→B→A into a single render.
    const unsubscribe = useAppStore.subscribe((state, previous) => {
      if (state.activeSessionId !== previous.activeSessionId ||
        state.selectingSessionId !== previous.selectingSessionId) request.abort();
    });
    request.signal.addEventListener("abort", () => {
      unsubscribe();
      if (editRequest.current === request) setLoadingEdit(false);
    }, { once: true });
    try {
      const full = await prepareUserMessageEdit(message.id, request.signal);
      if (!full || request.signal.aborted || editRequest.current !== request) return;
      setEditValue(full.command || full.content || "");
      setEditing(true);
    } finally {
      unsubscribe();
      if (editRequest.current === request) {
        editRequest.current = null;
        setLoadingEdit(false);
      }
    }
  };
  const cancelEdit = () => {
    setEditValue(editSeed);
    setEditing(false);
  };
  const retryEdit = async () => {
    const next = editValue.trim();
    if (!editableUserMessage || retryingEdit || (!next && !message.attachments?.length)) return;
    setRetryingEdit(true);
    const saved = await editUserMessage(message.id, next, message.attachments);
    setRetryingEdit(false);
    if (saved) setEditing(false);
  };
  /*
    The pointer path to the actions the hover row already offers. Only a human
    turn is owned here: an assistant answer belongs to its turn, so this row
    must not answer for one — it would offer Copy without the Regenerate and
    Branch items that live on the turn.
  */
  const onContextMenu = (event: ReactMouseEvent<HTMLDivElement>) => {
    if (!isUser) return;
    openTranscriptMenu(event, {
      label: t("chat.messageMenu"),
      items: userMessageMenuItems({
        t,
        text: editing ? editValue : message.content || "",
        selectTarget: event.currentTarget.querySelector<HTMLElement>(
          editing ? ".message-edit-input" : ".message-bubble",
        ),
        editable: editableUserMessage && !editing && !loadingEdit,
        running: isRunning,
        revision: !editing && showRevisionPager
          ? { count: revisionCount, active: activeRevision }
          : null,
        actions: { copyText, selectText },
        onEdit: () => void beginEdit(),
        onDelete: () => void deleteMessage(message.id),
        onActivateRevision: (index) =>
          void activateMessageRevision(message.id, index),
      }),
    });
  };
  return (
    <div
      className={`message-row ${isSessionMessage ? "session-message" : isUser ? "user" : message.role}`}
      data-minimap-id={message.id}
      data-message-id={message.id}
      data-row-role={isSessionMessage ? undefined : "user"}
      onContextMenu={onContextMenu}
      role="article"
      aria-label={isSessionMessage ? t("sessionCollaboration.agentMessage") : isUser ? t("chat.userMessage") : t("chat.assistantMessage")}
    >
      <div className="message-col">
        {message.sessionMessage ? <SessionMessageOrigin origin={message.sessionMessage} /> : null}
        {isUser || displayed ? (
          <div className="message-bubble">
            {editing && editableUserMessage ? (
              <form
                className="message-edit"
                aria-busy={retryingEdit || undefined}
                onSubmit={(event) => {
                  event.preventDefault();
                  void retryEdit();
                }}
              >
                <textarea
                  className="message-edit-input selectable"
                  value={editValue}
                  rows={Math.min(12, Math.max(3, editValue.split("\n").length))}
                  aria-label={editLabel}
                  autoFocus
                  spellCheck={false}
                  autoCorrect="off"
                  autoCapitalize="off"
                  disabled={retryingEdit}
                  onChange={(event) => setEditValue(event.target.value)}
                  onKeyDown={(event) => {
                    if (event.nativeEvent.isComposing || event.keyCode === 229) return;
                    if (event.key === "Escape") {
                      cancelEdit();
                    } else if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
                      event.preventDefault();
                      void retryEdit();
                    }
                  }}
                />
                <div className="message-edit-actions">
                  <button
                    type="button"
                    className="icon-btn message-edit-cancel"
                    disabled={retryingEdit}
                    onClick={cancelEdit}
                  >
                    {t("chat.cancelEdit")}
                  </button>
                  <button
                    type="submit"
                    className="send-btn message-edit-submit"
                    disabled={retryingEdit || (!editValue.trim() && !message.attachments?.length)}
                  >
                    {retryingEdit ? t("chat.retryingEdit") : t("chat.retryEdit")}
                  </button>
                </div>
              </form>
            ) : isUser ? (
              <>
                {directories.paths.length ? (
                  <div className="directory-reference-list">
                    {directories.paths.map((path, index) => (
                      <DirectoryReferenceCard key={`${index}:${path}`} path={path} sessionId={folderSessionId} />
                    ))}
                  </div>
                ) : null}
                {directories.text || message.command ? (
                  <CollapsibleUserText messageId={message.id} dir={userTextDirection}>
                    {editableUserMessage && message.command ? (
                      <>
                        {message.skillMentions?.length || message.agentMentions?.length ? (
                          <MentionInvocationText message={message} />
                        ) : (
                          // Templates retain the existing whole-invocation chip.
                          <code
                            className="chat-command-chip"
                            data-source-start={0}
                            data-source-end={message.content.length}
                            title={directories.text}
                          >
                            {message.command}
                          </code>
                        )}
                        {userContentParts
                          .filter((part) => part.kind === "attachment")
                          .map((part, index) => renderContentPart(part, index, "inline"))}
                      </>
                    ) : (
                      userParagraphs
                    )}
                    {attachmentChips}
                  </CollapsibleUserText>
                ) : attachmentChips}
              </>
            ) : (
              <div className="prose-chat">
                <Markdown source={displayed} />
              </div>
            )}
          </div>
        ) : null}
        {!editing && (hasAnswer || showRevisionPager) ? (
          <div className="message-actions">
            <MessageTimestamp createdAt={message.createdAt} />
            <ActionSlotSide slot="userAction" side="left" message={slotUser} />
            {showRevisionPager ? (
              <div className="message-revision-pager" role="group" aria-label={t("chat.revisions")}>
                <TooltipButton
                  className="copy-btn icon revision-nav"
                  tooltip={t("chat.revisionPrev")}
                  ariaLabel={t("chat.revisionPrev")}
                  disabled={isRunning || activeRevision <= 1}
                  onClick={() =>
                    void activateMessageRevision(message.id, Math.max(1, activeRevision - 1))
                  }
                >
                  <IconChevronLeft size={13} />
                </TooltipButton>
                <span className="message-revision-label">
                  {t("chat.revisionPager", {
                    current: activeRevision,
                    total: revisionCount,
                  })}
                </span>
                <TooltipButton
                  className="copy-btn icon revision-nav"
                  tooltip={t("chat.revisionNext")}
                  ariaLabel={t("chat.revisionNext")}
                  disabled={isRunning || activeRevision >= revisionCount}
                  onClick={() =>
                    void activateMessageRevision(
                      message.id,
                      Math.min(revisionCount, activeRevision + 1),
                    )
                  }
                >
                  <IconChevronRight size={13} />
                </TooltipButton>
              </div>
            ) : null}
            {hasAnswer ? <CopyButton text={message.content} label={copyLabel} /> : null}
            {editableUserMessage ? (
              <TooltipButton
                className="copy-btn icon"
                tooltip={editLabel}
                ariaLabel={editLabel}
                disabled={isRunning || loadingEdit}
                onClick={() => void beginEdit()}
              >
                <IconPencil size={13} />
              </TooltipButton>
            ) : null}
            {editableUserMessage ? (
              <TooltipButton
                className="copy-btn icon danger"
                tooltip={deleteLabel}
                ariaLabel={deleteLabel}
                disabled={isRunning}
                onClick={() => void deleteMessage(message.id)}
              >
                <IconTrash size={13} />
              </TooltipButton>
            ) : null}
            <ActionSlotSide slot="userAction" side="right" message={slotUser} />
          </div>
        ) : null}
      </div>
    </div>
  );
});