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.

16.0 KB
Raw Blame
/**
 * Flat bot silhouettes and eye placement adapted from Nous Research's
 * hermes-agent, apps/desktop/src/plugins/hermes-bots/avatar.tsx.
 * Source revision: 4a2bd7406ee3e09bd30a42cf9a7b970aac6edf9e.
 * Copyright (c) 2025 Nous Research. MIT license; see THIRD_PARTY_NOTICES.txt.
 *
 * Jarvis adaptation: stable ID-based appearance and state-driven CSS motion,
 * without a SDK, JavaScript frame clock, model download or WebGL in the roster.
 *
 * Where a worker is available the symbol is shown as a 3D figure instead: the
 * map companion's own meshes, drawn once on the CPU (companion/ShapeFigure).
 * The flat drawing below stays for the working dots and as the fallback.
 */

import { useId, useMemo, type ReactNode } from "react";
import { defaultCompanion, companionEyeColors, type SymbolShape } from "./companion/appearance";
import {
  ACCESSORY_CATALOG, FACE_SLOTS, partDepth, resolveFill, smoothPath, symbolViewBox, wornAccessories,
  type AccessoryChoice, type AccessoryItem, type AccessoryPart,
} from "./companion/accessories";
import type { FigureLook } from "./companion/figureScene";
import { ShapeFigure, useFiguresSupported } from "./companion/ShapeFigure";
import { SkinDefs, SkinGlow, SkinOverlay, SkinStars } from "./companion/SkinLayers";
import { SKIN_EFFECTS, skinEffect, skinHighlight, type CompanionSkin } from "./companion/skins";
import "./agentSymbol.css";
export type { SymbolShape } from "./companion/appearance";

export function symbolAppearance(identity: string): { shape: SymbolShape; color: string } {
  const { shape, color } = defaultCompanion(identity);
  return { shape, color };
}

/** The upstream superellipse sampler keeps the square soft without a bevel. */
function roundedOutline(shape: "squircle" | "pill"): string {
  const points: string[] = [];
  for (let i = 0; i < 52; i++) {
    const angle = (i / 52) * Math.PI * 2 - Math.PI / 2;
    const c = Math.cos(angle);
    const s = Math.sin(angle);
    const power = shape === "pill" ? 8 : 5;
    const divisor = Math.pow(Math.abs(c) ** power + Math.abs(s / (shape === "pill" ? 0.72 : 1)) ** power, 1 / power) || 1;
    const radius = (shape === "pill" ? 16 : 16.2) / divisor;
    points.push(`${i === 0 ? "M" : "L"}${(20 + radius * c).toFixed(2)} ${(20 + radius * s).toFixed(2)}`);
  }
  return points.join(" ") + "Z";
}

const ROUNDED_OUTLINES = { squircle: roundedOutline("squircle"), pill: roundedOutline("pill") };

/** grow > 0 widens the silhouette by that many units on every side (a hood rim). */
function SymbolBody({ shape, color, grow = 0 }: { shape: SymbolShape; color: string; grow?: number }) {
  const edge = (base: number) => base + grow * 2 > 0 ? { stroke: color, strokeWidth: base + grow * 2, strokeLinejoin: "round" as const } : {};
  switch (shape) {
    case "squircle":
    case "pill":
      return <path d={ROUNDED_OUTLINES[shape]} fill={color} {...edge(0)} />;
    case "triangle":
      return <path d="M20 5.5 L36 33.5 L4 33.5 Z" fill={color} {...edge(5)} />;
    case "hexagon":
      return <path d="M20 3.5 L34.5 11.75 L34.5 28.25 L20 36.5 L5.5 28.25 L5.5 11.75 Z" fill={color} {...edge(3)} />;
    case "cloud":
      return <path d="M11 32 a7.5 7.5 0 0 1 -1 -14.9 A9.5 9.5 0 0 1 29 12.5 A7 7 0 0 1 30 32 Z" fill={color} {...edge(0)} />;
    case "drop":
      return <path d="M20 3 C20 3 6 20 6 27 a14 13.5 0 0 0 28 0 C34 20 20 3 20 3 Z" fill={color} {...edge(0)} />;
    default:
      return <circle cx={20} cy={20} fill={color} r={16.2} {...edge(0)} />;
  }
}

/** A monochrome thinking interlude, using the agent's own identity colour. */
export function SymbolThinkingDots({ color }: { color: string }) {
  return <g className="agent-symbol-thoughts" fill={color}>
    <circle cx={10} cy={22} r={3} /><circle cx={20} cy={22} r={4} /><circle cx={30} cy={22} r={3} />
  </g>;
}

