Back

Voice Agent RelayNew

A realtime voice call with a voice agent that hands work to a working agent, answers questions about its progress and speaks short updates and a summary while it runs.

JobOversee
Seen in

Press the waveform button to start a scripted call.

"use client";

import * as React from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { ArrowRightLeft, CircleCheck, LoaderCircle, Mic, MicOff, PhoneOff, Volume2 } from "lucide-react";

import { cn } from "@/lib/utils";
import { VoiceWaveform } from "../voice-waveform/component";

/** Who is talking on the call right now. */
export type RelayCallState = "listening" | "speaking" | "muted";

export type RelayItem =
  /** What the person said, as a short transcript line. */
  | { id: string; kind: "you"; text: string }
  /** What the voice agent said back. */
  | { id: string; kind: "voice"; text: string }
  /** The voice agent passed a request to the working agent. */
  | { id: string; kind: "handoff"; text: string }
  /** A short progress note the voice agent read out while the work ran. */
  | { id: string; kind: "update"; text: string }
  /** The spoken summary when the run finished. */
  | { id: string; kind: "summary"; text: string };

export interface RelayRun {
  /** The working agent's task, as the voice agent handed it over. */
  title: string;
  status: "running" | "done";
  /** What it is doing now, in a few words ("Editing pricing.tsx"). */
  step?: string;
}

export interface VoiceAgentRelayProps {
  state: RelayCallState;
  /** The call so far, oldest first. Only the last few lines are shown. */
  items: RelayItem[];
  /** The run the voice agent is relaying, once something has been handed off. */
  run?: RelayRun | null;
  /** Elapsed call time, already formatted ("1:24"). */
  elapsed?: string;
  onToggleMute: () => void;
  onEnd: () => void;
  /** How many recent lines to keep on screen. */
  visibleItems?: number;
  className?: string;
}

const STATE_LABEL: Record<RelayCallState, string> = {
  listening: "Listening",
  speaking: "Speaking",
  muted: "Muted",
};

/**
 * A realtime voice call with a front-line voice agent that relays work to a
 * separate working agent. The call bar shows who is talking; the run strip
 * shows what the working agent is doing; the feed keeps the last few spoken
 * lines, with hand-offs, progress notes and the final summary marked apart
 * from conversation, so a glance tells what was asked and what came back.
 */
export function VoiceAgentRelay({
  state,
  items,
  run,
  elapsed,
  onToggleMute,
  onEnd,
  visibleItems = 4,
  className,
}: VoiceAgentRelayProps) {
  const reduce = useReducedMotion();
  const recent = items.slice(-visibleItems);
  const last = items[items.length - 1];
  const muted = state === "muted";

  return (
    <section
      aria-label="Voice chat with agent"
      className={cn("flex flex-col overflow-hidden rounded-2xl bg-card border", className)}
    >
      {run && <RunStrip run={run} />}

      <ol className="flex min-h-28 flex-col justify-end gap-1.5 px-4 py-3" aria-label="Call transcript">
        <AnimatePresence initial={false}>
          {recent.map((item) => (
            <motion.li
              key={item.id}
              layout={!reduce}
              initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}
              animate={{ opacity: 1, y: 0 }}
              transition={{ duration: 0.2, ease: "easeOut" }}
            >
              <RelayLine item={item} />
            </motion.li>
          ))}
        </AnimatePresence>
      </ol>

      {/* Announce only what the voice agent says, not every transcript tick. */}
      <p className="sr-only" aria-live="polite">
        {last && last.kind !== "you" ? last.text : ""}
      </p>

      <div className="flex items-center gap-3 border-t bg-muted/30 px-3 py-2">
        <span className="flex min-w-16 flex-col">
          <span className="text-xs font-medium">{STATE_LABEL[state]}</span>
          {elapsed && <span className="text-[11px] tabular-nums text-muted-foreground">{elapsed}</span>}
        </span>
        <VoiceWaveform
          state={muted ? "idle" : state}
          barCount={21}
          className={cn("h-8 flex-1", state === "speaking" ? "text-foreground" : "text-muted-foreground")}
        />
        <button
          type="button"
          onClick={onToggleMute}
          aria-pressed={muted}
          className={cn(
            "flex size-8 items-center justify-center rounded-full transition-colors hover:bg-foreground/10 bg-muted",
            muted && "bg-foreground text-background hover:bg-foreground/90"
          )}
        >
          {muted ? <MicOff className="size-4" aria-hidden /> : <Mic className="size-4" aria-hidden />}
          <span className="sr-only">{muted ? "Unmute" : "Mute"}</span>
        </button>
        <button
          type="button"
          onClick={onEnd}
          className="flex h-8 items-center gap-1.5 rounded-full bg-destructive px-3 text-xs font-medium text-white transition-colors hover:bg-destructive/90"
        >
          <PhoneOff className="size-3.5" aria-hidden />
          End
        </button>
      </div>
    </section>
  );
}

