AI Mention

Pointing an agent at context: an @ menu for files and agents, removable context chips, and inline mentions inside a sentence.

Installation

Installation

One command adds ai-mention and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-mention.json
ai-mention-demo
components/ui/ai-mention-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { typeInto, useAiLoop } from "@/lib/ai-kit"
import {
  AiContextChips,
  AiMentionInput,
  AiMentionText,
  type AiMentionItem,
  type AiMentionPart,
} from "@/registry/tween/ai-mention"

const HERO: AiMentionItem = {
  id: "hero",
  kind: "file",
  name: "hero.tsx",
  detail: "registry/tween/hero.tsx",
  tokens: 2140,
}
const MIRA: AiMentionItem = {
  id: "mira",
  kind: "agent",
  name: "Mira",
  detail: "Builds pages and sections",
  tone: "blue",
  shape: "orb",
}
const MOTION: AiMentionItem = {
  id: "motion",
  kind: "doc",
  name: "Motion principles",
  detail: "Guides · eases and timing",
  tokens: 1820,
}
const ITEMS: AiMentionItem[] = [
  HERO,
  {
    id: "pricing",
    kind: "file",
    name: "pricing-section.tsx",
    detail: "components/pricing-section.tsx",
    tokens: 3360,
  },
  {
    id: "globals",
    kind: "file",
    name: "globals.css",
    detail: "styles/globals.css",
    tokens: 5210,
  },
  MIRA,
  {
    id: "odo",
    kind: "agent",
    name: "Odo",
    detail: "Writes and runs the tests",
    tone: "violet",
    shape: "blob",
  },
  MOTION,
  {
    id: "registry",
    kind: "doc",
    name: "Registry guide",
    detail: "Docs · adding a component",
    tokens: 960,
  },
]

function Menu() {
  const root = React.useRef<HTMLDivElement>(null)
  const [value, setValue] = React.useState("Tighten the copy in @")
  const [row, setRow] = React.useState<number | undefined>(0)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setValue("")
      setRow(0)
      await sleep(700)
      await typeInto(setValue, "Tighten the copy in @", sleep)
      await sleep(700)
      setRow(1)
      await sleep(420)
      setRow(2)
      await sleep(420)
      setRow(0)
      await typeInto(
        (text) => setValue(`Tighten the copy in @${text}`),
        "he",
        sleep,
        140
      )
      await sleep(700)
      setValue("Tighten the copy in @hero.tsx ")
      await sleep(500)
      await typeInto(
        (text) => setValue(`Tighten the copy in @hero.tsx ${text}`),
        "with @",
        sleep
      )
      await sleep(500)
      setRow(3)
      await sleep(900)
      setValue("Tighten the copy in @hero.tsx with @Mira ")
      await sleep(1900)
    },
    { gap: 400, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{
        display: "grid",
        alignContent: "end",
        width: "min(100%, 400px)",
        minHeight: 380,
      }}
      onPointerDown={() => {
        setHands(true)
        setRow(undefined)
      }}
      onKeyDown={() => {
        setHands(true)
        setRow(undefined)
      }}
    >
      <AiMentionInput
        items={ITEMS}
        value={value}
        onValueChange={setValue}
        highlight={row}
        onSubmit={() => setValue("")}
      />
    </div>
  )
}

const POOL = ITEMS.filter((item) => item.kind !== "agent")

function Chips() {
  const root = React.useRef<HTMLDivElement>(null)
  const [items, setItems] = React.useState<AiMentionItem[]>(POOL.slice(0, 3))
  const [removing, setRemoving] = React.useState<string[]>([])
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setRemoving([])
      setItems(POOL.slice(0, 2))
      await sleep(1100)
      setItems(POOL.slice(0, 3))
      await sleep(1000)
      setItems(POOL.slice(0, 4))
      await sleep(1500)
      setRemoving(["globals"])
      await sleep(180)
      setItems((list) => list.filter((item) => item.id !== "globals"))
      setRemoving([])
      await sleep(1700)
    },
    { gap: 500, enabled: !hands }
  )

  const next = POOL.find((item) => !items.some((had) => had.id === item.id))
  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 400px)" }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
    >
      <AiContextChips
        items={items}
        removing={removing}
        budget={16000}
        onRemove={(id) =>
          setItems((list) => list.filter((item) => item.id !== id))
        }
        onAdd={next ? () => setItems((list) => [...list, next]) : undefined}
      />
    </div>
  )
}