/** Drifting smoke puffs; CSS staggers them, reduced motion leaves only the still wisps. */
function SmokePuffs({ c, size, fill, id }: { c: [number, number, number]; size: number; fill: string; id: string }) {
  return <g data-smoke>
    <defs><filter id={id} x="-50%" y="-50%" width="200%" height="200%"><feGaussianBlur stdDeviation={0.45 * size} /></filter></defs>
    <g className="agent-acc-smoke" fill={fill} filter={`url(#${id})`}>
      {[0, 1, 2, 3].map(i => <circle key={i} cx={c[0]} cy={c[1]} r={1.35 * size} style={{ animationDelay: `${-i * 0.8}s` }} />)}
    </g>
  </g>;
}

/** The front projection of one accessory part in symbol units. */
function AccessoryPartShape({ part, color, gradientId }: { part: AccessoryPart; color: string; gradientId: string }): ReactNode {
  const fill = part.gradient ? `url(#${gradientId})` : resolveFill(part.fill, color);
  const spin = "c" in part && part.rot ? `rotate(${part.rot} ${part.c[0]} ${part.c[1]})` : undefined;
  switch (part.t) {
    case "sphere":
      return <ellipse cx={part.c[0]} cy={part.c[1]} rx={part.r[0]} ry={part.r[1]} fill={fill} transform={spin} />;
    case "dome": {
      const [cx, cy] = part.c; const [rx, ry] = part.r;
      return <path d={`M${cx - rx} ${cy} A${rx} ${ry} 0 0 1 ${cx + rx} ${cy}Z`} fill={fill} transform={spin} />;
    }
    case "box": {
      const [w, h] = part.s;
      return <rect x={part.c[0] - w / 2} y={part.c[1] - h / 2} width={w} height={h} rx={Math.min(part.round, w / 2, h / 2)} fill={fill} transform={spin} />;
    }
    case "cyl": {
      const [cx, cy] = part.c; const [rb, rt] = part.r; const h = part.h / 2;
      return <path d={`M${cx - rb} ${cy + h} L${cx + rb} ${cy + h} L${cx + rt} ${cy - h} L${cx - rt} ${cy - h}Z`} fill={fill} transform={spin} />;
    }
    case "torus":
      return <ellipse cx={part.c[0]} cy={part.c[1]} rx={part.R} ry={Math.max(part.R * Math.abs(Math.cos(part.tilt * Math.PI / 180)), 0.15)} fill="none" stroke={fill} strokeWidth={part.r * 2} transform={spin} />;
    case "poly":
      return part.smooth ? <path d={smoothPath(part.pts, true)} fill={fill} /> : <polygon points={part.pts.map(p => p.join(",")).join(" ")} fill={fill} />;
    case "tube": {
      const flat = part.pts.map(p => [p[0], p[1]] as [number, number]);
      const d = part.smooth ? smoothPath(flat, false) : `M${flat.map(p => p.join(" ")).join(" L")}`;
      return <path d={d} fill="none" stroke={fill} strokeWidth={part.w} strokeLinecap="round" strokeLinejoin="round" />;
    }
    case "region":
      return <polygon points={part.pts.map(p => p.join(",")).join(" ")} fill={fill} />;
    case "rim":
      return null; // Drawn behind the body by AgentSymbol itself.
    case "smoke":
      return <SmokePuffs c={part.c} size={part.size} fill={fill} id={`${gradientId}-blur`} />;
  }
}

/** A part with its flat extras: a vertical gradient, an opacity, a looping animation. */
function DecoratedPart({ part, color, gradientId }: { part: AccessoryPart; color: string; gradientId: string }) {
  let node: ReactNode = <AccessoryPartShape part={part} color={color} gradientId={gradientId} />;
  if (part.gradient) node = <>
    <defs><linearGradient id={gradientId} x1="0" y1="0" x2="0" y2="1">
      <stop offset="0" stopColor={part.gradient[0]} /><stop offset="1" stopColor={part.gradient[1]} />
    </linearGradient></defs>
    {node}
  </>;
  if (part.anim) node = <g className={`agent-acc-${part.anim}`}>{node}</g>;
  if (part.opacity !== undefined) node = <g opacity={part.opacity}>{node}</g>;
  return node;
}

/** One worn item at its slot anchor: either its body patches or its free parts. */
function AccessoryItemShapes({ item, shape, color, regions, uid }: { item: AccessoryItem; shape: SymbolShape; color: string; regions: boolean; uid: string }) {
  const parts = item.parts.filter(part => part.t !== "rim" && part.only !== "3d" && (part.t === "region") === regions);
  if (!parts.length) return null;
  const [anchorX, y, anchorK] = ACCESSORY_CATALOG.shapes[shape].anchors[item.slot];
  const k = anchorK * (item.scale ?? 1);
  const x = anchorX + (ACCESSORY_CATALOG.slotFlatShift[item.slot] ?? 0);
  // Paint back to front; body patches keep their authored layering.
  const ordered = regions ? parts : [...parts].sort((a, b) => partDepth(a) - partDepth(b));
  return <g data-accessory={item.id} transform={`translate(${x} ${y}) scale(${k})`}>
    {ordered.map((part, index) => <DecoratedPart key={index} part={part} color={color} gradientId={`${uid}-${item.id}-${index}`} />)}
  </g>;
}

