Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Mascot: a blink that cuts an alert short no longer leaves the bot stuck on its "!"

milind-soni56b0adb8b182
diff --git a/src/components/Avatar.tsx b/src/components/Avatar.tsx
index d2fc0c0a4f..ecc2988cb1 100644
--- a/src/components/Avatar.tsx
+++ b/src/components/Avatar.tsx
@@ -12,7 +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 { MOTION_FACE, useBeatState } from "./mascot-beat";
import { avatarCropRadius, botAvatarProfile, clampAvatarFocus, clampAvatarZoom, mascotBodyColorSchema, mascotFaceInk, mascotMaterialSchema, type BotAvatarCrop, type MascotFaceColor, type MascotMaterial } from "../../shared/bot-avatar";
import { MASCOT_BODIES, botMascotBody } from "../../shared/mascot-bodies";
import { isCarvedBody, type BotBodyId } from "../../shared/bot-body";
@@ -28,26 +28,6 @@ export const MOUTH_WEIGHT = 11;
const POINTER_GAZE = { forward: 1, authored: 0.25 };
-interface MotionFaces
- extends Partial<
- Record<Exclude<MausMotion, "none">, { state?: MausState; blink?: boolean; spin?: number }>
- > {}
-
-const MOTION_FACE: MotionFaces = {
- arrive: { state: "spawning", spin: 900 },
- switch: { state: "waking", spin: 620 },
- customize: { state: "proud", blink: true },
- alert: { state: "alerting" },
- thinking: { state: "thinking" },
- working: { state: "working" },
- launch: { state: "loading" },
- success: { state: "happy", blink: true },
- celebrate: { state: "celebrate", spin: 700 },
- blink: { blink: true },
- surprise: { state: "surprised", blink: true },
- failure: { state: "sad" },
-};
-
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);
@@ -194,18 +174,12 @@ function GradientAvatarComponent(
setExpression: (index: number) => inner.current?.setExpression(index),
}));
- const [motionState, setMotionState] = useState<MausState | null>(null);
+ const motionState = useBeatState(motion, motionKey, animated);
useEffect(() => {
- // 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);
+ if (motion === "none" || animated === false) return;
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_BEAT_MS);
- return () => clearTimeout(timer);
+ if (beat?.blink) inner.current?.blink();
+ if (beat?.spin) inner.current?.spin(beat.spin);
}, [motion, motionKey, animated]);
const live = animated ?? (ACTIVE_STATES.has(state) || motionState !== null);
diff --git a/src/components/mascot-beat.dom.test.ts b/src/components/mascot-beat.dom.test.ts
index d50a5cf40c..8770f3cc08 100644
--- a/src/components/mascot-beat.dom.test.ts
+++ b/src/components/mascot-beat.dom.test.ts
@@ -3,7 +3,7 @@ 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";
+import { MOTION_BEAT_MS, useBeatState, useMotionBeat } from "./mascot-beat";
let container: HTMLDivElement, root: Root;
const seen: boolean[] = [];
@@ -62,3 +62,36 @@ it("no motion is never a beat", async () => {
await show("none", 3);
expect(playing()).toBe(false);
});
+
+const faces: Array<string | null> = [];
+function Face({ motion, motionKey, animated }: { motion: MausMotion; motionKey: number; animated?: boolean }) {
+ faces.push(useBeatState(motion, motionKey, animated));
+ return null;
+}
+const face = (motion: MausMotion, motionKey: number, animated?: boolean) => act(async () => root.render(createElement(Face, { motion, motionKey, animated })));
+
+it("an alert beat shows its alerting face, then leaves it when the beat ends", async () => {
+ faces.length = 0;
+ await face("alert", 1);
+ expect(faces.at(-1)).toBe("alerting");
+ await wait(MOTION_BEAT_MS);
+ expect(faces.at(-1)).toBeNull();
+});
+
+it("a beat with no face ends the face of the beat it cut short, so the bot does not keep alerting", async () => {
+ faces.length = 0;
+ await face("alert", 1);
+ expect(faces.at(-1)).toBe("alerting");
+ await wait(MOTION_BEAT_MS / 2);
+ await face("blink", 2);
+ expect(faces.at(-1)).toBeNull();
+ await wait(60_000);
+ expect(faces.at(-1)).toBeNull();
+});
+
+it("a caller that stops animating mid-beat drops the beat's face", async () => {
+ faces.length = 0;
+ await face("alert", 1);
+ await face("alert", 1, false);
+ expect(faces.at(-1)).toBeNull();
+});
diff --git a/src/components/mascot-beat.ts b/src/components/mascot-beat.ts
index 9e5935cdea..5a88842a7a 100644
--- a/src/components/mascot-beat.ts
+++ b/src/components/mascot-beat.ts
@@ -1,9 +1,45 @@
import { useEffect, useState } from "react";
-import type { MausMotion } from "@/lib/mascot";
+import type { MausMotion, MausState } 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;
+interface MotionFaces
+ extends Partial<
+ Record<Exclude<MausMotion, "none">, { state?: MausState; blink?: boolean; spin?: number }>
+ > {}
+
+export const MOTION_FACE: MotionFaces = {
+ arrive: { state: "spawning", spin: 900 },
+ switch: { state: "waking", spin: 620 },
+ customize: { state: "proud", blink: true },
+ alert: { state: "alerting" },
+ thinking: { state: "thinking" },
+ working: { state: "working" },
+ launch: { state: "loading" },
+ success: { state: "happy", blink: true },
+ celebrate: { state: "celebrate", spin: 700 },
+ blink: { blink: true },
+ surprise: { state: "surprised", blink: true },
+ failure: { state: "sad" },
+};
+
+/** The state a beat puts on the face while it plays. A beat's face ends with it: a caller that stops animating mid-beat
+ * leaves the bot's own state, not the beat's, and a beat with no face of its own (a blink) that cuts a face beat short
+ * ends that face too, so the bot never keeps it. */
+export function useBeatState(motion: MausMotion | undefined, motionKey: number | undefined, animated: boolean | undefined): MausState | null {
+ const [face, setFace] = useState<MausState | null>(null);
+ useEffect(() => {
+ if (motion === undefined || motion === "none" || animated === false) return setFace(null);
+ const state = MOTION_FACE[motion]?.state;
+ if (!state) return setFace(null);
+ setFace(state);
+ const timer = setTimeout(() => setFace(null), MOTION_BEAT_MS);
+ return () => clearTimeout(timer);
+ }, [motion, motionKey, animated]);
+ return face;
+}
+
/** 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. */