function RunStrip({ run }: { run: RelayRun }) {
  const done = run.status === "done";
  return (
    <div className="flex items-center gap-2 border-b bg-muted/30 px-4 py-2 text-xs">
      {done ? (
        <CircleCheck className="size-3.5 shrink-0 text-muted-foreground" aria-hidden />
      ) : (
        <LoaderCircle className="size-3.5 shrink-0 animate-spin text-muted-foreground motion-reduce:animate-none" aria-hidden />
      )}
      <span className="min-w-0 flex-1 truncate">
        <span className="font-medium">{run.title}</span>
        {run.step && !done && <span className="text-muted-foreground"> · {run.step}</span>}
      </span>
      <span className="shrink-0 text-muted-foreground">{done ? "Done" : "Agent working"}</span>
    </div>
  );
}

function RelayLine({ item }: { item: RelayItem }) {
  switch (item.kind) {
    case "you":
      return (
        <p className="text-sm">
          <span className="sr-only">You: </span>
          {item.text}
        </p>
      );
    case "voice":
      return (
        <p className="text-sm text-muted-foreground">
          <span className="sr-only">Voice agent: </span>
          {item.text}
        </p>
      );
    case "handoff":
      return (
        <p className="flex items-center gap-1.5 text-[11px] text-muted-foreground">
          <ArrowRightLeft className="size-3 shrink-0" aria-hidden />
          <span>
            Handed to agent: <span className="text-foreground">{item.text}</span>
          </span>
        </p>
      );
    case "update":
      return (
        <p className="flex items-center gap-1.5 text-[11px] text-muted-foreground">
          <Volume2 className="size-3 shrink-0" aria-hidden />
          <span>
            <span className="sr-only">Progress: </span>
            {item.text}
          </span>
        </p>
      );
    case "summary":
      return (
        <p className="rounded-lg bg-muted/60 px-2.5 py-1.5 text-sm">
          <span className="mb-0.5 block text-[11px] font-medium text-muted-foreground">Summary</span>
          {item.text}
        </p>
      );
  }
}

A UX spec for this pattern — written for agents implementing or reusing it, not the code.

# Voice Agent Relay

## Summary
A realtime voice call with a front-line voice agent that hands work to a separate working agent and keeps the person posted while it runs. The person describes a change out loud, can ask what the agent is doing at any point, and hears short progress notes and a spoken summary at the end — without reading the full written output. A run strip shows the working agent's status, and the transcript marks hand-offs, progress notes and the summary apart from conversation.

## When to use
- A working agent runs tasks that take minutes, and people want to delegate and follow them hands-free or away from the screen (walking, cooking, on a phone).
- The product already has an agent that edits code, documents or projects; voice is a way in, not a separate assistant.
- People want to ask "how's it going?" or "what changed?" without opening the run's trace or diff.

