Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Mascots: a motion beat ends, so an idle chat's faces hold still (idle window flicker) (#7147)
Milind Soni2fb5a9f9c72b
diff --git a/src/components/Avatar.tsx b/src/components/Avatar.tsxindex 609aee9b6d..033cf97346 100644--- a/src/components/Avatar.tsx+++ b/src/components/Avatar.tsx@@ -12,6 +12,7 @@ import { avatarMarks, type AvatarKind } from "@/lib/avatar-marks";import { CHROME_EYES, CHROME_GRADIENT, MAUS_COLORS, type MausColor, type MausMotion, type MausState } from "@/lib/mascot";import { CursorAvatar, type CursorAvatarHandle } from "./CursorAvatar";import { ACTIVE_STATES } from "./mascot-motion";+import { MOTION_BEAT_MS } from "./mascot-beat";import { avatarCropRadius, botAvatarProfile, clampAvatarFocus, clampAvatarZoom, mascotFaceInk, type BotAvatarCrop, type MascotFaceColor } from "../../shared/bot-avatar";import { MASCOT_BODIES, botMascotBody } from "../../shared/mascot-bodies";import { isCarvedBody, type BotBodyId } from "../../shared/bot-body";@@ -45,8 +46,6 @@ const MOTION_FACE: MotionFaces = {failure: { state: "sad" },};-const MOTION_FACE_MS = 1400;-function mix(hex: string, toward: string, t: number): string {const a = Number.parseInt(hex.slice(1), 16);const b = Number.parseInt(toward.slice(1), 16);@@ -168,14 +167,15 @@ function GradientAvatarComponent(const [motionState, setMotionState] = useState<MausState | null>(null);useEffect(() => {- if (motion === "none" || animated === false) return;+ // A beat's face ends with it: a caller that stops animating mid-beat leaves the bot's own state, not the beat's.+ if (motion === "none" || animated === false) return setMotionState(null);const beat = MOTION_FACE[motion];if (!beat) return;if (beat.blink) inner.current?.blink();if (beat.spin) inner.current?.spin(beat.spin);if (!beat.state) return;setMotionState(beat.state);- const timer = setTimeout(() => setMotionState(null), MOTION_FACE_MS);+ const timer = setTimeout(() => setMotionState(null), MOTION_BEAT_MS);return () => clearTimeout(timer);}, [motion, motionKey, animated]);diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsxindex 43cf90a6de..a35aed55c0 100644--- a/src/components/ChatView.tsx+++ b/src/components/ChatView.tsx@@ -79,7 +79,7 @@ import { ConnectorCard } from "./ConnectorCard";import { SecretRequestCard } from "./SecretRequestCard";import { SignInRequestCard } from "./SignInRequestCard";import { isPinnedSignIn, NO_PINNED_SIGN_INS, PinnedSignInSlot } from "./PinnedSignInSlot";-import { ActivityChip, ChatRowsContext, useChatRows, type ChatRows } from "./ChatRows";+import { ActivityChip, ChatRowsContext, useChatRows, type ChatRows } from "./ChatRows"; import { useMotionBeat } from "./mascot-beat";import { AutomationCaption, RoutineRunRow } from "./RoutineRunCard"; import { InboxAnswers, InboxCaption, RelayLabel } from "./InboxLine"; import { InboxCardSlot } from "./inbox/InboxCard";import { CallRecordRow } from "./CallRecordRow";import { AttachmentGallery, collectMessageFiles, replyAttachmentGroup, splitMessageAttachments } from "./AttachmentGallery";@@ -812,7 +812,9 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = falconst busy = Boolean(bot.busy);const stoppedAt = useStoppedAt(bot.threadId);const openedAt = useThreadOpenedAt(bot.threadId);- const headerAnimated = busy || (mascotMotion?.kind ?? "none") !== "none";+ // A beat the store still holds after it played is no beat: the face neither keeps moving nor replays it when a turn starts.+ const headerBeat = useMotionBeat(mascotMotion?.kind, mascotMotion?.nonce ?? 0), headerMotion = (headerBeat && mascotMotion?.kind) || "none";+ const headerAnimated = headerBeat || busy;const branch = useRef(messages);branch.current = messages;const onBranch = useCallback((messageId: string) => branch.current.some((m) => m.id === messageId), []);@@ -935,7 +937,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = fal{simpleHead ? (<ChatHeadPillbot={bot}- avatar={{ state: stateForBot({ ...bot, messages }), motion: mascotMotion?.kind ?? "none", motionKey: mascotMotion?.nonce ?? 0, animated: headerAnimated }}+ avatar={{ state: stateForBot({ ...bot, messages }), motion: headerMotion, motionKey: mascotMotion?.nonce ?? 0, animated: headerAnimated }}privacy={privateChat ? { kind: "private" } : ownHelper ? { kind: "ownHelper" } : sharedChat && team ? { kind: "shared", team: team.name, onlyPeopleAdded: bot.onlyPeopleAdded, sharedWith: currentTask?.sharedWith?.name } : null}onToggle={() => dispatch({ type: "toggleSettings", open: !state.settingsOpen })}/>@@ -952,7 +954,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = falbot={bot}state={stateForBot({ ...bot, messages })}size={24}- motion={mascotMotion?.kind ?? "none"}+ motion={headerMotion}motionKey={mascotMotion?.nonce ?? 0}animated={headerAnimated}/>@@ -986,7 +988,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = falclassName={cn("group flex h-9 min-w-0 items-center gap-2.5 rounded-full py-0.5 pl-2 pr-3.5 text-ink", ELEVATED_FLOAT, ELEVATED_HOVER)}><BotAvatar bot={bot} state={stateForBot({ ...bot, messages })} size={24}- motion={mascotMotion?.kind ?? "none"} motionKey={mascotMotion?.nonce ?? 0} animated={headerAnimated} />+ motion={headerMotion} motionKey={mascotMotion?.nonce ?? 0} animated={headerAnimated} /><span className="min-w-0 truncate text-[14px] font-medium text-ink">{bot.name}</span>{chiefOfStaffBadge(bot)}<ChevronRight size={14} aria-hidden="true" className="-ml-2.5 w-0 shrink-0 text-ink-secondary opacity-0 transition-[width,margin,opacity] duration-150 group-hover:ml-0 group-hover:w-3.5 group-hover:opacity-100 group-focus-visible:ml-0 group-focus-visible:w-3.5 group-focus-visible:opacity-100" />diff --git a/src/components/PinnedCircleThreads.tsx b/src/components/PinnedCircleThreads.tsxindex c3089fce6a..9a8f54a450 100644--- a/src/components/PinnedCircleThreads.tsx+++ b/src/components/PinnedCircleThreads.tsx@@ -4,7 +4,7 @@ import { ChevronRight, FolderPlus, Plus } from "lucide-react";import { openBotRow, visibleMessages, type Bot, type Message } from "@/state/store";import { avatarCropRadius, botAvatarProfile } from "../../shared/bot-avatar";import { liveActivityLabel, sidebarMessage } from "@/lib/live-activity";-import { BotAvatar } from "./Avatar";+import { BotAvatar } from "./Avatar"; import { useMotionBeat } from "./mascot-beat";import { stateForBot } from "@/lib/mascot";import { cn } from "@/lib/cn";import { searchMatchesThreads, useSearchDisclosure } from "@/lib/search-disclosure";@@ -190,7 +190,7 @@ export const PinnedBotCircle = memo(function PinnedBotCircle({const waiting = bot.activity === "waiting-on-you" || activityTasks.some((task) => task.activity === "waiting-on-you");const working = !waiting && (Boolean(bot.busy) || activityTasks.some((task) => task.busy || task.activity === "working"));const unread = bot.unread || activityTasks.some((task) => task.unread);- const noticing = useUnreadNotice(Boolean(bot.unread));+ const noticing = useUnreadNotice(Boolean(bot.unread)); const beat = useMotionBeat(mascotMotion?.kind, mascotMotion?.nonce ?? 0);const radius = avatarCropRadius(botAvatarProfile(bot).avatarCrop);return (<div className="flex w-full min-w-0 flex-col items-center">@@ -226,9 +226,9 @@ export const PinnedBotCircle = memo(function PinnedBotCircle({bot={bot}state={stateForBot({ ...bot, messages: visible })}size={64}- motion={mascotMotion?.kind ?? "none"}+ motion={(beat && mascotMotion?.kind) || "none"}motionKey={mascotMotion?.nonce ?? 0}- animated={working || noticing || (mascotMotion?.kind ?? "none") !== "none"}+ animated={beat || working || noticing}/></span>{unread && (diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsxindex 6a5d1a9ef0..cd214c953d 100644--- a/src/components/Sidebar.tsx+++ b/src/components/Sidebar.tsx@@ -9,7 +9,7 @@ import { liveActivityLabel, sidebarMessage } from "@/lib/live-activity";import { SidebarWorkingStep } from "./SidebarWorkingStep"; import { FadeLine } from "./FadeLine";import { openBotRow } from "@/state/thread-navigation";-import { BotAvatar } from "./Avatar";+import { BotAvatar } from "./Avatar"; import { useMotionBeat } from "./mascot-beat";import { BotEngineMark, BotModelBadge, botTooltip } from "./BotModelBadge"; import { SidebarBotRole } from "./BotRole";import { stateForBot } from "@/lib/mascot";import { cn } from "@/lib/cn";@@ -727,7 +727,7 @@ export const BotListItem = memo(function BotListItem(props: BotRowProps) {simpleRow && rowSurface(rowSelected),);const { waiting, working, failed, teammateWait, queued } = sidebarBotStatus(bot, pendingQueued, visible);- const { unread, noticing } = useBotUnread(bot);+ const { unread, noticing } = useBotUnread(bot); const beat = useMotionBeat(mascotMotion?.kind, mascotMotion?.nonce ?? 0);const onLiveCall = liveBadgeFor(bot.id, liveMedia, liveCall);const body = (<>@@ -736,9 +736,9 @@ export const BotListItem = memo(function BotListItem(props: BotRowProps) {bot={bot}state={stateForBot({ ...bot, messages: visible })}size={simpleRow ? avatarSize + 6 : avatarSize}- motion={mascotMotion?.kind ?? "none"}+ motion={(beat && mascotMotion?.kind) || "none"}motionKey={mascotMotion?.nonce ?? 0}- animated={working || noticing || (mascotMotion?.kind ?? "none") !== "none"}+ animated={beat || working || noticing}/>{/* Bottom-left, clear of the status dots on the right and of the row's hover actions. */}{(iconOnly || !simpleList) && <BotModelBadge bot={bot} instances={instances} avatarSize={avatarSize} />}diff --git a/src/components/SidebarTiles.tsx b/src/components/SidebarTiles.tsxindex 7ed07359bd..fd98a450d8 100644--- a/src/components/SidebarTiles.tsx+++ b/src/components/SidebarTiles.tsx@@ -9,7 +9,7 @@ import { useShowThreads } from "@/lib/thread-preferences";import { api, openBotRow, useStore, type Bot } from "@/state/store";import { useChiefStepOpen } from "@/lib/default-bot";-import { BotAvatar } from "./Avatar";+import { BotAvatar } from "./Avatar"; import { useMotionBeat } from "./mascot-beat";import { botTooltip } from "./BotModelBadge";import { liveBadgeFor } from "./LiveCallPill";import { pinnedCircleDragClass, pinnedCircleDragHandlers, usePinnedCircleDrag, type PinnedCircleDragProps } from "./PinnedCircleThreads";@@ -31,7 +31,7 @@ export const SidebarBotTile = memo(function SidebarBotTile({ primary = false, drconst [renaming, setRenaming] = useState(false);const visible = useVisibleMessages(bot);const { waiting, working, failed, teammateWait, queued } = sidebarBotStatus(bot, pendingQueued, visible);- const { unread, noticing } = useBotUnread(bot);+ const { unread, noticing } = useBotUnread(bot); const beat = useMotionBeat(mascotMotion?.kind, mascotMotion?.nonce ?? 0);const onLiveCall = liveBadgeFor(bot.id, liveMedia, liveCall);const tooltip = botTooltip(bot, instances);const status = waiting ? t("sidebar.preview.waiting") : working ? t("chat.activity.working") : failed ? t("sidebar.preview.failed") : teammateWait ? t("sidebar.preview.waitingOnTeammate") : queued ? t("task.queued") : "";@@ -85,9 +85,9 @@ export const SidebarBotTile = memo(function SidebarBotTile({ primary = false, drbot={bot}state={stateForBot({ ...bot, messages: visible })}size={60}- motion={mascotMotion?.kind ?? "none"}+ motion={(beat && mascotMotion?.kind) || "none"}motionKey={mascotMotion?.nonce ?? 0}- animated={working || noticing || (mascotMotion?.kind ?? "none") !== "none"}+ animated={beat || working || noticing}/>{primary && bot.chiefOfStaff && (<span data-testid="chief-badge" className={cn("absolute -bottom-px -right-px flex size-4 items-center justify-center rounded-full bg-[#E8993A] text-white", RING)}>diff --git a/src/components/mascot-beat.dom.test.ts b/src/components/mascot-beat.dom.test.tsnew file mode 100644index 0000000000..d50a5cf40c--- /dev/null+++ b/src/components/mascot-beat.dom.test.ts@@ -0,0 +1,64 @@+// @vitest-environment happy-dom+import { act, createElement } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import type { MausMotion } from "@/lib/mascot";+import { MOTION_BEAT_MS, useMotionBeat } from "./mascot-beat";++let container: HTMLDivElement, root: Root;+const seen: boolean[] = [];+function Probe({ motion, motionKey }: { motion: MausMotion | undefined; motionKey: number }) {+ seen.push(useMotionBeat(motion, motionKey));+ return null;+}+const show = (motion: MausMotion | undefined, motionKey: number) => act(async () => root.render(createElement(Probe, { motion, motionKey })));+const wait = (ms: number) => act(async () => { vi.advanceTimersByTime(ms); });+const playing = () => seen.at(-1);++beforeEach(() => {+ vi.useFakeTimers();+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ seen.length = 0;+ container = document.createElement("div"); document.body.append(container); root = createRoot(container);+});+afterEach(async () => { await act(async () => root.unmount()); container.remove(); vi.unstubAllGlobals(); vi.useRealTimers(); });++it("plays a beat from the render that brings it and ends it, though the store keeps the motion", async () => {+ await show("blink", 1);+ expect(playing()).toBe(true);+ await wait(MOTION_BEAT_MS - 1);+ expect(playing()).toBe(true);+ await wait(1);+ expect(playing()).toBe(false);+ // The open chat sits idle with the same last motion: the face stays at rest.+ await wait(60_000);+ await show("blink", 1);+ expect(playing()).toBe(false);+});++it("plays again for the next beat of the same kind, from its first render", async () => {+ await show("blink", 1);+ await wait(MOTION_BEAT_MS);+ expect(playing()).toBe(false);+ await show("blink", 2);+ expect(seen.at(-1)).toBe(true);+ await wait(MOTION_BEAT_MS);+ expect(playing()).toBe(false);+});++it("a newer beat during one restarts the time it plays", async () => {+ await show("working", 1);+ await wait(MOTION_BEAT_MS - 100);+ await show("success", 2);+ await wait(MOTION_BEAT_MS - 1);+ expect(playing()).toBe(true);+ await wait(1);+ expect(playing()).toBe(false);+});++it("no motion is never a beat", async () => {+ await show(undefined, 0);+ expect(playing()).toBe(false);+ await show("none", 3);+ expect(playing()).toBe(false);+});diff --git a/src/components/mascot-beat.ts b/src/components/mascot-beat.tsnew file mode 100644index 0000000000..9e5935cdea--- /dev/null+++ b/src/components/mascot-beat.ts@@ -0,0 +1,19 @@+import { useEffect, useState } from "react";+import type { MausMotion } from "@/lib/mascot";++/** How long a motion beat (a blink, a spin, a face for a moment) plays; the longest beat fits inside it. */+export const MOTION_BEAT_MS = 1400;++/** The store keeps the last beat it played for a bot, so a beat counts as playing only from the render that brings its+ * key until MOTION_BEAT_MS later. A face that animates "while a beat plays" then rests again once it ends, instead of+ * moving at the frame rate for as long as the chat stays open. */+export function useMotionBeat(motion: MausMotion | undefined, motionKey: number): boolean {+ const [ended, setEnded] = useState<{ motion: MausMotion; key: number } | null>(null);+ const beat = motion !== undefined && motion !== "none";+ useEffect(() => {+ if (!beat) return;+ const timer = setTimeout(() => setEnded({ motion, key: motionKey }), MOTION_BEAT_MS);+ return () => clearTimeout(timer);+ }, [beat, motion, motionKey]);+ return beat && (ended?.motion !== motion || ended.key !== motionKey);+}