/** Plain ink eyes share one resting gaze; only a working agent moves. */
/**
 * One eye that can change its expression: the open ellipse it always has,
 * plus a happy arc and a closed dash that stay hidden until a stage
 * (components/agentchat/botStage) animates between them. Each eye is its own
 * group, so the two can look, squint or wink independently.
 */
function ExpressiveEye({ side, cx, cy, rx, ry }: { side: "l" | "r"; cx: number; cy: number; rx: number; ry: number }) {
  return <g className={`ae-eye ae-${side}`}>
    <ellipse className="ae-open" cx={cx} cy={cy} rx={rx} ry={ry} />
    <path className="ae-happy" d={`M${cx - 2.6} ${cy + 1}Q${cx} ${cy - 2.9} ${cx + 2.6} ${cy + 1}`} />
    <path className="ae-shut" d={`M${cx - 2.4} ${cy}H${cx + 2.4}`} />
  </g>;
}

interface SymbolProps { shape: SymbolShape; color: string; size: number; eyes?: "dots" | "lines"; thinking?: boolean; accessories?: AccessoryChoice; expressive?: boolean; skin?: CompanionSkin }

function symbolEyeY(shape: SymbolShape): number {
  return shape === "cloud" || shape === "triangle" ? 23 : shape === "drop" ? 25 : 17.2;
}

/**
 * An agent's shape: a small 3D figure of its body, eyes and accessories, or
 * the flat symbol where figures cannot be drawn. At work the figure stays in
 * the picture: it nods over what it does, under the working bubble. Only the
 * flat symbol still turns into three dots.
 */
export function AgentSymbol(props: SymbolProps) {
  const supported = useFiguresSupported();
  if (!supported) return <FlatSymbol {...props} />;
  return <FigureSymbol {...props} />;
}

function FigureSymbol(props: SymbolProps) {
  const { shape, color, size, eyes = "lines", accessories, expressive = false, skin, thinking = false } = props;
  const id = useId().replace(/[^a-zA-Z0-9_-]/g, "");
  const worn = wornAccessories(accessories);
  // Living eyes are drawn on top of an eyeless figure; glasses cover them, so the figure keeps its own.
  const blind = !worn.some(item => item.slot === "face");
  // A stage plays its own expressions on vector eyes; everywhere else the eyes blink and glance by themselves.
  const moving = expressive && blind;
  const wornKey = worn.map(item => item.id).join(","), skinKey = skin ? JSON.stringify(skin) : "";
  const look = useMemo<FigureLook>(() => ({
    shape, color, eyes, accessories, blind,
    skin: skin && { colors: skin.colors, radial: skin.pattern === "radial", angle: skin.angle, effect: SKIN_EFFECTS.indexOf(skinEffect(skin)), highlight: skinHighlight(skin.colors) },
    // eslint-disable-next-line react-hooks/exhaustive-deps -- accessories and skin are read through their keys
  }), [shape, color, eyes, wornKey, skinKey, blind]);
  const effect = skin ? skinEffect(skin) : "none";
  const eyeY = symbolEyeY(shape);
  return <ShapeFigure look={look} size={size} shape={shape} fallback={<FlatSymbol {...props} />} working={thinking}
    eyes={blind && !moving ? { kind: eyes, y: eyeY, color: companionEyeColors(color).eye } : undefined}
    flow={skin && effect === "flow" ? { colors: skin.colors, angle: skin.angle } : undefined} shine={effect === "shimmer"}
    inside={effect === "stars" ? <SkinStars /> : undefined}
    smoke={(plume, index) => <SmokePuffs c={[0, 0, 0]} size={plume.size} fill={plume.fill} id={`${id}-smoke-${index}`} />}
    behind={skin && effect === "glow" ? <span aria-hidden className="agent-figure-halo" style={{ background: `radial-gradient(closest-side, ${skinHighlight(skin.colors)}, transparent)` }} /> : undefined}
    overlay={skin && effect === "sparkle" ? <svg aria-hidden focusable="false" viewBox="0 0 40 44" className="society-agent-symbol"><SkinOverlay id={id} skin={skin} body={() => null} /></svg> : undefined}>
    {moving && <g className="agent-symbol-gaze"><g data-agent-eyes fill={companionEyeColors(color).eye}><g className="agent-symbol-lids">
      <ExpressiveEye side="l" cx={15.2} cy={eyeY} rx={eyes === "lines" ? 1.8 : 2.4} ry={eyes === "lines" ? 3.5 : 2.7} />
      <ExpressiveEye side="r" cx={24.8} cy={eyeY} rx={eyes === "lines" ? 1.8 : 2.4} ry={eyes === "lines" ? 3.5 : 2.7} />
    </g></g></g>}
  </ShapeFigure>;
}

