Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | "use client" |
| 2 | |
| 3 | import * as React from "react" |
| 4 | |
| 5 | import { useResolvedTerminalTheme } from "@/hooks/use-resolved-terminal-theme" |
| 6 | import { cn } from "@/lib/utils" |
| 7 | |
| 8 | /** Claude thinking spinner — cycles in this order (claude.ai / Claude Code). */ |
| 9 | export const CLAUDE_THINKING_FRAMES = [ |
| 10 | "\u00B7", // middle dot / bullet |
| 11 | "\u273B", // teardrop-spoked asterisk ✻ |
| 12 | "\u273D", // heavy teardrop-spoked asterisk ✽ |
| 13 | "\u2736", // six pointed black star ✶ |
| 14 | "\u2733", // eight spoked asterisk ✳ |
| 15 | "\u2722", // four balloon-spoked asterisk ✢ |
| 16 | ] as const |
| 17 | |
| 18 | export type TerminalAsciiSpinnerProps = { |
| 19 | /** Single-character frames cycled in order. */ |
| 20 | frames?: ReadonlyArray<string> |
| 21 | /** Milliseconds per frame. */ |
| 22 | speed?: number |
| 23 | color?: string |
| 24 | className?: string |
| 25 | } |
| 26 | |
| 27 | export function TerminalAsciiSpinner({ |
| 28 | frames = CLAUDE_THINKING_FRAMES, |
| 29 | speed = 150, |
| 30 | color = "var(--terminal-teal)", |
| 31 | className, |
| 32 | }: TerminalAsciiSpinnerProps) { |
| 33 | const [step, setStep] = React.useState(0) |
| 34 | const [reduceMotion, setReduceMotion] = React.useState(false) |
| 35 | const frameCount = Math.max(1, frames.length) |
| 36 | |
| 37 | React.useEffect(() => { |
| 38 | const media = window.matchMedia("(prefers-reduced-motion: reduce)") |
| 39 | const update = () => setReduceMotion(media.matches) |
| 40 | update() |
| 41 | media.addEventListener("change", update) |
| 42 | return () => media.removeEventListener("change", update) |
| 43 | }, []) |
| 44 | |
| 45 | React.useEffect(() => { |
| 46 | if (reduceMotion) return |
| 47 | |
| 48 | const interval = window.setInterval(() => { |
| 49 | setStep((current) => (current + 1) % frameCount) |
| 50 | }, Math.max(16, speed)) |
| 51 | |
| 52 | return () => window.clearInterval(interval) |
| 53 | }, [reduceMotion, speed, frameCount]) |
| 54 | |
| 55 | const frame = frames[reduceMotion ? 0 : step % frameCount] ?? frames[0] ?? "·" |
| 56 | |
| 57 | return ( |
| 58 | <span |
| 59 | aria-hidden |
| 60 | className={cn( |
| 61 | "inline-flex w-[1em] shrink-0 items-center justify-center font-mono leading-none", |
| 62 | className |
| 63 | )} |
| 64 | style={{ color }} |
| 65 | > |
| 66 | {frame} |
| 67 | </span> |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | // 3x3 grid (row-major indices). The 8 perimeter cells form a clockwise ring |
| 72 | // swept by a comet head with a fading trail; the center (index 4) pulses as a |
| 73 | // core in sync with each revolution. |
| 74 | const DOT_GRID_SIZE = 9 |
| 75 | const DOT_RING = [0, 1, 2, 5, 8, 7, 6, 3] |
| 76 | const DOT_CENTER = 4 |
| 77 | |
| 78 | /** A custom animation frame: opacity (0–1) per grid cell, row-major (length 9). */ |
| 79 | export type TerminalDotFrame = ReadonlyArray<number> |
| 80 | |
| 81 | export type TerminalDotMatrixProps = { |
| 82 | tone?: "default" | "active" |
| 83 | /** Milliseconds per step (lower is faster). */ |
| 84 | speed?: number |
| 85 | /** Number of cells that fade behind the comet head. (Built-in spin only.) */ |
| 86 | trail?: number |
| 87 | /** Pulse the center dot as a core. (Built-in spin only.) */ |
| 88 | pulseCenter?: boolean |
| 89 | /** Dot size in pixels. */ |
| 90 | dotSize?: number |
| 91 | /** Override the dot color (any CSS color). Defaults to the tone color. */ |
| 92 | color?: string |
| 93 | /** |
| 94 | * Custom animation as a list of frames, each an array of opacity values |
| 95 | * (0–1) for the 3x3 grid in row-major order. Overrides the built-in spin. |
| 96 | */ |
| 97 | frames?: ReadonlyArray<TerminalDotFrame> |
| 98 | className?: string |
| 99 | } |
| 100 | |
| 101 | function clampOpacity(value: number | undefined) { |
| 102 | if (value == null || Number.isNaN(value)) return 0 |
| 103 | return Math.min(1, Math.max(0, value)) |
| 104 | } |
| 105 | |
| 106 | export function TerminalDotMatrix({ |
| 107 | tone = "default", |
| 108 | speed = 120, |
| 109 | trail = 4, |
| 110 | pulseCenter = true, |
| 111 | dotSize = 2, |
| 112 | color, |
| 113 | frames, |
| 114 | className, |
| 115 | }: TerminalDotMatrixProps) { |
| 116 | const [step, setStep] = React.useState(0) |
| 117 | const [reduceMotion, setReduceMotion] = React.useState(false) |
| 118 | |
| 119 | const hasCustomFrames = Boolean(frames && frames.length > 0) |
| 120 | const frameCount = hasCustomFrames ? frames!.length : DOT_RING.length |
| 121 | |
| 122 | React.useEffect(() => { |
| 123 | const media = window.matchMedia("(prefers-reduced-motion: reduce)") |
| 124 | const update = () => setReduceMotion(media.matches) |
| 125 | update() |
| 126 | media.addEventListener("change", update) |
| 127 | return () => media.removeEventListener("change", update) |
| 128 | }, []) |
| 129 | |
| 130 | React.useEffect(() => { |
| 131 | if (reduceMotion) return |
| 132 | |
| 133 | const interval = window.setInterval(() => { |
| 134 | setStep((current) => (current + 1) % frameCount) |
| 135 | }, Math.max(16, speed)) |
| 136 | |
| 137 | return () => window.clearInterval(interval) |
| 138 | }, [reduceMotion, speed, frameCount]) |
| 139 | |
| 140 | const dotColor = |
| 141 | color ?? |
| 142 | (tone === "active" ? "var(--terminal-blue-bright)" : "var(--terminal-white)") |
| 143 | |
| 144 | const opacities = new Array<number>(DOT_GRID_SIZE).fill(0) |
| 145 | |
| 146 | if (hasCustomFrames) { |
| 147 | const frame = frames![(reduceMotion ? 0 : step) % frames!.length] ?? [] |
| 148 | for (let index = 0; index < DOT_GRID_SIZE; index += 1) { |
| 149 | opacities[index] = clampOpacity(frame[index]) |
| 150 | } |
| 151 | } else { |
| 152 | const head = reduceMotion ? 0 : step % DOT_RING.length |
| 153 | const safeTrail = Math.max(1, trail) |
| 154 | DOT_RING.forEach((cellIndex, ringPos) => { |
| 155 | const distance = (head - ringPos + DOT_RING.length) % DOT_RING.length |
| 156 | opacities[cellIndex] = distance < safeTrail ? 1 - distance / safeTrail : 0 |
| 157 | }) |
| 158 | |
| 159 | if (pulseCenter) { |
| 160 | const phase = head / DOT_RING.length |
| 161 | opacities[DOT_CENTER] = |
| 162 | 0.3 + 0.45 * (0.5 + 0.5 * Math.cos(2 * Math.PI * phase)) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return ( |
| 167 | <span |
| 168 | aria-hidden |
| 169 | className={cn("inline-grid shrink-0 align-middle leading-none", className)} |
| 170 | style={{ |
| 171 | gridTemplateColumns: `repeat(3, ${dotSize}px)`, |
| 172 | gridTemplateRows: `repeat(3, ${dotSize}px)`, |
| 173 | gap: 1, |
| 174 | }} |
| 175 | > |
| 176 | {opacities.map((opacity, index) => ( |
| 177 | <span |
| 178 | key={index} |
| 179 | className="rounded-full" |
| 180 | style={{ |
| 181 | width: dotSize, |
| 182 | height: dotSize, |
| 183 | backgroundColor: dotColor, |
| 184 | opacity, |
| 185 | transition: `opacity ${Math.max(16, speed)}ms linear`, |
| 186 | }} |
| 187 | /> |
| 188 | ))} |
| 189 | </span> |
| 190 | ) |
| 191 | } |
| 192 | |
| 193 | export type ThinkingIndicatorProps = { |
| 194 | label?: string |
| 195 | children?: React.ReactNode |
| 196 | /** |
| 197 | * `auto` — Claude symbol loop inside `.terminal-theme-claude`, dot matrix elsewhere. |
| 198 | * `ascii` — always use the symbol loop. `dots` / `cursor` — force that style. |
| 199 | */ |
| 200 | variant?: "auto" | "dots" | "ascii" | "cursor" |
| 201 | tone?: "default" | "active" |
| 202 | /** Customize the dot-matrix animation (speed, trail, color, etc.). */ |
| 203 | dotProps?: Omit<TerminalDotMatrixProps, "tone" | "className"> |
| 204 | /** Customize the Claude-style ASCII symbol loop. */ |
| 205 | asciiProps?: Omit<TerminalAsciiSpinnerProps, "className"> |
| 206 | /** |
| 207 | * SessionContent integration — skip the streaming queue and pin this line at the |
| 208 | * transcript tail (e.g. live thinking). Not read by ThinkingIndicator; SessionContent |
| 209 | * inspects the prop on the child element. |
| 210 | */ |
| 211 | sessionTail?: boolean |
| 212 | className?: string |
| 213 | } |
| 214 | |
| 215 | export function ThinkingIndicator({ |
| 216 | label, |
| 217 | children, |
| 218 | variant = "auto", |
| 219 | tone = "default", |
| 220 | dotProps, |
| 221 | asciiProps, |
| 222 | sessionTail: _sessionTail, |
| 223 | className, |
| 224 | }: ThinkingIndicatorProps) { |
| 225 | const content = children ?? label |
| 226 | const { ref, isClaude, isCompact } = useResolvedTerminalTheme() |
| 227 | const useAscii = variant === "ascii" || (variant === "auto" && isClaude) |
| 228 | const useCursor = variant === "cursor" || (variant === "auto" && isCompact) |
| 229 | const asciiColor = asciiProps?.color ?? "var(--terminal-teal)" |
| 230 | const textColor = useAscii |
| 231 | ? asciiColor |
| 232 | : tone === "active" |
| 233 | ? "var(--terminal-white)" |
| 234 | : "var(--terminal-dim)" |
| 235 | |
| 236 | return ( |
| 237 | <div |
| 238 | ref={ref} |
| 239 | className={cn("flex items-center gap-2.5 text-[12px]", className)} |
| 240 | role="status" |
| 241 | aria-live="polite" |
| 242 | > |
| 243 | {useCursor ? ( |
| 244 | <span |
| 245 | aria-hidden |
| 246 | className="terminal-cursor inline-block h-3 w-[0.55em]" |
| 247 | style={{ backgroundColor: "var(--terminal-progress-fill)" }} |
| 248 | /> |
| 249 | ) : useAscii ? ( |
| 250 | <TerminalAsciiSpinner {...asciiProps} color={asciiColor} /> |
| 251 | ) : ( |
| 252 | <TerminalDotMatrix tone={tone} {...dotProps} /> |
| 253 | )} |
| 254 | {content && <span style={{ color: textColor }}>{content}</span>} |
| 255 | </div> |
| 256 | ) |
| 257 | } |
| 258 | |
| 259 | /** @deprecated Use ThinkingIndicator */ |
| 260 | export type BusyIndicatorProps = ThinkingIndicatorProps |
| 261 | |
| 262 | /** @deprecated Use ThinkingIndicator */ |
| 263 | export function BusyIndicator(props: ThinkingIndicatorProps) { |
| 264 | return <ThinkingIndicator {...props} /> |
| 265 | } |