mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 04:21:24 +01:00
Refine agent details, identity editing, responsive controls, and organization charts with three card tiers, clearer save feedback, and stable filtering. Guard instruction-file renames and root configuration updates in one transaction through the agent lifecycle integration, preserving conflict checks, rollback, and configuration history. Co-Authored-By: Paperclip <noreply@paperclip.ing>
75 lines
4.7 KiB
TypeScript
75 lines
4.7 KiB
TypeScript
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type RefObject } from "react";
|
|
import { resolveAgentAppearance, type CharacterState } from "@paperclipai/shared";
|
|
import { cn } from "@/lib/utils";
|
|
import { characterSlot, orgChartCharacterSlots } from "@/lib/agent-character-slot";
|
|
import { AgentAvatar, avatarSizeClasses, type AgentAvatarProps } from "./AgentAvatar";
|
|
import type { createCharacter } from "@paperclipai/shared/cliplab/runtime";
|
|
|
|
type Player = ReturnType<typeof createCharacter>;
|
|
let characterModules: Promise<[typeof import("@paperclipai/shared/cliplab/runtime"), typeof import("@paperclipai/shared/cliplab/definition")]> | undefined;
|
|
function loadCharacterModules() {
|
|
return characterModules ??= Promise.all([import("@paperclipai/shared/cliplab/runtime"), import("@paperclipai/shared/cliplab/definition")]).catch(error => {
|
|
characterModules = undefined;
|
|
throw error;
|
|
});
|
|
}
|
|
export interface AgentCharacterProps extends Omit<AgentAvatarProps, "pose"> {
|
|
state?: CharacterState;
|
|
motion?: "auto" | "still";
|
|
trackingRegion?: RefObject<HTMLElement | null>;
|
|
trackingScope?: "region" | "page";
|
|
followCursor?: boolean;
|
|
followRotation?: boolean;
|
|
/** 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) {
|
|
const identity = useMemo(() => resolveAgentAppearance(appearance ?? agent?.appearance, agent?.id), [appearance, agent?.appearance, agent?.id]);
|
|
const root = useRef<HTMLSpanElement>(null), host = useRef<HTMLSpanElement>(null), player = useRef<Player | null>(null);
|
|
const slotId = useRef(Symbol("agent-character"));
|
|
const slots = animationGroup === "org-chart" ? orgChartCharacterSlots : characterSlot;
|
|
const owners = useSyncExternalStore(slots.subscribe, slots.getSnapshot, slots.getSnapshot);
|
|
const ownsSlot = owners.has(slotId.current);
|
|
const [visible, setVisible] = useState(false), [reduced, setReduced] = useState(true), [failed, setFailed] = useState(false), [ready, setReady] = useState(false);
|
|
const active = !identity.customAvatarAssetId && visible && !reduced && !failed && motion === "auto" && state !== "rest";
|
|
useEffect(() => {
|
|
if (typeof matchMedia !== "function" || typeof IntersectionObserver !== "function") return;
|
|
const media = matchMedia("(prefers-reduced-motion: reduce)");
|
|
const change = () => setReduced(media.matches); change(); media.addEventListener("change", change);
|
|
const observer = new IntersectionObserver(entries => setVisible(entries[0]?.isIntersecting ?? false));
|
|
if (root.current) observer.observe(root.current);
|
|
return () => { observer.disconnect(); media.removeEventListener("change", change); };
|
|
}, []);
|
|
useEffect(() => {
|
|
if (active && !ownsSlot) slots.acquire(slotId.current);
|
|
if (!active) slots.release(slotId.current);
|
|
}, [active, owners, ownsSlot, slots]);
|
|
useEffect(() => () => slots.release(slotId.current), [slots]);
|
|
useEffect(() => {
|
|
if (!active || !ownsSlot) return;
|
|
let disposed = false;
|
|
setReady(false);
|
|
void loadCharacterModules().then(([runtime, library]) => {
|
|
if (disposed || !host.current) return;
|
|
player.current = runtime.createCharacter(host.current, library.characterDefinition(identity, muted), {
|
|
animation: library.animationId(state), trackingRegion: trackingRegion?.current ?? root.current ?? undefined,
|
|
followCursor, followRotation, trackingScope, displaySize: size, onError: () => setFailed(true),
|
|
});
|
|
setReady(true);
|
|
}).catch(() => { if (!disposed) setFailed(true); });
|
|
return () => { disposed = true; player.current?.destroy(); player.current = null; setReady(false); };
|
|
}, [active, ownsSlot, trackingRegion, trackingScope, size, followCursor, followRotation]);
|
|
useEffect(() => {
|
|
if (!player.current) return;
|
|
void loadCharacterModules().then(([, library]) => {
|
|
player.current?.setDefinition(library.characterDefinition(identity, muted));
|
|
player.current?.setAnimation(library.animationId(state));
|
|
});
|
|
}, [identity, muted, state, ready]);
|
|
return <span ref={root} role={label ? "img" : undefined} aria-label={label} aria-hidden={label ? undefined : true}
|
|
className={cn("relative inline-block shrink-0", avatarSizeClasses[size], className)}>
|
|
<AgentAvatar agent={agent} appearance={identity} size={size < 256 ? 256 : size} name={name} pose={state} muted={muted} className={cn("size-full", ready && !identity.customAvatarAssetId && "invisible")} />
|
|
<span ref={host} className="absolute inset-0" />
|
|
</span>;
|
|
}
|