agent-robot-avatar 0.2.0

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.
@@ -0,0 +1,394 @@
1
+ import AgentRobotAvatar, { registerAvatarExtension } from './agent-robot-avatar-extension-host.js';
2
+
3
+ if (!AgentRobotAvatar) {
4
+ throw new Error('Agent Robot Avatar core failed to load.');
5
+ }
6
+
7
+ const proto = AgentRobotAvatar.prototype;
8
+ const baseUpdateLook = proto._updateLook;
9
+ const baseOnPointerMove = proto._onPointerMove;
10
+
11
+ const clamp01 = value => Math.max(0, Math.min(1, value));
12
+ const lerp = (a, b, t) => a + (b - a) * t;
13
+ const outQuint = t => 1 - Math.pow(1 - t, 5);
14
+ const smooth = t => {
15
+ t = clamp01(t);
16
+ return t * t * (3 - 2 * t);
17
+ };
18
+
19
+ const WARNING_TIMING = Object.freeze({
20
+ W1: 480,
21
+ W2: 400,
22
+ W4: 600,
23
+ W5: 200,
24
+ W5P: 300,
25
+ W6: 200,
26
+ W6P: 300,
27
+ W7: 100,
28
+ W8: 800,
29
+ W9: 227,
30
+ });
31
+
32
+ function buildWarningPhases() {
33
+ let cursor = 0;
34
+ const phases = {};
35
+ for (const key of ['W2', 'W4', 'W5', 'W5P', 'W6', 'W6P', 'W7', 'W8', 'W9']) {
36
+ phases[`${key.toLowerCase()}Start`] = cursor;
37
+ cursor += WARNING_TIMING[key];
38
+ phases[`${key.toLowerCase()}End`] = cursor;
39
+ }
40
+ phases.duration = cursor;
41
+ return Object.freeze(phases);
42
+ }
43
+
44
+ const WARNING_PHASES = buildWarningPhases();
45
+
46
+ function segment(t, a, b, from, to) {
47
+ if (t <= a) return from;
48
+ if (t >= b) return to;
49
+ return lerp(from, to, smooth((t - a) / (b - a)));
50
+ }
51
+
52
+ function errorEyeSweep(t) {
53
+ if (t < 0.16) return segment(t, 0, 0.16, 0, -1);
54
+ if (t < 0.40) return segment(t, 0.16, 0.40, -1, 1);
55
+ if (t < 0.64) return segment(t, 0.40, 0.64, 1, -0.88);
56
+ if (t < 0.84) return segment(t, 0.64, 0.84, -0.88, 0.58);
57
+ return segment(t, 0.84, 1, 0.58, 0);
58
+ }
59
+
60
+ function dispatchActionState(instance, state) {
61
+ instance.dispatchEvent(new CustomEvent('face-state', { detail: { state } }));
62
+ }
63
+
64
+ function centerHeadTransform(instance) {
65
+ if (!instance._headMotion) return;
66
+ instance._headMotion.style.transformBox = 'view-box';
67
+ instance._headMotion.style.transformOrigin = '120px 120px';
68
+ }
69
+
70
+ function cancelCustomMotion(instance) {
71
+ instance._systemErrorShake = null;
72
+ instance._warningFx = null;
73
+ instance._look.x = 0;
74
+ instance._look.y = 0;
75
+ }
76
+
77
+ function interruptRunningAction(instance) {
78
+ ++instance._transitionToken;
79
+ instance._inputWanted = false;
80
+ instance._boredRoutine = null;
81
+ instance._boredLookSpeed = null;
82
+ instance._eyeNod = null;
83
+ instance._eyeBob = null;
84
+ instance._angryEyeDrop = null;
85
+ instance._surpriseShake = null;
86
+ cancelCustomMotion(instance);
87
+ if (instance._headMotion?.getAnimations) {
88
+ instance._headMotion.getAnimations().forEach(animation => animation.cancel());
89
+ }
90
+ if (instance._headMotion) instance._headMotion.style.transform = '';
91
+ }
92
+
93
+ function playBuiltInAction(action) {
94
+ if (action === 'idle') return this.reset();
95
+ if (action === 'input' && (this._inputWanted || this._state === 'input')) return this;
96
+ if (action === 'wake' && !this._sleeping && this._state !== 'sleep') return this;
97
+ if (action === 'sleep' && this._sleeping) return this;
98
+
99
+ interruptRunningAction(this);
100
+
101
+ if (action === 'warning') return this.warning();
102
+ if (action === 'system-error' || action === 'connection-error') return this.error();
103
+ if (action === 'blocked' || action === 'policy-blocked') return this.angry();
104
+ return this[action]();
105
+ }
106
+
107
+ const BUILT_IN_ACTIONS = [
108
+ 'idle',
109
+ 'bored',
110
+ 'input',
111
+ 'send',
112
+ 'success',
113
+ 'warning',
114
+ 'error',
115
+ 'system-error',
116
+ 'connection-error',
117
+ 'angry',
118
+ 'blocked',
119
+ 'policy-blocked',
120
+ 'surprise',
121
+ 'sleep',
122
+ 'wake',
123
+ ];
124
+
125
+ const actionHandlers = Object.fromEntries(BUILT_IN_ACTIONS.map(action => [
126
+ action,
127
+ function() { return playBuiltInAction.call(this, action); },
128
+ ]));
129
+
130
+ proto.setPointerFollow = function(enabled = true) {
131
+ this._pointerFollowEnabled = enabled !== false;
132
+ if (!this._pointerFollowEnabled) {
133
+ this._pointer.active = false;
134
+ this._pointer.influence = 0;
135
+ this._pointer.lastMove = 0;
136
+ this._headCenteringUntil = performance.now() + 320;
137
+ }
138
+ return this;
139
+ };
140
+
141
+ proto._onPointerMove = function(event) {
142
+ if (this._pointerFollowEnabled === false) return;
143
+ return baseOnPointerMove.call(this, event);
144
+ };
145
+
146
+ proto._returnToIdle = async function(duration = 320, ease = outQuint) {
147
+ const token = this._transitionToken;
148
+ this.setState('idle', { duration, ease, keepGazeLock: true });
149
+ this._headCenteringUntil = Math.max(this._headCenteringUntil || 0, performance.now() + duration);
150
+ await this._wait(duration);
151
+ if (token === this._transitionToken && this._state === 'idle') this._releaseExpressionLock();
152
+ };
153
+
154
+ proto.send = async function() {
155
+ this.noteActivity();
156
+ this._inputWanted = false;
157
+ if (!(await this._prepareExpression({ normalizePose: true, duration: 180, pause: 320 }))) return;
158
+ const token = this._transitionToken;
159
+ this._eyeNod = { start: performance.now(), duration: 760 };
160
+ this._animateHead([
161
+ { transform: 'translateY(0px)' },
162
+ { transform: 'translateY(5px)', offset: .52 },
163
+ { transform: 'translateY(0px)' }
164
+ ], 920);
165
+ await this._wait(860);
166
+ if (token !== this._transitionToken) return;
167
+ this._eyeNod = null;
168
+ this._releaseExpressionLock();
169
+ };
170
+
171
+ proto.wake = async function() {
172
+ if (!this._sleeping && this._state !== 'sleep') return;
173
+ this._resumeFrames?.();
174
+ const token = ++this._transitionToken;
175
+ this._expressionLock = true;
176
+ this._sleeping = false;
177
+ this._animateHead([
178
+ { transform: 'translateY(4px) scale(1)' },
179
+ { transform: 'translateY(-2px) scale(1.015)', offset: .55 },
180
+ { transform: 'translateY(0) scale(1)' }
181
+ ], 500);
182
+ this.setState('sleepy', { duration: 240, keepGazeLock: true });
183
+ await this._wait(260);
184
+ if (token !== this._transitionToken) return;
185
+ this.setState('surprise', { duration: 120, keepGazeLock: true });
186
+ await this._wait(190);
187
+ if (token !== this._transitionToken) return;
188
+ await this._returnToIdle(300);
189
+ if (token !== this._transitionToken) return;
190
+ this._lastActivity = performance.now();
191
+ };
192
+
193
+ proto.error = async function() {
194
+ this.noteActivity();
195
+ this._inputWanted = false;
196
+ cancelCustomMotion(this);
197
+ if (!(await this._prepareExpression({ normalizePose: true, duration: 180, pause: 240 }))) return;
198
+
199
+ const token = this._transitionToken;
200
+ const eyeDuration = 980;
201
+ const headDuration = 1380;
202
+ this._systemErrorShake = { start: performance.now(), duration: eyeDuration };
203
+ dispatchActionState(this, 'error');
204
+
205
+ centerHeadTransform(this);
206
+ if (this._headMotion?.animate) {
207
+ const anim = this._headMotion.animate([
208
+ { transform: 'translateX(0px)', offset: 0, easing: 'cubic-bezier(.38,0,.28,1)' },
209
+ { transform: 'translateX(-4px)', offset: 0.34, easing: 'cubic-bezier(.35,0,.22,1)' },
210
+ { transform: 'translateX(4px)', offset: 0.72, easing: 'cubic-bezier(.35,0,.22,1)' },
211
+ { transform: 'translateX(0px)', offset: 1 }
212
+ ], { duration: headDuration, easing: 'linear', fill: 'forwards' });
213
+ anim.onfinish = () => { this._headMotion.style.transform = 'translateX(0px)'; };
214
+ }
215
+
216
+ await this._wait(headDuration + 40);
217
+ if (token !== this._transitionToken) return;
218
+ this._systemErrorShake = null;
219
+ this._look.x = 0;
220
+ this._look.y = 0;
221
+ this._releaseExpressionLock();
222
+ dispatchActionState(this, 'idle');
223
+ };
224
+
225
+ proto.warning = async function() {
226
+ this.noteActivity();
227
+ this._inputWanted = false;
228
+ cancelCustomMotion(this);
229
+ if (!(await this._prepareExpression({ normalizePose: true, duration: 200, pause: WARNING_TIMING.W1 - 200 }))) return;
230
+
231
+ const token = this._transitionToken;
232
+ const duration = WARNING_PHASES.duration;
233
+ this._warningFx = { start: performance.now(), duration };
234
+ dispatchActionState(this, 'warning');
235
+
236
+ centerHeadTransform(this);
237
+ if (this._headMotion?.animate) {
238
+ const anim = this._headMotion.animate([
239
+ { transform: 'translateY(0px)', offset: 0 },
240
+ { transform: 'translateY(0px)', offset: WARNING_PHASES.w5Start / duration, easing: 'cubic-bezier(.38,0,.25,1)' },
241
+ { transform: 'translateY(5px)', offset: WARNING_PHASES.w5End / duration, easing: 'cubic-bezier(.35,0,.22,1)' },
242
+ { transform: 'translateY(5px)', offset: WARNING_PHASES.w5pEnd / duration, easing: 'cubic-bezier(.35,0,.22,1)' },
243
+ { transform: 'translateY(-4px)', offset: WARNING_PHASES.w6End / duration },
244
+ { transform: 'translateY(-4px)', offset: WARNING_PHASES.w6pEnd / duration, easing: 'cubic-bezier(.35,0,.22,1)' },
245
+ { transform: 'translateY(0px)', offset: WARNING_PHASES.w7End / duration },
246
+ { transform: 'translateY(0px)', offset: 1 }
247
+ ], { duration, easing: 'linear', fill: 'forwards' });
248
+ anim.onfinish = () => { this._headMotion.style.transform = 'translateY(0px)'; };
249
+ }
250
+
251
+ await this._wait(duration + 40);
252
+ if (token !== this._transitionToken) return;
253
+ this._warningFx = null;
254
+ this._look.x = 0;
255
+ this._look.y = 0;
256
+ this._releaseExpressionLock();
257
+ dispatchActionState(this, 'idle');
258
+ };
259
+
260
+ proto._startBoredRoutine = function(now) {
261
+ this._boredRoutine = {
262
+ index: 0,
263
+ nextAt: now,
264
+ steps: [
265
+ { x: -18, y: -16, hold: 1400, lookSpeed: 0.5 },
266
+ { x: 18, y: -16, hold: 1400, lookSpeed: 0.5 },
267
+ { x: 0, y: 0, hold: 320, lookSpeed: 0.5 },
268
+ ]
269
+ };
270
+ };
271
+
272
+ proto._updateLook = function(now, dt) {
273
+ baseUpdateLook.call(this, now, dt);
274
+
275
+ if (this._systemErrorShake) {
276
+ const t = clamp01((now - this._systemErrorShake.start) / this._systemErrorShake.duration);
277
+ this._look.x = errorEyeSweep(t) * 30;
278
+ this._look.y = 0;
279
+ if (t >= 1) this._systemErrorShake = null;
280
+ }
281
+ };
282
+
283
+ function drawWarning(now) {
284
+ const fx = this._warningFx;
285
+ if (!fx) return;
286
+
287
+ const elapsed = Math.max(0, Math.min(fx.duration, now - fx.start));
288
+
289
+ let proximity = elapsed < WARNING_PHASES.w2End
290
+ ? smooth(elapsed / WARNING_TIMING.W2)
291
+ : 1;
292
+
293
+ let lids = elapsed < WARNING_PHASES.w2End
294
+ ? smooth(elapsed / WARNING_TIMING.W2)
295
+ : 1;
296
+
297
+ const release = elapsed > WARNING_PHASES.w8End
298
+ ? 1 - smooth((elapsed - WARNING_PHASES.w8End) / WARNING_TIMING.W9)
299
+ : 1;
300
+ proximity *= release;
301
+ lids *= release;
302
+
303
+ let lidShift = 0;
304
+ const lightPress = 4.2;
305
+ const w4PressEnd = Math.min(WARNING_PHASES.w4Start + 120, WARNING_PHASES.w4End);
306
+ if (elapsed >= WARNING_PHASES.w4Start && elapsed < WARNING_PHASES.w4End) {
307
+ lidShift = elapsed < w4PressEnd
308
+ ? segment(elapsed, WARNING_PHASES.w4Start, w4PressEnd, 0, lightPress)
309
+ : lightPress;
310
+ } else if (elapsed >= WARNING_PHASES.w5Start && elapsed < WARNING_PHASES.w5End) {
311
+ lidShift = segment(elapsed, WARNING_PHASES.w5Start, WARNING_PHASES.w5End, lightPress, 20.5);
312
+ } else if (elapsed >= WARNING_PHASES.w5pStart && elapsed < WARNING_PHASES.w5pEnd) {
313
+ lidShift = 20.5;
314
+ } else if (elapsed >= WARNING_PHASES.w6Start && elapsed < WARNING_PHASES.w6End) {
315
+ lidShift = segment(elapsed, WARNING_PHASES.w6Start, WARNING_PHASES.w6End, 20.5, -6.5);
316
+ } else if (elapsed >= WARNING_PHASES.w6pStart && elapsed < WARNING_PHASES.w6pEnd) {
317
+ lidShift = -6.5;
318
+ } else if (elapsed >= WARNING_PHASES.w7Start && elapsed < WARNING_PHASES.w7End) {
319
+ lidShift = segment(elapsed, WARNING_PHASES.w7Start, WARNING_PHASES.w7End, -6.5, lightPress);
320
+ } else if (elapsed >= WARNING_PHASES.w8Start && elapsed < WARNING_PHASES.w8End) {
321
+ lidShift = lightPress;
322
+ }
323
+
324
+ let farLowOffset = 0;
325
+ if (elapsed >= WARNING_PHASES.w5Start && elapsed < WARNING_PHASES.w5End) {
326
+ farLowOffset = segment(elapsed, WARNING_PHASES.w5Start, WARNING_PHASES.w5End, 0, 2.4);
327
+ } else if (elapsed >= WARNING_PHASES.w5pStart && elapsed < WARNING_PHASES.w5pEnd) {
328
+ farLowOffset = 2.4;
329
+ } else if (elapsed >= WARNING_PHASES.w6Start && elapsed < WARNING_PHASES.w6End) {
330
+ farLowOffset = segment(elapsed, WARNING_PHASES.w6Start, WARNING_PHASES.w6End, 2.4, 0);
331
+ }
332
+
333
+ let farDeepLidDrop = 0;
334
+ if (elapsed >= WARNING_PHASES.w5Start && elapsed < WARNING_PHASES.w5End) {
335
+ farDeepLidDrop = segment(elapsed, WARNING_PHASES.w5Start, WARNING_PHASES.w5End, 0, 3.5);
336
+ } else if (elapsed >= WARNING_PHASES.w5pStart && elapsed < WARNING_PHASES.w5pEnd) {
337
+ farDeepLidDrop = 3.5;
338
+ } else if (elapsed >= WARNING_PHASES.w6Start && elapsed < WARNING_PHASES.w6End) {
339
+ farDeepLidDrop = segment(elapsed, WARNING_PHASES.w6Start, WARNING_PHASES.w6End, 3.5, 0);
340
+ }
341
+
342
+ const nearSX = lerp(1, 1.48, proximity);
343
+ const nearSY = lerp(1, 1.40, proximity);
344
+ const farSX = lerp(1, 0.78, proximity);
345
+ const farSY = lerp(1, 0.97, proximity);
346
+
347
+ const nearX = lerp(86, 102.0, proximity);
348
+ const farX = lerp(154, 178.5, proximity);
349
+ const nearY = lerp(126, 125.5, proximity);
350
+ const farY = lerp(126, 134.0, proximity) + farLowOffset;
351
+
352
+ this._leftEye.setAttribute(
353
+ 'transform',
354
+ `translate(${nearX.toFixed(2)} ${nearY.toFixed(2)}) scale(${nearSX.toFixed(3)} ${nearSY.toFixed(3)})`
355
+ );
356
+ this._rightEye.setAttribute(
357
+ 'transform',
358
+ `translate(${farX.toFixed(2)} ${farY.toFixed(2)}) scale(${farSX.toFixed(3)} ${farSY.toFixed(3)})`
359
+ );
360
+
361
+ const nearTopY = lerp(-36, -10.0, lids) + lidShift;
362
+ const farTopY = lerp(-36, -18.0, lids) + lidShift * 0.95 + farDeepLidDrop;
363
+ const nearTopRot = lerp(0, 10.0, lids);
364
+ const farTopRot = lerp(0, -7.5, lids);
365
+
366
+ this._leftTop.setAttribute('y', (nearTopY - 90).toFixed(2));
367
+ this._leftTop.setAttribute('height', '90');
368
+ this._leftTop.setAttribute('transform', `rotate(${nearTopRot.toFixed(2)} 0 ${nearTopY.toFixed(2)})`);
369
+
370
+ this._rightTop.setAttribute('y', (farTopY - 90).toFixed(2));
371
+ this._rightTop.setAttribute('height', '90');
372
+ this._rightTop.setAttribute('transform', `rotate(${farTopRot.toFixed(2)} 0 ${farTopY.toFixed(2)})`);
373
+
374
+ const nearBottomY = lerp(36, 35.3, lids);
375
+ const farBottomY = lerp(36, 35.6, lids);
376
+ this._leftBottom.setAttribute('y', nearBottomY.toFixed(2));
377
+ this._leftBottom.setAttribute('height', '90');
378
+ this._leftBottom.setAttribute('transform', `rotate(0 0 ${nearBottomY.toFixed(2)})`);
379
+ this._rightBottom.setAttribute('y', farBottomY.toFixed(2));
380
+ this._rightBottom.setAttribute('height', '90');
381
+ this._rightBottom.setAttribute('transform', `rotate(0 0 ${farBottomY.toFixed(2)})`);
382
+ }
383
+
384
+ registerAvatarExtension({
385
+ name: 'actions',
386
+ actions: actionHandlers,
387
+ reset() {
388
+ interruptRunningAction(this);
389
+ },
390
+ draw: drawWarning,
391
+ });
392
+
393
+ export { AgentRobotAvatar };
394
+ export default AgentRobotAvatar;
@@ -0,0 +1,127 @@
1
+ import AgentRobotAvatar, { registerAvatarExtension } from './agent-robot-avatar-extension-host.js';
2
+
3
+ const proto = AgentRobotAvatar.prototype;
4
+
5
+ const FLASH_DEFAULTS = Object.freeze({
6
+ period: 1000,
7
+ duty: 0.80,
8
+ });
9
+
10
+ const FLASH_MULTIPLIER = Object.freeze({
11
+ input: 1.20,
12
+ send: 1.00,
13
+ success: 1.10,
14
+ warning: 1.00,
15
+ angry: 0.90,
16
+ error: 0.80,
17
+ surprise: 0.80,
18
+ });
19
+
20
+ const runtimeWindow = typeof window !== 'undefined' ? window : null;
21
+ const flashConfig = (runtimeWindow?.AgentRobotAvatarAntennaFlashConfig && typeof runtimeWindow.AgentRobotAvatarAntennaFlashConfig === 'object')
22
+ ? runtimeWindow.AgentRobotAvatarAntennaFlashConfig
23
+ : {};
24
+ for (const [key, value] of Object.entries(FLASH_DEFAULTS)) {
25
+ if (!Number.isFinite(Number(flashConfig[key]))) flashConfig[key] = value;
26
+ }
27
+ if (runtimeWindow) {
28
+ runtimeWindow.AgentRobotAvatarAntennaFlashConfig = flashConfig;
29
+ runtimeWindow.AgentRobotAvatarAntennaFlashDefaults = FLASH_DEFAULTS;
30
+ }
31
+
32
+ function configNumber(key, fallback, min, max) {
33
+ const value = Number(flashConfig[key]);
34
+ if (!Number.isFinite(value)) return fallback;
35
+ return Math.max(min, Math.min(max, value));
36
+ }
37
+
38
+ function flashPeriod(action) {
39
+ const base = configNumber('period', FLASH_DEFAULTS.period, 200, 4000);
40
+ return base * (FLASH_MULTIPLIER[action] || 1);
41
+ }
42
+
43
+ function flashDuty() {
44
+ return configNumber('duty', FLASH_DEFAULTS.duty, 0.05, 0.95);
45
+ }
46
+
47
+ function normalizeFlashAction(action) {
48
+ if (action === 'blocked' || action === 'policy-blocked') return 'angry';
49
+ if (action === 'system-error' || action === 'connection-error') return 'error';
50
+ return action;
51
+ }
52
+
53
+ function beginAntennaFlash(instance, action) {
54
+ const normalized = normalizeFlashAction(action);
55
+ if (!FLASH_MULTIPLIER[normalized]) {
56
+ instance._antennaFlashAction = null;
57
+ return null;
58
+ }
59
+ const token = (instance._antennaFlashToken || 0) + 1;
60
+ instance._antennaFlashToken = token;
61
+ instance._antennaFlashAction = normalized;
62
+ instance._antennaFlashStart = performance.now();
63
+ return token;
64
+ }
65
+
66
+ function clearAntennaFlash(instance, token = null) {
67
+ if (token != null && token !== instance._antennaFlashToken) return;
68
+ instance._antennaFlashAction = null;
69
+ const dot = instance._antennaDot;
70
+ if (dot) dot.style.opacity = '1';
71
+ }
72
+
73
+ proto.setAntennaFlash = function(enabled = true) {
74
+ this._antennaFlashEnabled = enabled !== false;
75
+ if (!this._antennaFlashEnabled) clearAntennaFlash(this);
76
+ return this;
77
+ };
78
+
79
+ function drawAntennaFlash(now) {
80
+ const dot = this._antennaDot;
81
+ if (!dot) return;
82
+
83
+ if (this._antennaFlashAction === 'input' && !this._inputWanted && this._state !== 'input') {
84
+ clearAntennaFlash(this);
85
+ }
86
+
87
+ const action = this._antennaFlashAction;
88
+ const enabled = this._antennaFlashEnabled === true;
89
+ if (!enabled || !action || !FLASH_MULTIPLIER[action]) {
90
+ dot.style.opacity = '1';
91
+ return;
92
+ }
93
+
94
+ const period = flashPeriod(action);
95
+ const phase = ((now - (this._antennaFlashStart || now)) % period) / period;
96
+ dot.style.opacity = phase < flashDuty() ? '1' : '0';
97
+ }
98
+
99
+ registerAvatarExtension({
100
+ name: 'antenna-flash',
101
+ beforePlay(name, context) {
102
+ const action = normalizeFlashAction(name);
103
+ context.antennaFlashAction = action;
104
+ context.antennaFlashToken = FLASH_MULTIPLIER[action]
105
+ ? beginAntennaFlash(this, action)
106
+ : null;
107
+ if (!context.antennaFlashToken) clearAntennaFlash(this);
108
+ },
109
+ afterPlay(_name, result, context) {
110
+ const action = context.antennaFlashAction;
111
+ const token = context.antennaFlashToken;
112
+ if (context.error) {
113
+ clearAntennaFlash(this, token);
114
+ return;
115
+ }
116
+ if (token && action !== 'input' && result && typeof result.then === 'function') {
117
+ Promise.resolve(result).finally(() => clearAntennaFlash(this, token));
118
+ }
119
+ },
120
+ reset() {
121
+ clearAntennaFlash(this);
122
+ },
123
+ draw: drawAntennaFlash,
124
+ });
125
+
126
+ export { AgentRobotAvatar, FLASH_DEFAULTS };
127
+ export default AgentRobotAvatar;
@@ -0,0 +1,173 @@
1
+ import AgentRobotAvatar, { registerAvatarExtension } from './agent-robot-avatar-extension-host.js';
2
+
3
+ const SVG_NS = 'http://www.w3.org/2000/svg';
4
+ const HEAD_HEIGHT_SCALE = 0.9;
5
+ const ANTENNA_DEFAULTS = Object.freeze({
6
+ x: 120,
7
+ y: 12,
8
+ radius: 15,
9
+ stiffness: 180,
10
+ damping: 5.6,
11
+ });
12
+
13
+ const runtimeWindow = typeof window !== 'undefined' ? window : null;
14
+ const antennaConfig = (runtimeWindow?.AgentRobotAvatarAntennaConfig && typeof runtimeWindow.AgentRobotAvatarAntennaConfig === 'object')
15
+ ? runtimeWindow.AgentRobotAvatarAntennaConfig
16
+ : {};
17
+ for (const [key, value] of Object.entries(ANTENNA_DEFAULTS)) {
18
+ if (!Number.isFinite(Number(antennaConfig[key]))) antennaConfig[key] = value;
19
+ }
20
+ if (runtimeWindow) {
21
+ runtimeWindow.AgentRobotAvatarAntennaConfig = antennaConfig;
22
+ runtimeWindow.AgentRobotAvatarAntennaDefaults = ANTENNA_DEFAULTS;
23
+ }
24
+
25
+ function configNumber(key, fallback, min, max) {
26
+ const value = Number(antennaConfig[key]);
27
+ if (!Number.isFinite(value)) return fallback;
28
+ return Math.max(min, Math.min(max, value));
29
+ }
30
+
31
+ function antennaHome() {
32
+ return {
33
+ x: configNumber('x', ANTENNA_DEFAULTS.x, 60, 180),
34
+ y: configNumber('y', ANTENNA_DEFAULTS.y, -40, 60),
35
+ };
36
+ }
37
+
38
+ function smooth01(t) {
39
+ t = Math.max(0, Math.min(1, t));
40
+ return t * t * (3 - 2 * t);
41
+ }
42
+
43
+ function flattenHead(instance) {
44
+ if (instance._headFlattenedR32 || !instance._headShape || !instance._baseHeadPoints?.length) return;
45
+
46
+ const source = instance._baseHeadPoints.map(point => ({ x: point.x, y: point.y }));
47
+ const minY = Math.min(...source.map(point => point.y));
48
+ const maxY = Math.max(...source.map(point => point.y));
49
+ const height = maxY - minY;
50
+ const centerY = (minY + maxY) / 2;
51
+ const inset = height * (1 - HEAD_HEIGHT_SCALE) / 2;
52
+
53
+ const upperLockY = minY + height * 0.40;
54
+ const lowerLockY = maxY - height * 0.40;
55
+
56
+ const flattened = source.map(point => {
57
+ let shiftY = 0;
58
+ if (point.y <= upperLockY) {
59
+ shiftY = inset;
60
+ } else if (point.y < centerY) {
61
+ const t = (centerY - point.y) / Math.max(0.001, centerY - upperLockY);
62
+ shiftY = inset * smooth01(t);
63
+ } else if (point.y >= lowerLockY) {
64
+ shiftY = -inset;
65
+ } else if (point.y > centerY) {
66
+ const t = (point.y - centerY) / Math.max(0.001, lowerLockY - centerY);
67
+ shiftY = -inset * smooth01(t);
68
+ }
69
+ return { x: point.x, y: point.y + shiftY };
70
+ });
71
+
72
+ const path = typeof instance._pointsToPath === 'function'
73
+ ? instance._pointsToPath(flattened)
74
+ : `M ${flattened.map((point, index) => `${index ? 'L ' : ''}${point.x.toFixed(2)} ${point.y.toFixed(2)}`).join(' ')} Z`;
75
+
76
+ instance._baseHeadPoints = flattened;
77
+ instance._baseHeadPathD = path;
78
+ instance._headShape.setAttribute('d', path);
79
+ instance._headFlattenedR32 = true;
80
+ }
81
+
82
+ function ensureAntenna(instance, now) {
83
+ flattenHead(instance);
84
+ if (instance._antennaDot?.isConnected && instance._antennaSpring) return true;
85
+
86
+ const svg = instance.shadowRoot?.querySelector('svg');
87
+ if (!svg || !instance._headMotion) return false;
88
+
89
+ const home = antennaHome();
90
+ let dot = instance.shadowRoot.getElementById('antennaDot');
91
+ if (!dot) {
92
+ dot = document.createElementNS(SVG_NS, 'circle');
93
+ dot.id = 'antennaDot';
94
+ dot.setAttribute('cx', String(home.x));
95
+ dot.setAttribute('cy', String(home.y));
96
+ dot.setAttribute('r', String(configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40)));
97
+ dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
98
+ dot.setAttribute('aria-hidden', 'true');
99
+ dot.style.pointerEvents = 'none';
100
+ svg.appendChild(dot);
101
+ }
102
+
103
+ instance._antennaDot = dot;
104
+ instance._antennaSpring = {
105
+ x: home.x,
106
+ y: home.y,
107
+ vx: 0,
108
+ vy: 0,
109
+ last: now,
110
+ initialized: false,
111
+ };
112
+ return true;
113
+ }
114
+
115
+ function antennaTarget(instance) {
116
+ const svg = instance.shadowRoot?.querySelector('svg');
117
+ const headMatrix = instance._headMotion?.getScreenCTM?.();
118
+ const svgMatrix = svg?.getScreenCTM?.();
119
+ const home = antennaHome();
120
+ if (!svg || !headMatrix || !svgMatrix || typeof DOMPoint === 'undefined') return home;
121
+
122
+ try {
123
+ const screenPoint = new DOMPoint(home.x, home.y).matrixTransform(headMatrix);
124
+ const localPoint = screenPoint.matrixTransform(svgMatrix.inverse());
125
+ return { x: localPoint.x, y: localPoint.y };
126
+ } catch (_) {
127
+ return home;
128
+ }
129
+ }
130
+
131
+ function updateAntenna(instance, now) {
132
+ if (!ensureAntenna(instance, now)) return;
133
+
134
+ const spring = instance._antennaSpring;
135
+ const dot = instance._antennaDot;
136
+ const target = antennaTarget(instance);
137
+
138
+ if (!spring.initialized) {
139
+ spring.x = target.x;
140
+ spring.y = target.y;
141
+ spring.last = now;
142
+ spring.initialized = true;
143
+ }
144
+
145
+ const dt = Math.max(0.001, Math.min(0.05, (now - spring.last) / 1000 || 0.033));
146
+ spring.last = now;
147
+
148
+ const stiffness = configNumber('stiffness', ANTENNA_DEFAULTS.stiffness, 5, 300);
149
+ const damping = configNumber('damping', ANTENNA_DEFAULTS.damping, 0.5, 40);
150
+ const ax = (target.x - spring.x) * stiffness - spring.vx * damping;
151
+ const ay = (target.y - spring.y) * stiffness - spring.vy * damping;
152
+ spring.vx += ax * dt;
153
+ spring.vy += ay * dt;
154
+ spring.x += spring.vx * dt;
155
+ spring.y += spring.vy * dt;
156
+
157
+ dot.setAttribute('cx', spring.x.toFixed(2));
158
+ dot.setAttribute('cy', spring.y.toFixed(2));
159
+ dot.setAttribute('r', configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40).toFixed(2));
160
+ dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
161
+ }
162
+
163
+ function drawAntenna(now) {
164
+ updateAntenna(this, now);
165
+ }
166
+
167
+ registerAvatarExtension({
168
+ name: 'antenna',
169
+ draw: drawAntenna,
170
+ });
171
+
172
+ export { AgentRobotAvatar, ANTENNA_DEFAULTS };
173
+ export default AgentRobotAvatar;