function FlatSymbol({ shape, color, size, eyes = "lines", thinking = false, accessories, expressive = false, skin }: SymbolProps) {
  const eyeY = symbolEyeY(shape);
  const ink = companionEyeColors(color);
  const maskId = `agent-body-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
  const worn = useMemo(() => wornAccessories(accessories), [accessories]);
  const viewBox = useMemo(() => symbolViewBox(shape, worn), [shape, worn]);
  const draw = (items: AccessoryItem[], regions = false) => items.map(item => <AccessoryItemShapes key={item.id} item={item} shape={shape} color={color} regions={regions} uid={maskId} />);
  const faceTransform = `translate(2 -0.6) rotate(-14 20 ${eyeY})`;
  const onFace = worn.filter(item => FACE_SLOTS.has(item.slot));
  // A design paints the body with its gradient; `color` stays its blend for everything else.
  const skinId = `${maskId}-skin`;
  const body = (fill: string) => <SymbolBody shape={shape} color={fill} />;
  return (
    <svg aria-hidden focusable="false" data-agent-symbol={shape} data-expressive={expressive ? "true" : undefined} data-thinking={thinking ? "true" : undefined} width={size} height={size} style={{ width: size, height: size, flexShrink: 0 }} viewBox={viewBox} className="society-agent-symbol block">
      <g className="agent-symbol-character">
        {draw(worn.filter(item => item.slot === "back"))}
        {worn.flatMap(item => item.parts.map((part, index) => {
          if (part.t !== "rim" || part.only === "3d") return null;
          // A hood frames the head only: clip the grown silhouette at maxY below the neckline.
          const [, anchorY, anchorK] = ACCESSORY_CATALOG.shapes[shape].anchors[item.slot];
          const clipId = `${maskId}-rim-${index}`;
          const bottom = part.maxY === undefined ? 90 : anchorY + part.maxY * anchorK;
          return <g key={`${item.id}-rim-${index}`} data-accessory-rim={item.id}>
            <clipPath id={clipId}><rect x={-30} y={-30} width={100} height={bottom + 30} /></clipPath>
            <g clipPath={`url(#${clipId})`}><SymbolBody shape={shape} color={resolveFill(part.fill, color)} grow={part.w} /></g>
          </g>;
        }))}
        {skin && <><SkinDefs id={skinId} skin={skin} /><SkinGlow id={skinId} skin={skin} body={body} /></>}
        <g data-agent-body data-skin={skin ? skin.effect : undefined}>{body(skin ? `url(#${skinId})` : color)}</g>
        {skin && <SkinOverlay id={skinId} skin={skin} body={body} />}
        {worn.some(item => item.regions) && <>
          <mask id={maskId} maskUnits="userSpaceOnUse" x={-20} y={-20} width={80} height={84}><SymbolBody shape={shape} color="#ffffff" /></mask>
          <g mask={`url(#${maskId})`}>{draw(worn, true)}</g>
        </>}
        {draw(worn.filter(item => item.slot === "outfit" || item.slot === "neck"))}
        <g className="agent-symbol-gaze">
          <g data-agent-eyes fill={ink.eye} transform={faceTransform}>
            <g className="agent-symbol-lids">
              {expressive ? <>
                {/* A little larger than the resting eyes: at chat size the expression must read. */}
                <ExpressiveEye side="l" cx={15.2} cy={eyeY} rx={eyes === "lines" ? 1.8 : 2.4} ry={eyes === "lines" ? 3.5 : 2.7} />
                <ExpressiveEye side="r" cx={24.8} cy={eyeY} rx={eyes === "lines" ? 1.8 : 2.4} ry={eyes === "lines" ? 3.5 : 2.7} />
              </> : <>
                <ellipse cx={15.4} cy={eyeY} rx={eyes === "lines" ? 1.45 : 2} ry={eyes === "lines" ? 3.1 : 2.3} />
                <ellipse cx={24.6} cy={eyeY} rx={eyes === "lines" ? 1.45 : 2} ry={eyes === "lines" ? 3.1 : 2.3} />
              </>}
            </g>
          </g>
          {onFace.length > 0 && <g transform={faceTransform}>{draw(onFace)}</g>}
        </g>
        {draw(worn.filter(item => item.slot === "head" || item.slot === "held"))}
      </g>
      {thinking && <SymbolThinkingDots color={color} />}
    </svg>
  );
}