# AI Handoff
> The assistant hands the chat to a person, with what they will see so you don't repeat yourself. The queue counts down and the agent joins the thread.

Source: https://kokonutui.com/docs/ai/ai-handoff

## Installation

```bash
npx shadcn@latest add @kokonutui/ai-handoff
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `prompt?` | `string` |  | The user's message, drawn as a bubble. Only the demo draws one by default. |
| `message?` | `string` |  | What the assistant says before handing over. |
| `team?` | `string` | `"Billing"` |  |
| `shared?` | `string[]` | `demo` | What the person taking over gets, so nobody asks twice. |
| `agents?` | `HandoffAgent[]` | `demo` | The team. The first one is who picks it up, so put them first. |
| `position?` | `number` | `3` | Place in line. Live, pass each new place; the demo counts down from it. |
| `status?` | `HandoffStatus` |  | Pass it to drive the card from your queue: `waiting` shows `position`, `joined` brings the first agent in. Leave it out and the demo plays on `stepMs`. |
| `reply?` | `string` |  | The agent's first message. Live, they show as typing until you pass it; the demo types for a moment, then shows it. |
| `waitHint?` | `string` | `"Usually replies in about 2 min"` | One line under the team while waiting. |
| `stepMs?` | `number` | `1300` | Demo only: ms per place in line. |
| `onStay?` | `() => void` |  | Shows "Keep chatting with AI" while waiting. |
| `className?` | `string` |  | Extra classes for the root element. |

### HandoffAgent

| Field | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `initials` | `string` |  |
| `tone` | `string` | Tailwind classes for the avatar's colours. |

### HandoffStatus

Where the handoff is, from your support queue.

```ts
type HandoffStatus = "waiting" | "joined";
```

## Source

```tsx
"use client";

/**
 * @author: @kokonutui
 * @description: AI Handoff
 * @version: 1.1.0
 * @date: 2026-11-05
 * @license: MIT
 * @website: https://kokonutui.com
 * @github: https://github.com/kokonut-labs/kokonutui
 */

import { Headset } from "lucide-react";
import { AnimatePresence, MotionConfig, motion } from "motion/react";
import { type ReactNode, useEffect, useId, useRef, useState } from "react";
import { cn } from "@/lib/utils";

export interface HandoffAgent {
  name: string;
  initials: string;
  /** Tailwind classes for the avatar's colours. */
  tone: string;
}

/** Where the handoff is, from your support queue. */
export type HandoffStatus = "waiting" | "joined";

export interface AIHandoffProps {
  /** The user's message, drawn as a bubble. Only the demo draws one by default. */
  prompt?: string;
  /** What the assistant says before handing over. */
  message?: string;
  team?: string;
  /** What the person taking over gets, so nobody asks twice. */
  shared?: string[];
  /** The team. The first one is who picks it up, so put them first. */
  agents?: HandoffAgent[];
  /** Place in line. Live, pass each new place; the demo counts down from it. */
  position?: number;
  /**
   * Pass it to drive the card from your queue: `waiting` shows `position`,
   * `joined` brings the first agent in. Leave it out and the demo plays on
   * `stepMs`.
   */
  status?: HandoffStatus;
  /**
   * The agent's first message. Live, they show as typing until you pass it;
   * the demo types for a moment, then shows it.
   */
  reply?: string;
  /** One line under the team while waiting. */
  waitHint?: string;
  /** Demo only: ms per place in line. */
  stepMs?: number;
  /** Shows "Keep chatting with AI" while waiting. */
  onStay?: () => void;
  className?: string;
}

type Status = "waiting" | "picking" | "joined" | "stayed";

const EASE_OUT = [0.23, 1, 0.32, 1] as const;
const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;
const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;
const TYPING_MS = 1600;
/** How long the picked agent is shown before they join. */
const PICK_MS = 900;
/** How long the agent avatar takes to fly into the joined line, in s. */
const FLIGHT_S = 0.5;

const DEMO_AGENTS: HandoffAgent[] = [
  {
    name: "Nora",
    initials: "N",
    tone: "bg-rose-100 text-rose-800 dark:bg-rose-500/20 dark:text-rose-200",
  },
  {
    name: "Theo",
    initials: "T",
    tone: "bg-sky-100 text-sky-800 dark:bg-sky-500/20 dark:text-sky-200",
  },
  {
    name: "Ama",
    initials: "A",
    tone: "bg-orange-100 text-orange-800 dark:bg-orange-500/20 dark:text-orange-200",
  },
];

