ai handoff.md

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.

Installation

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.

type HandoffStatus = "waiting" | "joined";

Source

"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-amber-100 text-amber-800 dark:bg-amber-500/20 dark:text-amber-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 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-[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{