Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

6.8 KB
Raw Blame
import { Component, Suspense, useEffect, useRef, type ReactNode } from "react";
import { Canvas, useFrame, useThree } from "@react-three/fiber";
import type { Group } from "three";
import { ContactShadows, Environment, Lightformer, OrbitControls } from "@react-three/drei";
import { useReducedMotion } from "framer-motion";
import { useCanvasAwake } from "@/hooks/useCanvasAwake";
import { useWebglSurface } from "@/hooks/useWebglSurface";
import { useWebglSupported } from "@/lib/graphDimension";
import { CompanionModel, WORN_PET_LIFT_M } from "./AgentFollower";
import type { CompanionAppearance } from "./appearance";
import { useSyncCompanionPet, useWornPet } from "./companionPetStore";
import { companionFlies } from "./petCompanions";

class PreviewBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
  state = { failed: false };
  static getDerivedStateFromError() { return { failed: true }; }
  componentDidCatch(error: Error) { console.warn("Companion preview unavailable", error); }
  render() { return this.state.failed ? null : this.props.children; }
}

/** Vertical field of view of the preview camera, degrees. */
const FOV = 30;
/** How much taller than the body the frame is, so it never touches the edges. */
const MARGIN = 1.55;

/**
 * Where the camera looks and how far back it stands, so the body fills the
 * frame whatever its size: a 0.5 m shape, a 0.28 m blob or a cat that walks
 * on the floor. Every companion knows its own height, so no bounds have to
 * be measured after the model loads.
 */
export function previewFraming(heightM: number, liftM: number): { target: [number, number, number]; position: [number, number, number] } {
  const centre = liftM + heightM / 2;
  const distance = (heightM * MARGIN) / 2 / Math.tan((FOV * Math.PI) / 360);
  // A three-quarter view, slightly from above, the way a figure sits on a desk.
  return { target: [0, centre, 0], position: [distance * 0.5, centre + distance * 0.24, distance * 0.84] };
}

/**
 * A soft studio around the body: an environment lit only by local light
 * panels (no image is downloaded), a warm key that casts the shadow, a cool
 * rim that separates the silhouette from the dark dialog, and a contact
 * shadow that sets it on the ground.
 */
function Studio() {
  return <>
    <Environment resolution={256} frames={1}>
      <Lightformer form="rect" intensity={2.2} position={[2.5, 3, 2.5]} scale={[3, 3, 1]} target={[0, 0, 0]} />
      <Lightformer form="rect" intensity={0.9} color="#cfe0ff" position={[-3, 1.5, 1]} scale={[2, 3, 1]} target={[0, 0, 0]} />
      <Lightformer form="ring" intensity={1.4} position={[0, 2, -3]} scale={2} target={[0, 0, 0]} />
    </Environment>
    <ambientLight intensity={0.25} />
    <directionalLight position={[1.6, 2.6, 1.8]} intensity={1.7} color="#fff4e6" castShadow
      shadow-mapSize={[1024, 1024]} shadow-bias={-0.0004} />
    <directionalLight position={[-1.8, 1.4, -2.2]} intensity={1.1} color="#b9d4ff" />
  </>;
}

type Framing = ReturnType<typeof previewFraming>;

/**
 * Moves the camera when the body changes. The canvas itself stays: building
 * a new one per pet would drop the old WebGL context, which the surface
 * reads as a lost GPU and stops drawing.
 */
function Frame({ framing }: { framing: Framing }) {
  const camera = useThree((state) => state.camera);
  const controls = useThree((state) => state.controls) as { target: { set: (x: number, y: number, z: number) => void }; update: () => void } | null;
  const invalidate = useThree((state) => state.invalidate);
  const [px, py, pz] = framing.position;
  const [tx, ty, tz] = framing.target;
  useEffect(() => {
    camera.position.set(px, py, pz);
    camera.lookAt(tx, ty, tz);
    controls?.target.set(tx, ty, tz);
    controls?.update();
    invalidate();
  }, [camera, controls, invalidate, px, py, pz, tx, ty, tz]);
  return null;
}

/**
 * A shape at rest on the table: it sways to each side, breathes, leans and
 * hops every few seconds, so the preview shows its volume and its character
 * without being turned by hand. A pet brings
 * its own life (PetModel) and is left alone.
 */
/** The preview's shape hops this often, and one hop takes this long, seconds. */
const HOP_EVERY_S = 3.4, HOP_S = 0.7;

function Idle({ enabled, heightM, children }: { enabled: boolean; heightM: number; children: ReactNode }) {
  const group = useRef<Group>(null);
  useFrame(({ clock }) => {
    const node = group.current;
    if (!node || !enabled) return;
    const t = clock.getElapsedTime();
    const breath = Math.sin(t * 1.9) * 0.02;
    // Every few seconds a hop: down into a squash, up, and a soft landing.
    const beat = (t % HOP_EVERY_S) / HOP_S;
    const air = beat < 1 ? Math.sin(beat * Math.PI) : 0;
    const squash = beat < 1 ? Math.sin(beat * Math.PI * 2) * -0.07 : 0;
    node.rotation.y = Math.sin(t * 0.8) * 0.5;
    node.rotation.z = Math.sin(t * 1.3) * 0.05;
    node.position.y = air * heightM * 0.22;
    node.scale.set(1 + breath - squash, 1 - breath + squash, 1 + breath - squash);
  });
  return <group ref={group}>{children}</group>;
}

export function CompanionPreview({ appearance, lead = false }: { appearance: CompanionAppearance; lead?: boolean }) {
  const host = useRef<HTMLDivElement>(null);
  const { generation } = useWebglSurface(host);
  const awake = useCanvasAwake(host);
  const supported = useWebglSupported();
  const reduced = useReducedMotion() ?? false;
  // A worn pet's model comes from the pets catalog, which the canvas cannot query itself.
  useSyncCompanionPet();
  const pet = useWornPet(lead ? undefined : appearance.pet);
  const framing = pet
    ? previewFraming(pet.heightM, companionFlies(pet) ? WORN_PET_LIFT_M : 0)
    : previewFraming(appearance.sizeM, 0);
  // A pet breathes, blinks and turns on the table; a shape sways and breathes. Reduced motion draws only when asked.
  const live = !reduced;
  const turning = !!pet && live;
  return <div ref={host} className="h-56 w-full touch-none" data-testid="companion-preview">
    {supported && awake && <PreviewBoundary key={generation}>
      <Canvas frameloop={live ? "always" : "demand"} shadows dpr={[1, 2]}
        camera={{ position: framing.position, fov: FOV, near: 0.02, far: 20 }} gl={{ alpha: true, antialias: true }}>
        <Studio />
        <Frame framing={framing} />
        <Suspense fallback={null}><Idle enabled={live && !pet} heightM={appearance.sizeM}><CompanionModel appearance={appearance} lead={lead} /></Idle></Suspense>
        <ContactShadows position={[0, 0.001, 0]} opacity={0.5} scale={Math.max(1, framing.target[1] * 6)} blur={2.6} far={1} resolution={512} frames={live ? Infinity : 1} />
        <OrbitControls makeDefault target={framing.target} enablePan={false} enableZoom={false} autoRotate={turning} autoRotateSpeed={0.9}
          minPolarAngle={0.25} maxPolarAngle={Math.PI * 0.55} />
      </Canvas>
    </PreviewBoundary>}
  </div>;
}