const DEMO_SHARED = [
  "Two charges of $24.00 on Nov 3",
  "Invoice INV-2291, card ending 4242",
  "You’d like one of them refunded",
];

const place = (n: number) => {
  if (n <= 1) {
    return "Next in line";
  }
  const suffix = { 2: "nd", 3: "rd" }[n] ?? "th";
  return `${n}${suffix} in line`;
};

const DEMO = {
  prompt: "I got charged twice for October",
  message:
    "I can see both charges. Refunds need someone on the billing team, so I’m handing you over with the details.",
  reply:
    "Hi Mara, Nora here. I can see the duplicate and I’ve refunded $24.00. It’ll be back on your card in 3 to 5 days.",
};

/** The chat text around the card: the demo's only when it's the demo. */
function shownText(
  text: Pick<AIHandoffProps, "prompt" | "message" | "reply">,
  isDemo: boolean
) {
  if (!isDemo) {
    return text;
  }
  return {
    prompt: text.prompt ?? DEMO.prompt,
    message: text.message ?? DEMO.message,
    reply: text.reply ?? DEMO.reply,
  };
}

/**
 * Where the handoff is. Live it follows `status` and `position`; the demo
 * moves up the line on `stepMs`. Either way the first agent is shown picking
 * it up for a moment before they join.
 */
function useQueue(
  statusProp: HandoffStatus | undefined,
  position: number,
  stepMs: number
) {
  const driven = statusProp !== undefined;
  const [status, setStatus] = useState<Status>("waiting");
  const [counted, setLine] = useState(position);
  const line = driven ? position : counted;

  // Live: the queue says they've joined, so they're picked, then join.
  useEffect(() => {
    if (statusProp === "joined" && status === "waiting") {
      setStatus("picking");
    }
  }, [statusProp, status]);

  // Demo: moves up the line, then the first agent joins.
  useEffect(() => {
    if (status !== "waiting" || driven) {
      return;
    }
    const t = setTimeout(() => {
      if (line > 1) {
        setLine((n) => n - 1);
      } else {
        setStatus("picking");
      }
    }, stepMs);
    return () => clearTimeout(t);
  }, [status, line, stepMs, driven]);

  // Holds on who picked it up, then they join.
  useEffect(() => {
    if (status !== "picking") {
      return;
    }
    const t = setTimeout(() => setStatus("joined"), PICK_MS);
    return () => clearTimeout(t);
  }, [status]);

  return { status, setStatus, line };
}

