cortico-world-agent-speed 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,54 +1,117 @@
1
- // Pick stock figure actions without replacing the official movement or input loop.
1
+ // Activity is advisory: the host remains in charge of input, clips and locomotion.
2
2
  const FRESH_MS = 5000;
3
+ const FINISH_MS = 1200;
4
+ const TAU = Math.PI * 2;
3
5
  const PROFILES = {
4
- tools: ['thinking', 'focused', 'curious'],
5
- slow: ['content', 'nod', 'happy'],
6
- medium: ['nod', 'happy', 'wave'],
7
- fast: ['wave', 'excited', 'cheer', 'happy'],
8
- done: ['bow', 'nod', 'happy'],
6
+ tools: ['thinking', 'look', 'determined', 'nod', 'confused', 'curious', 'focused'],
7
+ slow: ['thinking', 'nod', 'look', 'happy', 'content', 'wink'],
8
+ medium: ['nod', 'happy', 'hop', 'wave', 'wink', 'look'],
9
+ fast: ['flap', 'excited', 'dance', 'hop', 'happy', 'cheer', 'wave', 'spin'],
10
+ done: ['bow', 'happy', 'wave', 'nod', 'flap', 'excited', 'cheer'],
9
11
  };
12
+ const STOCK = new Set(Object.values(PROFILES).flat());
10
13
 
11
14
  function kind(sample, now) {
12
- if (!sample || !Number.isFinite(sample.updatedAtMs) || sample.updatedAtMs > now ||
13
- now - sample.updatedAtMs > FRESH_MS) return null;
15
+ if (!sample || !Number.isFinite(now) || !Number.isFinite(sample.updatedAtMs) ||
16
+ sample.updatedAtMs > now || now - sample.updatedAtMs > FRESH_MS) return null;
14
17
  return sample.generating > 0 ? 'output' : sample.tools > 0 ? 'tools' : 'idle';
15
18
  }
16
19
 