const PARTS: AiMentionPart[] = [
  "I rewrote the headline in ",
  HERO,
  " to match the pacing in ",
  MOTION,
  ", then handed the tests to ",
  {
    ...MIRA,
    id: "odo",
    name: "Odo",
    detail: "Writes and runs the tests",
    tone: "violet",
    shape: "blob",
  },
  ".",
]
const TOUR = ["hero", "motion", "odo", null] as const

function Inline() {
  const root = React.useRef<HTMLDivElement>(null)
  const [active, setActive] = React.useState<string | null>("hero")
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (const id of TOUR) {
        setActive(id)
        await sleep(id ? 1700 : 700)
      }
    },
    { gap: 200, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 400px)" }}
      onPointerEnter={() => setHands(true)}
      onFocus={() => setHands(true)}
    >
      <AiMentionText parts={PARTS} active={hands ? undefined : active} />
    </div>
  )
}

export function AiMentionDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "@ menu", node: <Menu /> },
        { name: "Context chips", node: <Chips /> },
        { name: "Inline mentions", node: <Inline /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-mention.json

Usage

import {
  AiContextChips,
  AiMentionInput,
  AiMentionText,
} from "@/components/ui/ai-mention"
const items = [
  { id: "hero", kind: "file", name: "hero.tsx", detail: "registry/tween/hero.tsx", tokens: 2140 },
  { id: "mira", kind: "agent", name: "Mira", detail: "Builds pages", tone: "blue" },
  { id: "motion", kind: "doc", name: "Motion principles", detail: "Guides", tokens: 1820 },
]
 
const [value, setValue] = useState("")
 
<AiMentionInput
  items={items}
  value={value}
  onValueChange={setValue}
  onMention={(item) => attach(item)}
  onSubmit={send}
/>

Versions

  • AiMentionInput is a prompt field. Typing @ opens a menu grouped into files, agents and docs that filters as you type. Arrow keys move a sliding highlight, Enter inserts the name, Escape closes.
  • AiContextChips shows what the agent already has as removable chips with a token count each and a running total. Pass budget for a bar that turns amber past 80 percent.
  • AiMentionText renders mentions as pills inside a sentence. Hovering or focusing one shows a card with its path or role.

Props

AiMentionInput

PropTypeDefaultDescription
itemsAiMentionItem[]Everything that can be mentioned.
valuestringThe field's text.
onValueChange(value: string) => voidCalled on every edit and when a mention is inserted.
onMention(item: AiMentionItem) => voidCalled when an item is picked from the menu.
onSubmit(value: string) => voidCalled on Enter while the menu is closed.
highlightnumberControls the highlighted row. Omit to use the arrow keys.

AiContextChips

PropTypeDefaultDescription
itemsAiMentionItem[]The chips.
onRemove(id: string) => voidCalled once a chip has finished leaving.
onAdd() => voidShows the add button when set.
removingstring[][]Ids to animate out when you remove items yourself.
budgetnumberToken budget for the bar.

AiMentionText

PropTypeDefaultDescription
parts(string | AiMentionItem)[]The sentence, as strings and mentions in order.
activestring | nullControls which card shows. Omit to follow hover and focus.
onActiveChange(id: string | null) => voidCalled as the active mention changes.
hintstringShown while no mention is active.

An AiMentionItem is { id, kind, name, detail?, tokens?, tone?, shape? }, where kind is "file", "agent" or "doc".