返回 CodeWhale
whale-motion.ts
根目录 / web / lib / whale-motion.ts
1 /**
2 * The v2 whale performance, loaded in the browser only. The vendored files
3 * (vendor/whale-character-v2, see its PROVENANCE.md) attach `WhaleActing`
4 * and `WhaleHabitat` to window in dependency order — the same executable
5 * reference the desktop app and its web client draw, so the site never
6 * keeps a second motion implementation.
7 */
8 import type { WhaleStateKey } from "./content/whale-states";
9
10 export type WhalePresence = "Offline" | "Idle" | "Listening" | "Thinking" | "Working" | "NeedsYou" | "Done";
11
12 export type WhaleActivity = { kind: string; observed: boolean; parallel?: number };
13 export type WhaleContext = { freshness?: "Live" | "Stale"; turnId?: string; status?: "completed" };
14
15 export type WhaleDirector = {
16 f: number;
17 reduced: boolean;
18 acting: string;
19 set(presence: WhalePresence, activity?: WhaleActivity | null, context?: WhaleContext): void;
20 setReduced(value: boolean): void;
21 step(dt: number): void;
22 };
23
24 type View = { theme: "paper" | "charcoal"; size: number; dpr: number; lod: number; pose?: Record<string, number> };
25
26 type RigShape = { id: string; role: string; opacity: number; path: unknown };
27
28 export type WhaleRig = {
29 build(direction: "mark" | "cruise" | "open", pose?: Record<string, number>): { shapes: RigShape[] };
30 holesFor(id: string, shapes: RigShape[], mono?: boolean): unknown[];
31 pathString(path: unknown): string;
32 };
33
34 export type WhaleReference = {
35 rig: WhaleRig;
36 acting: {
37 Director: new (options?: { seed?: number; reduced?: boolean }) => WhaleDirector;
38 render(ctx: CanvasRenderingContext2D, director: WhaleDirector, view: View): void;
39 };
40 habitat: {
41 pose(director: WhaleDirector): Record<string, number>;
42 observe(x: number, y: number): void;
43 leave(): void;
44 };
45 };
46
47 let reference: Promise<WhaleReference> | null = null;
48
49 export function loadWhaleReference(): Promise<WhaleReference> {
50 if (typeof window === "undefined") return Promise.reject(new Error("The whale renderer needs a browser."));
51 if (!reference) reference = (async () => {
52 // @ts-expect-error The vendor reference has no TypeScript declarations.
53 await import("../vendor/whale-character-v2/mark-data.js");
54 // @ts-expect-error The vendor reference has no TypeScript declarations.
55 await import("../vendor/whale-character-v2/rig.js");
56 // @ts-expect-error The vendor reference has no TypeScript declarations.
57 await import("../vendor/whale-character-v2/props.js");
58 // @ts-expect-error The vendor reference has no TypeScript declarations.
59 await import("../vendor/whale-character-v2/acting.js");
60 // @ts-expect-error The vendor reference has no TypeScript declarations.
61 await import("../vendor/whale-character-v2/habitat.js");
62 const globals = window as unknown as { WhaleActing?: WhaleReference["acting"]; WhaleHabitat?: WhaleReference["habitat"]; WhaleRig?: WhaleRig };
63 if (!globals.WhaleActing?.Director || !globals.WhaleHabitat?.pose || !globals.WhaleRig?.build) throw new Error("The whale motion reference did not load.");
64 return { rig: globals.WhaleRig, acting: globals.WhaleActing, habitat: globals.WhaleHabitat };
65 })();
66 return reference;
67 }
68
69 /**
70 * What the homepage whale is acting out. The terminal gallery sets it when a
71 * reader picks a view, so the character mirrors the screen beside it. This is
72 * illustration, not telemetry: no session is being observed.
73 */
74 export type WhaleCue = {
75 presence: WhalePresence;
76 kind?: string;
77 parallel?: number;
78 /** The word shown under the whale (a WHALE_STATE_TEXT key). */
79 label: WhaleStateKey;
80 /** A reader picked this (a terminal view); the autoplay yields to it. */
81 chosen?: boolean;
82 };
83
84 const listeners = new Set<(cue: WhaleCue) => void>();
85 let current: WhaleCue = { presence: "Idle", label: "rest" };
86
87 export function cueWhale(cue: WhaleCue): void {
88 current = cue;
89 listeners.forEach((listener) => listener(cue));
90 }
91
92 export function onWhaleCue(listener: (cue: WhaleCue) => void): () => void {
93 listeners.add(listener);
94 listener(current);
95 return () => { listeners.delete(listener); };
96 }
97
98 /**
99 * The session the hero whale acts out on its own: a request arrives, it
100 * plans, reads, edits, runs the tests, finishes, and rests. Seconds per step.
101 */
102 export const WHALE_PERFORMANCE: { cue: WhaleCue; seconds: number }[] = [
103 { cue: { presence: "Idle", label: "rest" }, seconds: 2.5 },
104 { cue: { presence: "Listening", label: "listen" }, seconds: 2.8 },
105 { cue: { presence: "Thinking", label: "think" }, seconds: 2.6 },
106 { cue: { presence: "Working", kind: "reading", label: "read" }, seconds: 3.2 },
107 { cue: { presence: "Working", kind: "editing", label: "write" }, seconds: 3.4 },
108 { cue: { presence: "Working", kind: "testing", label: "run" }, seconds: 3.2 },
109 { cue: { presence: "Done", label: "done" }, seconds: 3.6 },
110 ];
111
111 lines TYPESCRIPT