20
+ // Only infer actions whose whole identifier describes a known stationary gesture or
21
+ // expression. Arbitrary pack words may have side effects even when not lasting.
22
+ function customCategory(word) {
23
+ const id = word.id.toLowerCase();
24
+ if (!/^[a-z]+(?:[_-][a-z]+)*$/.test(id)) return null;
25
+ if (word.kind === 'expression') {
26
+ if (/^(?:thinking|focused|curious|determined|confused)(?:[_-](?:face|expression))?$/.test(id)) return 'thought';
27
+ if (/^(?:happy|content|excited|cheerful|joyful|smile|smiling|wink)(?:[_-](?:face|expression))?$/.test(id)) return 'joy';
28
+ }
29
+ if (word.kind === 'motion' && /^(?:(?:small|little|gentle|happy|quick)[_-])?(?:nod|wave|bow|hop|flap|dance|look)(?:[_-](?:once|twice))?$/.test(id)) return 'gesture';
30
+ return null;
31
+ }
32
+
17
33
  export function createActivityMotion() {
18
34
  let sample = null;
19
- let finish = false;
20
- let next = 0;
21
- let count = 0;
35
+ let finishUntil = 0;
36
+ let nextCue = 0;
37
+ let lastCue = null;
38
+ let rotation = 0;
39
+ const pose = { energy: 0, focus: 0, phase: 0, cue: null };
40
+
22
41
  return {
23
42
  receive(value, now) {
24
43
  const before = kind(sample, now);
25
44
  const after = kind(value, now);
26
- if (after !== before) {
27
- next = 0;
28
- finish = value?.complete === true && after === 'idle' && (before === 'output' || before === 'tools');
45
+ if (after === 'idle' && value?.complete === true && (before === 'output' || before === 'tools')) {
46
+ finishUntil = now + FINISH_MS;
47
+ } else if (after !== 'idle' || value?.complete !== true) {
48
+ finishUntil = 0;
29
49
  }
30
- if (value?.complete !== true) finish = false;
50
+ // Samples must not restart an active clip or reset the scheduler.
31
51
  sample = value;
32
52
  },
33
- step(now, time, ready, words, reported) {
53
+ step(now, dt, eligible, canCue, words, reported) {
34
54
  const activity = kind(sample, now);
35
- if (!activity) { finish = false; return null; }
36
- if (!ready || time < next) return null;
37
- if (!finish && activity === 'idle') return null;
38
- const rate = Math.min(240, Math.max(0, sample.outputPerSecond || 0));
39
- const profile = finish ? 'done' : activity === 'tools' ? 'tools' : rate < 45 ? 'slow' : rate < 120 ? 'medium' : 'fast';
40
- finish = false;
41
- const choices = PROFILES[profile];
42
- const start = count++ % choices.length;
43
- for (let i = 0; i < choices.length; i++) {
44
- const word = words.get(choices[(start + i) % choices.length]);
45
- if (word && !word.lasting && (reported === null || reported.includes(word.id))) {
46
- next = time + Math.max(word.seconds || 0, activity === 'output' ? 3.6 - rate / 120 : 3.6);
47
- return word.id;
55
+ const rate = activity === 'output' && Number.isFinite(sample.outputPerSecond)
56
+ ? Math.max(0, Math.min(240, sample.outputPerSecond)) : 0;
57
+ const targetEnergy = activity === 'output' ? 0.28 + 0.72 * rate / 240 : 0;
58
+ const targetFocus = activity === 'tools' ? 1 : 0;
59
+ const seconds = Number.isFinite(dt) ? Math.max(0, Math.min(dt, 0.25)) : 0;
60
+ pose.cue = null;
61
+ if (!eligible) {
62
+ pose.energy = 0;
63
+ pose.focus = 0;
64
+ pose.phase = 0;
65
+ } else {
66
+ const blend = 1 - Math.exp(-seconds * (targetEnergy || targetFocus ? 5 : 3));
67
+ pose.energy += (targetEnergy - pose.energy) * blend;
68
+ pose.focus += (targetFocus - pose.focus) * blend;
69
+ if (!targetEnergy && pose.energy < .004) pose.energy = 0;
70
+ if (!targetFocus && pose.focus < .004) pose.focus = 0;
71
+ pose.phase = (pose.phase + seconds * (1.5 + pose.energy * 5)) % TAU;
72
+ }
73
+
74
+ if (!activity || now > finishUntil) finishUntil = 0;
75
+ // A suppressed completion is discarded, never replayed after a modal or action.
76
+ if (!eligible || !canCue) {
77
+ finishUntil = 0;
78
+ return pose;
79
+ }
80
+ const finishing = finishUntil > 0;
81
+ if ((!finishing && activity !== 'output' && activity !== 'tools') || now < nextCue) return pose;
82
+ const profile = finishing ? 'done' : activity === 'tools' ? 'tools' : rate < 45 ? 'slow' : rate < 120 ? 'medium' : 'fast';
83
+ const choices = [];
84
+ const allowed = word => word && typeof word.id === 'string' && !word.lasting &&
85
+ (word.kind === 'motion' || word.kind === 'expression') &&
86
+ (reported === null || (Array.isArray(reported) && reported.includes(word.id)));
87
+ for (const id of PROFILES[profile]) {
88
+ const word = words?.get(id);
89
+ if (allowed(word)) choices.push(word);
90
+ }
91
+ if (words) {
92
+ for (const word of words.values()) {
93
+ if (!allowed(word) || STOCK.has(word.id)) continue;
94
+ const category = customCategory(word);
95
+ if (category && (profile === 'tools' ? category === 'thought' : profile === 'done' || profile === 'fast' ? category !== 'thought' : true)) {
96
+ choices.push(word);
97
+ }
48
98
  }
49
99
  }
50
- next = time + 1;
51
- return null;
100
+ if (choices.length) {
101
+ // Rotate the ranked pool, avoiding an immediate repeat when possible.
102
+ let index = rotation++ % choices.length;
103
+ if (choices.length > 1 && choices[index].id === lastCue) index = (index + 1) % choices.length;
104
+ const word = choices[index];
105
+ pose.cue = lastCue = word.id;
106
+ const duration = Number.isFinite(word.seconds) ? Math.max(0, word.seconds) : 0;
107
+ const interval = activity === 'output' ? 3.8 - rate / 240 * 1.5 : 3.8;
108
+ nextCue = now + 1000 * Math.max(interval, duration + 0.35);
109
+ if (finishing) finishUntil = 0;
110
+ } else {
111
+ nextCue = now + 1000;
112
+ if (finishing) finishUntil = 0;
113
+ }
114
+ return pose;
52
115
  },
53
116
  };
54
117
  }
package/web/body-host.js CHANGED
@@ -166,7 +166,7 @@ export function loadBody({ layer, pack, start, theme, bounds, onEvent, onSound,
166
166
  /** Whether stage point `p` is on the body. */
167
167
  hit: (p) => onBody(layout, p),
168
168
  /** Advances the body `dt` seconds; with `draw` false (the pet window is hidden) it need not draw. */
169
- tick(dt, draw = true) { if (ready) post({ t: 'tick', dt, draw }); },
169
+ tick(dt, draw = true, activity = null) { if (ready) post({ t: 'tick', dt, draw, activity }); },
170
170
  do: (word) => post({ t: 'do', word }),
171
171
  walk: (x, run, id) => post({ t: 'walk', x, run, id }),
172
172
  /** One of the pack's own targets, `{ to, run, params }` as the World checked it. */
@@ -99,6 +99,7 @@ addEventListener('message', async (e) => {
99
99
  } else if (!body) {
100
100
  // calls that come before the body is ready have nothing to act on
101
101
  } else if (m.t === 'tick') {
102
+ body.setActivityPose?.(m.activity);
102
103
  body.step(m.dt, m.draw);
103
104
  post({ t: 'frame', layout: body.layout(), events, sounds, z: body.z ?? null });
104
105
  events = []; sounds = [];
package/web/kit/body.js CHANGED
@@ -158,6 +158,8 @@ export function createPet(els, opts) {
158
158
  pulse: null, walkId: 0, walkWord: null, listening: false, thinking: false, thoughtShown: false, placed: false, noteAt: 0, tearN: 0,
159
159
  cursor: '', status: null,
160
160
  };
161
+ let activityPose = null;
162
+ const activityLook = [0, 0];
161
163
  const pointer = { x: -1e4, y: -1e4, inside: false, vx: 0, samples: [] };
162
164
  let press = null, strokeAcc = 0, petCool = 0;
163
165
  /** Whether the last frame found the horizontal speed not finite: a run of such frames is logged once. */
@@ -714,16 +716,23 @@ export function createPet(els, opts) {
714
716
  function render(draw = true) {
715
717
  const fc = pet._fc || FACES.neutral.f(0), fname = pet._fname || 'neutral';
716
718
  const drag = pet.mode === 'drag';
719
+ // Overlay activity on the existing stance: feet stay anchored and interaction/physics keep control.
720
+ const activity = !press && !pet.listening && !pet.thinking &&
721
+ (pet.mode === 'idle' || pet.mode === 'walk' || pet.mode === 'run') ? activityPose : null;
722
+ const energy = activity?.energy ?? 0, focus = activity?.focus ?? 0;
723
+ const beat = energy ? Math.sin(activity.phase) : 0;
717
724
  const br = Math.sin(T * (pet.mode === 'sleep' ? 1.7 : 2.4));
718
- const sx = (1 + pet.sq * .7) * (1 - .05 * pet.stretch) * (1 - .009 * br);
719
- const sy = (1 - pet.sq) * (1 + .09 * pet.stretch) * (1 + .016 * br);
725
+ const bounce = beat * energy;
726
+ const sx = (1 + pet.sq * .7) * (1 - .05 * pet.stretch) * (1 - .009 * br - .043 * bounce + .025 * focus);
727
+ const sy = (1 - pet.sq) * (1 + .09 * pet.stretch) * (1 + .016 * br + .086 * bounce - .048 * focus);
720
728
  const ax = 128, ay = drag ? 36 : 256;
721
729
  let AX = drag ? pet.dx : pet.x, AY = drag ? pet.dy : pet.fy;
722
730
  if (fc.shake) AX += Math.sin(T * 60) * 1.4;
723
731
  else if (pet.pulse?.kind === 'shiver') AX += Math.sin(T * 75) * 1.1 * envelope((T - pet.pulse.t0) / pet.pulse.dur, .08, .85);
724
732
  // a figure may keep tilt and lean off the whole group and bend its own parts instead
725
- const kx = S * pet.faceVis * sx, ky = S * sy, lean = pet.lean * pet.faceVis;
726
- const rot = custom?.groupTilt ? custom.groupTilt(pet.mode, pet.tilt, lean) : pet.tilt + lean;
733
+ const kx = S * pet.faceVis * sx, ky = S * sy, lean = (pet.lean + 5 * focus + 3 * energy) * pet.faceVis;
734
+ const baseRot = custom?.groupTilt ? custom.groupTilt(pet.mode, pet.tilt, lean) : pet.tilt + lean;
735
+ const rot = baseRot + (2.8 * bounce + 2 * focus) * pet.faceVis;
727
736
  pet.xf = { AX, AY, ax, ay, kx, ky, rot };
728
737
  const extent = figureExtent();
729
738
  if (!drag && extent && Math.min(...sideReach(extent)) < DEFAULT_HALF_WIDTH) {
@@ -745,8 +754,14 @@ export function createPet(els, opts) {
745
754
  else if (pet.swapAge < .16) eyeClose = 1 - (pet.swapAge - .07) / .09;
746
755
  const face = { ...fc, eyes, gap: pet.gap.map(g => Math.min(64, g + pet.talkK * 12)), blush: pet.blushK };
747
756
  const gesture = pet.pulse ? { kind: pet.pulse.kind, k: clamp((T - pet.pulse.t0) / pet.pulse.dur, 0, 1) } : null;
757
+ let look = pet.look;
758
+ if (energy || focus) {
759
+ activityLook[0] = look[0] + beat * 5 * energy + 3 * focus;
760
+ activityLook[1] = look[1] - beat * 3 * energy + 6 * focus;
761
+ look = activityLook;
762
+ }
748
763
  custom.draw(petG, face, {
749
- look: pet.look, legs, low: pet.low, t: T, blink, eyeClose, acc: skin, swing: pet.swing,
764
+ look, legs, low: pet.low, t: T, blink, eyeClose, acc: skin, swing: pet.swing,
750
765
  face: fname, mode: pet.mode, modeT: pet.modeT, talk: pet.talkK, status: pet.status, drowse: pet.drowse, sit: pet.sitK, facing: pet.faceVis, tilt: pet.tilt, lean, groupRot: rot, gesture,
751
766
  });
752
767
 
@@ -928,6 +943,7 @@ export function createPet(els, opts) {
928
943
  custom?.setSkin?.(skin);
929
944
  return {
930
945
  pet, step, render, resize, act, setExpr, doWord, walkTo, stopWalk, toStage, hitPet, busy, layout,
946
+ setActivityPose: (pose) => { activityPose = pose; },
931
947
  pointerDown, pointerMove, pointerUp, pointerLeave, dropAt, shiftDrag,
932
948
  get pressing() { return !!press; },
933
949
  /** Pressed, carried, airborne, walking, running, dancing, turning round, or in a short gesture (nod, wave, bow…): motion that frames far apart show as jumps. */
@@ -1025,6 +1041,7 @@ export function createBody(host, opts) {
1025
1041
  });
1026
1042
  return {
1027
1043
  step(dt, draw = true) { ctl.step(dt); ctl.render(draw); },
1044
+ setActivityPose: ctl.setActivityPose,
1028
1045
  layout: ctl.layout,
1029
1046
  resize: ctl.resize,
1030
1047
  do: ctl.doWord,
package/web/pet-app.js CHANGED
@@ -2942,6 +2942,7 @@ function frame(steps, now, last) {
2942
2942
  }
2943
2943
  }
2944
2944
  framed = true;
2945
+ let activityMoving = false;
2945
2946
  try {
2946
2947
  steps.forEach((dt, i) => {
2947
2948
  T += dt;
@@ -2952,15 +2953,22 @@ function frame(steps, now, last) {
2952
2953
  stepLines();
2953
2954
  stepStatus();
2954
2955
  setBody({ dialogOpen: !!item || !!listen.phase, still: !menu.hidden });
2955
- if (host && drawn && body && !lowMotion()) {
2956
+ let pose = null;
2957
+ if (host) {
2956
2958
  const figure = at();
2957
- const ready = figure?.mode === 'idle' && !figure.busy && !figure.pressing &&
2958
- !bodyState.dialogOpen && !bodyState.still && !bodyState.thinking && !acts.length && T >= actUntil;
2959
- const cue = agentSpeedMotion.step(Date.now(), T, ready, words, body.words);
2960
- if (cue) body.do(cue);
2959
+ const eligible = drawn && !!body && !lowMotion() && !pressing && !figure?.pressing &&
2960
+ !bodyState.dialogOpen && !bodyState.still && !bodyState.thinking && !bodyState.listening && !sliding() &&
2961
+ !acts.length && T >= actUntil;
2962
+ const canCue = eligible && figure?.mode === 'idle' && !figure.busy && !figure.moving;
2963
+ const activity = agentSpeedMotion.step(Date.now(), dt, eligible, canCue, words, body?.words ?? null);
2964
+ if (activity.cue) body.do(activity.cue);
2965
+ if (activity.energy || activity.focus) {
2966
+ pose = activity;
2967
+ activityMoving = true;
2968
+ }
2961
2969
  }
2962
2970
  if (drawnScale.step(dt)) body?.set({ bounds: bounds() });
2963
- body?.tick(dt, drawn && i === steps.length - 1);
2971
+ body?.tick(dt, drawn && i === steps.length - 1, pose);
2964
2972
  });
2965
2973
  if (drawn) {
2966
2974
  const dt = steps.reduce((a, b) => a + b, 0);
@@ -2983,7 +2991,7 @@ function frame(steps, now, last) {
2983
2991
  }
2984
2992
  for (const done of afterFrame.splice(0)) done();
2985
2993
  // a switch of figure or look, or a size on its way, is motion too: at the resting rate it would show as stutter
2986
- const full = drawn && (prefs.lockFrameRate || !!at()?.moving || (!!host && drawnScale.moving) || performance.now() < smoothUntil);
2994
+ const full = drawn && (prefs.lockFrameRate || activityMoving || !!at()?.moving || (!!host && drawnScale.moving) || performance.now() < smoothUntil);
2987
2995
  // a moving rate of 0 draws on every display refresh
2988
2996
  return { fps: full ? prefs.frameRate : drawn ? RESTING_FPS : HIDDEN_FPS, refresh: full };
2989
2997
  }