export default function AIHandoff({
  prompt,
  message,
  team = "Billing",
  shared = DEMO_SHARED,
  agents = DEMO_AGENTS,
  position = 3,
  status: statusProp,
  reply,
  waitHint = "Usually replies in about 2 min",
  stepMs = 1300,
  onStay,
  className,
}: AIHandoffProps) {
  const id = useId();
  const driven = statusProp !== undefined;
  const shown = shownText({ prompt, message, reply }, !driven);
  const { status, setStatus, line } = useQueue(statusProp, position, stepMs);
  const [typingDemo, setTyping] = useState(true);
  // Live, the agent types until you pass their first message.
  const typing = driven ? shown.reply === undefined : typingDemo;
  const [landed, setLanded] = useState(false);
  const agent = agents[0];
  const picking = status === "picking";
  const open = status === "waiting" || picking;

  // They type for a moment before the first message lands.
  useEffect(() => {
    if (status !== "joined") {
      return;
    }
    const land = setTimeout(() => setLanded(true), FLIGHT_S * 1000 + 50);
    const t = setTimeout(() => setTyping(false), TYPING_MS);
    return () => {
      clearTimeout(land);
      clearTimeout(t);
    };
  }, [status]);

  const stay = () => {
    setStatus("stayed");
    onStay?.();
  };

  return (
    <MotionConfig reducedMotion="user">
      <div
        className={cn("flex w-full max-w-md flex-col gap-4", className)}
        data-state={status}
      >
        {shown.prompt ? (
          <p className="max-w-[85%] self-end text-pretty rounded-2xl rounded-br-md bg-zinc-100 px-3.5 py-2 text-[14px] text-zinc-800 leading-snug dark:bg-zinc-800 dark:text-zinc-100">
            {shown.prompt}
          </p>
        ) : null}
        {shown.message ? (
          <p className="text-pretty text-[15px] text-zinc-800 leading-7 dark:text-zinc-200">
            {shown.message}
          </p>
        ) : null}

        <AutoHeight className="rounded-[22px] bg-black/[0.04] dark:bg-white/[0.06]">
          <AnimatePresence initial={false} mode="popLayout">
            {open ? (
              <motion.div
                exit={{ opacity: 0, filter: "blur(4px)" }}
                key="card"
                transition={{ duration: 0.2 }}
              >
                <div className="flex h-8 items-center gap-2 px-3 text-[12px] text-zinc-500 dark:text-zinc-400">
                  <Headset aria-hidden="true" className="size-3.5" />
                  <span className="min-w-0 truncate">
                    Handing over to{" "}
                    <span className="font-medium text-zinc-700 dark:text-zinc-300">
                      {team}
                    </span>
                  </span>
                  <span className="relative ml-auto h-4 shrink-0 overflow-hidden tabular-nums">
                    <AnimatePresence initial={false} mode="popLayout">
                      <motion.span
                        animate={{ y: 0, opacity: 1 }}
                        className="block leading-4"
                        exit={{ y: -10, opacity: 0 }}
                        initial={{ y: 10, opacity: 0 }}
                        key={picking ? "picked" : line}
                        transition={{ duration: 0.25, ease: EASE_OUT }}
                      >
                        {picking ? `${agent.name} is joining` : place(line)}
                      </motion.span>
                    </AnimatePresence>
                  </span>
                </div>

                <div className="rounded-[18px] bg-white shadow-[0_1px_2px_rgb(0_0_0/0.04)] ring-1 ring-black/[0.06] dark:bg-zinc-900 dark:ring-white/[0.08]">
                  <p className="px-3.5 pt-3 font-medium text-[11.5px] text-zinc-400 dark:text-zinc-500">
                    They’ll see this, so you won’t repeat yourself
                  </p>
                  <ul className="space-y-1.5 px-3.5 pt-2 pb-3 text-[13px]">
                    {shared.map((item, i) => (
                      <li
                        className="flex items-center gap-2.5 text-zinc-700 dark:text-zinc-300"
                        key={item}
                      >
                        <DrawnCheck delay={0.25 + i * 0.22} />
                        <span className="min-w-0 truncate">{item}</span>
                      </li>
                    ))}
                  </ul>
                  <div className="flex items-center gap-2.5 border-black/[0.06] border-t px-3.5 py-2.5 dark:border-white/[0.08]">
                    <span className="flex">
                      {agents.map((a, i) => (
                        <motion.span
                          animate={{ opacity: picking && i > 0 ? 0.25 : 1 }}
                          className={cn(
                            "-ml-1 flex size-6 items-center justify-center rounded-full font-semibold text-[10px] ring-2 transition-[box-shadow] duration-200 first:ml-0",
                            picking && i === 0
                              ? "ring-zinc-900 dark:ring-zinc-100"
                              : "ring-white dark:ring-zinc-900",
                            a.tone
                          )}
                          initial={false}
                          key={a.name}
                          layoutId={i === 0 ? `${id}-agent` : undefined}
                          style={{ zIndex: agents.length - i }}
                          title={a.name}
                          transition={{ duration: 0.3, ease: EASE_OUT }}
                        >
                          {a.initials}
                        </motion.span>
                      ))}
                    </span>
                    <span className="min-w-0 flex-1 truncate text-[12px] text-zinc-500 dark:text-zinc-400">
                      {picking ? `${agent.name} picked it up` : waitHint}
                    </span>
                  </div>
                </div>

                <div
                  className={cn(
                    "flex justify-end px-1 pt-1",
                    driven && !onStay && "hidden"
                  )}
                >
                  <button
                    className="h-8 rounded-full px-3 font-medium text-[12.5px] text-zinc-600 outline-hidden transition-colors duration-150 hover:bg-black/[0.04] hover:text-zinc-900 focus-visible:ring-2 focus-visible:ring-blue-500/40 active:bg-black/[0.07] disabled:opacity-50 dark:text-zinc-400 dark:active:bg-white/[0.1] dark:hover:bg-white/[0.06] dark:hover:text-zinc-100"
                    disabled={picking}
                    onClick={stay}
                    type="button"
                  >
                    Keep chatting with AI
                  </button>
                </div>
              </motion.div>
            ) : (
              <motion.div
                animate={{ opacity: 1, filter: "blur(0px)" }}
                className="flex h-9 items-center gap-2 px-3 text-[12.5px] text-zinc-500 dark:text-zinc-400"
                initial={{ opacity: 0, filter: "blur(3px)" }}
                key="folded"
                transition={{ duration: 0.3, ease: EASE_OUT, delay: 0.1 }}
              >
                <Headset aria-hidden="true" className="size-3.5" />
                <span className="min-w-0 truncate">
                  {status === "joined"
                    ? `Handed over to ${team} with ${shared.length} details`
                    : "Handoff cancelled"}
                </span>
              </motion.div>
            )}
          </AnimatePresence>
        </AutoHeight>

        <AnimatePresence initial={false} mode="popLayout">
          {status === "joined" && (
            <motion.div
              animate={{ opacity: 1 }}
              className="flex flex-col gap-3"
              initial={{ opacity: 0 }}
              key="joined"
              transition={{ duration: 0.2 }}
            >
              <div className="flex items-center gap-3 text-[12px] text-zinc-500 dark:text-zinc-400">
                <span className="h-px flex-1 bg-black/[0.08] dark:bg-white/10" />
                <span className="flex items-center gap-2">
                  <motion.span
                    className={cn(
                      "flex size-5 items-center justify-center rounded-full font-semibold text-[9px]",
                      agent.tone
                    )}
                    // Shared only for the flight in; once landed it stays put
                    // while the messages below it change height.
                    layoutId={landed ? undefined : `${id}-agent`}
                    transition={{ duration: FLIGHT_S, ease: EASE_IN_OUT }}
                  >
                    {agent.initials}
                  </motion.span>
                  <span>
                    <span className="font-medium text-zinc-700 dark:text-zinc-300">
                      {agent.name}
                    </span>{" "}
                    from {team} joined
                  </span>
                </span>
                <span className="h-px flex-1 bg-black/[0.08] dark:bg-white/10" />
              </div>

              <motion.div
                animate={{ opacity: 1, y: 0 }}
                className="flex items-end gap-2"
                initial={{ opacity: 0, y: 6 }}
                transition={{ duration: 0.3, ease: EASE_OUT, delay: 0.35 }}
              >
                <span
                  className={cn(
                    "flex size-7 shrink-0 items-center justify-center rounded-full font-semibold text-[11px]",
                    agent.tone
                  )}
                >
                  {agent.initials}
                </span>
                {/* Typing dots and the reply are two bubbles that crossfade,
                    so the bubble never stretches its text mid-way. */}
                <AnimatePresence initial={false} mode="popLayout">
                  {typing ? (
                    <motion.span
                      className="flex h-9 items-center gap-1 rounded-2xl rounded-bl-md bg-white px-3.5 ring-1 ring-black/[0.08] dark:bg-zinc-900 dark:ring-white/10"
                      exit={{ opacity: 0, filter: "blur(2px)" }}
                      key="typing"
                      transition={{ duration: 0.15 }}
                    >
                      <span className="sr-only">{agent.name} is typing</span>
                      {[0, 1, 2].map((d) => (
                        <motion.span
                          animate={{ y: [0, -3, 0], opacity: [0.4, 1, 0.4] }}
                          className="size-1.5 rounded-full bg-zinc-400 dark:bg-zinc-500"
                          key={d}
                          transition={{
                            duration: 0.9,
                            delay: d * 0.15,
                            repeat: Number.POSITIVE_INFINITY,
                          }}
                        />
                      ))}
                    </motion.span>
                  ) : (
                    <motion.p
                      animate={{ opacity: 1, filter: "blur(0px)", y: 0 }}
                      className="min-w-0 max-w-[85%] rounded-2xl rounded-bl-md bg-white px-3.5 py-2 text-[14px] text-zinc-800 leading-6 ring-1 ring-black/[0.08] dark:bg-zinc-900 dark:text-zinc-200 dark:ring-white/10"
                      initial={{ opacity: 0, filter: "blur(4px)", y: 4 }}
                      key="reply"
                      transition={{ duration: 0.3, ease: EASE_OUT }}
                    >
                      {shown.reply}
                    </motion.p>
                  )}
                </AnimatePresence>
              </motion.div>
            </motion.div>
          )}

          {status === "stayed" && (
            <motion.p
              animate={{ opacity: 1, filter: "blur(0px)", y: 0 }}
              className="text-pretty text-[15px] text-zinc-800 leading-7 dark:text-zinc-200"
              initial={{ opacity: 0, filter: "blur(4px)", y: 4 }}
              key="stayed"
              transition={{ duration: 0.35, ease: EASE_OUT }}
            >
              Okay, I’ll stay with you. I can’t refund it myself, but I can
              draft the request so it’s ready whenever you want to send it.
            </motion.p>
          )}
        </AnimatePresence>
        <p aria-live="polite" className="sr-only">
          {status === "waiting" ? place(line) : ""}
          {status === "joined" ? `${agent.name} from ${team} joined` : ""}
        </p>
      </div>
    </MotionConfig>
  );
}

