Skip to content

The platform for continuously improving AI agents.

3.1 KB
Raw Blame
// Adapted from AI SDK Agents Prompt Input: https://www.aisdkagents.com/ai-components/prompt-input

import { type ComponentProps, useLayoutEffect, useRef } from "react";

import { motion, useReducedMotion } from "motion/react";

import { useGhostCompletion } from "@/components/ai-components/ghost-completion";
import { cn } from "@/lib/utils";
import { MentionTextarea } from "./mention-textarea";

export function PromptInput({
  className,
  containerClassName,
  value,
  onKeyDown,
  onValueChange,
  onExpandedChange,
  projectId,
  references,
  onReferencesChange,
  onMention,
  ...props
}: ComponentProps<typeof MentionTextarea> & {
  containerClassName?: string;
  onExpandedChange: (expanded: boolean) => void;
}) {
  const field = useRef<HTMLTextAreaElement>(null);
  const ghost = useGhostCompletion(value, onValueChange);
  const measure = useRef<HTMLSpanElement>(null);
  const reduce = useReducedMotion();
  useLayoutEffect(() => {
    const node = field.current;
    const row = node?.closest<HTMLElement>("[data-prompt-row]");
    if (!node || !row) return;
    const resize = () => {
      const rowStyle = getComputedStyle(row),
        inputStyle = getComputedStyle(node);
      const actions = Array.from(row.querySelectorAll<HTMLElement>("[data-composer-action]"));
      const available =
        row.clientWidth -
        parseFloat(rowStyle.paddingLeft) -
        parseFloat(rowStyle.paddingRight) -
        actions.reduce((width, action) => width + action.offsetWidth, 0) -
        2 * parseFloat(rowStyle.columnGap) -
        parseFloat(inputStyle.paddingLeft) -
        parseFloat(inputStyle.paddingRight);
      onExpandedChange(
        String(value ?? "").includes("\n") || (measure.current?.scrollWidth ?? 0) > available
      );
      node.style.height = "auto";
      node.style.height = `${Math.min(node.scrollHeight, 208)}px`;
    };
    resize();
    const observer = new ResizeObserver(resize);
    observer.observe(row);
    return () => observer.disconnect();
  }, [value, onExpandedChange]);
  return (
    <motion.div
      className={cn("relative min-w-0 home-ghost", containerClassName)}
      data-slot="prompt-input"
      data-variant="Rounded"
      layout={!reduce}
      transition={{ duration: reduce ? 0 : 0.22, ease: [0.22, 1, 0.36, 1] }}
    >
      <span
        aria-hidden="true"
        className="pointer-events-none invisible absolute w-max whitespace-pre px-0 text-xs leading-4"
        ref={measure}
      >
        {value}
      </span>
      {ghost.layer}
      {ghost.hint}
      <MentionTextarea
        {...ghost.inputProps}
        onMention={onMention}
        onReferencesChange={onReferencesChange}
        onValueChange={onValueChange}
        projectId={projectId}
        references={references}
        {...props}
        className={cn(
          "block max-h-52 min-h-7 w-full resize-none border-0 bg-transparent px-1 py-1.5 text-xs leading-4 text-foreground outline-none placeholder:text-muted-foreground",
          className
        )}
        enterKeyHint="send"
        onKeyDown={onKeyDown}
        ref={field}
        rows={1}
        value={value}
      />
    </motion.div>
  );
}