diff --git a/packages/shared/src/cliplab/runtime.ts b/packages/shared/src/cliplab/runtime.ts index a197f0f0eb2..e722e7352be 100644 --- a/packages/shared/src/cliplab/runtime.ts +++ b/packages/shared/src/cliplab/runtime.ts @@ -1,7 +1,7 @@ // ClipLab runtime adapted for Paperclip; geometry/animation model remain upstream. // See PROVENANCE.md and LICENSE. import { CharacterRenderer } from './renderer.js' -import { sampleDefinition, animationDuration, type Definition } from './model.js' +import { sampleDefinition, animationDuration, faceLayers, mixFaceLayers, mixPose, type Sample, type Definition } from './model.js' import { centeredGaze, easeGaze, pointerGaze } from './gaze.js' export interface CharacterOptions { animation?: string; followCursor?: boolean; followRotation?: boolean; @@ -22,7 +22,27 @@ export function createCharacter(target: HTMLElement, definition: Definition, opt const reduced = window.matchMedia('(prefers-reduced-motion: reduce)') const coarse = window.matchMedia('(pointer: coarse)') const canRun = () => !destroyed && visible && !document.hidden && !reduced.matches - const render = () => renderer.render({ ...definition.character, followCursor: options.followCursor !== false, followRotation: options.followRotation !== false }, sampleDefinition(definition, animation, elapsed), { cursor: gaze, reducedMotion: reduced.matches }, gaze) + let transition: { from: Sample; seconds: number } | undefined + function currentSample(): Sample { + const next = sampleDefinition(definition, animation, elapsed) + if (!transition) return next + if (elapsed >= transition.seconds || reduced.matches) { + transition = undefined + return next + } + const { from, seconds } = transition + const progress = elapsed / seconds + const t = progress * progress * (3 - 2 * progress) + return { + ...next, + pose: mixPose(from.pose, next.pose, t), + faceLayers: mixFaceLayers(from.faceLayers ?? faceLayers(from.pose), next.faceLayers ?? faceLayers(next.pose), t), + blink: from.blink + (next.blink - from.blink) * t, + bob: from.bob + (next.bob - from.bob) * t, + breathe: from.breathe + (next.breathe - from.breathe) * t, + } + } + const render = () => renderer.render({ ...definition.character, followCursor: options.followCursor !== false, followRotation: options.followRotation !== false }, currentSample(), { cursor: gaze, reducedMotion: reduced.matches }, gaze) const pointer = (event: PointerEvent) => { if (event.pointerType !== 'touch') goal = pointerGaze(event, target.getBoundingClientRect()) } const leave = () => { goal = centeredGaze() } function track(enabled: boolean) { @@ -75,7 +95,13 @@ export function createCharacter(target: HTMLElement, definition: Definition, opt } catch (error) { destroy(); throw error } return { - setAnimation(id: string) { if (animation === id) return; animation = id; elapsed = 0; renderSafely() }, + setAnimation(id: string, transitionSeconds = 0) { + if (animation === id) return + transition = transitionSeconds > 0 && playing && canRun() + ? { from: currentSample(), seconds: transitionSeconds } + : undefined + animation = id; elapsed = 0; renderSafely() + }, setDefinition(value: Definition) { definition = value; renderSafely() }, seek(seconds: number) { elapsed = Math.max(0, seconds); renderSafely() }, pause() { playing = false; sync() }, diff --git a/ui/src/components/AgentCharacter.tsx b/ui/src/components/AgentCharacter.tsx index f0349a9f3bc..d3b4af04ca6 100644 --- a/ui/src/components/AgentCharacter.tsx +++ b/ui/src/components/AgentCharacter.tsx @@ -1,5 +1,6 @@ import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type RefObject } from "react"; import { resolveAgentAppearance, type CharacterState } from "@paperclipai/shared"; +import { parseCssTimeMs } from "@/components/task-chat/motion-tokens"; import { cn } from "@/lib/utils"; import { characterSlot, orgChartCharacterSlots } from "@/lib/agent-character-slot"; import { AgentAvatar, avatarSizeClasses, type AgentAvatarProps } from "./AgentAvatar"; @@ -16,6 +17,7 @@ function loadCharacterModules() { export interface AgentCharacterProps extends Omit { state?: CharacterState; motion?: "auto" | "still"; + transitionStates?: boolean; trackingRegion?: RefObject; trackingScope?: "region" | "page"; followCursor?: boolean; @@ -23,7 +25,7 @@ export interface AgentCharacterProps extends Omit { /** Org leadership cards may animate together, within a bounded canvas budget. */ animationGroup?: "hero" | "org-chart"; } -export function AgentCharacter({ agent, appearance, size = 256, state = "idle", muted = false, motion = "auto", trackingRegion, trackingScope = "region", followCursor = true, followRotation = true, animationGroup = "hero", className, label, name }: AgentCharacterProps) { +export function AgentCharacter({ agent, appearance, size = 256, state = "idle", muted = false, motion = "auto", transitionStates = false, trackingRegion, trackingScope = "region", followCursor = true, followRotation = true, animationGroup = "hero", className, label, name }: AgentCharacterProps) { const identity = useMemo(() => resolveAgentAppearance(appearance ?? agent?.appearance, agent?.id), [appearance, agent?.appearance, agent?.id]); const root = useRef(null), host = useRef(null), player = useRef(null); const slotId = useRef(Symbol("agent-character")); @@ -63,9 +65,12 @@ export function AgentCharacter({ agent, appearance, size = 256, state = "idle", if (!player.current) return; void loadCharacterModules().then(([, library]) => { player.current?.setDefinition(library.characterDefinition(identity, muted)); - player.current?.setAnimation(library.animationId(state)); + const duration = transitionStates && root.current + ? parseCssTimeMs(getComputedStyle(root.current).getPropertyValue("--motion-duration-fast")) / 1000 + : 0; + player.current?.setAnimation(library.animationId(state), duration); }); - }, [identity, muted, state, ready]); + }, [identity, muted, state, ready, transitionStates]); return diff --git a/ui/src/components/AgentCharacterRuntime.test.ts b/ui/src/components/AgentCharacterRuntime.test.ts index f240df5cb15..c39139e6519 100644 --- a/ui/src/components/AgentCharacterRuntime.test.ts +++ b/ui/src/components/AgentCharacterRuntime.test.ts @@ -83,3 +83,19 @@ it("can follow the whole page while measuring gaze from the character and cleani player.destroy(); expect(remove.mock.calls.some(([event]) => event === "pointermove")).toBe(true); }); + +it("blends from the displayed pose and can reverse a sleep transition mid-frame", () => { + const player = create(); + intersect([{ isIntersecting: true }]); + const pose = () => renderer.render.mock.calls.at(-1)![1].pose; + const awake = pose(); + player.setAnimation("sleepy", 0.16); + expect(pose()).toEqual(awake); + const [id, frame] = [...frames][0]; + frames.delete(id); + frame(performance.now() + 80); + const midway = pose(); + expect(midway).not.toEqual(awake); + player.setAnimation("idle", 0.16); + expect(pose()).toEqual(midway); +}); diff --git a/ui/src/pages/AgentDetail.tsx b/ui/src/pages/AgentDetail.tsx index fbf422e7abc..8de653c3925 100644 --- a/ui/src/pages/AgentDetail.tsx +++ b/ui/src/pages/AgentDetail.tsx @@ -876,7 +876,7 @@ export function AgentDetail() { {/* Header */}
- + {sleeping && !agent.appearance?.customAvatarAssetId && }