Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: pet improve with state syncing

Gus351848ecb9a0
diff --git a/src/web/src/app/(app)/w/[slug]/home/page.tsx b/src/web/src/app/(app)/w/[slug]/home/page.tsx
index 49d192a0d..9f7d6b749 100644
--- a/src/web/src/app/(app)/w/[slug]/home/page.tsx
+++ b/src/web/src/app/(app)/w/[slug]/home/page.tsx
@@ -1,7 +1,6 @@
"use client";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
-import dynamic from "next/dynamic";
import { useRouter } from "next/navigation";
import {
ReactFlow,
@@ -49,16 +48,6 @@ import { LinkSidecar } from "@/components/canvas/link-sidecar";
import { ActiveTasksFloat } from "@/components/canvas/active-tasks-float";
import { UpcomingEventsFloat } from "@/components/canvas/upcoming-events-float";
import { getAutoLayout } from "@/components/canvas/auto-layout";
-import type { CloudCodeMonsterPetProps } from "@/components/home-pet/cloud-code-monster-pet";
-import { useHomePetSettings } from "@/lib/home-pet-settings";
-
-const CloudCodeMonsterPet = dynamic<CloudCodeMonsterPetProps>(
- () =>
- import("@/components/home-pet/cloud-code-monster-pet").then(
- (module) => module.CloudCodeMonsterPet
- ),
- { ssr: false }
-);
const nodeTypes = { agent: AgentNode };
const edgeTypes = { link: LinkEdge };
@@ -92,7 +81,6 @@ function AgentCanvas({ onAgentClick }: { onAgentClick?: (agent: Agent) => void }
const { slug, workspaceId } = useWorkspace();
const { zoomIn, zoomOut, fitView } = useReactFlow();
const canvasRef = useRef<HTMLDivElement>(null);
- const petSettings = useHomePetSettings();
const [nodes, setNodes] = useState<Node[]>([]);
const [edges, setEdges] = useState<Edge[]>([]);
@@ -381,15 +369,6 @@ function AgentCanvas({ onAgentClick }: { onAgentClick?: (agent: Agent) => void }
<Background variant={BackgroundVariant.Dots} gap={24} size={1.5} color="var(--color-border)" />
</ReactFlow>
- {petSettings.enabled ? (
- <CloudCodeMonsterPet
- boundaryRef={canvasRef}
- activityTriggerMode={
- petSettings.displayScope === "global" ? "global" : "home"
- }
- />
- ) : null}
-
{/* Custom floating toolbar */}
<div
className="absolute bottom-4 left-4 z-40 bg-background/80 backdrop-blur-sm rounded-lg ring-1 ring-foreground/5 p-1 flex gap-0.5 animate-[fade-up_300ms_ease-out_both]"
diff --git a/src/web/src/app/(app)/w/[slug]/settings/pet-tab.tsx b/src/web/src/app/(app)/w/[slug]/settings/pet-tab.tsx
index e3938bb38..83ccb2110 100644
--- a/src/web/src/app/(app)/w/[slug]/settings/pet-tab.tsx
+++ b/src/web/src/app/(app)/w/[slug]/settings/pet-tab.tsx
@@ -1,7 +1,7 @@
"use client";
import { useEffect, useState } from "react";
-import { Monitor, PanelTop, Sparkles } from "lucide-react";
+import { Sparkles } from "lucide-react";
import {
CLOUD_CODE_MONSTER_PET_PRESETS,
@@ -16,33 +16,11 @@ import { Switch } from "@/components/ui/switch";
import {
readHomePetSettings,
writeHomePetSettings,
- type HomePetDisplayScope,
} from "@/lib/home-pet-settings";
import { cn } from "@/lib/utils";
-const DISPLAY_SCOPE_OPTIONS: Array<{
- id: HomePetDisplayScope;
- label: string;
- description: string;
- icon: typeof PanelTop;
-}> = [
- {
- id: "home",
- label: "Homepage only",
- description: "Show inside the relationship canvas",
- icon: PanelTop,
- },
- {
- id: "global",
- label: "Global Display",
- description: "Show across workspace pages",
- icon: Monitor,
- },
-];
-
export function PetTab() {
const [enabled, setEnabled] = useState(false);
- const [displayScope, setDisplayScope] = useState<HomePetDisplayScope>("home");
const [selectedPresetId, setSelectedPresetId] = useState(
CLOUD_CODE_MONSTER_PET_PRESETS[0]!.id
);
@@ -51,7 +29,6 @@ export function PetTab() {
useEffect(() => {
const settings = readHomePetSettings();
setEnabled(settings.enabled);
- setDisplayScope(settings.displayScope);
setSelectedPresetId(readCloudCodeMonsterPetPresetId());
const handlePresetChange = (event: Event) => {
@@ -82,11 +59,6 @@ export function PetTab() {
writeHomePetSettings({ enabled: checked });
};
- const handleDisplayScopeChange = (scope: HomePetDisplayScope) => {
- setDisplayScope(scope);
- writeHomePetSettings({ displayScope: scope });
- };
-
return (
<div className="space-y-8">
<section className="space-y-4">
@@ -107,43 +79,6 @@ export function PetTab() {
onCheckedChange={handleEnabledChange}
/>
</div>
-
- {enabled ? (
- <div className="space-y-2">
- <p className="text-xs text-muted-foreground">Display range</p>
- <div className="grid gap-2">
- {DISPLAY_SCOPE_OPTIONS.map((option) => {
- const Icon = option.icon;
- const isSelected = displayScope === option.id;
-
- return (
- <button
- key={option.id}
- type="button"
- aria-pressed={isSelected}
- onClick={() => handleDisplayScopeChange(option.id)}
- className={cn(
- "flex items-center gap-3 rounded-md border px-3 py-2 text-left transition-colors",
- isSelected
- ? "border-primary/50 bg-accent text-foreground"
- : "border-border/60 bg-background/60 text-muted-foreground hover:bg-accent/50 hover:text-foreground"
- )}
- >
- <Icon className="size-4 shrink-0" />
- <span className="min-w-0">
- <span className="block text-sm font-medium">
- {option.label}
- </span>
- <span className="block text-xs text-muted-foreground">
- {option.description}
- </span>
- </span>
- </button>
- );
- })}
- </div>
- </div>
- ) : null}
</div>
</section>
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet-activity.ts b/src/web/src/components/home-pet/cloud-code-monster-pet-activity.ts
index cff4eae04..1a080bf75 100644
--- a/src/web/src/components/home-pet/cloud-code-monster-pet-activity.ts
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet-activity.ts
@@ -428,10 +428,26 @@ export function writeStoredActivity(activityState: StoredCloudCodeMonsterActivit
} catch {}
}
-export function getBounds(boundary: HTMLElement | null): PetBounds {
+export function createWalkToTargetVelocity(
+ from: PetPoint,
+ to: PetPoint,
+ speed: number
+): PetPoint {
+ const dx = to.x - from.x;
+ const dy = to.y - from.y;
+ const distance = Math.hypot(dx, dy);
+
+ if (!Number.isFinite(distance) || distance < 0.01) {
+ return { x: 0, y: 0 };
+ }
+
+ return { x: (dx / distance) * speed, y: (dy / distance) * speed };
+}
+
+export function getBounds(_boundary: HTMLElement | null): PetBounds {
return {
- width: boundary?.clientWidth ?? window.innerWidth,
- height: boundary?.clientHeight ?? window.innerHeight,
+ width: window.innerWidth,
+ height: window.innerHeight,
};
}
@@ -457,20 +473,17 @@ export function resolveCloudCodeMonsterPeekPosition(
): PetPoint {
if (target.agentId) {
const agentNode = findAgentPeekNode(boundary, target.agentId);
- const boundaryRect = boundary?.getBoundingClientRect();
const agentRect = agentNode?.getBoundingClientRect();
- if (agentRect && boundaryRect) {
+ if (agentRect) {
return clampPetPosition(
{
x:
- agentRect.left -
- boundaryRect.left +
+ agentRect.left +
agentRect.width / 2 -
CLOUD_CODE_MONSTER_SIZE.width / 2,
y:
agentRect.top -
- boundaryRect.top -
CLOUD_CODE_MONSTER_SIZE.height * 0.18,
},
bounds,
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet-types.ts b/src/web/src/components/home-pet/cloud-code-monster-pet-types.ts
index d57d5a271..b1e972fcc 100644
--- a/src/web/src/components/home-pet/cloud-code-monster-pet-types.ts
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet-types.ts
@@ -17,8 +17,6 @@ export type CloudCodeMonsterPeekTarget = PetPoint & {
agentId?: string;
};
-export type CloudCodeMonsterActivityTriggerMode = "home" | "global";
-
export type CloudCodeMonsterActivityId =
| "coding"
| "sleeping"
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet-walk-target.ts b/src/web/src/components/home-pet/cloud-code-monster-pet-walk-target.ts
new file mode 100644
index 000000000..c8d2d5847
--- /dev/null
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet-walk-target.ts
@@ -0,0 +1,150 @@
+"use client";
+
+import { type RefObject, useCallback, useEffect, useRef, useState } from "react";
+
+import { clampPetPosition, createWalkToTargetVelocity, getBounds } from "./cloud-code-monster-pet-activity";
+import { CLOUD_CODE_MONSTER_AUTO_WALK_STEP_MS, CLOUD_CODE_MONSTER_SIZE } from "./cloud-code-monster-pet-constants";
+import type { PetPoint } from "./cloud-code-monster-pet-types";
+
+const WALK_TO_TARGET_SPEED = 3.2;
+const ARRIVAL_THRESHOLD = 10;
+const TARGET_OFFSET_X = 22;
+
+export type WalkToTargetState = {
+ isWalking: boolean;
+ isIdlingAtTarget: boolean;
+ walkDirection: "left" | "right";
+};
+
+type UseWalkToTargetParams = {
+ boundaryRef: RefObject<HTMLElement | null>;
+ targetId: string | null;
+ enabled: boolean;
+ position: PetPoint | null;
+ setPosition: (updater: (pos: PetPoint | null) => PetPoint | null) => void;
+ onArrive: () => void;
+ onStep: (position: PetPoint, intensity: number) => void;
+};
+
+function getTargetPosition(
+ boundaryEl: HTMLElement | null,
+ targetId: string
+): PetPoint | null {
+ if (!boundaryEl) return null;
+
+ const targetEl =
+ boundaryEl.querySelector<HTMLElement>(`[data-pet-target-id="${targetId}"]`) ??
+ document.querySelector<HTMLElement>(`[data-pet-target-id="${targetId}"]`);
+ if (!targetEl) return null;
+
+ const targetRect = targetEl.getBoundingClientRect();
+ const bounds = getBounds(boundaryEl);
+
+ const x = targetRect.right + TARGET_OFFSET_X;
+ const y = targetRect.top + targetRect.height / 2 - CLOUD_CODE_MONSTER_SIZE.height / 2;
+
+ return clampPetPosition({ x, y }, bounds, CLOUD_CODE_MONSTER_SIZE);
+}
+
+export function useWalkToTarget({
+ boundaryRef,
+ targetId,
+ enabled,
+ position,
+ setPosition,
+ onArrive,
+ onStep,
+}: UseWalkToTargetParams): WalkToTargetState {
+ const [isWalking, setIsWalking] = useState(false);
+ const [isIdlingAtTarget, setIsIdlingAtTarget] = useState(false);
+ const [walkDirection, setWalkDirection] = useState<"left" | "right">("left");
+ const stepTimerRef = useRef<number | null>(null);
+ const arrivedRef = useRef(false);
+ const enabledRef = useRef(enabled);
+ const positionRef = useRef(position);
+
+ useEffect(() => { enabledRef.current = enabled; }, [enabled]);
+ useEffect(() => { positionRef.current = position; }, [position]);
+
+ const clearStepTimer = useCallback(() => {
+ if (stepTimerRef.current !== null) {
+ window.clearTimeout(stepTimerRef.current);
+ stepTimerRef.current = null;
+ }
+ }, []);
+
+ const cancel = useCallback(() => {
+ clearStepTimer();
+ setIsWalking(false);
+ setIsIdlingAtTarget(false);
+ arrivedRef.current = false;
+ }, [clearStepTimer]);
+
+ useEffect(() => {
+ if (!enabled || !targetId || !position) {
+ cancel();
+ return;
+ }
+
+ if (arrivedRef.current) {
+ return;
+ }
+
+ setIsWalking(true);
+ setIsIdlingAtTarget(false);
+
+ const scheduleStep = () => {
+ stepTimerRef.current = window.setTimeout(() => {
+ stepTimerRef.current = null;
+
+ if (!enabledRef.current) {
+ setIsWalking(false);
+ return;
+ }
+
+ const target = getTargetPosition(boundaryRef.current, targetId);
+ const current = positionRef.current;
+
+ if (!target || !current) {
+ scheduleStep();
+ return;
+ }
+
+ const dx = target.x - current.x;
+ const dy = target.y - current.y;
+ const distance = Math.hypot(dx, dy);
+
+ if (distance < ARRIVAL_THRESHOLD) {
+ arrivedRef.current = true;
+ setIsWalking(false);
+ setIsIdlingAtTarget(true);
+ setWalkDirection("left");
+ setPosition(() => target);
+ onArrive();
+ return;
+ }
+
+ const velocity = createWalkToTargetVelocity(current, target, WALK_TO_TARGET_SPEED);
+ setWalkDirection(velocity.x >= 0 ? "right" : "left");
+
+ const next = clampPetPosition(
+ { x: current.x + velocity.x, y: current.y + velocity.y },
+ getBounds(boundaryRef.current),
+ CLOUD_CODE_MONSTER_SIZE
+ );
+
+ setPosition(() => next);
+ onStep(next, 1.45);
+ scheduleStep();
+ }, CLOUD_CODE_MONSTER_AUTO_WALK_STEP_MS);
+ };
+
+ scheduleStep();
+
+ return () => {
+ clearStepTimer();
+ };
+ }, [enabled, targetId, !!position, boundaryRef, cancel, clearStepTimer, onArrive, onStep, setPosition]);
+
+ return { isWalking, isIdlingAtTarget, walkDirection };
+}
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet.module.css b/src/web/src/components/home-pet/cloud-code-monster-pet.module.css
index 4beb791cd..642018646 100644
--- a/src/web/src/components/home-pet/cloud-code-monster-pet.module.css
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet.module.css
@@ -3,10 +3,10 @@
}
.pet {
- position: absolute;
+ position: fixed;
top: 0;
left: 0;
- z-index: 12;
+ z-index: 51;
width: 82px;
transform: translate3d(var(--cloud-code-monster-pet-x), var(--cloud-code-monster-pet-y), 0);
pointer-events: none;
@@ -15,9 +15,9 @@
}
.footsteps {
- position: absolute;
+ position: fixed;
inset: 0;
- z-index: 11;
+ z-index: 50;
overflow: hidden;
pointer-events: none;
}
@@ -343,6 +343,13 @@
50% { transform: translateY(-3px); }
}
+@media (max-width: 639px) {
+ .pet {
+ transform: translate3d(var(--cloud-code-monster-pet-x), var(--cloud-code-monster-pet-y), 0) scale(0.68);
+ transform-origin: bottom right;
+ }
+}
+
@media (prefers-reduced-motion: reduce) {
.button,
.petLayer :global(.cloud-code-monster-pet-character),
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet.test.ts b/src/web/src/components/home-pet/cloud-code-monster-pet.test.ts
index 40331e594..cae4726fb 100644
--- a/src/web/src/components/home-pet/cloud-code-monster-pet.test.ts
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet.test.ts
@@ -13,6 +13,7 @@ import {
createCloudCodeMonsterHiddenState,
createCloudCodeMonsterIdleState,
createCloudCodeMonsterWalkVelocity,
+ createWalkToTargetVelocity,
getCloudCodeMonsterExpression,
getCloudCodeMonsterPreset,
getMonsterFootstepIntervalMs,
@@ -99,7 +100,6 @@ describe("Cloud Code monster PET helpers", () => {
it("defaults to opt-in behavior", () => {
expect(readHomePetSettings()).toMatchObject({
enabled: false,
- displayScope: "home",
});
});
@@ -236,7 +236,23 @@ describe("Cloud Code monster PET helpers", () => {
boundary,
{ width: 900, height: 700 }
)
- ).toEqual({ x: 309, y: 305.24 });
+ ).toEqual({ x: 329, y: 345.24 });
+ });
+
+ it("computes walk-to-target velocity correctly", () => {
+ const v = createWalkToTargetVelocity({ x: 0, y: 0 }, { x: 100, y: 0 }, 3.2);
+ expect(v.x).toBeCloseTo(3.2);
+ expect(v.y).toBeCloseTo(0);
+
+ const diagonal = createWalkToTargetVelocity({ x: 0, y: 0 }, { x: 3, y: 4 }, 5);
+ expect(diagonal.x).toBeCloseTo(3);
+ expect(diagonal.y).toBeCloseTo(4);
+
+ // Zero distance → zero velocity (NaN safety)
+ expect(createWalkToTargetVelocity({ x: 5, y: 5 }, { x: 5, y: 5 }, 3.2)).toEqual({ x: 0, y: 0 });
+
+ // NaN inputs → zero velocity
+ expect(createWalkToTargetVelocity({ x: NaN, y: 0 }, { x: 10, y: 0 }, 3.2)).toEqual({ x: 0, y: 0 });
});
});
@@ -291,10 +307,30 @@ describe("production workspace PET mounting", () => {
path.join(root, "src/components/home-pet/cloud-code-monster-pet.module.css"),
"utf8"
);
+ const petWalkTarget = readFileSync(
+ path.join(root, "src/components/home-pet/cloud-code-monster-pet-walk-target.ts"),
+ "utf8"
+ );
const agentNode = readFileSync(
path.join(root, "src/components/canvas/agent-node.tsx"),
"utf8"
);
+ const inboxPopover = readFileSync(
+ path.join(root, "src/components/inbox-popover.tsx"),
+ "utf8"
+ );
+ const landingPage = readFileSync(
+ path.join(root, "src/components/home/home-page.tsx"),
+ "utf8"
+ );
+ const inboxCountContext = readFileSync(
+ path.join(root, "src/contexts/inbox-count-context.tsx"),
+ "utf8"
+ );
+ const agentContext = readFileSync(
+ path.join(root, "src/contexts/agent-context.tsx"),
+ "utf8"
+ );
const globalCss = readFileSync(path.join(root, "src/app/globals.css"), "utf8");
const clientPetSources = [petTypes, petPresets, petDirectShapes].join("\n");
const sensitiveShapeIds = [
@@ -327,13 +363,13 @@ describe("production workspace PET mounting", () => {
"chop" + "per",
];
- expect(workspaceHomePage).toContain("CloudCodeMonsterPet");
- expect(workspaceHomePage).toContain("dynamic<CloudCodeMonsterPetProps>");
- expect(workspaceHomePage).toContain("useHomePetSettings");
+ // Home page no longer renders pet directly
+ expect(workspaceHomePage).not.toContain("CloudCodeMonsterPet");
+ expect(workspaceHomePage).not.toContain("useHomePetSettings");
expect(settingsPage).toContain('{ id: "pet", label: "Pet" }');
expect(petTab).toContain("Enable pet");
- expect(petTab).toContain("Homepage only");
- expect(petTab).toContain("Global Display");
+ expect(petTab).not.toContain("Homepage only");
+ expect(petTab).not.toContain("Global Display");
expect(petTab).toContain("CloudCodeMonsterPresetPreview");
expect(petTab).toContain("cloud-code-monster-pet-presets");
expect(petTab).not.toContain(
@@ -341,7 +377,10 @@ describe("production workspace PET mounting", () => {
);
expect(workspaceShell).toContain("WorkspacePetLayer");
expect(workspaceShell).toContain("RuntimeVersionGate");
- expect(workspacePetLayer).toContain('petSettings.displayScope !== "global"');
+ // Pet layer renders on all pages — no displayScope or isHome check
+ expect(workspacePetLayer).not.toContain("displayScope");
+ expect(workspacePetLayer).not.toContain("isHome");
+ expect(workspacePetLayer).toContain("petSettings.enabled");
expect(workspacePetLayer).toContain("dynamic<CloudCodeMonsterPetProps>");
expect(petComponent).toContain("const EMPTY_PEEK_TARGETS");
expect(petComponent).toContain("peekTargets = EMPTY_PEEK_TARGETS");
@@ -350,10 +389,28 @@ describe("production workspace PET mounting", () => {
expect(petComponent).toContain("function usePetTimers()");
expect(petComponent).toContain("setPetTimer(\"peek\"");
expect(petComponent).toContain("usePetDrag({");
+ expect(petComponent).toContain("useInboxCount");
+ expect(petComponent).toContain("useWalkToTarget");
+ expect(petComponent).toContain("useAgentContextSafe");
+ expect(petComponent).toContain("activeTaskDetails");
+ expect(petComponent).toContain("hasRunningTasks");
+ expect(petComponent).not.toContain("activityTriggerMode");
expect(petDragHook).toContain("export function usePetDrag");
expect(petDragHook).toContain("const handlePointerMove = useCallback");
expect(petComponent).not.toContain("peekTargets = []");
expect(petComponent).not.toContain("TimerRef = useRef");
+ // Walk-to-target hook exists and has correct exports
+ expect(petWalkTarget).toContain("export function useWalkToTarget");
+ expect(petWalkTarget).toContain("createWalkToTargetVelocity");
+ expect(petWalkTarget).toContain('data-pet-target-id');
+ // Inbox button has pet target attribute
+ expect(inboxPopover).toContain('data-pet-target-id="inbox"');
+ // Landing page renders pet for logged-in users
+ expect(landingPage).toContain("CloudCodeMonsterPet");
+ expect(landingPage).toContain("isLoggedIn && petSettings.enabled");
+ // Context hooks are safe outside providers
+ expect(inboxCountContext).toContain("FALLBACK_INBOX_COUNT");
+ expect(agentContext).toContain("useAgentContextSafe");
for (const sensitiveShapeId of sensitiveShapeIds) {
expect(clientPetSources).not.toContain(`"${sensitiveShapeId}"`);
}
@@ -365,6 +422,7 @@ describe("production workspace PET mounting", () => {
expect(petCssModule).toContain(".pet {");
expect(petCssModule).toContain(".button {");
expect(petCssModule).toContain(".footprint {");
+ expect(petCssModule).toContain("z-index: 51");
expect(petCssModule).not.toContain(":global(.cloud-code-monster-pet)");
expect(globalCss).not.toContain(".cloud-code-monster-pet");
expect(globalCss).not.toContain(".home-pet");
diff --git a/src/web/src/components/home-pet/cloud-code-monster-pet.tsx b/src/web/src/components/home-pet/cloud-code-monster-pet.tsx
index 40069b536..3ae26dde4 100644
--- a/src/web/src/components/home-pet/cloud-code-monster-pet.tsx
+++ b/src/web/src/components/home-pet/cloud-code-monster-pet.tsx
@@ -10,6 +10,9 @@ import {
useState,
} from "react";
+import { useAgentContextSafe } from "@/contexts/agent-context";
+import { useInboxCount } from "@/contexts/inbox-count-context";
+
import styles from "./cloud-code-monster-pet.module.css";
import {
@@ -40,6 +43,7 @@ import {
CLOUD_CODE_MONSTER_SIZE,
} from "./cloud-code-monster-pet-constants";
import { usePetDrag } from "./cloud-code-monster-pet-drag";
+import { useWalkToTarget } from "./cloud-code-monster-pet-walk-target";
import { MonsterSvg } from "./cloud-code-monster-pet-pixel-parts";
import {
CLOUD_CODE_MONSTER_PET_PRESETS,
@@ -47,7 +51,7 @@ import {
readCloudCodeMonsterPetPresetId,
} from "./cloud-code-monster-pet-presets";
import type {
- CloudCodeMonsterActivityTriggerMode,
+ CloudCodeMonsterActivityId,
CloudCodeMonsterPeekTarget,
Footprint,
PetPoint,
@@ -61,6 +65,7 @@ export {
createCloudCodeMonsterIdleState,
createCloudCodeMonsterPreviewAwayState,
createCloudCodeMonsterWalkVelocity,
+ createWalkToTargetVelocity,
getCloudCodeMonsterExpression,
getMonsterFootstepIntervalMs,
hasViolentMonsterDirectionChange,
@@ -96,7 +101,6 @@ export {
} from "./cloud-code-monster-pet-presets";
export type {
CloudCodeMonsterActivityId,
- CloudCodeMonsterActivityTriggerMode,
CloudCodeMonsterExpression,
CloudCodeMonsterPeekTarget,
CloudCodeMonsterPetPreset,
@@ -108,7 +112,6 @@ export type {
export type CloudCodeMonsterPetProps = {
boundaryRef: RefObject<HTMLElement | null>;
initialPosition?: PetPoint;
- activityTriggerMode?: CloudCodeMonsterActivityTriggerMode;
previewComebackToken?: number;
notificationToken?: number;
peekTargets?: CloudCodeMonsterPeekTarget[];
@@ -123,7 +126,8 @@ type PetTimerKey =
| "peek"
| "peekStop"
| "notification"
- | "walkSettle";
+ | "walkSettle"
+ | "walkToTargetPeek";
function createPetTimerRecord(): Record<PetTimerKey, number | null> {
return {
@@ -135,6 +139,7 @@ function createPetTimerRecord(): Record<PetTimerKey, number | null> {
peekStop: null,
notification: null,
walkSettle: null,
+ walkToTargetPeek: null,
};
}
@@ -174,7 +179,6 @@ function usePetTimers() {
export function CloudCodeMonsterPet({
boundaryRef,
initialPosition,
- activityTriggerMode = "global",
previewComebackToken = 0,
notificationToken = 0,
peekTargets = EMPTY_PEEK_TARGETS,
@@ -264,13 +268,8 @@ export function CloudCodeMonsterPet({
return () => {
document.removeEventListener("visibilitychange", handleVisibility);
- if (activityTriggerMode === "home" && document.visibilityState === "visible") {
- writeStoredActivity(
- createCloudCodeMonsterHiddenState(readStoredActivity(), Date.now())
- );
- }
};
- }, [activityTriggerMode]);
+ }, []);
useEffect(() => {
if (previewComebackToken <= 0) {
@@ -331,7 +330,7 @@ export function CloudCodeMonsterPet({
);
}, [activityState]);
const preset = useMemo(() => getCloudCodeMonsterPreset(presetId), [presetId]);
- const isWalking = isDragging || isAutoWalking;
+ const isWalkingBasic = isDragging || isAutoWalking;
const hasPosition = position !== null;
const hasPeekTargets = peekTargets.length > 0;
const shouldAutoWalk = shouldCloudCodeMonsterAutoWalk(
@@ -342,6 +341,126 @@ export function CloudCodeMonsterPet({
peekTargetsRef.current = peekTargets;
}, [peekTargets]);
+ // --- Inbox walk-to-target integration ---
+ const { count: inboxCount } = useInboxCount();
+ const [inboxWalkEnabled, setInboxWalkEnabled] = useState(false);
+ const inboxDebounceRef = useRef<number | null>(null);
+
+ useEffect(() => {
+ if (inboxCount > 0 && !fainted && !isDragging) {
+ if (inboxDebounceRef.current === null) {
+ inboxDebounceRef.current = window.setTimeout(() => {
+ inboxDebounceRef.current = null;
+ setInboxWalkEnabled(true);
+ }, 300);
+ }
+ } else if (inboxCount === 0) {
+ if (inboxDebounceRef.current !== null) {
+ window.clearTimeout(inboxDebounceRef.current);
+ inboxDebounceRef.current = null;
+ }
+ setInboxWalkEnabled(false);
+ }
+
+ return () => {
+ if (inboxDebounceRef.current !== null) {
+ window.clearTimeout(inboxDebounceRef.current);
+ inboxDebounceRef.current = null;
+ }
+ };
+ }, [inboxCount, fainted, isDragging]);
+
+ const handleWalkToTargetStep = useCallback((nextPosition: PetPoint, intensity: number) => {
+ setWalkIntensity(intensity);
+ const now = performance.now();
+ if (now - lastFootstepAtRef.current >= getMonsterFootstepIntervalMs(intensity)) {
+ const side = nextFootSideRef.current;
+ nextFootSideRef.current = side === "left" ? "right" : "left";
+ const sideOffset = side === "left" ? 25 : 52;
+ setFootprints((current) => [
+ ...current.slice(-13),
+ {
+ id: nextFootprintIdRef.current++,
+ x: nextPosition.x + sideOffset,
+ y: nextPosition.y + CLOUD_CODE_MONSTER_SIZE.height - 7,
+ side,
+ intensity,
+ },
+ ]);
+ lastFootstepAtRef.current = now;
+ }
+ }, []);
+
+ const handleWalkToTargetArrive = useCallback(() => {
+ setIsPeeking(true);
+ setWalkIntensity(1);
+ setPetTimer("walkToTargetPeek", () => {
+ setIsPeeking(false);
+ // Re-peek periodically while inbox > 0
+ const schedulePeek = () => {
+ setPetTimer("walkToTargetPeek", () => {
+ setIsPeeking(true);
+ setPetTimer("peekStop", () => {
+ setIsPeeking(false);
+ schedulePeek();
+ }, CLOUD_CODE_MONSTER_PEEK_MS);
+ }, CLOUD_CODE_MONSTER_PEEK_INTERVAL_MS);
+ };
+ schedulePeek();
+ }, CLOUD_CODE_MONSTER_PEEK_MS);
+ }, [setPetTimer]);
+
+ const walkToTarget = useWalkToTarget({
+ boundaryRef,
+ targetId: inboxWalkEnabled ? "inbox" : null,
+ enabled: inboxWalkEnabled && !isDragging && !reacting && !shaken && !fainted,
+ position,
+ setPosition,
+ onArrive: handleWalkToTargetArrive,
+ onStep: handleWalkToTargetStep,
+ });
+
+ const isWalkingToTarget = walkToTarget.isWalking || walkToTarget.isIdlingAtTarget;
+ const isWalking = isWalkingBasic || walkToTarget.isWalking;
+ const wasWalkingToTargetRef = useRef(false);
+
+ useEffect(() => {
+ if (wasWalkingToTargetRef.current && !isWalkingToTarget) {
+ clearPetTimer("walkToTargetPeek");
+ setIsPeeking(false);
+ }
+ wasWalkingToTargetRef.current = isWalkingToTarget;
+ }, [isWalkingToTarget, clearPetTimer]);
+
+ // --- Working state: lock activity when agents have running tasks ---
+ const agentCtx = useAgentContextSafe();
+ const hasRunningTasks = (agentCtx?.activeTaskDetails.length ?? 0) > 0;
+ const workingActivityRef = useRef<CloudCodeMonsterActivityId | null>(null);
+
+ useEffect(() => {
+ if (hasRunningTasks && !isWalkingToTarget) {
+ if (!workingActivityRef.current) {
+ const workingActivities: CloudCodeMonsterActivityId[] = ["coding", "thinking", "reading"];
+ workingActivityRef.current =
+ workingActivities[Math.floor(Math.random() * workingActivities.length)]!;
+ }
+ const lockedActivity = workingActivityRef.current;
+ setActivityState((current) => {
+ if (current?.activityId === lockedActivity) return current;
+ const next = { activityId: lockedActivity, updatedAt: Date.now(), hiddenAt: null };
+ writeStoredActivity(next);
+ return next;
+ });
+ } else if (!hasRunningTasks) {
+ if (workingActivityRef.current) {
+ workingActivityRef.current = null;
+ const nextState = createCloudCodeMonsterIdleState();
+ writeStoredActivity(nextState);
+ setActivityState(nextState);
+ }
+ }
+ }, [hasRunningTasks, isWalkingToTarget]);
+
const pushFootprint = useCallback((nextPosition: PetPoint, intensity: number) => {
const side = nextFootSideRef.current;
nextFootSideRef.current = side === "left" ? "right" : "left";
@@ -368,7 +487,8 @@ export function CloudCodeMonsterPet({
reacting ||
shaken ||
fainted ||
- isPeeking
+ isPeeking ||
+ isWalkingToTarget
) {
setIsAutoWalking(false);
setWalkIntensity(1);
@@ -432,6 +552,7 @@ export function CloudCodeMonsterPet({
hasPosition,
isDragging,
isPeeking,
+ isWalkingToTarget,
reacting,
clearPetTimer,
pushFootprint,
@@ -447,7 +568,8 @@ export function CloudCodeMonsterPet({
isDragging ||
reacting ||
shaken ||
- fainted
+ fainted ||
+ isWalkingToTarget
) {
return;
}
@@ -491,6 +613,7 @@ export function CloudCodeMonsterPet({
hasPeekTargets,
hasPosition,
isDragging,
+ isWalkingToTarget,
reacting,
shaken,
setPetTimer,
@@ -517,6 +640,7 @@ export function CloudCodeMonsterPet({
clearPetTimer("autonomousWalk");
clearPetTimer("peek");
clearPetTimer("peekStop");
+ clearPetTimer("walkToTargetPeek");
}, [clearPetTimer]);
const startShockReaction = useCallback(() => {
@@ -665,7 +789,7 @@ export function CloudCodeMonsterPet({
data-activity={displayedActivity?.id ?? "idle"}
data-dragging={isDragging}
data-walking={isWalking}
- data-direction={walkDirection}
+ data-direction={isWalkingToTarget ? walkToTarget.walkDirection : walkDirection}
data-reaction={shaken ? "shake" : reacting ? "shock" : "none"}
data-reacting={reacting}
data-shaken={shaken}
diff --git a/src/web/src/components/home-pet/workspace-pet-layer.tsx b/src/web/src/components/home-pet/workspace-pet-layer.tsx
index 8ab6313c4..e149eb929 100644
--- a/src/web/src/components/home-pet/workspace-pet-layer.tsx
+++ b/src/web/src/components/home-pet/workspace-pet-layer.tsx
@@ -2,7 +2,6 @@
import { type RefObject } from "react";
import dynamic from "next/dynamic";
-import { usePathname } from "next/navigation";
import { useHomePetSettings } from "@/lib/home-pet-settings";
import type { CloudCodeMonsterPetProps } from "./cloud-code-monster-pet";
@@ -17,21 +16,15 @@ const CloudCodeMonsterPet = dynamic<CloudCodeMonsterPetProps>(
type WorkspacePetLayerProps = {
boundaryRef: RefObject<HTMLElement | null>;
- slug: string;
+ slug?: string;
};
-export function WorkspacePetLayer({ boundaryRef, slug }: WorkspacePetLayerProps) {
- const pathname = usePathname();
+export function WorkspacePetLayer({ boundaryRef }: WorkspacePetLayerProps) {
const petSettings = useHomePetSettings();
- const isHome = pathname === `/w/${slug}/home`;
- if (
- !petSettings.enabled ||
- petSettings.displayScope !== "global" ||
- isHome
- ) {
+ if (!petSettings.enabled) {
return null;
}
- return <CloudCodeMonsterPet boundaryRef={boundaryRef} activityTriggerMode="global" />;
+ return <CloudCodeMonsterPet boundaryRef={boundaryRef} />;
}
diff --git a/src/web/src/components/home/home-page.tsx b/src/web/src/components/home/home-page.tsx
index 3c42eb0fa..c762fc8d3 100644
--- a/src/web/src/components/home/home-page.tsx
+++ b/src/web/src/components/home/home-page.tsx
@@ -1,9 +1,12 @@
"use client";
import { useRef } from "react";
+import dynamic from "next/dynamic";
import gsap from "gsap";
import { useGSAP } from "@gsap/react";
import { ScrollTrigger } from "gsap/ScrollTrigger";
+import { useHomePetSettings } from "@/lib/home-pet-settings";
+import type { CloudCodeMonsterPetProps } from "@/components/home-pet/cloud-code-monster-pet";
import { HeroSection } from "./hero-section";
import { FeatureShowcase } from "./feature-showcase";
import { ArchitectureOverview } from "./architecture-overview";
@@ -13,6 +16,14 @@ import { ByoaSection } from "./byoa-section";
import { UseCasesSection } from "./use-cases-section";
import { QuickstartSection } from "./quickstart-section";
+const CloudCodeMonsterPet = dynamic<CloudCodeMonsterPetProps>(
+ () =>
+ import("@/components/home-pet/cloud-code-monster-pet").then(
+ (module) => module.CloudCodeMonsterPet
+ ),
+ { ssr: false }
+);
+
gsap.registerPlugin(useGSAP, ScrollTrigger);
// Respect reduced motion preference
@@ -27,6 +38,7 @@ if (typeof window !== "undefined") {
export function HomePage({ isLoggedIn }: { isLoggedIn: boolean }) {
const mainRef = useRef<HTMLDivElement>(null);
+ const petSettings = useHomePetSettings();
useGSAP(
() => {
@@ -67,6 +79,9 @@ export function HomePage({ isLoggedIn }: { isLoggedIn: boolean }) {
<QuickstartSection />
<ArchitectureOverview />
<MarketingFooter />
+ {isLoggedIn && petSettings.enabled ? (
+ <CloudCodeMonsterPet boundaryRef={mainRef} />
+ ) : null}
</div>
);
}
diff --git a/src/web/src/components/inbox-popover.tsx b/src/web/src/components/inbox-popover.tsx
index 5b23ac623..e0297ab25 100644
--- a/src/web/src/components/inbox-popover.tsx
+++ b/src/web/src/components/inbox-popover.tsx
@@ -109,6 +109,7 @@ export function InboxPopover({
<TooltipTrigger render={
<button
type="button"
+ data-pet-target-id="inbox"
className={cn(
"relative flex items-center justify-center size-10 rounded-xl transition-colors duration-200 cursor-pointer",
"text-muted-foreground hover:text-foreground hover:bg-accent",
diff --git a/src/web/src/contexts/agent-context.tsx b/src/web/src/contexts/agent-context.tsx
index 37ddee2b5..5c2d08e3d 100644
--- a/src/web/src/contexts/agent-context.tsx
+++ b/src/web/src/contexts/agent-context.tsx
@@ -73,6 +73,10 @@ export function useAgentContext() {
return ctx;
}
+export function useAgentContextSafe() {
+ return useContext(AgentContext);
+}
+
export function AgentProvider({
workspaceId,
children,
diff --git a/src/web/src/contexts/inbox-count-context.tsx b/src/web/src/contexts/inbox-count-context.tsx
index 120f1e632..b1d5def39 100644
--- a/src/web/src/contexts/inbox-count-context.tsx
+++ b/src/web/src/contexts/inbox-count-context.tsx
@@ -24,10 +24,11 @@ interface InboxCountContextValue {
const InboxCountContext = createContext<InboxCountContextValue | null>(null);
+const FALLBACK_INBOX_COUNT: InboxCountContextValue = { count: 0, refresh: () => {}, decrement: () => {} };
+
export function useInboxCount() {
const ctx = useContext(InboxCountContext);
- if (!ctx) throw new Error("useInboxCount must be used within InboxCountProvider");
- return ctx;
+ return ctx ?? FALLBACK_INBOX_COUNT;
}
export function InboxCountProvider({ children }: { children: ReactNode }) {
diff --git a/src/web/src/lib/home-pet-settings.ts b/src/web/src/lib/home-pet-settings.ts
index 6dd893404..9ad6417ff 100644
--- a/src/web/src/lib/home-pet-settings.ts
+++ b/src/web/src/lib/home-pet-settings.ts
@@ -4,37 +4,24 @@ import { useEffect, useState } from "react";
export const HOME_PET_SETTINGS_CHANGED_EVENT = "alook-home-pet-settings-changed";
export const HOME_PET_ENABLED_STORAGE_KEY = "alook-home-pet-enabled-v1";
-export const HOME_PET_DISPLAY_SCOPE_STORAGE_KEY = "alook-home-pet-display-scope-v1";
-
-export type HomePetDisplayScope = "home" | "global";
export type HomePetSettings = {
enabled: boolean;
- displayScope: HomePetDisplayScope;
};
const DEFAULT_HOME_PET_SETTINGS: HomePetSettings = {
enabled: false,
- displayScope: "home",
};
-export function isHomePetDisplayScope(value: string | null): value is HomePetDisplayScope {
- return value === "home" || value === "global";
-}
-
export function readHomePetSettings(): HomePetSettings {
if (typeof window === "undefined") {
return DEFAULT_HOME_PET_SETTINGS;
}
const enabled = window.localStorage.getItem(HOME_PET_ENABLED_STORAGE_KEY);
- const displayScope = window.localStorage.getItem(HOME_PET_DISPLAY_SCOPE_STORAGE_KEY);
return {
enabled: enabled === null ? DEFAULT_HOME_PET_SETTINGS.enabled : enabled === "true",
- displayScope: isHomePetDisplayScope(displayScope)
- ? displayScope
- : DEFAULT_HOME_PET_SETTINGS.displayScope,
};
}
@@ -45,7 +32,6 @@ export function writeHomePetSettings(settings: Partial<HomePetSettings>) {
};
window.localStorage.setItem(HOME_PET_ENABLED_STORAGE_KEY, String(next.enabled));
- window.localStorage.setItem(HOME_PET_DISPLAY_SCOPE_STORAGE_KEY, next.displayScope);
window.dispatchEvent(
new CustomEvent<HomePetSettings>(HOME_PET_SETTINGS_CHANGED_EVENT, {
detail: next,
@@ -69,10 +55,7 @@ export function useHomePetSettings() {
);
};
const handleStorageChanged = (event: StorageEvent) => {
- if (
- event.key === HOME_PET_ENABLED_STORAGE_KEY ||
- event.key === HOME_PET_DISPLAY_SCOPE_STORAGE_KEY
- ) {
+ if (event.key === HOME_PET_ENABLED_STORAGE_KEY) {
setSettings(readHomePetSettings());
}
};