Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

8.1 KB
Raw Blame
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com

"use client";

import React, { forwardRef, memo, useEffect, useLayoutEffect, useRef } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { ChevronDown } from "lucide-react";
import styles from "./chat-primitives.module.css";
import { useGT } from "gt-react";


// Extracted from Screenpipe's MarkdownBlock. Completed blocks stay formatted
// while only the unfinished tail changes; the final render uses the exact text.
function scanStreamingMarkdown(text: string) {
  let fenceCharacter: string | null = null;
  let fenceLength = 0;
  let lastBoundary = 0;
  let blockStart = 0;
  let lineStart = 0;
  const blocks: string[] = [];
  while (lineStart < text.length) {
    const newlineIndex = text.indexOf("\n", lineStart);
    const lineEnd = newlineIndex === -1 ? text.length : newlineIndex;
    const line = text.slice(lineStart, lineEnd).replace(/\r$/, "");
    const nextLineStart = newlineIndex === -1 ? text.length : newlineIndex + 1;
    const fenceMatch = line.match(/^ {0,3}(`{3,}|~{3,})(.*)$/);
    if (fenceMatch) {
      const marker = fenceMatch[1];
      const character = marker[0];
      if (!fenceCharacter) { fenceCharacter = character; fenceLength = marker.length; }
      else if (character === fenceCharacter && marker.length >= fenceLength && fenceMatch[2].trim() === "") {
        fenceCharacter = null; fenceLength = 0;
      }
    } else if (!fenceCharacter && line.trim() === "") {
      lastBoundary = nextLineStart;
      const block = text.slice(blockStart, lastBoundary);
      if (block.trim()) blocks.push(block);
      blockStart = lastBoundary;
    }
    lineStart = nextLineStart;
  }
  return { prefix: text.slice(0, lastBoundary), blocks };
}

export function stableStreamingMarkdownPrefix(text: string): string {
  return scanStreamingMarkdown(text).prefix;
}

type ChatMarkdownProps = {
  text: string;
  streaming?: boolean;
  className?: string;
  tailClassName?: string;
  /** Main Chat keeps its existing rich-media and native-link adapters. */
  renderBlock?: (text: string, index: number) => React.ReactNode;
  allowLink?: (url: string) => boolean;
  onOpenLink?: (url: string) => void;
};

const FormattedBlock = memo(function FormattedBlock({ text, allowLink, onOpenLink }: Pick<ChatMarkdownProps, "text" | "allowLink" | "onOpenLink">) {
  const ui = useGT();
  const allowed = (url: string) => {
    if (allowLink) return allowLink(url);
    try { const link = new URL(url); return ["http:", "https:"].includes(link.protocol) && !link.username && !link.password; }
    catch { return false; }
  };
  return <ReactMarkdown
    remarkPlugins={[remarkGfm]}
    skipHtml
    urlTransform={(url) => allowed(url) ? url : ""}
    components={{
      // A secondary memory answer never fetches arbitrary remote images.
      img: () => null,
      a: ({ href, children }) => href && allowed(href)
        ? <a href={href} target="_blank" rel="noreferrer noopener" onClick={onOpenLink ? (event) => { event.preventDefault(); onOpenLink(href); } : undefined}>{children}</a>
        : <span>{children}</span>,
      table: ({ children }) => <div className={styles.tableScroll} role="region" aria-label={ui("Scrollable table")} tabIndex={0}><table>{children}</table></div>,
    }}
  >{text}</ReactMarkdown>;
});

export const ChatMarkdown = memo(function ChatMarkdown({ text, streaming = false, renderBlock, className, tailClassName, ...links }: ChatMarkdownProps) {
  const { blocks, prefix } = streaming ? scanStreamingMarkdown(text) : { blocks: text ? [text] : [], prefix: text };
  const tail = text.slice(prefix.length);
  const content = <>
    {blocks.map((block, index) => renderBlock
      ? <React.Fragment key={index}>{renderBlock(block, index)}</React.Fragment>
      : <FormattedBlock key={index} text={block} {...links} />)}
    {tail && <div className={tailClassName ?? styles.tail} data-testid="streaming-markdown-tail">{tail}</div>}
  </>;
  return renderBlock ? content : <div className={[styles.markdown, className].filter(Boolean).join(" ")}>{content}</div>;
});

type ComposerTextAreaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement> & {
  onSend?: () => void;
};

// Height follows the text up to the stylesheet's max-height, and only a box held
// there scrolls. Measuring with overflow hidden keeps a passing scrollbar from
// narrowing the wrap width.
function fitToContent(element: HTMLTextAreaElement) {
  // A hidden field measures zero; keep the last height until it is shown again.
  if (!element.offsetWidth) return;
  // Measuring collapses the box for a moment. Hold the parent's height so the
  // layout around it does not shrink too: WebKit clamps a scrolled transcript
  // above it during that collapse and the newest lines drop out of view. This
  // assumes the parent does not stretch the field to its own height.
  const parent = element.parentElement;
  const parentMinHeight = parent?.style.minHeight ?? "";
  if (parent) parent.style.minHeight = `${parent.getBoundingClientRect().height}px`;
  element.style.overflowY = "hidden";
  element.style.height = "auto";
  // An empty box keeps its natural height. Browsers count placeholder text in
  // scrollHeight, and a long placeholder (e.g. a hovered Home card's prompt)
  // must not resize the box.
  if (element.value) {
    element.style.height = `${element.scrollHeight}px`;
    // WebKit can round scrollHeight 1px past the height just set (page zoom).
    element.style.overflowY = element.scrollHeight - element.clientHeight > 1 ? "auto" : "hidden";
  }
  if (parent) parent.style.minHeight = parentMinHeight;
}

/** The same input primitive used by the standalone Chat composer and its embeds. */
export const ComposerTextArea = forwardRef<HTMLTextAreaElement, ComposerTextAreaProps>(function ComposerTextArea({
  onSend, onKeyDown, onCompositionStart, onCompositionEnd, ...props
}, forwardedRef) {
  const input = useRef<HTMLTextAreaElement | null>(null);
  const composing = useRef(false);
  // Runs for every value, including text set by code rather than typed, and
  // when an indent or padding change rewraps the same text.
  useLayoutEffect(() => {
    if (input.current) fitToContent(input.current);
  }, [props.value, props.className, props.style?.textIndent]);
  // A new width rewraps the text. Refit a frame later so the height change
  // happens outside the observer callback.
  useEffect(() => {
    const element = input.current;
    if (!element) return;
    let width = element.offsetWidth;
    let frame = 0;
    const observer = new ResizeObserver(() => {
      if (element.offsetWidth === width) return;
      width = element.offsetWidth;
      cancelAnimationFrame(frame);
      frame = requestAnimationFrame(() => fitToContent(element));
    });
    observer.observe(element);
    return () => { observer.disconnect(); cancelAnimationFrame(frame); };
  }, []);
  return <textarea {...props}
    ref={(element) => { input.current = element; if (typeof forwardedRef === "function") forwardedRef(element); else if (forwardedRef) forwardedRef.current = element; }}
    onCompositionStart={(event) => { composing.current = true; onCompositionStart?.(event); }}
    onCompositionEnd={(event) => { composing.current = false; onCompositionEnd?.(event); }}
    onKeyDown={(event) => {
      onKeyDown?.(event);
      if (!event.defaultPrevented && onSend && event.key === "Enter" && !event.shiftKey && !composing.current && !event.nativeEvent.isComposing && event.keyCode !== 229) {
        event.preventDefault(); onSend();
      }
    }}
  />;
});

export function ChatJumpToLatest({ hasMessages, scrolledUp, onJump, className, anchorClassName }: {
  hasMessages: boolean; scrolledUp: boolean; onJump: () => void;
  className?: string; anchorClassName?: string;
}) {
  const ui = useGT();
  if (!hasMessages) return null;
  return <div className={anchorClassName ?? styles.jumpAnchor}>
    <button type="button" data-testid="chat-jump-to-latest" aria-label={ui("Jump to latest")} aria-hidden={!scrolledUp}
      tabIndex={scrolledUp ? 0 : -1} onClick={onJump} className={className ?? styles.jump} data-visible={scrolledUp}>
      <ChevronDown size={16} aria-hidden="true" />
    </button>
  </div>;
}