Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧠 feat: Add Lia's Behavior Engine (#16683)

Marco Beretta69d88f49309a
diff --git a/client/src/components/Lia/__tests__/catalog.spec.ts b/client/src/components/Lia/__tests__/catalog.spec.ts
new file mode 100644
index 0000000000..96c18e6f3c
--- /dev/null
+++ b/client/src/components/Lia/__tests__/catalog.spec.ts
@@ -0,0 +1,56 @@
+import { EXPRESSIONS, EXPRESSION_BY_KEY } from '../engine/face';
+import { ARMS, POSES, BUILTIN_SCREENS } from '../engine/body';
+import { ACTIONS } from '../engine/catalog';
+
+describe('Lia catalog', () => {
+ it('has unique action ids', () => {
+ const ids = ACTIONS.map((a) => a.id);
+ expect(new Set(ids).size).toBe(ids.length);
+ });
+
+ it('only references expressions, arms, poses and screens that exist', () => {
+ for (const action of ACTIONS) {
+ for (const [, spec] of action.steps) {
+ if (spec.f != null) {
+ expect({ id: action.id, f: EXPRESSION_BY_KEY.has(spec.f) }).toEqual({
+ id: action.id,
+ f: true,
+ });
+ }
+ if (spec.a != null) {
+ expect({ id: action.id, a: spec.a in ARMS }).toEqual({ id: action.id, a: true });
+ }
+ if (spec.o != null) {
+ expect({ id: action.id, o: spec.o in POSES }).toEqual({ id: action.id, o: true });
+ }
+ if (spec.s != null && spec.s !== 'face') {
+ expect({ id: action.id, s: BUILTIN_SCREENS.has(spec.s) }).toEqual({
+ id: action.id,
+ s: true,
+ });
+ }
+ }
+ }
+ });
+
+ it('gives every expression an emote whose arms and pose exist', () => {
+ for (const ex of EXPRESSIONS) {
+ expect(ACTIONS.some((a) => a.id === `feel-${ex.key}`)).toBe(true);
+ if (ex.arms) {
+ expect(ex.arms in ARMS).toBe(true);
+ }
+ if (ex.pose) {
+ expect(ex.pose in POSES).toBe(true);
+ }
+ }
+ });
+
+ it('marks reactions as never chosen at random and flags actions that move', () => {
+ for (const action of ACTIONS) {
+ if (action.cat === 'react') {
+ expect(action.noAuto).toBe(true);
+ }
+ expect(action.moves).toBe(action.steps.some(([, spec]) => spec.m != null));
+ }
+ });
+});
diff --git a/client/src/components/Lia/__tests__/engine.spec.ts b/client/src/components/Lia/__tests__/engine.spec.ts
new file mode 100644
index 0000000000..6b47fac040
--- /dev/null
+++ b/client/src/components/Lia/__tests__/engine.spec.ts
@@ -0,0 +1,447 @@
+import type { Bubble } from '../engine/types';
+import { ACTION_BY_ID } from '../engine/catalog';
+import { LiaEngine } from '../engine/engine';
+import * as body from '../engine/body';
+
+const PLATFORM = { y: 400, x0: 100, x1: 700 };
+
+function setup() {
+ const bubbles: Array<Bubble | null> = [];
+ const canvas = document.createElement('canvas');
+ const engine = new LiaEngine(
+ canvas,
+ { platform: () => PLATFORM, onBubble: (b) => bubbles.push(b) },
+ 0,
+ );
+ return { engine, bubbles, canvas };
+}
+
+/** Advances the engine frame by frame, the way requestAnimationFrame would. */
+function run(engine: LiaEngine, from: number, to: number) {
+ for (let t = from; t <= to; t += 16) {
+ engine.tick(t);
+ }
+}
+
+/** A seeded generator, so choices are repeatable. */
+function seeded(seed = 7) {
+ let s = seed;
+ return () => {
+ s = (s * 16807) % 2147483647;
+ return (s - 1) / 2147483646;
+ };
+}
+
+function bigShare(engine: LiaEngine, now: number) {
+ const random = seeded();
+ let big = 0;
+ for (let i = 0; i < 400; i++) {
+ const def = ACTION_BY_ID.get(engine.chooseLife(now, random) ?? '');
+ if (def && (def.moves || def.cat === 'life' || def.cat === 'dance' || def.cat === 'travel')) {
+ big += 1;
+ }
+ }
+ return big / 400;
+}
+
+describe('LiaEngine', () => {
+ it('plays an action through its steps and then finishes', () => {
+ const { engine } = setup();
+ engine.life = false;
+ expect(engine.play('intro', 4, 0)).toBe(true);
+ expect(engine.current?.id).toBe('intro');
+ run(engine, 0, 4000);
+ expect(engine.current).toBeNull();
+ });
+
+ it('keeps a more important action from being interrupted', () => {
+ const { engine } = setup();
+ engine.play('r-crash', 3, 0);
+ expect(engine.play('feel-happy', 1, 10)).toBe(false);
+ expect(engine.current?.id).toBe('r-crash');
+ });
+
+ it('stays quiet while the user types and saves big routines for when they step away', () => {
+ const { engine } = setup();
+ engine.noteTyping(1000);
+ expect(engine.chooseLife(1000)).toBeNull();
+
+ engine.noteActivity(false, 10_000);
+ const present = bigShare(engine, 10_000);
+ const idle = bigShare(engine, 60_000);
+ expect(present).toBeLessThan(0.25);
+ expect(idle).toBeGreaterThan(0.5);
+ });
+
+ it('stops a big routine as soon as the user starts typing', () => {
+ const { engine } = setup();
+ engine.play('travel-walk', 1, 0);
+ engine.noteTyping(100);
+ expect(engine.current).toBeNull();
+ });
+
+ it('never picks moving actions under reduced motion', () => {
+ const { engine } = setup();
+ engine.reducedMotion = true;
+ const random = seeded(3);
+ let chosen = 0;
+ for (let i = 0; i < 300; i++) {
+ const id = engine.chooseLife(60_000, random);
+ if (id == null) {
+ continue;
+ }
+ const def = ACTION_BY_ID.get(id);
+ expect(def).toBeDefined();
+ expect(def?.moves).toBe(false);
+ chosen += 1;
+ }
+ expect(chosen).toBeGreaterThan(0);
+ });
+
+ it('is not busy before the user has typed', () => {
+ const { engine } = setup();
+ expect(engine.attention(100)).not.toBe('busy');
+ expect(engine.chooseLife(100, seeded())).not.toBeNull();
+ });
+
+ it('only walks to the pointer once there is one', () => {
+ const { engine } = setup();
+ const toPointer = (id: string | null) =>
+ ACTION_BY_ID.get(id ?? '')?.steps.some(([, spec]) => spec.m?.to === 'pointer') ?? false;
+ const picks = (seed: number) => {
+ const random = seeded(seed);
+ return Array.from({ length: 2000 }, () => engine.chooseLife(60_000, random));
+ };
+ expect(picks(5).some(toPointer)).toBe(false);
+ engine.pointer = { x: 300, y: 380 };
+ expect(picks(5).some(toPointer)).toBe(true);
+ });
+
+ it('holds a step while there is no layout and plays it once Lia is visible', () => {
+ let platform: typeof PLATFORM | null = null;
+ const engine = new LiaEngine(
+ document.createElement('canvas'),
+ { platform: () => platform, onBubble: () => undefined },
+ 0,
+ );
+ engine.life = false;
+ engine.play('feel-happy', 2, 0);
+ run(engine, 0, 3000);
+ platform = PLATFORM;
+ /* Its first step lasts at least 1.5 s; a clock that kept running would end it at once. */
+ run(engine, 3016, 4000);
+ expect(engine.current?.id).toBe('feel-happy');
+ });
+
+ it('resumes the screen where it was after layout comes back', () => {
+ const progress: number[] = [];
+ const draw = jest.spyOn(body, 'drawScreen');
+ draw.mockImplementation((_c, name, _t, p) => {
+ if (name === 'boot') {
+ progress.push(p);
+ }
+ });
+ let platform: typeof PLATFORM | null = PLATFORM;
+ const engine = new LiaEngine(
+ document.createElement('canvas'),
+ { platform: () => platform, onBubble: () => undefined },
+ 0,
+ );
+ engine.life = false;
+ engine.play('intro', 4, 0);
+ run(engine, 0, 1104);
+ const before = progress[progress.length - 1];
+ platform = null;
+ run(engine, 1120, 5000);
+ platform = PLATFORM;
+ run(engine, 5008, 5008);
+ draw.mockRestore();
+ expect(before).toBeLessThan(0.2);
+ expect(progress[progress.length - 1]).toBeLessThan(0.2);
+ });
+
+ it('keeps a glance that started before layout went away', () => {
+ const faces: string[] = [];
+ const draw = jest.spyOn(body, 'drawScreen');
+ draw.mockImplementation((_c, _name, _t, _p, fs) => {
+ faces.push(fs.face);
+ });
+ let platform: typeof PLATFORM | null = PLATFORM;
+ const engine = new LiaEngine(
+ document.createElement('canvas'),
+ { platform: () => platform, onBubble: () => undefined },
+ 0,
+ );
+ engine.life = false;
+ run(engine, 0, 96);
+ engine.glance('surprised', 400, undefined, engine.clock(96));
+ run(engine, 112, 160);
+ platform = null;
+ run(engine, 176, 3000);
+ platform = PLATFORM;
+ run(engine, 3008, 3008);
+ draw.mockRestore();
+ expect(faces[faces.length - 1]).toBe('surprised');
+ });
+
+ it('replaces an action without reporting a gap the host could fill', () => {
+ const labels: Array<string | null> = [];
+ const engine: LiaEngine = new LiaEngine(
+ document.createElement('canvas'),
+ {
+ platform: () => PLATFORM,
+ onBubble: () => undefined,
+ onAction: (label) => {
+ labels.push(label);
+ if (label == null) {
+ engine.play('r-crash', 3, 0);
+ }
+ },
+ },
+ 0,
+ );
+ engine.life = false;
+ engine.play('intro', 4, 0);
+ engine.play('feel-happy', 4, 0);
+ expect(labels).toEqual([
+ ACTION_BY_ID.get('intro')?.label,
+ ACTION_BY_ID.get('feel-happy')?.label,
+ ]);
+ expect(engine.current?.id).toBe('feel-happy');
+ });
+
+ it('walks an action the host starts when the previous one ends', () => {
+ let chained = false;
+ let clock = 0;
+ const engine: LiaEngine = new LiaEngine(
+ document.createElement('canvas'),
+ {
+ platform: () => PLATFORM,
+ onBubble: () => undefined,
+ onAction: (label) => {
+ if (label == null && !chained) {
+ chained = true;
+ engine.play('sit-edge', 2, clock);
+ }
+ },
+ },
+ 0,
+ );
+ engine.life = false;
+ engine.play('feel-happy', 2, 0);
+ for (; clock <= 8000; clock += 16) {
+ engine.tick(clock);
+ }
+ expect(chained).toBe(true);
+ /* The walk reaches the edge well before the nine-second move timeout would snap it there. */
+ expect(engine.position.x).toBe(PLATFORM.x1);
+ });
+
+ it('wakes from a nap the host played at a high priority', () => {
+ const { engine } = setup();
+ engine.life = false;
+ engine.play('nap', 4, 0);
+ engine.noteTyping(100);
+ expect(engine.current?.id).toBe('r-wake');
+ });
+
+ it('escalates from petting to dizziness to a crash', () => {
+ const { engine } = setup();
+ engine.pet(0);
+ expect(engine.current?.id).toMatch(/^r-pet-/);
+ for (let i = 1; i < 5; i++) {
+ engine.pet(i * 100);
+ }
+ expect(engine.current?.id).toBe('r-dizzy');
+ for (let i = 5; i < 8; i++) {
+ engine.pet(i * 100);
+ }
+ expect(engine.current?.id).toBe('r-crash');
+ });
+
+ it('wakes from a nap only on a deliberate action', () => {
+ const { engine } = setup();
+ engine.play('nap', 1, 0);
+ engine.noteActivity(false, 100);
+ expect(engine.current?.id).toBe('nap');
+ engine.noteActivity(true, 200);
+ expect(engine.current?.id).toBe('r-wake');
+ });
+
+ it('wakes from a nap when the user starts typing', () => {
+ const { engine } = setup();
+ engine.play('nap', 1, 0);
+ engine.noteTyping(100);
+ expect(engine.current?.id).toBe('r-wake');
+ });
+
+ it('wakes from a nap on a click without turning it into a pet', () => {
+ const { engine } = setup();
+ engine.play('nap', 1, 0);
+ engine.pet(100);
+ expect(engine.current?.id).toBe('r-wake');
+ });
+
+ it('runs a single animation loop however often it is started', () => {
+ const { engine } = setup();
+ const raf = jest.spyOn(window, 'requestAnimationFrame').mockImplementation(() => 7);
+ const cancel = jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => undefined);
+ engine.start();
+ engine.start();
+ expect(raf).toHaveBeenCalledTimes(1);
+ engine.stop();
+ expect(cancel).toHaveBeenCalledWith(7);
+ engine.start();
+ expect(raf).toHaveBeenCalledTimes(2);
+ });
+
+ it('stays stopped when a host callback stops it mid-frame', () => {
+ const frames: FrameRequestCallback[] = [];
+ jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => {
+ frames.push(cb);
+ return frames.length;
+ });
+ jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => undefined);
+ const canvas = document.createElement('canvas');
+ const engine: LiaEngine = new LiaEngine(
+ canvas,
+ { platform: () => PLATFORM, onBubble: () => undefined, onFrame: () => engine.stop() },
+ 0,
+ );
+ engine.start();
+ frames[0](16);
+ expect(frames).toHaveLength(1);
+ });
+
+ it('keeps one animation loop when a host callback restarts the engine mid-frame', () => {
+ const frames: FrameRequestCallback[] = [];
+ jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => {
+ frames.push(cb);
+ return frames.length;
+ });
+ jest.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => undefined);
+ let restart = true;
+ const engine: LiaEngine = new LiaEngine(
+ document.createElement('canvas'),
+ {
+ platform: () => PLATFORM,
+ onBubble: () => undefined,
+ onFrame: () => {
+ if (restart) {
+ restart = false;
+ engine.stop();
+ engine.start();
+ }
+ },
+ },
+ 0,
+ );
+ engine.start();
+ frames[0](16);
+ /* The first frame scheduled only the restarted loop, not a second copy of its own. */
+ expect(frames).toHaveLength(2);
+ frames[1](32);
+ expect(frames).toHaveLength(3);
+ });
+
+ it('plays every step of an action the host starts from onAction', () => {
+ let chained = false;
+ const engine: LiaEngine = new LiaEngine(
+ document.createElement('canvas'),
+ {
+ platform: () => PLATFORM,
+ onBubble: () => undefined,
+ onAction: (label) => {
+ if (label && !chained) {
+ chained = true;
+ engine.play('feel-happy', 4, 0);
+ }
+ },
+ },
+ 0,
+ );
+ engine.life = false;
+ engine.play('intro', 4, 0);
+ expect(engine.current?.id).toBe('feel-happy');
+ /* Its first step lasts at least 1.5 s; skipping it would end the action within 0.3 s. */
+ run(engine, 0, 1000);
+ expect(engine.current?.id).toBe('feel-happy');
+ });
+
+ it('does the first move of a reduced-motion action where it snapped, before any frame', () => {
+ const { engine } = setup();
+ engine.life = false;
+ engine.reducedMotion = true;
+ engine.play('sit-edge', 2, 0);
+ expect(engine.position.x).toBe(PLATFORM.x1);
+ run(engine, 0, 100);
+ expect(engine.position.x).toBe(PLATFORM.x1);
+ });
+
+ it('finishes a walk at once when reduced motion turns on', () => {
+ const { engine } = setup();
+ engine.life = false;
+ engine.play('travel-walk', 2, 0);
+ run(engine, 0, 200);
+ engine.reducedMotion = true;
+ const at = engine.position.x;
+ run(engine, 200, 1200);
+ expect(engine.position.x).toBe(at);
+ expect(engine.current?.id).not.toBe('travel-walk');
+ });
+
+ it('finishes a walk at the platform edge when the platform shrinks under it', () => {
+ jest.spyOn(Math, 'random').mockReturnValue(0.99);
+ const platform = { ...PLATFORM };
+ const engine = new LiaEngine(
+ document.createElement('canvas'),
+ { platform: () => platform, onBubble: () => undefined },
+ 0,
+ );
+ engine.life = false;
+ engine.play('travel-walk', 2, 0);
+ run(engine, 0, 200);
+ platform.x1 = 300;
+ run(engine, 200, 1200);
+ expect(engine.position.x).toBe(300);
+ expect(engine.current).toBeNull();
+ });
+
+ it('walks once the page has a layout when an action starts before it', () => {
+ jest.spyOn(Math, 'random').mockReturnValue(0.99);
+ let ready = false;
+ const engine = new LiaEngine(
+ document.createElement('canvas'),
+ { platform: () => (ready ? PLATFORM : null), onBubble: () => undefined },
+ 0,
+ );
+ engine.life = false;
+ engine.play('travel-walk', 2, 0);
+ run(engine, 0, 100);
+ ready = true;
+ run(engine, 100, 400);
+ const start = engine.position.x;
+ run(engine, 400, 6000);
+ expect(engine.position.x).toBeGreaterThan(start);
+ expect(engine.position.x).toBeCloseTo(PLATFORM.x0 + 0.99 * (PLATFORM.x1 - PLATFORM.x0));
+ });
+
+ it('reports speech bubbles to the host and clears them', () => {
+ const { engine, bubbles } = setup();
+ engine.life = false;
+ engine.play('r-hello', 2, 0);
+ run(engine, 0, 2500);
+ expect(bubbles).toContainEqual({ say: 'hi' });
+ expect(bubbles[bubbles.length - 1]).toBeNull();
+ });
+
+ it('positions the canvas on the platform', () => {
+ const { engine, canvas } = setup();
+ engine.life = false;
+ run(engine, 0, 100);
+ expect(engine.position.y).toBe(PLATFORM.y);
+ expect(engine.position.x).toBeGreaterThanOrEqual(PLATFORM.x0);
+ expect(engine.position.x).toBeLessThanOrEqual(PLATFORM.x1);
+ expect(canvas.style.transform).toMatch(/^translate\(/);
+ });
+});
diff --git a/client/src/components/Lia/__tests__/span.spec.ts b/client/src/components/Lia/__tests__/span.spec.ts
new file mode 100644
index 0000000000..e1287b703f
--- /dev/null
+++ b/client/src/components/Lia/__tests__/span.spec.ts
@@ -0,0 +1,41 @@
+import { freeSpan } from '../engine/span';
+
+describe('freeSpan', () => {
+ it('returns the whole range when nothing blocks it', () => {
+ expect(freeSpan(0, 100, [], 50)).toEqual([0, 100]);
+ });
+
+ it('keeps the free stretch Lia is standing in', () => {
+ expect(freeSpan(0, 100, [[40, 60]], 80)).toEqual([60, 100]);
+ expect(freeSpan(0, 100, [[40, 60]], 10)).toEqual([0, 40]);
+ });
+
+ it('falls back to the widest stretch when Lia stands on a blocked part', () => {
+ expect(freeSpan(0, 100, [[20, 40]], 30)).toEqual([40, 100]);
+ });
+
+ it('drops stretches narrower than the minimum and reports when none is left', () => {
+ expect(freeSpan(0, 100, [[10, 95]], 0, 20)).toBeNull();
+ expect(freeSpan(0, 100, [[10, 70]], 0, 20)).toEqual([70, 100]);
+ });
+
+ it('handles several overlapping obstacles', () => {
+ expect(
+ freeSpan(
+ 0,
+ 100,
+ [
+ [10, 30],
+ [25, 50],
+ [80, 120],
+ ],
+ 60,
+ ),
+ ).toEqual([50, 80]);
+ });
+
+ it('ignores empty obstacles and reads reversed ones the right way round', () => {
+ expect(freeSpan(0, 100, [[50, 50]], 20, 60)).toEqual([0, 100]);
+ expect(freeSpan(0, 100, [[60, 40]], 80)).toEqual([60, 100]);
+ });
+});
diff --git a/client/src/components/Lia/engine/catalog.ts b/client/src/components/Lia/engine/catalog.ts
new file mode 100644
index 0000000000..0ed2297eb9
--- /dev/null
+++ b/client/src/components/Lia/engine/catalog.ts
@@ -0,0 +1,431 @@
+import type { ActionDef, Category, LabelKey, MoveStyle, SayKey, Step, StepSpec } from './types';
+import { EXPRESSIONS } from './face';
+
+type Options = Partial<Pick<ActionDef, 'weight' | 'tags' | 'noAuto'>>;
+
+const actions: ActionDef[] = [];
+
+function add(
+ id: string,
+ label: LabelKey,
+ cat: Category,
+ steps: readonly Step[],
+ opts: Options = {},
+) {
+ actions.push({
+ id,
+ label,
+ cat,
+ steps,
+ weight: opts.weight ?? 1,
+ tags: opts.tags ?? [],
+ noAuto: opts.noAuto ?? cat === 'react',
+ moves: steps.some(([, spec]) => spec.m != null),
+ });
+}
+const step = (dur: Step[0], spec: StepSpec = {}): Step => [dur, spec];
+
+/* ---------- Feelings: one per expression ---------- */
+for (const ex of EXPRESSIONS) {
+ add(`feel-${ex.key}`, ex.label, 'emote', [
+ step([1500, 2300], { f: ex.key, a: ex.arms ?? 'none', o: ex.pose ?? null }),
+ step(300),
+ ]);
+}
+
+/* ---------- Speech ---------- */
+const SAYINGS: ReadonlyArray<readonly [SayKey, LabelKey, string, string?]> = [
+ ['hi', 'com_ui_lia_act_say_hi', 'happy', 'wave'],
+ ['hmm', 'com_ui_lia_act_say_hmm', 'thinking', 'scratch'],
+ ['haha', 'com_ui_lia_act_say_haha', 'laughing'],
+ ['yay', 'com_ui_lia_act_say_yay', 'joyful', 'cheer'],
+ ['oops', 'com_ui_lia_act_say_oops', 'embarrassed', 'facepalm'],
+ ['brb', 'com_ui_lia_act_say_brb', 'neutral', 'salute'],
+ ['gg', 'com_ui_lia_act_say_gg', 'proud', 'thumbs'],
+ ['wow', 'com_ui_lia_act_say_wow', 'amazed'],
+ ['beep', 'com_ui_lia_act_say_beep', 'deadpan', 'robot'],
+ ['okay', 'com_ui_lia_act_say_okay', 'wink', 'thumbs'],
+];
+for (const [say, label, f, a] of SAYINGS) {
+ add(`say-${say}`, label, 'say', [
+ step(1800, { b: { say }, f, a: a ?? 'none' }),
+ step(300, { b: null }),
+ ]);
+}
+add('say-dots', 'com_ui_lia_act_say_dots', 'say', [
+ step(1800, { b: { symbol: '...' }, f: 'deadpan' }),
+ step(300, { b: null }),
+]);
+add('say-love', 'com_ui_lia_act_say_love', 'say', [
+ step(1800, { b: { symbol: '♥' }, f: 'love', a: 'hug' }),
+ step(300, { b: null }),
+]);
+
+/* ---------- Gestures ---------- */
+const GESTURES: ReadonlyArray<
+ readonly [arm: string, label: LabelKey, face: string, pose?: string]
+> = [
+ ['wave', 'com_ui_lia_act_gesture_wave', 'happy'],
+ ['waveL', 'com_ui_lia_act_gesture_waveL', 'happy'],
+ ['waveBoth', 'com_ui_lia_act_gesture_waveBoth', 'joyful', 'bounce'],
+ ['cheer', 'com_ui_lia_act_gesture_cheer', 'excited', 'bounce'],
+ ['clap', 'com_ui_lia_act_gesture_clap', 'joyful'],
+ ['shrug', 'com_ui_lia_act_gesture_shrug', 'deadpan'],
+ ['pointL', 'com_ui_lia_act_gesture_pointL', 'curious'],
+ ['pointR', 'com_ui_lia_act_gesture_pointR', 'curious'],
+ ['pointUp', 'com_ui_lia_act_gesture_pointUp', 'amazed'],
+ ['pointDown', 'com_ui_lia_act_gesture_pointDown', 'curious'],
+ ['hips', 'com_ui_lia_act_gesture_hips', 'proud', 'tiptoe'],
+ ['facepalm', 'com_ui_lia_act_gesture_facepalm', 'embarrassed'],
+ ['scratch', 'com_ui_lia_act_gesture_scratch', 'confused'],
+ ['stretch', 'com_ui_lia_act_gesture_stretch', 'content', 'stretchUp'],
+ ['flex', 'com_ui_lia_act_gesture_flex', 'determined'],
+ ['hug', 'com_ui_lia_act_gesture_hug', 'love'],
+ ['salute', 'com_ui_lia_act_gesture_salute', 'determined'],
+ ['thumbs', 'com_ui_lia_act_gesture_thumbs', 'wink'],
+ ['thumbsBoth', 'com_ui_lia_act_gesture_thumbsBoth', 'joyful'],
+ ['rub', 'com_ui_lia_act_gesture_rub', 'sleepy'],
+ ['cover', 'com_ui_lia_act_gesture_cover', 'shy'],
+ ['conduct', 'com_ui_lia_act_gesture_conduct', 'focused', 'sway'],
+ ['airguitar', 'com_ui_lia_act_gesture_airguitar', 'excited', 'headbang'],
+ ['jazz', 'com_ui_lia_act_gesture_jazz', 'starstruck', 'bounce'],
+ ['box', 'com_ui_lia_act_gesture_box', 'determined', 'stomp'],
+ ['swim', 'com_ui_lia_act_gesture_swim', 'happy', 'float'],
+ ['drum', 'com_ui_lia_act_gesture_drum', 'excited'],
+ ['reach', 'com_ui_lia_act_gesture_reach', 'hopeful', 'tiptoe'],
+ ['tpose', 'com_ui_lia_act_gesture_tpose', 'deadpan'],
+ ['pray', 'com_ui_lia_act_gesture_pray', 'zen'],
+ ['knock', 'com_ui_lia_act_gesture_knock', 'curious'],
+ ['fan', 'com_ui_lia_act_gesture_fan', 'tired'],
+ ['bow', 'com_ui_lia_act_gesture_bow', 'proud', 'bow'],
+ ['shield', 'com_ui_lia_act_gesture_shield', 'scared', 'crouch'],
+ ['catch', 'com_ui_lia_act_gesture_catch', 'focused', 'crouch'],
+];
+for (const [a, label, f, o] of GESTURES) {
+ add(`gesture-${a}`, label, 'gesture', [
+ step([1500, 2200], { a, f, o: o ?? null }),
+ step(300, { a: 'none', o: null }),
+ ]);
+}
+
+/* ---------- Dances ---------- */
+add('dance-wiggle', 'com_ui_lia_act_dance_wiggle', 'dance', [
+ step(3000, { a: 'jazz', o: 'wiggle', f: 'joyful', ft: 'tap' }),
+]);
+add('dance-robot', 'com_ui_lia_act_dance_robot', 'dance', [
+ step(3200, { a: 'robot', o: 'robot', f: 'deadpan', b: { say: 'beep' } }),
+]);
+add('dance-disco', 'com_ui_lia_act_dance_disco', 'dance', [
+ step(3200, { a: 'disco', o: 'disco', f: 'cool' }),
+]);
+add('dance-twist', 'com_ui_lia_act_dance_twist', 'dance', [
+ step(3000, { a: 'out', o: 'twist', f: 'happy', ft: 'slide' }),
+]);
+add(
+ 'dance-headbang',
+ 'com_ui_lia_act_dance_headbang',
+ 'dance',
+ [step(2800, { a: 'airguitar', o: 'headbang', f: 'excited' })],
+ {
+ tags: ['energetic'],
+ },
+);
+add('dance-spin', 'com_ui_lia_act_dance_spin', 'dance', [
+ step(1800, { a: 'cheer', o: 'spin', f: 'joyful' }),
+ step(500, { f: 'dizzy', a: 'none', o: 'dizzy' }),
+]);
+add(
+ 'dance-cartwheel',
+ 'com_ui_lia_act_dance_cartwheel',
+ 'dance',
+ [
+ step(900, { a: 'tpose', o: 'flip', f: 'excited' }),
+ step(600, { a: 'cheer', o: null, f: 'proud' }),
+ ],
+ { tags: ['energetic'] },
+);
+add('dance-macarena', 'com_ui_lia_act_dance_macarena', 'dance', [
+ step(4500, { a: 'macarena', o: 'sway', f: 'happy' }),
+]);
+add('dance-ballet', 'com_ui_lia_act_dance_ballet', 'dance', [
+ step(1200, { a: 'stretch', o: 'tiptoe', f: 'zen' }),
+ step(1400, { o: 'spin' }),
+ step(600, { a: 'bow', o: 'bow', f: 'proud' }),
+]);
+add('dance-victory', 'com_ui_lia_act_dance_victory', 'dance', [
+ step(2600, { a: 'cheer', o: 'hop', f: 'excited' }),
+]);
+add('dance-moonwalk', 'com_ui_lia_act_dance_moonwalk', 'dance', [
+ step('move', { m: { to: 'near', style: 'moonwalk' }, a: 'disco', f: 'cool' }),
+ step(600, { a: 'salute', f: 'proud' }),
+]);
+add('dance-tap', 'com_ui_lia_act_dance_tap', 'dance', [
+ step(2800, { a: 'hips', ft: 'tap', f: 'happy', o: 'bounce' }),
+]);
+add(
+ 'dance-jacks',
+ 'com_ui_lia_act_dance_jacks',
+ 'dance',
+ [
+ step(3000, { a: 'jacks', o: 'hop', f: 'determined' }),
+ step(600, { a: 'none', o: null, f: 'tired' }),
+ ],
+ { tags: ['energetic'] },
+);
+
+/* ---------- Travel ---------- */
+const SLOW: ReadonlySet<MoveStyle> = new Set(['tiptoe', 'sneak', 'shuffle', 'wobble', 'moonwalk']);
+const TRAVEL: ReadonlyArray<readonly [MoveStyle, LabelKey]> = [
+ ['walk', 'com_ui_lia_act_travel_walk'],
+ ['tiptoe', 'com_ui_lia_act_travel_tiptoe'],
+ ['run', 'com_ui_lia_act_travel_run'],
+ ['hop', 'com_ui_lia_act_travel_hop'],
+ ['moonwalk', 'com_ui_lia_act_travel_moonwalk'],
+ ['skip', 'com_ui_lia_act_travel_skip'],
+ ['sneak', 'com_ui_lia_act_travel_sneak'],
+ ['march', 'com_ui_lia_act_travel_march'],
+ ['slide', 'com_ui_lia_act_travel_slide'],
+ ['wobble', 'com_ui_lia_act_travel_wobble'],
+ ['shuffle', 'com_ui_lia_act_travel_shuffle'],
+ ['dash', 'com_ui_lia_act_travel_dash'],
+];
+function travelFace(style: MoveStyle) {
+ if (style === 'sneak') {
+ return 'mischievous';
+ }
+ return style === 'run' || style === 'dash' ? 'determined' : 'happy';
+}
+for (const [style, label] of TRAVEL) {
+ const fast = style === 'run' || style === 'dash';
+ add(
+ `travel-${style}`,
+ label,
+ 'travel',
+ [
+ step('move', {
+ m: { to: SLOW.has(style) ? 'near' : 'random', style },
+ f: travelFace(style),
+ }),
+ step(400, { f: null }),
+ ],
+ { weight: style === 'walk' ? 3 : 1, tags: fast || style === 'hop' ? ['energetic'] : [] },
+ );
+}
+add('travel-teleport', 'com_ui_lia_act_travel_teleport', 'travel', [
+ step(400, { o: 'collapse', s: 'off' }),
+ step('move', { m: { to: 'random', style: 'teleport' } }),
+ step(400, { o: 'expand' }),
+ step(600, { o: null, s: 'face', f: 'surprised' }),
+]);
+add('travel-explore', 'com_ui_lia_act_travel_explore', 'travel', [
+ step('move', { m: { to: 'random', style: 'walk' }, f: 'curious' }),
+ step(1800, { o: 'lookAround' }),
+ step(300, { o: null }),
+]);
+
+/* ---------- Life ---------- */
+add('stretch-yawn', 'com_ui_lia_act_stretch_yawn', 'life', [
+ step(1200, { a: 'stretch', f: 'sleepy', o: 'stretchUp' }),
+ step(800, { a: 'none', o: null, f: 'content' }),
+]);
+add('sneeze', 'com_ui_lia_act_sneeze', 'life', [
+ step(900, { f: 'sneezy', o: 'sneeze' }),
+ step(250, { f: 'hurt', s: 'glitch' }),
+ step(700, { s: 'face', o: null, f: 'embarrassed' }),
+]);
+add('peekaboo', 'com_ui_lia_act_peekaboo', 'life', [
+ step(1300, { a: 'cover', f: 'giggle', look: 'pointer' }),
+ step(600, { a: 'none', f: 'joyful', b: { say: 'boo' } }),
+ step(700, { b: null, f: 'laughing' }),
+]);
+add('hide-peek', 'com_ui_lia_act_hide_peek', 'life', [
+ step(800, { o: 'hide', f: 'mischievous' }),
+ step(2200, { o: 'hidden' }),
+ step(700, { o: 'emerge', f: 'excited', b: { say: 'boo' } }),
+ step(600, { o: null, b: null, f: 'laughing' }),
+]);
+add(
+ 'sit-edge',
+ 'com_ui_lia_act_sit_edge',
+ 'life',
+ [
+ step('move', { m: { to: 'right', style: 'walk' } }),
+ step(4200, { o: 'sit', ft: 'hidden', f: 'dreamy', look: 'up' }),
+ step(400, { o: null, ft: 'stand' }),
+ ],
+ { tags: ['rest'] },
+);
+add(
+ 'nap',
+ 'com_ui_lia_act_nap',
+ 'life',
+ [
+ step(900, { f: 'sleepy', a: 'rub' }),
+ step(12000, { a: 'none', f: 'asleep', o: 'sit' }),
+ step(10000, { s: 'saver' }),
+ step(900, { s: 'face', o: 'stretchUp', a: 'stretch', f: 'content' }),
+ ],
+ { tags: ['sleepy', 'rest'] },
+);
+add('trip', 'com_ui_lia_act_trip', 'life', [
+ step('move', { m: { to: 'near', style: 'walk' } }),
+ step(400, { o: 'fall', f: 'startled' }),
+ step(1000, { o: 'lie', f: 'dazed' }),
+ step(500, { o: 'getup', f: 'embarrassed' }),
+]);
+add('look-around', 'com_ui_lia_act_look_around', 'life', [
+ step(2600, { o: 'lookAround', f: 'curious' }),
+]);
+add('hum', 'com_ui_lia_act_hum', 'life', [step(3200, { f: 'whistling', o: 'sway', ft: 'tap' })]);
+add('reboot', 'com_ui_lia_act_reboot', 'life', [
+ step(600, { s: 'off' }),
+ step(1500, { s: 'boot' }),
+ step(600, { f: 'happy', s: 'face' }),
+]);
+add('shake-dust', 'com_ui_lia_act_shake_dust', 'life', [
+ step(900, { o: 'shake', f: 'laughing' }),
+ step(400, { o: null, f: 'content' }),
+]);
+add('hiccups', 'com_ui_lia_act_hiccups', 'life', [
+ step(500, { f: 'neutral' }),
+ step(300, { o: 'jump', f: 'surprised', b: { symbol: '!' } }),
+ step(600, { o: null, f: 'neutral', b: null }),
+ step(300, { o: 'jump', f: 'surprised', b: { symbol: '!' } }),
+ step(700, { o: null, f: 'annoyed', b: null }),
+]);
+add('stare-contest', 'com_ui_lia_act_stare_contest', 'life', [
+ step(2600, { f: 'focused', look: 'pointer' }),
+ step(300, { f: 'blushing' }),
+ step(800, { f: 'pouting' }),
+]);
+add('high-five', 'com_ui_lia_act_high_five', 'life', [
+ step(1200, { a: 'reach', f: 'hopeful', look: 'pointer' }),
+ step(700, { a: 'cheer', f: 'joyful' }),
+]);
+add('come-closer', 'com_ui_lia_act_come_closer', 'life', [
+ step('move', { m: { to: 'pointer', style: 'walk' }, look: 'pointer' }),
+ step(1500, { a: 'wave', f: 'happy', look: 'pointer', b: { say: 'hi' } }),
+ step(200, { b: null }),
+]);
+add('sneak-pointer', 'com_ui_lia_act_sneak_pointer', 'life', [
+ step('move', { m: { to: 'pointer', style: 'sneak' }, f: 'mischievous', look: 'pointer' }),
+ step(700, { b: { say: 'boo' }, f: 'excited', o: 'jump' }),
+ step(500, { b: null, o: null }),
+]);
+add(
+ 'bounce-around',
+ 'com_ui_lia_act_bounce_around',
+ 'life',
+ [step(2400, { o: 'hop', f: 'joyful' })],
+ {
+ tags: ['energetic'],
+ },
+);
+add(
+ 'screensaver',
+ 'com_ui_lia_act_screensaver',
+ 'life',
+ [step(6000, { s: 'saver' }), step(300, { s: 'face' })],
+ {
+ tags: ['rest'],
+ },
+);
+
+/* ---------- Reactions to the page ---------- */
+const react = (id: string, label: LabelKey, steps: readonly Step[]) =>
+ add(id, label, 'react', steps);
+react('intro', 'com_ui_lia_act_intro', [
+ step(300, { o: 'hidden', s: 'off' }),
+ step(650, { o: 'emerge' }),
+ step(1500, { o: null, s: 'boot' }),
+ step(900, { s: 'face', f: 'happy', a: 'wave' }),
+]);
+react('r-focus', 'com_ui_lia_act_r_focus', [
+ step(1000, { f: 'curious', look: 'caret', a: 'wave' }),
+]);
+react('r-type-fast', 'com_ui_lia_act_r_type_fast', [
+ step(1800, { f: 'excited', a: 'type', o: 'bounce', look: 'caret' }),
+]);
+react('r-paste', 'com_ui_lia_act_r_paste', [
+ step(300, { f: 'startled', o: 'jump' }),
+ step(1200, { o: null, f: 'amazed', a: 'catch' }),
+]);
+react('r-long', 'com_ui_lia_act_r_long', [
+ step(1600, { f: 'shocked', b: { say: 'wow' } }),
+ step(900, { b: null, f: 'determined', a: 'flex' }),
+]);
+react('r-question', 'com_ui_lia_act_r_question', [
+ step(1800, { f: 'thinking', a: 'scratch', b: { symbol: '?' } }),
+ step(200, { b: null }),
+]);
+react('r-exclaim', 'com_ui_lia_act_r_exclaim', [
+ step(1100, { f: 'excited', o: 'jump', b: { symbol: '!' } }),
+ step(200, { b: null }),
+]);
+react('r-hello', 'com_ui_lia_act_r_hello', [
+ step(1600, { a: 'waveBoth', f: 'joyful', b: { say: 'hi' } }),
+ step(200, { b: null }),
+]);
+react('r-thanks', 'com_ui_lia_act_r_thanks', [step(1400, { a: 'bow', o: 'bow', f: 'blushing' })]);
+react('r-name', 'com_ui_lia_act_r_name', [
+ step(1800, { f: 'starstruck', a: 'jazz', b: { say: 'thatsMe' }, o: 'bounce' }),
+ step(200, { b: null }),
+]);
+react('r-love', 'com_ui_lia_act_r_love', [
+ step(1800, { f: 'love', a: 'hug', b: { symbol: '♥' } }),
+ step(200, { b: null }),
+]);
+react('r-drag', 'com_ui_lia_act_r_drag', [
+ step(20000, { s: 'file', a: 'reachBoth', o: 'bounce', b: { say: 'ready' } }),
+]);
+react('r-drop', 'com_ui_lia_act_r_drop', [
+ step(300, { a: 'catch', f: 'startled', b: null }),
+ step(1300, { f: 'chewing' }),
+ step(500, { f: 'happy' }),
+]);
+react('r-send', 'com_ui_lia_act_r_send', [
+ step(500, { s: 'loading', f: 'focused' }),
+ step(700, { s: 'heart', a: 'wave' }),
+ step(600, { o: 'hide', a: 'wave' }),
+ step(5000, { o: 'hidden' }),
+]);
+react('r-drag-cancel', 'com_ui_lia_act_r_drag_cancel', [
+ step(1100, { f: 'sad', a: 'shrug', b: null }),
+]);
+react('r-dark', 'com_ui_lia_act_r_dark', [step(2600, { f: 'cool', a: 'thumbs' })]);
+react('r-light', 'com_ui_lia_act_r_light', [
+ step(1500, { f: 'annoyed', a: 'shield' }),
+ step(500, { f: 'happy', a: 'none' }),
+]);
+react('r-hover', 'com_ui_lia_act_r_hover', [step(1600, { f: 'content', o: 'wiggle' })]);
+react('r-near', 'com_ui_lia_act_r_near', [step(1200, { f: 'shy', a: 'hug' })]);
+react('r-shake', 'com_ui_lia_act_r_shake', [step(1600, { f: 'dizzy', o: 'dizzy' })]);
+const PETS: ReadonlyArray<readonly [string, string | null]> = [
+ ['giggle', 'wiggle'],
+ ['blushing', null],
+ ['catface', 'wiggle'],
+ ['joyful', 'spin'],
+ ['wink', null],
+ ['laughing', 'shake'],
+];
+PETS.forEach(([f, o], i) => react(`r-pet-${i}`, 'com_ui_lia_act_pet', [step(1000, { f, o })]));
+react('r-dizzy', 'com_ui_lia_act_r_dizzy', [step(1700, { f: 'dazed', o: 'dizzy' })]);
+react('r-crash', 'com_ui_lia_act_r_crash', [
+ step(1900, { s: 'bsod' }),
+ step(1500, { s: 'boot' }),
+ step(700, { s: 'face', f: 'embarrassed' }),
+]);
+react('r-welcome', 'com_ui_lia_act_r_welcome', [
+ step(1800, { a: 'waveBoth', f: 'joyful', b: { say: 'hi' } }),
+ step(200, { b: null }),
+]);
+react('r-squish', 'com_ui_lia_act_r_squish', [step(800, { o: 'squash', f: 'startled' })]);
+react('r-wake', 'com_ui_lia_act_r_wake', [
+ step(600, { f: 'startled', o: 'jump', b: { symbol: '!' }, s: 'face' }),
+ step(900, { o: null, f: 'sleepy', a: 'rub', b: null }),
+]);
+react('r-select', 'com_ui_lia_act_r_select', [step(1300, { f: 'surprised', a: 'shrug' })]);
+
+export const ACTIONS: readonly ActionDef[] = actions;
+export const ACTION_BY_ID: ReadonlyMap<string, ActionDef> = new Map(actions.map((a) => [a.id, a]));
+export const PET_COUNT = PETS.length;
diff --git a/client/src/components/Lia/engine/engine.ts b/client/src/components/Lia/engine/engine.ts
new file mode 100644
index 0000000000..27479dad69
--- /dev/null
+++ b/client/src/components/Lia/engine/engine.ts
@@ -0,0 +1,721 @@
+import type {
+ Duration,
+ ActionDef,
+ Bubble,
+ LabelKey,
+ Category,
+ Channels,
+ Look,
+ MoveStyle,
+ MoveTarget,
+ Platform,
+ Point,
+ StepSpec,
+} from './types';
+import type { FeetMode, PoseOffset } from './body';
+import {
+ BX,
+ BY,
+ SX,
+ SY,
+ ARMS,
+ POSES,
+ GRID_W,
+ GRID_H,
+ FOOT_Y,
+ HEAD_ROWS,
+ drawArm,
+ drawBody,
+ drawFeet,
+ drawScreen,
+} from './body';
+import { SCREEN_W, SCREEN_H, EXPRESSION_BY_KEY } from './face';
+import { ACTIONS, ACTION_BY_ID, PET_COUNT } from './catalog';
+import { C, rect, clamp, toggle } from './pixels';
+
+/** What the engine needs from the page around it. */
+export interface LiaHost {
+ /** The surface Lia stands on, or null while the page has no layout yet. */
+ platform(): Platform | null;
+ /** Called when the speech bubble changes. */
+ onBubble(bubble: Bubble | null): void;
+ /** Called when Lia starts or stops an action, with its label key. */
+ onAction?(label: LabelKey | null): void;
+ /** Called every frame with the point above Lia's head, for positioning the bubble. */
+ onFrame?(head: Point): void;
+}
+
+export type Attention = 'busy' | 'present' | 'idle';
+
+interface MoveStyleSpec {
+ speed: number;
+ feet: FeetMode;
+ bob?: number;
+ hop?: number;
+ tilt?: number;
+ pose?: string;
+}
+
+const STYLES: Readonly<Record<MoveStyle, MoveStyleSpec>> = {
+ walk: { speed: 22, feet: 'walk', bob: 1 },
+ tiptoe: { speed: 10, feet: 'tiptoe', pose: 'tiptoe' },
+ run: { speed: 70, feet: 'run', bob: 1, tilt: 8 },
+ hop: { speed: 36, feet: 'stand', hop: 7 },
+ moonwalk: { speed: 16, feet: 'slide', tilt: -6 },
+ skip: { speed: 30, feet: 'walk', hop: 4 },
+ sneak: { speed: 11, feet: 'walk', pose: 'crouch' },
+ march: { speed: 18, feet: 'march', bob: 1 },
+ slide: { speed: 46, feet: 'stand', tilt: 6 },
+ wobble: { speed: 16, feet: 'walk', pose: 'wobble' },
+ shuffle: { speed: 8, feet: 'slide' },
+ dash: { speed: 150, feet: 'run', tilt: 12 },
+ teleport: { speed: 0, feet: 'stand' },
+};
+
+/** How strongly each kind of activity is preferred when Lia picks for herself. */
+const CATEGORY_WEIGHT: Readonly<Partial<Record<Category, number>>> = {
+ life: 30,
+ emote: 10,
+ say: 7,
+ gesture: 8,
+ dance: 6,
+ travel: 12,
+};
+const BIG: ReadonlySet<Category> = new Set(['life', 'dance', 'travel']);
+/** Actions that walk to the pointer; without a pointer they would address nobody. */
+const TO_POINTER: ReadonlySet<string> = new Set(
+ ACTIONS.filter((def) => def.steps.some(([, spec]) => spec.m?.to === 'pointer')).map(
+ (def) => def.id,
+ ),
+);
+/** A step that waits for a move gives up and snaps after this long, so a slow style never drags. */
+const MOVE_TIMEOUT_MS = 9000;
+const TYPING_QUIET_MS = 2500;
+const PRESENT_MS = 12000;
+
+interface Run {
+ def: ActionDef;
+ prio: number;
+ index: number;
+ ch: Channels;
+ stepStart: number;
+ stepDur: number;
+ waitMove: boolean;
+ moved: boolean;
+ /** A move waiting for the page to have a layout. */
+ pending: NonNullable<StepSpec['m']> | null;
+ poseStart: number;
+}
+
+interface Overlay extends Channels {
+ start: number;
+ until: number;
+}
+
+interface Move {
+ tx: number;
+ style: MoveStyle;
+ start: number;
+ done: () => void;
+}
+
+export class LiaEngine {
+ /** Screen pixels per grid pixel. */
+ scale = 2;
+ /** Whether Lia chooses activities on her own. */
+ life = true;
+ pointer: Point | null = null;
+ caret: Point | null = null;
+ readonly mood = { energy: 0.85, joy: 0.6 };
+
+ private reduced = false;
+ private gx = 0;
+ private gy = 0;
+ private placed = false;
+ private move: Move | null = null;
+ private landAt = -1e9;
+ private pose: Required<PoseOffset> = { ox: 0, oy: 0, rot: 0, sx: 1, sy: 1 };
+ private run: Run | null = null;
+ private overlay: Overlay | null = null;
+ private nextLifeAt = 0;
+ private recent: string[] = [];
+ private screen = { name: 'face', start: 0, dur: 1000 };
+ private expression = { name: 'neutral', start: 0 };
+ private blinkUntil = 0;
+ private nextBlink = 0;
+ private lastDraw = 0;
+ private lastFrame = 0;
+ /** Wall time spent without a layout; Lia's own clock stands still for it. */
+ private hidden = 0;
+ private bubble: Bubble | null = null;
+ /** Unset until the user first types, so a fresh engine is never busy. */
+ private typingUntil = -Infinity;
+ private lastUser = 0;
+ private clicks: number[] = [];
+ private petIndex = 0;
+ private frameId = 0;
+ /** The running animation loop; a frame from an older loop never schedules another. */
+ private loop: object | null = null;
+ private readonly ctx: CanvasRenderingContext2D | null;
+
+ constructor(
+ private readonly canvas: HTMLCanvasElement,
+ private readonly host: LiaHost,
+ now = performance.now(),
+ ) {
+ canvas.width = GRID_W;
+ canvas.height = GRID_H;
+ this.ctx = canvas.getContext('2d');
+ this.lastUser = now;
+ this.lastFrame = now;
+ }
+
+ start() {
+ if (this.loop) {
+ return;
+ }
+ const token = {};
+ this.loop = token;
+ const loop = (now: number) => {
+ this.tick(now);
+ /* A host callback may have stopped, or stopped and restarted, the engine this frame. */
+ if (this.loop === token) {
+ this.frameId = requestAnimationFrame(loop);
+ }
+ };
+ this.frameId = requestAnimationFrame(loop);
+ }
+
+ stop() {
+ cancelAnimationFrame(this.frameId);
+ this.frameId = 0;
+ this.loop = null;
+ }
+
+ get reducedMotion() {
+ return this.reduced;
+ }
+
+ /** Turning reduced motion on finishes any walk in progress at once instead of animating it. */
+ set reducedMotion(value: boolean) {
+ this.reduced = value;
+ if (value && this.move) {
+ const move = this.move;
+ this.gx = move.tx;
+ this.move = null;
+ move.done();
+ }
+ }
+
+ /** The action playing now, if any. */
+ get current(): ActionDef | null {
+ return this.run?.def ?? null;
+ }
+
+ get position(): Point {
+ return { x: this.gx, y: this.gy };
+ }
+
+ attention(now = this.clock()): Attention {
+ if (now < this.typingUntil + TYPING_QUIET_MS) {
+ return 'busy';
+ }
+ if (now - this.lastUser < PRESENT_MS) {
+ return 'present';
+ }
+ return 'idle';
+ }
+
+ /**
+ * Something the user did on the page; keeps Lia's bigger routines for when they step away.
+ * A `deliberate` action (a click, typing, a drop) also wakes Lia from a nap.
+ */
+ noteActivity(deliberate = false, now = this.clock()) {
+ this.lastUser = now;
+ if (deliberate && this.run?.def.id === 'nap') {
+ /* A nap the host played above reaction priority still wakes. */
+ this.play('r-wake', Math.max(3, this.run.prio), now);
+ }
+ }
+
+ noteTyping(now = this.clock()) {
+ this.typingUntil = now + 1500;
+ /* Typing is deliberate: it wakes a napping Lia rather than cancelling the nap. */
+ this.noteActivity(true, now);
+ if (this.run && this.run.prio <= 1 && (this.run.def.moves || BIG.has(this.run.def.cat))) {
+ this.stopRun();
+ this.nextLifeAt = now + this.gap(now);
+ }
+ }
+
+ /** Plays an action unless something more important is running. Higher `prio` wins. */
+ play(id: string, prio = 2, now = this.clock()): boolean {
+ const def = ACTION_BY_ID.get(id);
+ if (!def || (this.run && this.run.prio > prio)) {
+ return false;
+ }
+ /* Replaced without a null notification: onAction below reports the new action, and a host
+ reacting to null here could start an action this call would then overwrite. */
+ this.run = null;
+ this.move = null;
+ const run: Run = {
+ def,
+ prio,
+ index: -1,
+ ch: { s: 'face', a: 'none', ft: 'stand' },
+ stepStart: now,
+ stepDur: 0,
+ waitMove: false,
+ moved: false,
+ pending: null,
+ poseStart: now,
+ };
+ this.run = run;
+ this.recent.push(id);
+ if (this.recent.length > 30) {
+ this.recent.shift();
+ }
+ this.host.onAction?.(def.label);
+ /* The host may have played another action from onAction; that run has already started. */
+ if (this.run === run) {
+ this.nextStep(now);
+ }
+ return true;
+ }
+
+ /** A short change of face (and optionally gaze or arms) that does not interrupt the current action. */
+ glance(face: string, ms: number, look?: Look, now = this.clock()) {
+ this.overlay = { f: face, look, start: now, until: now + ms };
+ }
+
+ /** Clicking Lia: a pet, then dizziness, then a crash for the persistent. */
+ pet(now = this.clock()) {
+ if (this.run?.def.id === 'nap') {
+ /* A click on a napping Lia wakes her; the wake-up is the whole reaction. */
+ this.noteActivity(true, now);
+ return;
+ }
+ this.noteActivity(true, now);
+ if (this.run?.def.id === 'r-crash') {
+ return;
+ }
+ this.clicks = this.clicks.filter((t) => now - t < 1800);
+ this.clicks.push(now);
+ this.mood.joy = clamp(this.mood.joy + 0.06, 0, 1);
+ if (this.clicks.length >= 8) {
+ this.clicks = [];
+ this.play('r-crash', 3, now);
+ } else if (this.clicks.length >= 5) {
+ this.play('r-dizzy', 3, now);
+ } else {
+ this.play(`r-pet-${this.petIndex++ % PET_COUNT}`, 3, now);
+ }
+ }
+
+ /** Lia's time: wall time minus the time she spent without a layout. */
+ clock(wall = performance.now()) {
+ return wall - this.hidden;
+ }
+
+ tick(wall: number) {
+ const elapsed = Math.max(0, wall - this.lastFrame);
+ this.lastFrame = wall;
+ const platform = this.host.platform();
+ if (!platform) {
+ /* Her clock stops, so steps, screens, glances and walks resume where they left off. */
+ this.hidden += elapsed;
+ return;
+ }
+ const now = this.clock(wall);
+ const dt = clamp(elapsed / 1000, 0, 0.05);
+ this.place(platform);
+ this.updateRun(now);
+ this.updateLife(dt, now);
+ this.updateMotion(dt, now, platform);
+ this.render(now);
+ }
+
+ /** Picks the next activity Lia does on her own, or null when nothing fits right now. */
+ chooseLife(now = this.clock(), random = Math.random): string | null {
+ const attention = this.attention(now);
+ if (attention === 'busy') {
+ return null;
+ }
+ const pools = new Map<Category, Array<[ActionDef, number]>>();
+ for (const def of ACTIONS) {
+ const base = CATEGORY_WEIGHT[def.cat];
+ if (base == null || def.noAuto || (this.reducedMotion && def.moves)) {
+ continue;
+ }
+ if (!this.pointer && TO_POINTER.has(def.id)) {
+ continue;
+ }
+ let w = def.weight;
+ if (this.recent.includes(def.id)) {
+ w *= 0.03;
+ }
+ if (def.tags.includes('sleepy')) {
+ w *= this.mood.energy < 0.35 ? 6 : 0.2;
+ }
+ if (def.tags.includes('energetic')) {
+ w *= this.mood.energy > 0.5 ? 1.4 : 0.2;
+ }
+ const big = def.moves || BIG.has(def.cat);
+ if (big && attention === 'present') {
+ w *= 0.06;
+ } else if (big && attention === 'idle') {
+ w *= 2;
+ }
+ const pool = pools.get(def.cat) ?? [];
+ pool.push([def, w]);
+ pools.set(def.cat, pool);
+ }
+ const categories = [...pools.keys()].map((cat): [Category, number] => {
+ let sum = 0;
+ let baseSum = 0;
+ for (const [def, w] of pools.get(cat) ?? []) {
+ sum += w;
+ baseSum += def.weight;
+ }
+ return [cat, (CATEGORY_WEIGHT[cat] ?? 0) * clamp(baseSum ? sum / baseSum : 1, 0.03, 8)];
+ });
+ const cat = weighted(categories, random);
+ const def = cat ? weighted(pools.get(cat) ?? [], random) : null;
+ return def?.id ?? null;
+ }
+
+ private gap(now: number) {
+ return this.attention(now) === 'idle'
+ ? 1800 + Math.random() * 2700
+ : 6000 + Math.random() * 6000;
+ }
+
+ private stopRun() {
+ /* Cleared first: the host may start a replacement, with its own move, from onAction(null). */
+ this.move = null;
+ if (this.run) {
+ this.run = null;
+ this.host.onAction?.(null);
+ }
+ }
+
+ private nextStep(now: number) {
+ const run = this.run;
+ if (!run) {
+ return;
+ }
+ run.index += 1;
+ const steps = run.def.steps;
+ if (run.index >= steps.length) {
+ const prio = run.prio;
+ this.stopRun();
+ this.nextLifeAt = now + (prio >= 2 ? 2500 + Math.random() * 2500 : this.gap(now));
+ return;
+ }
+ const [dur, spec] = steps[run.index];
+ const prevPose = run.ch.o;
+ const { m, ...channels } = spec;
+ Object.assign(run.ch, channels);
+ if (run.ch.o !== prevPose || spec.o !== undefined) {
+ run.poseStart = now;
+ }
+ run.stepStart = now;
+ run.stepDur = stepDuration(dur);
+ run.waitMove = m != null;
+ run.moved = false;
+ run.pending = null;
+ if (m) {
+ this.startMove(m, run, now);
+ }
+ }
+
+ /** Puts Lia on her first platform; a move resolved before her first frame starts from here. */
+ private place(platform: Platform) {
+ if (!this.placed) {
+ this.gx = platform.x0 + (platform.x1 - platform.x0) * 0.75;
+ this.placed = true;
+ }
+ }
+
+ private startMove(m: NonNullable<StepSpec['m']>, run: Run, now: number) {
+ const platform = this.host.platform();
+ if (!platform) {
+ run.pending = m;
+ return;
+ }
+ run.pending = null;
+ this.place(platform);
+ const tx = clamp(this.resolveTarget(m.to, platform), platform.x0, platform.x1);
+ const style = m.style ?? 'walk';
+ if (this.reducedMotion || style === 'teleport') {
+ this.gx = tx;
+ run.moved = true;
+ return;
+ }
+ this.move = { tx, style, start: now, done: () => (run.moved = true) };
+ }
+
+ private resolveTarget(to: MoveTarget, p: Platform) {
+ const S = this.scale;
+ switch (to) {
+ case 'left':
+ return p.x0;
+ case 'right':
+ return p.x1;
+ case 'pointer':
+ return this.pointer?.x ?? this.gx;
+ case 'near':
+ return this.gx + (Math.random() < 0.5 ? -1 : 1) * (35 + Math.random() * 35) * S;
+ default:
+ return p.x0 + Math.random() * (p.x1 - p.x0);
+ }
+ }
+
+ private updateRun(now: number) {
+ const run = this.run;
+ if (!run) {
+ return;
+ }
+ if (run.pending) {
+ /* The layout arrived: the step starts now, with its move. */
+ run.stepStart = now;
+ this.startMove(run.pending, run, now);
+ }
+ if (run.waitMove && !run.moved && now - run.stepStart > MOVE_TIMEOUT_MS && this.move) {
+ this.gx = this.move.tx;
+ this.move = null;
+ run.moved = true;
+ }
+ const elapsed = now - run.stepStart;
+ const done = run.waitMove
+ ? run.moved && (run.stepDur < 0 || elapsed >= run.stepDur)
+ : elapsed >= run.stepDur;
+ if (done) {
+ this.nextStep(now);
+ }
+ }
+
+ private updateLife(dt: number, now: number) {
+ this.mood.energy = clamp(this.mood.energy - dt / 600, 0, 1);
+ this.mood.joy += (0.55 - this.mood.joy) * dt * 0.01;
+ if (this.run?.def.tags.includes('rest')) {
+ this.mood.energy = clamp(this.mood.energy + dt / 25, 0, 1);
+ }
+ if (!this.life || this.run || now < this.nextLifeAt) {
+ return;
+ }
+ const id = this.chooseLife(now);
+ if (id) {
+ this.play(id, 1, now);
+ } else {
+ this.nextLifeAt = now + 1000;
+ }
+ }
+
+ private updateMotion(dt: number, now: number, platform: Platform) {
+ const S = this.scale;
+ const pose = { ox: 0, oy: 0, rot: 0, sx: 1, sy: 1 };
+ this.gy = platform.y;
+ const move = this.move;
+ if (move) {
+ const style = STYLES[move.style];
+ /* The platform can shrink mid-walk; aim for the nearest end Lia can still reach. */
+ move.tx = clamp(move.tx, platform.x0, platform.x1);
+ const d = move.tx - this.gx;
+ const stepPx = style.speed * S * dt * (this.mood.energy < 0.3 ? 0.7 : 1);
+ if (Math.abs(d) <= stepPx) {
+ this.gx = move.tx;
+ this.move = null;
+ this.landAt = now;
+ move.done();
+ } else {
+ this.gx += Math.sign(d) * stepPx;
+ const t = now - move.start;
+ if (style.hop) {
+ pose.oy -= Math.round(Math.abs(Math.sin(t / 170)) * style.hop);
+ }
+ if (style.bob) {
+ pose.oy -= toggle(t, 150);
+ }
+ if (style.tilt) {
+ pose.rot += Math.sign(d) * style.tilt;
+ }
+ if (style.pose) {
+ Object.assign(pose, POSES[style.pose]?.(t, 0));
+ }
+ }
+ }
+ this.gx = clamp(this.gx, platform.x0, platform.x1);
+
+ if (!this.reducedMotion) {
+ const overlay = this.overlay && now < this.overlay.until ? this.overlay : null;
+ const name = overlay?.o ?? this.run?.ch.o;
+ const fn = name ? POSES[name] : undefined;
+ if (fn) {
+ const run = this.run;
+ const fromOverlay = overlay?.o != null;
+ const t = fromOverlay && overlay ? now - overlay.start : now - (run?.poseStart ?? now);
+ let p = 0.5;
+ if (fromOverlay && overlay) {
+ p = clamp((now - overlay.start) / (overlay.until - overlay.start), 0, 1);
+ } else if (run && run.stepDur > 0) {
+ p = clamp((now - run.stepStart) / run.stepDur, 0, 1);
+ }
+ const v = fn(t, p);
+ pose.ox += v.ox ?? 0;
+ pose.oy += v.oy ?? 0;
+ pose.rot += v.rot ?? 0;
+ pose.sx *= v.sx ?? 1;
+ pose.sy *= v.sy ?? 1;
+ }
+ const since = now - this.landAt;
+ if (since < 110) {
+ pose.sy *= 0.88;
+ pose.sx *= 1.1;
+ } else if (since < 385) {
+ pose.rot += [-4, 3, -2, 1, 0][Math.min(4, Math.floor((since - 110) / 55))];
+ }
+ }
+ this.pose = pose;
+ const left = Math.round((this.gx - 32 * S + pose.ox * S) / S) * S;
+ const top = Math.round((this.gy - FOOT_Y * S + pose.oy * S) / S) * S;
+ this.canvas.style.transform = `translate(${left}px, ${top}px) rotate(${pose.rot}deg) scale(${pose.sx}, ${pose.sy})`;
+ this.host.onFrame?.({ x: this.gx, y: this.gy - HEAD_ROWS * S + pose.oy * S });
+ }
+
+ private lookAt(look: Look | null | undefined): { dx: number; dy: number } {
+ const S = this.scale;
+ const face = { x: this.gx, y: this.gy - 26 * S };
+ let target: Point | null = null;
+ switch (look) {
+ case 'caret':
+ target = this.caret;
+ break;
+ case 'up':
+ target = { x: face.x, y: face.y - 100 * S };
+ break;
+ case 'down':
+ target = { x: face.x, y: face.y + 100 * S };
+ break;
+ case 'left':
+ target = { x: face.x - 100 * S, y: face.y };
+ break;
+ case 'right':
+ target = { x: face.x + 100 * S, y: face.y };
+ break;
+ default:
+ target = this.pointer;
+ }
+ if (!target) {
+ return { dx: 0, dy: 0 };
+ }
+ return {
+ dx: clamp(Math.round((target.x - face.x) / (40 * S)), -1, 1),
+ dy: clamp(Math.round((target.y - face.y) / (40 * S)), -1, 1),
+ };
+ }
+
+ private moodFace() {
+ if (this.mood.energy < 0.3) {
+ return 'sleepy';
+ }
+ return this.mood.joy > 0.85 ? 'content' : 'neutral';
+ }
+
+ private render(now: number) {
+ const c = this.ctx;
+ if (!c || now - this.lastDraw < 60) {
+ return;
+ }
+ this.lastDraw = now;
+ const t = this.reducedMotion ? 900 : now;
+ const overlay = this.overlay && now < this.overlay.until ? this.overlay : null;
+ const ch: Channels = this.run?.ch ?? { s: 'face', a: 'none', ft: 'stand' };
+ const moodFace = this.moodFace();
+ const face = overlay?.f ?? ch.f ?? moodFace;
+ if (face !== this.expression.name) {
+ this.expression = { name: face, start: now };
+ }
+ const screen = ch.s ?? 'face';
+ if (screen !== this.screen.name) {
+ const dur = this.run && this.run.stepDur > 0 ? this.run.stepDur : 1000;
+ this.screen = { name: screen, start: now, dur };
+ }
+ const typing = now < this.typingUntil;
+ const look = this.lookAt(overlay?.look ?? ch.look ?? (typing ? 'caret' : 'pointer'));
+ if (now > this.nextBlink) {
+ this.blinkUntil = now + 130;
+ this.nextBlink = now + 2400 + Math.random() * 3200;
+ }
+
+ c.clearRect(0, 0, GRID_W, GRID_H);
+ let led: string = Math.floor(now / 1400) % 3 !== 2 ? C.LED : C.LO;
+ if (screen === 'bsod') {
+ led = C.VEIN;
+ }
+ drawBody(c, led);
+ c.save();
+ c.translate(SX, SY);
+ c.beginPath();
+ c.rect(0, 0, SCREEN_W, SCREEN_H);
+ c.clip();
+ const screenT = this.reducedMotion ? 900 : now - this.screen.start;
+ drawScreen(c, screen, screenT, clamp((now - this.screen.start) / this.screen.dur, 0, 1), {
+ face: EXPRESSION_BY_KEY.has(face) ? face : 'neutral',
+ elapsed: now - this.expression.start,
+ dx: look.dx,
+ dy: look.dy,
+ feather: true,
+ t,
+ blink: !this.reducedMotion && now < this.blinkUntil,
+ });
+ for (let y = 1; y < SCREEN_H; y += 2) {
+ rect(c, 0, y, SCREEN_W, 1, 'rgba(0,0,0,0.09)');
+ }
+ c.restore();
+ drawFeet(c, this.move ? STYLES[this.move.style].feet : (ch.ft ?? 'stand'), t);
+ const armName = overlay?.a ?? ch.a ?? 'none';
+ const armT = this.reducedMotion ? 400 : now - (this.run?.poseStart ?? now);
+ const progress =
+ this.run && this.run.stepDur > 0
+ ? clamp((now - this.run.stepStart) / this.run.stepDur, 0, 1)
+ : 0;
+ const arms = (ARMS[armName] ?? ARMS.none)(armT, progress);
+ drawArm(c, BX - 1, BY + 15, arms.l);
+ drawArm(c, BX + 40, BY + 15, arms.r);
+
+ const bubble = ch.b ?? null;
+ if (bubble !== this.bubble) {
+ this.bubble = bubble;
+ this.host.onBubble(bubble);
+ }
+ }
+}
+
+function stepDuration(dur: Duration) {
+ if (dur === 'move') {
+ return -1;
+ }
+ if (typeof dur === 'number') {
+ return dur;
+ }
+ return dur[0] + Math.random() * (dur[1] - dur[0]);
+}
+
+function weighted<T>(items: ReadonlyArray<readonly [T, number]>, random: () => number): T | null {
+ let total = 0;
+ for (const [, w] of items) {
+ total += w;
+ }
+ if (total <= 0) {
+ return null;
+ }
+ let r = random() * total;
+ for (const [item, w] of items) {
+ r -= w;
+ if (r <= 0) {
+ return item;
+ }
+ }
+ return items[items.length - 1][0];
+}
diff --git a/client/src/components/Lia/engine/span.ts b/client/src/components/Lia/engine/span.ts
new file mode 100644
index 0000000000..cbdf720585
--- /dev/null
+++ b/client/src/components/Lia/engine/span.ts
@@ -0,0 +1,45 @@
+/**
+ * The part of [x0, x1] that no blocked range covers, preferring the free stretch that contains
+ * `near` (where Lia already stands) and otherwise the widest one. Returns null when nothing at
+ * least `minWidth` wide is left.
+ */
+export function freeSpan(
+ x0: number,
+ x1: number,
+ blocked: ReadonlyArray<readonly [number, number]>,
+ near: number,
+ minWidth = 0,
+): readonly [number, number] | null {
+ let spans: Array<[number, number]> = [[x0, x1]];
+ for (const [a, b] of blocked) {
+ const b0 = Math.min(a, b);
+ const b1 = Math.max(a, b);
+ /* An empty range covers nothing, so it splits nothing. */
+ if (b1 <= b0) {
+ continue;
+ }
+ const next: Array<[number, number]> = [];
+ for (const [s0, s1] of spans) {
+ if (b1 <= s0 || b0 >= s1) {
+ next.push([s0, s1]);
+ continue;
+ }
+ if (b0 > s0) {
+ next.push([s0, b0]);
+ }
+ if (b1 < s1) {
+ next.push([b1, s1]);
+ }
+ }
+ spans = next;
+ }
+ const usable = spans.filter(([s0, s1]) => s1 - s0 >= minWidth);
+ if (usable.length === 0) {
+ return null;
+ }
+ const containing = usable.find(([s0, s1]) => near >= s0 && near <= s1);
+ if (containing) {
+ return containing;
+ }
+ return usable.reduce((best, span) => (span[1] - span[0] > best[1] - best[0] ? span : best));
+}
diff --git a/client/src/components/Lia/engine/types.ts b/client/src/components/Lia/engine/types.ts
new file mode 100644
index 0000000000..ee9a2fd65d
--- /dev/null
+++ b/client/src/components/Lia/engine/types.ts
@@ -0,0 +1,93 @@
+import type { FeetMode } from './body';
+
+export interface Point {
+ x: number;
+ y: number;
+}
+
+/** A surface Lia can stand on, in the stage's pixel coordinates. */
+export interface Platform {
+ y: number;
+ x0: number;
+ x1: number;
+}
+
+export type SayKey =
+ | 'hi'
+ | 'hmm'
+ | 'haha'
+ | 'yay'
+ | 'oops'
+ | 'brb'
+ | 'gg'
+ | 'wow'
+ | 'beep'
+ | 'okay'
+ | 'boo'
+ | 'thatsMe'
+ | 'ready';
+
+/** Words are localized by the host; symbols are drawn as they are. */
+export type Bubble = { say: SayKey } | { symbol: '?' | '!' | '...' | '♥' | 'Zzz' };
+
+export type Look = 'pointer' | 'caret' | 'up' | 'down' | 'left' | 'right';
+
+export type MoveStyle =
+ | 'walk'
+ | 'tiptoe'
+ | 'run'
+ | 'hop'
+ | 'moonwalk'
+ | 'skip'
+ | 'sneak'
+ | 'march'
+ | 'slide'
+ | 'wobble'
+ | 'shuffle'
+ | 'dash'
+ | 'teleport';
+
+export type MoveTarget = 'random' | 'near' | 'left' | 'right' | 'pointer';
+
+/** What Lia shows during a step. Values persist into later steps until changed. */
+export interface Channels {
+ /** Expression key; `null` falls back to Lia's mood. */
+ f?: string | null;
+ /** Screen program; `face` shows the face. */
+ s?: string;
+ /** Arm pose. */
+ a?: string;
+ /** Body pose; `null` clears it. */
+ o?: string | null;
+ ft?: FeetMode;
+ b?: Bubble | null;
+ look?: Look | null;
+}
+
+export interface StepSpec extends Channels {
+ /** Walk somewhere; the step lasts until Lia arrives. */
+ m?: { to: MoveTarget; style?: MoveStyle };
+}
+
+/** A step lasts a fixed time, a random time in a range, or until a move finishes. */
+export type Duration = number | readonly [number, number] | 'move';
+export type Step = readonly [Duration, StepSpec];
+
+/** Translation key for something Lia does, shown when hovering her. */
+export type LabelKey = `com_ui_lia_${string}`;
+
+export type Category = 'life' | 'emote' | 'say' | 'gesture' | 'dance' | 'travel' | 'react';
+
+export interface ActionDef {
+ id: string;
+ label: LabelKey;
+ cat: Category;
+ steps: readonly Step[];
+ /** Relative weight within its category when Lia chooses for herself. */
+ weight: number;
+ tags: readonly ('sleepy' | 'energetic' | 'rest')[];
+ /** Played only in response to something, never chosen at random. */
+ noAuto: boolean;
+ /** Moves across the platform, so reduced motion and a busy user skip it. */
+ moves: boolean;
+}
diff --git a/e2e/specs/mock/scenarios/lia-engine.spec.ts b/e2e/specs/mock/scenarios/lia-engine.spec.ts
new file mode 100644
index 0000000000..d37ccbcb51
--- /dev/null
+++ b/e2e/specs/mock/scenarios/lia-engine.spec.ts
@@ -0,0 +1,119 @@
+import { expect, test } from '@playwright/test';
+import type { Page } from '@playwright/test';
+import type * as Catalog from '../../../../client/src/components/Lia/engine/catalog';
+import type * as Engine from '../../../../client/src/components/Lia/engine/engine';
+import { openLiaEngine } from '../lia.helpers';
+
+/**
+ * Lia's behavior engine, running on real animation frames and a real canvas. Nothing mounts it
+ * yet, so each test serves the engine modules to a blank page and drives it as a host would.
+ */
+type Lia = { engine: typeof Engine; catalog: typeof Catalog };
+
+const openEngine = (page: Page) => openLiaEngine(page, ['engine', 'catalog']);
+
+test.describe("Lia's behavior engine", () => {
+ test('Lia stays quiet while the user types and saves big routines for when they step away @scenario:lia-engine-attention-budget', async ({
+ page,
+ }) => {
+ await openEngine(page);
+ const result = await page.evaluate(async () => {
+ const { engine: mod, catalog } = (window as unknown as { lia: Lia }).lia;
+ const canvas = document.createElement('canvas');
+ document.body.appendChild(canvas);
+ const engine = new mod.LiaEngine(canvas, {
+ platform: () => ({ y: 200, x0: 100, x1: 700 }),
+ onBubble: () => undefined,
+ });
+ engine.life = false;
+ engine.start();
+ const frame = () => new Promise((resolve) => requestAnimationFrame(resolve));
+ for (let i = 0; i < 10; i++) {
+ await frame();
+ }
+ const pixels = (canvas.getContext('2d') as CanvasRenderingContext2D)
+ .getImageData(0, 0, canvas.width, canvas.height)
+ .data.some((v, i) => i % 4 === 3 && v > 0);
+
+ engine.play('travel-walk', 1);
+ await frame();
+ engine.noteTyping();
+ const afterTyping = engine.current?.id ?? null;
+ const choiceWhileTyping = engine.chooseLife();
+
+ engine.play('nap', 1);
+ engine.noteTyping();
+ const napAfterTyping = engine.current?.id ?? null;
+ engine.stop();
+
+ let s = 7;
+ const random = () => {
+ s = (s * 16807) % 2147483647;
+ return (s - 1) / 2147483646;
+ };
+ const bigShare = (now: number) => {
+ let big = 0;
+ for (let i = 0; i < 400; i++) {
+ const def = catalog.ACTION_BY_ID.get(engine.chooseLife(now, random) ?? '');
+ if (def && (def.moves || ['life', 'dance', 'travel'].includes(def.cat))) {
+ big += 1;
+ }
+ }
+ return big / 400;
+ };
+ const base = performance.now() + 10_000;
+ engine.noteActivity(false, base);
+ return {
+ pixels,
+ afterTyping,
+ choiceWhileTyping,
+ napAfterTyping,
+ present: bigShare(base),
+ away: bigShare(base + 60_000),
+ };
+ });
+ expect(result.pixels).toBe(true);
+ expect(result.afterTyping).toBeNull();
+ expect(result.choiceWhileTyping).toBeNull();
+ expect(result.napAfterTyping).toBe('r-wake');
+ expect(result.present).toBeLessThan(0.25);
+ expect(result.away).toBeGreaterThan(0.5);
+ });
+
+ test('Lia never walks under reduced motion @scenario:lia-engine-reduced-motion', async ({
+ page,
+ }) => {
+ await openEngine(page);
+ const result = await page.evaluate(async () => {
+ const { engine: mod, catalog } = (window as unknown as { lia: Lia }).lia;
+ const engine = new mod.LiaEngine(document.createElement('canvas'), {
+ platform: () => ({ y: 200, x0: 100, x1: 700 }),
+ onBubble: () => undefined,
+ });
+ engine.life = false;
+ const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+ engine.start();
+ await wait(100);
+ const before = engine.position.x;
+ engine.play('travel-run', 2);
+ await wait(150);
+ engine.reducedMotion = true;
+ const snapped = engine.position.x;
+ await wait(500);
+ const settled = engine.position.x;
+ engine.stop();
+
+ const moving: string[] = [];
+ for (let i = 0; i < 400; i++) {
+ const id = engine.chooseLife(performance.now() + 120_000);
+ if (id && catalog.ACTION_BY_ID.get(id)?.moves) {
+ moving.push(id);
+ }
+ }
+ return { before, snapped, settled, moving };
+ });
+ expect(result.snapped).not.toBe(result.before);
+ expect(result.settled).toBe(result.snapped);
+ expect(result.moving).toEqual([]);
+ });
+});