|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useEffect, useState } from "react" |
| 4 | +import type { CSSProperties } from "react" |
| 5 | +import { FONTS } from "@/lib/constants" |
| 6 | +import { useIsDark, useThemeColors } from "@/context/ThemeContext" |
| 7 | + |
| 8 | +export type AgentThinkingStatusPhase = "analyzing" | "planning" | "drafting" | "generating" |
| 9 | + |
| 10 | +export interface AgentThinkingStatusProps { |
| 11 | + phase: AgentThinkingStatusPhase |
| 12 | + compact?: boolean |
| 13 | + showSkeleton?: boolean |
| 14 | + className?: string |
| 15 | + style?: CSSProperties |
| 16 | +} |
| 17 | + |
| 18 | +const PHASE_LABELS: Record<AgentThinkingStatusPhase, string> = { |
| 19 | + analyzing: "Agent is analyzing your prompt", |
| 20 | + planning: "Agent is planning the response", |
| 21 | + drafting: "Agent is drafting an answer", |
| 22 | + generating: "Agent is generating the final response", |
| 23 | +} |
| 24 | + |
| 25 | +function SkeletonLine({ |
| 26 | + width, |
| 27 | + compact, |
| 28 | +}: { |
| 29 | + width: string |
| 30 | + compact: boolean |
| 31 | +}) { |
| 32 | + const COLORS = useThemeColors() |
| 33 | + const isDark = useIsDark() |
| 34 | + return ( |
| 35 | + <div |
| 36 | + aria-hidden="true" |
| 37 | + style={{ |
| 38 | + width, |
| 39 | + height: compact ? "8px" : "10px", |
| 40 | + borderRadius: "999px", |
| 41 | + background: isDark |
| 42 | + ? "linear-gradient(90deg, rgba(255,255,255,0.07) 18%, rgba(255,255,255,0.14) 50%, rgba(255,255,255,0.07) 82%)" |
| 43 | + : "linear-gradient(90deg, rgba(0,0,0,0.06) 18%, rgba(0,0,0,0.12) 50%, rgba(0,0,0,0.06) 82%)", |
| 44 | + backgroundSize: "220% 100%", |
| 45 | + animation: "fpSkeletonShimmer 1.2s linear infinite", |
| 46 | + border: `1px solid ${COLORS.border}`, |
| 47 | + }} |
| 48 | + /> |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +export function useAgentThinkingPhase({ |
| 53 | + active, |
| 54 | + hasStartedGenerating = false, |
| 55 | +}: { |
| 56 | + active: boolean |
| 57 | + hasStartedGenerating?: boolean |
| 58 | +}): AgentThinkingStatusPhase { |
| 59 | + const [phase, setPhase] = useState<AgentThinkingStatusPhase>("analyzing") |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + const resetTimer = setTimeout(() => { |
| 63 | + if (!active) { |
| 64 | + setPhase("analyzing") |
| 65 | + return |
| 66 | + } |
| 67 | + if (hasStartedGenerating) { |
| 68 | + setPhase("generating") |
| 69 | + return |
| 70 | + } |
| 71 | + setPhase("analyzing") |
| 72 | + }, 0) |
| 73 | + |
| 74 | + const planningTimer = setTimeout(() => { |
| 75 | + if (!active || hasStartedGenerating) return |
| 76 | + setPhase((prev) => (prev === "analyzing" ? "planning" : prev)) |
| 77 | + }, 1200) |
| 78 | + const draftingTimer = setTimeout(() => { |
| 79 | + if (!active || hasStartedGenerating) return |
| 80 | + setPhase((prev) => (prev === "analyzing" || prev === "planning" ? "drafting" : prev)) |
| 81 | + }, 3500) |
| 82 | + |
| 83 | + return () => { |
| 84 | + clearTimeout(resetTimer) |
| 85 | + clearTimeout(planningTimer) |
| 86 | + clearTimeout(draftingTimer) |
| 87 | + } |
| 88 | + }, [active, hasStartedGenerating]) |
| 89 | + |
| 90 | + return phase |
| 91 | +} |
| 92 | + |
| 93 | +export function AgentThinkingStatus({ |
| 94 | + phase, |
| 95 | + compact = false, |
| 96 | + showSkeleton = true, |
| 97 | + className, |
| 98 | + style, |
| 99 | +}: AgentThinkingStatusProps) { |
| 100 | + const COLORS = useThemeColors() |
| 101 | + const isDark = useIsDark() |
| 102 | + |
| 103 | + return ( |
| 104 | + <div |
| 105 | + role="status" |
| 106 | + aria-live="polite" |
| 107 | + aria-atomic="true" |
| 108 | + aria-busy="true" |
| 109 | + className={className} |
| 110 | + style={{ |
| 111 | + border: `1px solid ${COLORS.border}`, |
| 112 | + borderRadius: compact ? "10px" : "12px", |
| 113 | + background: isDark ? "rgba(255,255,255,0.02)" : "rgba(0,0,0,0.02)", |
| 114 | + padding: compact ? "10px 12px" : "12px 14px", |
| 115 | + display: "flex", |
| 116 | + flexDirection: "column", |
| 117 | + gap: compact ? "6px" : "8px", |
| 118 | + transition: "opacity 0.2s ease", |
| 119 | + ...style, |
| 120 | + }} |
| 121 | + > |
| 122 | + <span |
| 123 | + style={{ |
| 124 | + fontFamily: FONTS.mono, |
| 125 | + fontSize: compact ? "10px" : "11px", |
| 126 | + color: COLORS.textTertiary, |
| 127 | + textTransform: "uppercase", |
| 128 | + letterSpacing: "0.06em", |
| 129 | + }} |
| 130 | + > |
| 131 | + Agent is thinking |
| 132 | + </span> |
| 133 | + <span |
| 134 | + style={{ |
| 135 | + fontFamily: FONTS.sans, |
| 136 | + fontSize: compact ? "12px" : "13px", |
| 137 | + color: COLORS.textSecondary, |
| 138 | + lineHeight: 1.5, |
| 139 | + }} |
| 140 | + > |
| 141 | + {PHASE_LABELS[phase]} |
| 142 | + </span> |
| 143 | + {showSkeleton && ( |
| 144 | + <div style={{ display: "flex", flexDirection: "column", gap: compact ? "6px" : "8px", paddingTop: compact ? "2px" : "4px" }}> |
| 145 | + <SkeletonLine width={compact ? "94%" : "98%"} compact={compact} /> |
| 146 | + <SkeletonLine width={compact ? "87%" : "92%"} compact={compact} /> |
| 147 | + <SkeletonLine width={compact ? "72%" : "78%"} compact={compact} /> |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + ) |
| 152 | +} |
0 commit comments