Voice Session
A live voice call with an agent: both voices as waveforms with the pause between them measured, the words as they're spoken, and voice-to-voice latency split against a budget.
"use client"
import * as React from "react"
import {
AudioLines,
Clock,
Cpu,
Gauge,
Globe,
MessageSquare,
Mic,
Pause,
Play,
Volume2,
type LucideIcon,
} from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Roll,
toneColor,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
export type VoicePhase = "listening" | "thinking" | "speaking"
export interface VoiceLatencyPart {
id: "endpoint" | "transcribe" | "model" | "voice" | "network"
label: string
ms: number
tone: AiTone
}
export interface VoiceSessionProps {
agent: { name: string; role: string; tone: AiTone; shape?: AiShape }
/** The caller, shown with an initial; no photo needed. */
caller: { name: string; tone?: AiTone }
turn: number
phase: VoicePhase
/** What the caller said this turn. */
heard: string
/** What the agent says this turn, and how many characters are spoken so far. */
reply: string
spoken: number
/** Timeline of the turn, each from 0 to 1: caller speech, agent speech, playhead. */
callerSpan: [number, number]
agentStart: number
playhead: number
/** Silence between the caller stopping and the agent starting. */
gapMs: number
latency: { budget: number; parts: readonly VoiceLatencyPart[] }
duration: string
turns: number
p50: number
paused?: boolean
onPause?: () => void
onBargeIn?: () => void
className?: string
}
const ICONS: Record<VoiceLatencyPart["id"], LucideIcon> = {
endpoint: AudioLines,
transcribe: MessageSquare,
model: Cpu,
voice: Volume2,
network: Globe,
}
const BARS = 64
const CSS = `
.aik-vs{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,640px);container-type:inline-size}
.aik-vs-card{overflow:hidden}
.aik-vs-top{display:grid;gap:18px;padding:20px 22px 22px}
.aik-vs-head{display:flex;align-items:center;gap:12px}
.aik-vs-head b{font-size:16px;font-weight:600}
.aik-vs-head > span:not(.aik-face){color:var(--aik-text-3);font-size:14px}
.aik-vs-turn{display:flex;align-items:center;gap:10px;font-size:15px;font-weight:500}
.aik-vs-phase{display:inline-flex;align-items:center;gap:6px;height:26px;padding:0 10px;border-radius:8px;font-size:13px;font-weight:500;transition:background var(--aik-d-state),color var(--aik-d-state)}
.aik-vs-phase[data-p="speaking"]{color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 15%,transparent)}
.aik-vs-phase[data-p="listening"]{color:var(--aik-text-2);background:var(--aik-raised)}
.aik-vs-phase[data-p="thinking"]{color:var(--aik-purple);background:color-mix(in oklab,var(--aik-purple) 15%,transparent)}
.aik-vs-phase i{width:6px;height:6px;border-radius:50%;background:currentColor;animation:aik-vs-blink 1s ease-in-out infinite}
@keyframes aik-vs-blink{50%{opacity:.3}}
.aik-vs-lines{display:grid;grid-template-columns:auto minmax(0,1fr);gap:10px 16px;align-items:start;font-size:15px;line-height:22px;min-height:78px}
.aik-vs-who{display:flex;align-items:center;gap:8px;color:var(--aik-text-2);white-space:nowrap}
.aik-vs-av{display:grid;place-items:center;width:22px;height:22px;border-radius:50%;font-size:11px;font-weight:600;color:#fff;background:var(--c)}
.aik-vs-said{color:var(--aik-text)}
.aik-vs-said span{color:var(--aik-text-3);transition:color 120ms}
.aik-vs-waves{display:grid;grid-template-columns:auto minmax(0,1fr);align-items:center;gap:6px 16px;margin-top:4px}
.aik-vs-wave{position:relative;display:grid;grid-template-columns:repeat(${BARS},minmax(0,1fr));align-items:center;height:48px}
.aik-vs-wave i{justify-self:center;width:2.5px;border-radius:2px;height:calc(var(--h)*44px + 4px);background:var(--aik-line-3);transform:scaleY(calc(2 / (var(--h)*44 + 4)));transition:transform 220ms var(--aik-ease-out),background 220ms}
.aik-vs-wave i[data-on]{transform:none;background:var(--c)}
.aik-vs-wave i[data-on][data-now]{background:var(--aik-text)}
.aik-vs-wave[data-who="agent"] i[data-on]{box-shadow:0 0 6px color-mix(in oklab,var(--c) 40%,transparent)}
.aik-vs-gap{position:relative;height:22px;grid-column:2}
.aik-vs-gap div{position:absolute;top:0;bottom:0;display:flex;align-items:center;justify-content:center;border-inline:1.5px solid var(--aik-line-3);font:600 13px var(--aik-mono);color:var(--aik-text);transition:opacity var(--aik-d-state)}
.aik-vs-gap div::before{content:"";position:absolute;left:0;right:0;top:50%;border-top:1px dashed var(--aik-line-3)}
.aik-vs-gap span{position:relative;padding:0 6px;background:var(--aik-card)}
.aik-vs-lat{display:grid;gap:14px;padding:20px 22px;border-top:1px solid var(--aik-line)}
.aik-vs-latop{display:flex;align-items:baseline;gap:10px}
.aik-vs-latop b{font-size:32px;font-weight:600;letter-spacing:-.03em;font-variant-numeric:tabular-nums}
.aik-vs-latop small{font-size:14px;color:var(--aik-text-3)}
.aik-vs-under{margin-left:auto;display:inline-flex;align-items:center;height:28px;padding:0 10px;border-radius:8px;font-size:13px;font-weight:500;color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 15%,transparent);white-space:nowrap}
.aik-vs-under[data-over]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 14%,transparent)}
.aik-vs-bar{position:relative;display:flex;gap:3px;height:8px;border-radius:5px;background:var(--aik-sunk)}
.aik-vs-bar i{height:100%;border-radius:4px;background:var(--c);transform-origin:left;animation:aik-vs-grow 600ms var(--aik-ease-move) both;animation-delay:calc(var(--k)*70ms);transition:opacity var(--aik-d-state)}
@keyframes aik-vs-grow{from{transform:scaleX(0);opacity:0}}
.aik-vs-lat[data-hl] .aik-vs-bar i:not([data-on]){opacity:.3}
.aik-vs-budget{position:absolute;top:-5px;bottom:-5px;width:1.5px;background:var(--aik-text-2);border-radius:2px}
.aik-vs-parts{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:4px 14px}
.aik-vs-part{display:flex;align-items:center;gap:9px;height:36px;padding:0 8px;margin:0 -8px;border-radius:9px;font-size:14px;text-align:left;transition:background var(--aik-d-state),opacity var(--aik-d-state)}
.aik-vs-part svg{color:var(--c)}
.aik-vs-part span{color:var(--aik-text-2)}
.aik-vs-part b{margin-left:auto;font:500 13.5px var(--aik-mono);font-variant-numeric:tabular-nums;white-space:nowrap}
.aik-vs-latop small{white-space:nowrap}
.aik-vs-part[data-on]{background:var(--aik-hover)}
.aik-vs-lat[data-hl] .aik-vs-part:not([data-on]){opacity:.45}
.aik-vs-foot{display:flex;align-items:center;gap:8px;padding:14px 22px;border-top:1px solid var(--aik-line);flex-wrap:wrap}
.aik-vs-stat{display:inline-flex;align-items:center;gap:7px;height:32px;padding:0 10px;border-radius:9px;background:var(--aik-raised);font:13px var(--aik-mono);color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-vs-foot .aik-vs-acts{margin-left:auto;display:flex;gap:8px}
@container (max-width:520px){.aik-vs-parts{grid-template-columns:repeat(2,minmax(0,1fr))}.aik-vs-stat:nth-child(3){display:none}.aik-vs-top,.aik-vs-lat{padding-inline:16px}.aik-vs-foot{padding-inline:16px}}
@container (max-width:420px){.aik-vs-parts{grid-template-columns:minmax(0,1fr)}.aik-vs-latop{flex-wrap:wrap;row-gap:6px}.aik-vs-latop b{font-size:28px}.aik-vs-under{margin-left:0}.aik-vs-waves{column-gap:10px}}
@media (prefers-reduced-motion:reduce){.aik-vs-bar i{animation:none}}
@media (prefers-reduced-motion:reduce){.aik-vs-phase i{animation:none}}
`
// Deterministic bar heights, like a recorded voice: syllable bursts with small dips.
function heights(seed: number) {
return Array.from({ length: BARS }, (_, i) => {
const a = Math.sin(i * 0.9 + seed) * 0.5 + 0.5
const b = Math.sin(i * 0.37 + seed * 2.1) * 0.5 + 0.5
return Math.max(0.12, a * 0.55 + b * 0.45)
})
}
const CALLER_H = heights(1.7)
const AGENT_H = heights(4.2)
/**
* A live voice call with an agent: both voices as waveforms with the pause
* between them measured, the words as they are spoken, and the voice-to-voice
* latency split up against a budget. The agent is a face that talks.
*/
export function VoiceSession({
agent,
caller,
turn,
phase,
heard,
reply,
spoken,
callerSpan,
agentStart,
playhead,
gapMs,
latency,
duration,
turns,
p50,
paused = false,
onPause,
onBargeIn,
className,
}: VoiceSessionProps) {
const [hl, setHl] = React.useState<string | null>(null)
const total = latency.parts.reduce((sum, part) => sum + part.ms, 0)
const scale = Math.max(latency.budget * 1.25, total)
const at = Math.floor(playhead * BARS)
const mood =
phase === "speaking" ? "talk" : phase === "thinking" ? "think" : "listen"
const delta = latency.budget - total
const wave = (
who: "caller" | "agent",
from: number,
to: number,
hs: number[],
tone: string
) => (
<div
className="aik-vs-wave"
data-who={who}
style={{ "--c": tone } as React.CSSProperties}
aria-hidden="true"
>
{hs.map((h, i) => {
const x = i / BARS
const on = x >= from && x < to && i <= at
return (
<i
key={i}
data-on={on ? "" : undefined}
data-now={on && i === at && !paused ? "" : undefined}
style={{ "--h": h.toFixed(3) } as React.CSSProperties}
/>
)
})}
</div>
)
return (
<div className={cx("aik aik-vs aik-shell", className)}>
<AiKitStyles />
<AiStyle id="voice-session" css={CSS} />
<div className="aik-surface aik-vs-card">
<div className="aik-vs-top">
<div className="aik-vs-head">
<Face
mood={paused ? "sleep" : mood}
shape={agent.shape ?? "orb"}
tone={agent.tone}
size={40}
/>
<b>{agent.name}</b>
<span>{agent.role}</span>
</div>
<div className="aik-vs-turn">
Turn {turn}
<span className="aik-vs-phase" data-p={phase} aria-live="polite">
<i />
<Roll>
{paused ? "Paused" : phase[0].toUpperCase() + phase.slice(1)}
</Roll>
</span>
</div>
<div className="aik-vs-lines">
<span className="aik-vs-who">
<span
className="aik-vs-av"
style={
{
"--c": toneColor(caller.tone ?? "rose"),
} as React.CSSProperties
}
>
{caller.name[0]}
</span>
{caller.name}
</span>
<p className="aik-vs-said">{heard}</p>
<span className="aik-vs-who">
<Face
mood={mood}
shape={agent.shape ?? "orb"}
tone={agent.tone}
size={22}
/>
{agent.name}
</span>
<p className="aik-vs-said">
{reply.slice(0, spoken)}
<span>{reply.slice(spoken)}</span>
</p>
</div>
<div className="aik-vs-waves">
<span className="aik-vs-who">
<span
className="aik-vs-av"
style={
{
"--c": toneColor(caller.tone ?? "rose"),
} as React.CSSProperties
}
>
{caller.name[0]}
</span>
{caller.name}
</span>
{wave(
"caller",
callerSpan[0],
callerSpan[1],
CALLER_H,
"var(--aik-text-2)"
)}
<div className="aik-vs-gap">
<div
style={{
left: `${callerSpan[1] * 100}%`,
width: `${Math.max(agentStart - callerSpan[1], 0) * 100}%`,
opacity: playhead >= callerSpan[1] ? 1 : 0,
}}
>
<span>
<Roll>{playhead >= agentStart ? `${gapMs} ms` : "…"}</Roll>
</span>
</div>
</div>
<span className="aik-vs-who">
<Face
mood={mood}
shape={agent.shape ?? "orb"}
tone={agent.tone}
size={22}
/>
{agent.name}
</span>
{wave("agent", agentStart, 1, AGENT_H, toneColor(agent.tone))}
</div>
</div>
<div className="aik-vs-lat" data-hl={hl ? "" : undefined}>
<div className="aik-vs-latop">
<b>
<Roll>{String(total)}</Roll>
</b>
<small>ms voice to voice</small>
<span
className="aik-vs-under"
data-over={delta < 0 ? "" : undefined}
>
<Roll>
{delta >= 0 ? `${delta} ms under` : `${-delta} ms over`}
</Roll>
</span>
</div>
<div
className="aik-vs-bar"
role="img"
aria-label={`${total} of ${latency.budget} millisecond budget`}
>
{latency.parts.map((part, k) => (
<i
key={`${part.id}-${part.ms}`}
data-on={hl === part.id ? "" : undefined}
style={
{
"--c": toneColor(part.tone),
"--k": k,
width: `${(part.ms / scale) * 100}%`,
} as React.CSSProperties
}
/>
))}
<span
className="aik-vs-budget"
style={{ left: `${(latency.budget / scale) * 100}%` }}
/>
</div>
<div className="aik-vs-parts">
{latency.parts.map((part) => {
const Icon = ICONS[part.id]
return (
<button
key={part.id}
type="button"
className="aik-vs-part"
data-on={hl === part.id ? "" : undefined}
style={{ "--c": toneColor(part.tone) } as React.CSSProperties}
onPointerEnter={() => setHl(part.id)}
onPointerLeave={() => setHl(null)}
onFocus={() => setHl(part.id)}
onBlur={() => setHl(null)}
>
<Icon size={16} aria-hidden="true" />
<span>{part.label}</span>
<b>{part.ms} ms</b>
</button>
)
})}
</div>
</div>
<div className="aik-vs-foot">
<span className="aik-vs-stat">
<Clock size={14} />
{duration}
</span>
<span className="aik-vs-stat">
<MessageSquare size={14} />
{turns} turns
</span>
<span className="aik-vs-stat">
<Gauge size={14} />
P50 {p50} ms
</span>
<span className="aik-vs-acts">
<button type="button" className="aik-btn" onClick={onPause}>
{paused ? <Play size={15} /> : <Pause size={15} />}
{paused ? "Resume" : "Pause"}
</button>
<button type="button" className="aik-btn pri" onClick={onBargeIn}>
<Mic size={15} />
Barge in
</button>
</span>
</div>
</div>
</div>
)
}
Installation
One command adds voice-session 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/voice-session.json