## When not to use
- The voice conversation is the work itself (a voice assistant answering questions). Use **Voice Orb** or **Listening State** for that.
- Speech is only an input method. Dictation that turns speech into text in the composer is simpler and keeps the person in control of what is sent.
- Runs that need careful review before anything is applied. Speak the summary, but route approvals to the screen — don't ask people to approve diffs by ear.
- Shared or noisy spaces where speaking or playback is impractical; always keep the text chat as an equal path.

## Anatomy
- Start voice chat: the composer's Send slot becomes a waveform button when the agent is idle and the input is empty, so starting a call costs no extra space.
- Run strip: the working agent's task title, its current step in a few words, and a status (Agent working / Done).
- Transcript: the last few lines of the call — what the person said, what the voice agent said back, and three marked kinds of relay lines: **Handed to agent** (the request as passed on), progress notes, and the **Summary**.
- Call bar: who is talking (Listening / Speaking / Muted), elapsed time, a live waveform, Mute and End.
- Outside the app: the same mute and end controls on the lock screen or system call UI, where the platform allows.

## Behavior
- The voice agent restates the request before handing it off ("Got it — I'll ask the agent to…"), so a misheard instruction is caught before work starts. The hand-off line shows the exact request passed on.
- The person can keep talking while the working agent runs. Questions about progress are answered from the run's live state, not guessed.
- New requests mid-run are added to the run as further hand-offs; they don't start a parallel run unless the person asks.
- Progress notes are short and sparse — one when the work moves to something new, not one per tool call.
- When the run ends, the voice agent speaks a brief summary (what changed, how many files, anything that needs a look) and points to where the full reply and diff are. The written reply still lands in the chat.
- Mute stops listening but leaves the call and the run going. End stops the call only — it never cancels the working agent's run.
- If the working agent needs approval or hits an error, say so out loud and show it on screen; don't wait for the person to ask.

## Content guidelines
- Spoken lines are one or two short sentences. Lead with the outcome: "Done. Three files changed…", not "So I've been looking into…".
- Hand-off lines are imperative and specific, as the working agent received them: "Add a Monthly / Annual toggle to pricing, annual at 20% off".
- Progress notes name the thing, not the tool: "It's editing pricing.tsx", not "Running apply_patch".
- Say numbers and file names plainly; leave paths, code and diffs to the screen.

## Accessibility
- The start button, Mute and End are real buttons with accessible names; Mute uses `aria-pressed`.
- What the voice agent says is mirrored as text and announced through a polite live region, so the call works with a screen reader and for people who can't hear the audio. The person's own transcript isn't announced back to them.
- Relay lines carry text labels ("Handed to agent", "Progress", "Summary"), not only icons.
- Never make voice the only way to do something — every request and every result is also available in the text chat.
- The waveform is decorative; call state is given in text. Under `prefers-reduced-motion`, new lines fade in without sliding.

## Related patterns
- **Voice Orb** — Use it instead when the voice conversation is the work, not a channel to another agent.
- **Listening State** — Use it for the moment of capture when voice only fills the composer.
- **Background Tasks Drawer** — Use it as well to show the same run on screen, with stop controls.
- **Diff Summary Card** — Use it as well for the written result the spoken summary points to.

Do’s and don’ts

Do use it for

  • A working agent runs tasks that take minutes, and people want to delegate and follow them hands-free or away from the screen (walking, cooking, on a phone).
  • The product already has an agent that edits code, documents or projects; voice is a way in, not a separate assistant.
  • People want to ask "how's it going?" or "what changed?" without opening the run's trace or diff.

Don’t use it for

  • The voice conversation is the work itself (a voice assistant answering questions). Use Voice Orb or Listening State for that.
  • Speech is only an input method. Dictation that turns speech into text in the composer is simpler and keeps the person in control of what is sent.
  • Runs that need careful review before anything is applied. Speak the summary, but route approvals to the screen — don't ask people to approve diffs by ear.
  • Shared or noisy spaces where speaking or playback is impractical; always keep the text chat as an equal path.