Prompt box

A message box for an assistant: it grows as you type, has a research switch and a model picker, and its send button turns into a stop button while an answer is on its way.

Options

Size
Colour

Code

import { useRef, useState } from "react";
import "./prompt-box.css";

const SPARK = "M8 1c.4 0 .7.2.8.6l1 3.2a2 2 0 0 0 1.4 1.4l3.2 1a.8.8 0 0 1 0 1.6l-3.2 1a2 2 0 0 0-1.4 1.4l-1 3.2a.8.8 0 0 1-1.6 0l-1-3.2a2 2 0 0 0-1.4-1.4l-3.2-1a.8.8 0 0 1 0-1.6l3.2-1a2 2 0 0 0 1.4-1.4l1-3.2c.1-.4.4-.6.8-.6Z";
const PLUS = "M8 2.25a1 1 0 0 1 1 1V7h3.75a1 1 0 1 1 0 2H9v3.75a1 1 0 1 1-2 0V9H3.25a1 1 0 0 1 0-2H7V3.25a1 1 0 0 1 1-1Z";
const DOWN = "M4.2 5.7a1 1 0 0 1 1.4 0L8 8.1l2.4-2.4a1 1 0 1 1 1.4 1.4l-3.1 3.1a1 1 0 0 1-1.4 0L4.2 7.1a1 1 0 0 1 0-1.4Z";
const UP = "M8 2a1 1 0 0 1 .7.3l4 4a1 1 0 1 1-1.4 1.4L9 5.4V13a1 1 0 1 1-2 0V5.4L4.7 7.7a1 1 0 0 1-1.4-1.4l4-4A1 1 0 0 1 8 2Z";

function Icon({ d }) {
  return (
    <svg viewBox="0 0 16 16" aria-hidden="true">
      <path fill="currentColor" d={d} />
    </svg>
  );
}

/* onSend gets the text and the chosen options, and can be async: the send button is
   a stop button until it finishes. onStop is called if it is pressed meanwhile.

   <PromptBox models={["Swift", "Balanced", "Deep"]} onSend={ask} onStop={cancel} /> */
export function PromptBox({ models = ["Swift", "Balanced", "Deep"], placeholder = "Ask anything", onSend, onStop, onAttach }) {
  const [text, setText] = useState("");
  const [research, setResearch] = useState(false);
  const [model, setModel] = useState(models[Math.min(1, models.length - 1)]);
  const [busy, setBusy] = useState(false);
  const box = useRef(null);

  function fit() {
    if (CSS.supports("field-sizing", "content")) return;
    box.current.style.height = "auto";
    box.current.style.height = box.current.scrollHeight + "px";
  }

  async function submit(event) {
    event.preventDefault();
    if (busy) {
      onStop?.();
      return setBusy(false);
    }
    if (!text.trim()) return;
    const message = text;
    setText("");
    setBusy(true);
    requestAnimationFrame(fit);
    try {
      await onSend?.(message, { model, research });
    } finally {
      setBusy(false);
    }
  }

  return (
    <form className={"dh-prompt" + (busy ? " is-busy" : "")} onSubmit={submit}>
      <textarea
        ref={box}
        rows={1}
        placeholder={placeholder}
        aria-label="Message"
        value={text}
        onChange={(event) => {
          setText(event.target.value);
          fit();
        }}
        onKeyDown={(event) => {
          if (event.key === "Enter" && !event.shiftKey && !event.nativeEvent.isComposing) {
            event.preventDefault();
            event.currentTarget.form.requestSubmit();
          }
        }}
      />
      <div className="dh-prompt-bar">
        <button className="dh-prompt-tool" type="button" aria-label="Attach a file" onClick={onAttach}>
          <Icon d={PLUS} />
        </button>
        <button className="dh-prompt-chip" type="button" aria-pressed={research} onClick={() => setResearch(!research)}>
          <Icon d={SPARK} />
          Deep research
        </button>
        <button
          className="dh-prompt-model"
          type="button"
          aria-label={"Model: " + model + ". Press to change"}
          onClick={() => setModel(models[(models.indexOf(model) + 1) % models.length])}
        >
          <span key={model} className="is-new">
            {model}
          </span>
          <Icon d={DOWN} />
        </button>
        <button className="dh-prompt-send" type="submit" aria-label={busy ? "Stop" : "Send"} disabled={!busy && !text.trim()}>
          <Icon d={UP} />
          <i aria-hidden="true" />
        </button>
      </div>
    </form>
  );
}