function DrawnCheck({ delay }: { delay: number }) {
  return (
    <span className="flex size-4 shrink-0 items-center justify-center rounded-full bg-emerald-50 dark:bg-emerald-500/15">
      <svg
        aria-hidden="true"
        className="size-2.5 text-emerald-600 dark:text-emerald-400"
        fill="none"
        viewBox="0 0 16 16"
      >
        <motion.path
          animate={{ pathLength: 1, opacity: 1 }}
          d="M3.5 8.5l3 3 6-7"
          initial={{ pathLength: 0, opacity: 0 }}
          stroke="currentColor"
          strokeLinecap="round"
          strokeLinejoin="round"
          strokeWidth="2.5"
          transition={{ duration: 0.3, ease: EASE_OUT, delay }}
        />
      </svg>
    </span>
  );
}

/** Eases the card to its content's height when the content swaps. */
function AutoHeight({
  className,
  children,
}: {
  className?: string;
  children: ReactNode;
}) {
  const innerRef = useRef<HTMLDivElement>(null);
  const [height, setHeight] = useState<number | "auto">("auto");

  useEffect(() => {
    const inner = innerRef.current;
    if (!inner) {
      return;
    }
    const observer = new ResizeObserver(() => setHeight(inner.offsetHeight));
    observer.observe(inner);
    return () => observer.disconnect();
  }, []);

  return (
    <motion.div
      animate={{ height }}
      className={cn("overflow-hidden", className)}
      initial={false}
      transition={{ duration: 0.3, ease: EASE_DRAWER }}
    >
      <div className="p-1" ref={innerRef}>
        {children}
      </div>
    </motion.div>
  );
}
```

## Usage

With no props it plays the demo: the line counts down, Nora picks it up and replies. Pass `status` and it follows your support queue instead. While `waiting` it shows `position` as you pass it; `joined` brings the first of `agents` into the thread, and they show as typing until you pass `reply`. "Keep chatting with AI" only shows when you pass `onStay`, and the user's bubble only when you pass `prompt`.

```tsx
"use client";

import AIHandoff from "@/components/kokonutui/ai-handoff";

/** `ticket` comes from your helpdesk (a webhook, a socket, or polling). */
export function Handoff({
  ticket,
  cancel,
}: {
  ticket: {
    position: number;
    assignee?: { name: string };
    firstReply?: string;
  };
  cancel: () => void;
}) {
  return (
    <AIHandoff
      agents={[
        {
          name: ticket.assignee?.name ?? "Billing",
          initials: (ticket.assignee?.name ?? "B")[0],
          tone: "bg-rose-100 text-rose-800",
        },
      ]}
      message="Refunds need someone on the billing team, so I’m handing you over."
      onStay={cancel}
      position={ticket.position}
      reply={ticket.firstReply}
      shared={["Two charges of $24.00 on Nov 3", "Invoice INV-2291"]}
      status={ticket.assignee ? "joined" : "waiting"}
    />
  );
}
```

With the AI SDK, give the model a `handoff_to_human` tool. Its input is the `shared` summary; once its part reaches `output-available` with your ticket id, draw the card and feed it the queue updates.

<Demo(mdx/demos/ai-handoff-stream) />
