dsh-quay-bot 1.0.2 → 1.0.6
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.
- package/README.md +28 -105
- package/botscreen/requirements.txt +1 -1
- package/client.js +123 -53
- package/cordis.patch.yml +1 -1
- package/host/api.js +4 -0
- package/host/remote.js +285 -8
- package/host/screen.js +22 -86
- package/package.json +34 -32
- package/remote/bin/dsh-remote.mjs +435 -0
- package/remote/package.json +27 -0
- package/remote/src/config.mjs +183 -0
- package/remote/src/crypto.mjs +220 -0
- package/remote/src/keys.mjs +226 -0
- package/remote/src/proxy.mjs +1028 -0
- package/remote/src/sessions.mjs +163 -0
- package/remote/src/shim.js +838 -0
- package/remote/src/supervisor.mjs +207 -0
- package/remote/src/tunnel.mjs +533 -0
- package/remote/src/upstream.mjs +403 -0
- package/remote/src/util.mjs +149 -0
- package/remote/src/ws.mjs +313 -0
- package/LICENSE +0 -21
- package/avatar/build.mjs +0 -81
- package/avatar/gallery/droid.json +0 -14
- package/avatar/gallery/ghost.json +0 -14
- package/avatar/gallery/grok-pod.json +0 -14
- package/avatar/gallery/guardian.json +0 -14
- package/avatar/gallery/mecha.json +0 -14
- package/avatar/gallery/mochi.json +0 -14
- package/avatar/gallery/neko.json +0 -14
- package/avatar/gallery/orbiter.json +0 -14
- package/avatar/gallery/relay.json +0 -14
- package/avatar/gallery/spark.json +0 -14
- package/avatar/src/bot/clay.ts +0 -135
- package/avatar/src/bot/effects.ts +0 -100
- package/avatar/src/bot/grok-bot.ts +0 -626
- package/avatar/src/bot/index.ts +0 -15
- package/avatar/src/bot/palettes.ts +0 -62
- package/avatar/src/bot/ring.ts +0 -219
- package/avatar/src/bot/scheduler.ts +0 -239
- package/avatar/src/bot/shapes.ts +0 -277
- package/avatar/src/bot/spring.ts +0 -196
- package/avatar/src/core/color.ts +0 -92
- package/avatar/src/core/eyes.ts +0 -297
- package/avatar/src/core/field.ts +0 -482
- package/avatar/src/core/frame.ts +0 -655
- package/avatar/src/core/fx.ts +0 -375
- package/avatar/src/core/index.ts +0 -49
- package/avatar/src/core/paint.ts +0 -331
- package/avatar/src/core/path.ts +0 -495
- package/avatar/src/core/pose.ts +0 -581
- package/avatar/src/core/svg.ts +0 -98
- package/avatar/src/core/types.ts +0 -153
- package/avatar/src/core/validate.ts +0 -296
- package/avatar/src/core/volume.ts +0 -681
- package/avatar/src/entry.ts +0 -260
- package/bin/doctor.mjs +0 -148
- package/botscreen/requirements-offline.txt +0 -11
- package/market/index.json +0 -41
- package/vendor/pip/pip-25.1.1-py3-none-any.whl +0 -0
- package/vendor/pip/setuptools-78.1.1-py3-none-any.whl +0 -0
|
@@ -1,626 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 无框架 GrokBot 引擎控制器 + 原生 Web Component `<grok-bot>`。
|
|
3
|
-
*
|
|
4
|
-
* 特性:
|
|
5
|
-
* - 48 点闭合眼环 + 三缓冲弹簧物理 + 双节奏状态机调度;
|
|
6
|
-
* - 伪 3D 体积切片躯干 + 黏土高光与内阴影 + 轨道粒子 + 通知角标;
|
|
7
|
-
* - IntersectionObserver 离开视口自动暂停;
|
|
8
|
-
* - 休眠降帧(sleepWhenIdle):Idle 且弹簧/眨眼收敛后停掉 rAF,靠精确唤醒定时器恢复。
|
|
9
|
-
*/
|
|
10
|
-
import { computeFrame, DEFAULT_EYE_COLOR } from '../core/frame.ts';
|
|
11
|
-
import { mountCanvas, paintFrame, remountCanvas, type Surface } from '../core/paint.ts';
|
|
12
|
-
import { renderSvg } from '../core/svg.ts';
|
|
13
|
-
import { validateAvatar } from '../core/validate.ts';
|
|
14
|
-
import type { AvatarDefinition, AvatarMood, AvatarState, BadgeKind, PoseLike } from '../core/types.ts';
|
|
15
|
-
import { derivePalette, type BotPalette } from './palettes.ts';
|
|
16
|
-
import { BotScheduler } from './scheduler.ts';
|
|
17
|
-
import { findShape, SHAPES, type ShapeId } from './shapes.ts';
|
|
18
|
-
|
|
19
|
-
const BLEND_MS = 420;
|
|
20
|
-
|
|
21
|
-
export interface GrokBotOptions {
|
|
22
|
-
/** 容器元素(若传则内部创建包装 div + canvas)。 */
|
|
23
|
-
container?: HTMLElement;
|
|
24
|
-
/** 或直接传入现成的 canvas(需位于可定位容器内)。 */
|
|
25
|
-
canvas?: HTMLCanvasElement;
|
|
26
|
-
definition?: AvatarDefinition;
|
|
27
|
-
shape?: ShapeId;
|
|
28
|
-
palette?: string;
|
|
29
|
-
color?: string;
|
|
30
|
-
eyeColor?: string;
|
|
31
|
-
size?: number;
|
|
32
|
-
depth?: number;
|
|
33
|
-
clay?: boolean;
|
|
34
|
-
state?: AvatarState;
|
|
35
|
-
mood?: AvatarMood | 'auto' | null;
|
|
36
|
-
orbit?: boolean;
|
|
37
|
-
badge?: BadgeKind;
|
|
38
|
-
followPointer?: boolean;
|
|
39
|
-
interactive?: boolean;
|
|
40
|
-
reducedMotion?: boolean;
|
|
41
|
-
/** Idle 且弹簧/眨眼收敛后自动暂停 rAF,仅在下次事件时唤醒。默认 false。 */
|
|
42
|
-
sleepWhenIdle?: boolean;
|
|
43
|
-
/** 离开视口时自动暂停渲染循环。默认 true。 */
|
|
44
|
-
observeVisibility?: boolean;
|
|
45
|
-
onFrame?: (info: { mood: AvatarMood; sleeping: boolean; visible: boolean; fps: number }) => void;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export class GrokBot {
|
|
49
|
-
readonly canvas: HTMLCanvasElement;
|
|
50
|
-
readonly wrapper: HTMLElement;
|
|
51
|
-
readonly scheduler: BotScheduler;
|
|
52
|
-
|
|
53
|
-
private def: AvatarDefinition;
|
|
54
|
-
private palette: BotPalette;
|
|
55
|
-
private eyeColorOverride: string | undefined;
|
|
56
|
-
private size: number;
|
|
57
|
-
private depth: number;
|
|
58
|
-
private clay: boolean;
|
|
59
|
-
private orbit: boolean;
|
|
60
|
-
private badge: BadgeKind;
|
|
61
|
-
private followPointer: boolean;
|
|
62
|
-
private interactive: boolean;
|
|
63
|
-
private reducedMotion: boolean;
|
|
64
|
-
private sleepWhenIdle: boolean;
|
|
65
|
-
private surface: Surface;
|
|
66
|
-
|
|
67
|
-
private view = { yaw: 0, pitch: 0 };
|
|
68
|
-
private viewTarget: { yaw: number; pitch: number } | null = null;
|
|
69
|
-
private time = 0;
|
|
70
|
-
private lastTick = 0;
|
|
71
|
-
private rafId: number | null = null;
|
|
72
|
-
private wakeTimer: number | null = null;
|
|
73
|
-
private visible = true;
|
|
74
|
-
private sleeping = false;
|
|
75
|
-
private dirty = false;
|
|
76
|
-
private destroyed = false;
|
|
77
|
-
private dragging: { x: number; y: number; yaw: number; pitch: number; moved: boolean } | null = null;
|
|
78
|
-
|
|
79
|
-
private lastPose: PoseLike | null = null;
|
|
80
|
-
private blendFrom: { pose: PoseLike; start: number } | null = null;
|
|
81
|
-
|
|
82
|
-
private fpsFrames = 0;
|
|
83
|
-
private fpsSince = 0;
|
|
84
|
-
private currentFps = 0;
|
|
85
|
-
private onFrame?: GrokBotOptions['onFrame'];
|
|
86
|
-
|
|
87
|
-
private io: IntersectionObserver | null = null;
|
|
88
|
-
private cleanupListeners: (() => void)[] = [];
|
|
89
|
-
|
|
90
|
-
constructor(options: GrokBotOptions) {
|
|
91
|
-
const def = options.definition ?? findShape(options.shape)?.definition ?? SHAPES[0].definition;
|
|
92
|
-
const res = validateAvatar(def);
|
|
93
|
-
if (!res.ok) throw new Error(`Invalid avatar definition: ${res.errors.join('; ')}`);
|
|
94
|
-
this.def = res.value;
|
|
95
|
-
|
|
96
|
-
this.palette = derivePalette(options.palette ?? options.color ?? '#ff6a1a');
|
|
97
|
-
this.eyeColorOverride = options.eyeColor;
|
|
98
|
-
this.size = Math.max(24, options.size ?? 160);
|
|
99
|
-
this.depth = options.depth ?? 6;
|
|
100
|
-
this.clay = options.clay ?? true;
|
|
101
|
-
this.orbit = options.orbit ?? false;
|
|
102
|
-
this.badge = options.badge ?? 'none';
|
|
103
|
-
this.followPointer = options.followPointer ?? true;
|
|
104
|
-
this.interactive = options.interactive ?? true;
|
|
105
|
-
this.reducedMotion = options.reducedMotion ?? (typeof window !== 'undefined' && window.matchMedia?.('(prefers-reduced-motion: reduce)').matches);
|
|
106
|
-
this.sleepWhenIdle = options.sleepWhenIdle ?? false;
|
|
107
|
-
this.onFrame = options.onFrame;
|
|
108
|
-
|
|
109
|
-
if (options.canvas) {
|
|
110
|
-
this.canvas = options.canvas;
|
|
111
|
-
this.wrapper = options.canvas.parentElement ?? options.canvas;
|
|
112
|
-
} else if (options.container) {
|
|
113
|
-
this.wrapper = document.createElement('div');
|
|
114
|
-
this.wrapper.style.cssText = 'position:relative;display:block;margin:auto';
|
|
115
|
-
this.canvas = document.createElement('canvas');
|
|
116
|
-
this.canvas.setAttribute('role', 'img');
|
|
117
|
-
this.canvas.style.cssText = 'display:block;position:absolute;pointer-events:none';
|
|
118
|
-
this.wrapper.append(this.canvas);
|
|
119
|
-
options.container.append(this.wrapper);
|
|
120
|
-
} else {
|
|
121
|
-
throw new Error('GrokBot requires either container or canvas');
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const lockedMood = options.mood && options.mood !== 'auto' ? options.mood : null;
|
|
125
|
-
this.scheduler = new BotScheduler({
|
|
126
|
-
state: options.state ?? 'idle',
|
|
127
|
-
mood: lockedMood,
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
this.surface = mountCanvas(this.canvas, this.def, this.depth);
|
|
131
|
-
this.applyWrapperSize();
|
|
132
|
-
this.bindEvents(options.observeVisibility !== false);
|
|
133
|
-
this.renderOnce();
|
|
134
|
-
this.wake();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
private applyWrapperSize(): void {
|
|
138
|
-
this.wrapper.style.width = `${this.size}px`;
|
|
139
|
-
this.wrapper.style.height = `${this.size}px`;
|
|
140
|
-
this.canvas.setAttribute('aria-label', `${this.def.name} 头像`);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
private bindEvents(observeVisibility: boolean): void {
|
|
144
|
-
if (typeof window === 'undefined') return;
|
|
145
|
-
|
|
146
|
-
const onPointerMove = (e: PointerEvent) => {
|
|
147
|
-
if (this.dragging) {
|
|
148
|
-
const dx = e.clientX - this.dragging.x;
|
|
149
|
-
const dy = e.clientY - this.dragging.y;
|
|
150
|
-
if (Math.hypot(dx, dy) > 3) this.dragging.moved = true;
|
|
151
|
-
this.viewTarget = null;
|
|
152
|
-
this.view = {
|
|
153
|
-
yaw: this.dragging.yaw + (dx / 160) * 1.6,
|
|
154
|
-
pitch: Math.max(-0.9, Math.min(0.9, this.dragging.pitch - (dy / 160) * 1.2)),
|
|
155
|
-
};
|
|
156
|
-
this.wake();
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (!this.followPointer) {
|
|
160
|
-
this.scheduler.setGazeTarget(0, 0);
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
const rect = this.wrapper.getBoundingClientRect();
|
|
164
|
-
if (rect.width <= 0 || rect.height <= 0) return;
|
|
165
|
-
const cx = rect.left + rect.width / 2;
|
|
166
|
-
const cy = rect.top + rect.height / 2;
|
|
167
|
-
const dx = (e.clientX - cx) / (Math.max(rect.width, 160) * 0.75);
|
|
168
|
-
const dy = (e.clientY - cy) / (Math.max(rect.height, 160) * 0.75);
|
|
169
|
-
const m = Math.hypot(dx, dy);
|
|
170
|
-
const k = m < 1e-9 ? 0 : Math.tanh(m) / m;
|
|
171
|
-
this.scheduler.setGazeTarget(dx * k, dy * k);
|
|
172
|
-
this.wake();
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
window.addEventListener('pointermove', onPointerMove, { passive: true });
|
|
176
|
-
this.cleanupListeners.push(() => window.removeEventListener('pointermove', onPointerMove));
|
|
177
|
-
|
|
178
|
-
if (this.interactive) {
|
|
179
|
-
const target = this.wrapper;
|
|
180
|
-
const onDown = (e: PointerEvent) => {
|
|
181
|
-
this.dragging = { x: e.clientX, y: e.clientY, yaw: this.view.yaw, pitch: this.view.pitch, moved: false };
|
|
182
|
-
target.setPointerCapture?.(e.pointerId);
|
|
183
|
-
this.wake();
|
|
184
|
-
};
|
|
185
|
-
const onUp = () => {
|
|
186
|
-
if (this.dragging && !this.dragging.moved) {
|
|
187
|
-
this.poke();
|
|
188
|
-
}
|
|
189
|
-
this.dragging = null;
|
|
190
|
-
};
|
|
191
|
-
target.addEventListener('pointerdown', onDown);
|
|
192
|
-
target.addEventListener('pointerup', onUp);
|
|
193
|
-
target.addEventListener('pointercancel', onUp);
|
|
194
|
-
target.style.touchAction = 'none';
|
|
195
|
-
this.cleanupListeners.push(() => {
|
|
196
|
-
target.removeEventListener('pointerdown', onDown);
|
|
197
|
-
target.removeEventListener('pointerup', onUp);
|
|
198
|
-
target.removeEventListener('pointercancel', onUp);
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (observeVisibility && typeof IntersectionObserver !== 'undefined') {
|
|
203
|
-
this.io = new IntersectionObserver(
|
|
204
|
-
(entries) => {
|
|
205
|
-
const isVisible = entries.some((entry) => entry.isIntersecting);
|
|
206
|
-
if (isVisible === this.visible) return;
|
|
207
|
-
this.visible = isVisible;
|
|
208
|
-
if (this.visible) {
|
|
209
|
-
this.wake();
|
|
210
|
-
} else {
|
|
211
|
-
this.pauseLoop();
|
|
212
|
-
this.onFrame?.({ mood: this.scheduler.activeMood, sleeping: true, visible: false, fps: 0 });
|
|
213
|
-
}
|
|
214
|
-
},
|
|
215
|
-
{ threshold: 0.01 },
|
|
216
|
-
);
|
|
217
|
-
this.io.observe(this.wrapper);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
private pauseLoop(): void {
|
|
222
|
-
if (this.rafId !== null) {
|
|
223
|
-
cancelAnimationFrame(this.rafId);
|
|
224
|
-
this.rafId = null;
|
|
225
|
-
}
|
|
226
|
-
if (this.wakeTimer !== null) {
|
|
227
|
-
clearTimeout(this.wakeTimer);
|
|
228
|
-
this.wakeTimer = null;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** 捕获屏幕当前最后一帧姿态,作为下次切换动画/状态/表情的过渡起点。 */
|
|
233
|
-
private beginBlend(now = performance.now()): void {
|
|
234
|
-
if (!this.lastPose || this.reducedMotion) return;
|
|
235
|
-
this.blendFrom = { pose: this.lastPose, start: now };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/** 合并同帧多次属性修改,在下一帧 vsync 统一渲染,避免点击回调里同步连画多帧导致首帧掉帧。 */
|
|
239
|
-
private scheduleRender(): void {
|
|
240
|
-
this.dirty = true;
|
|
241
|
-
this.wake();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/** 唤醒动画循环。 */
|
|
245
|
-
wake(): void {
|
|
246
|
-
if (this.destroyed || !this.visible) return;
|
|
247
|
-
if (this.wakeTimer !== null) {
|
|
248
|
-
clearTimeout(this.wakeTimer);
|
|
249
|
-
this.wakeTimer = null;
|
|
250
|
-
}
|
|
251
|
-
this.sleeping = false;
|
|
252
|
-
if (this.rafId === null) {
|
|
253
|
-
this.lastTick = performance.now();
|
|
254
|
-
this.fpsSince = this.lastTick;
|
|
255
|
-
this.fpsFrames = 0;
|
|
256
|
-
this.rafId = requestAnimationFrame(this.tick);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private tick = (now: number): void => {
|
|
261
|
-
if (this.destroyed || !this.visible) {
|
|
262
|
-
this.rafId = null;
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
const dt = Math.min(0.05, Math.max(0, (now - this.lastTick) / 1000));
|
|
266
|
-
this.lastTick = now;
|
|
267
|
-
if (!this.reducedMotion) this.time += dt;
|
|
268
|
-
|
|
269
|
-
const prevMood = this.scheduler.activeMood;
|
|
270
|
-
this.scheduler.update(dt, this.reducedMotion);
|
|
271
|
-
if (this.scheduler.activeMood !== prevMood) {
|
|
272
|
-
this.beginBlend(now);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if (this.viewTarget) {
|
|
276
|
-
if (this.reducedMotion) {
|
|
277
|
-
this.view = { ...this.viewTarget };
|
|
278
|
-
this.viewTarget = null;
|
|
279
|
-
} else {
|
|
280
|
-
const k = 1 - Math.exp(-12 * dt);
|
|
281
|
-
this.view.yaw += (this.viewTarget.yaw - this.view.yaw) * k;
|
|
282
|
-
this.view.pitch += (this.viewTarget.pitch - this.view.pitch) * k;
|
|
283
|
-
if (Math.hypot(this.viewTarget.yaw - this.view.yaw, this.viewTarget.pitch - this.view.pitch) < 1e-3) {
|
|
284
|
-
this.view = { ...this.viewTarget };
|
|
285
|
-
this.viewTarget = null;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
this.dirty = false;
|
|
291
|
-
this.renderOnce(now);
|
|
292
|
-
|
|
293
|
-
this.fpsFrames++;
|
|
294
|
-
if (now - this.fpsSince > 500) {
|
|
295
|
-
this.currentFps = Math.round((this.fpsFrames * 1000) / (now - this.fpsSince));
|
|
296
|
-
this.fpsSince = now;
|
|
297
|
-
this.fpsFrames = 0;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const canSleep =
|
|
301
|
-
this.reducedMotion ||
|
|
302
|
-
(this.sleepWhenIdle &&
|
|
303
|
-
this.scheduler.state === 'idle' &&
|
|
304
|
-
!this.orbit &&
|
|
305
|
-
this.badge === 'none' &&
|
|
306
|
-
!this.dragging &&
|
|
307
|
-
!this.blendFrom &&
|
|
308
|
-
!this.viewTarget &&
|
|
309
|
-
!this.dirty &&
|
|
310
|
-
this.scheduler.eyes.isSettled());
|
|
311
|
-
|
|
312
|
-
if (canSleep) {
|
|
313
|
-
this.sleeping = true;
|
|
314
|
-
this.rafId = null;
|
|
315
|
-
this.onFrame?.({ mood: this.scheduler.activeMood, sleeping: true, visible: this.visible, fps: 0 });
|
|
316
|
-
if (!this.reducedMotion) {
|
|
317
|
-
const delayMs = this.scheduler.nextWakeDelayMs();
|
|
318
|
-
this.wakeTimer = window.setTimeout(() => {
|
|
319
|
-
this.scheduler.advanceSleep(delayMs / 1000);
|
|
320
|
-
this.wake();
|
|
321
|
-
}, delayMs);
|
|
322
|
-
}
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
this.onFrame?.({ mood: this.scheduler.activeMood, sleeping: false, visible: this.visible, fps: this.currentFps });
|
|
327
|
-
this.rafId = requestAnimationFrame(this.tick);
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
private currentRenderOptions(px?: number, now = performance.now()) {
|
|
331
|
-
const look = this.scheduler.eyes.getLookOffset();
|
|
332
|
-
const blendT = this.blendFrom ? Math.min(1, (now - this.blendFrom.start) / BLEND_MS) : 1;
|
|
333
|
-
if (this.blendFrom && blendT >= 1) this.blendFrom = null;
|
|
334
|
-
return {
|
|
335
|
-
color: this.palette.base,
|
|
336
|
-
highlightColor: this.palette.highlight,
|
|
337
|
-
shadowColor: this.palette.shadow,
|
|
338
|
-
eyeColor: this.eyeColorOverride ?? this.palette.eye ?? DEFAULT_EYE_COLOR,
|
|
339
|
-
depth: this.depth,
|
|
340
|
-
clay: this.clay,
|
|
341
|
-
size: px ?? this.size,
|
|
342
|
-
time: this.time,
|
|
343
|
-
stateAge: this.scheduler.stateAge,
|
|
344
|
-
state: this.scheduler.state,
|
|
345
|
-
mood: this.scheduler.activeMood,
|
|
346
|
-
gaze: { x: look.x, y: look.y, weight: 1 },
|
|
347
|
-
view: this.view,
|
|
348
|
-
reducedMotion: this.reducedMotion,
|
|
349
|
-
eyeRings: this.scheduler.eyes.getRings(),
|
|
350
|
-
blinkScale: this.scheduler.eyes.getBlinkScale(),
|
|
351
|
-
orbit: this.orbit,
|
|
352
|
-
badge: this.badge,
|
|
353
|
-
poseBlend: this.blendFrom ? { from: this.blendFrom.pose, t: blendT } : undefined,
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/** 立即渲染一帧到 Canvas。 */
|
|
358
|
-
renderOnce(now = performance.now()): void {
|
|
359
|
-
const frame = computeFrame(this.def, this.currentRenderOptions(undefined, now), this.size);
|
|
360
|
-
this.lastPose = frame.pose;
|
|
361
|
-
paintFrame(this.surface, frame, this.size);
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
// -------------------------------------------------------------------------
|
|
365
|
-
// 公共控制 API
|
|
366
|
-
|
|
367
|
-
setState(state: AvatarState): void {
|
|
368
|
-
if (state === this.scheduler.state) return;
|
|
369
|
-
this.beginBlend();
|
|
370
|
-
this.scheduler.setState(state, this.reducedMotion);
|
|
371
|
-
this.scheduleRender();
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
setMood(mood: AvatarMood | 'auto' | null): void {
|
|
375
|
-
this.beginBlend();
|
|
376
|
-
this.scheduler.setMood(mood === 'auto' ? null : mood, this.reducedMotion);
|
|
377
|
-
this.scheduleRender();
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
setShape(shapeOrDef: ShapeId | AvatarDefinition): void {
|
|
381
|
-
const next = typeof shapeOrDef === 'string' ? findShape(shapeOrDef)?.definition : shapeOrDef;
|
|
382
|
-
if (!next) return;
|
|
383
|
-
const res = validateAvatar(next);
|
|
384
|
-
if (!res.ok) throw new Error(`Invalid avatar definition: ${res.errors.join('; ')}`);
|
|
385
|
-
if (res.value.id === this.def.id && res.value.body.path === this.def.body.path) return;
|
|
386
|
-
this.beginBlend();
|
|
387
|
-
this.def = res.value;
|
|
388
|
-
this.surface = remountCanvas(this.surface, this.def, this.depth);
|
|
389
|
-
this.applyWrapperSize();
|
|
390
|
-
this.scheduleRender();
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
setPalette(idOrColor: string): void {
|
|
394
|
-
this.palette = derivePalette(idOrColor);
|
|
395
|
-
this.scheduleRender();
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
setEyeColor(hex: string | undefined): void {
|
|
399
|
-
this.eyeColorOverride = hex;
|
|
400
|
-
this.scheduleRender();
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
setSize(px: number): void {
|
|
404
|
-
this.size = Math.max(24, px);
|
|
405
|
-
this.applyWrapperSize();
|
|
406
|
-
this.scheduleRender();
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
setDepth(depth: number): void {
|
|
410
|
-
this.depth = depth;
|
|
411
|
-
this.surface = remountCanvas(this.surface, this.def, this.depth);
|
|
412
|
-
this.scheduleRender();
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
setClay(clay: boolean): void {
|
|
416
|
-
this.clay = clay;
|
|
417
|
-
this.scheduleRender();
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
setOrbit(orbit: boolean): void {
|
|
421
|
-
this.orbit = orbit;
|
|
422
|
-
this.scheduleRender();
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
setBadge(badge: BadgeKind): void {
|
|
426
|
-
this.badge = badge;
|
|
427
|
-
this.scheduleRender();
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
setFollowPointer(follow: boolean): void {
|
|
431
|
-
this.followPointer = follow;
|
|
432
|
-
if (!follow) {
|
|
433
|
-
this.scheduler.setGazeTarget(0, 0);
|
|
434
|
-
this.scheduleRender();
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
setReducedMotion(reduced: boolean): void {
|
|
439
|
-
this.reducedMotion = reduced;
|
|
440
|
-
if (reduced) this.blendFrom = null;
|
|
441
|
-
this.scheduler.update(0, reduced);
|
|
442
|
-
this.scheduleRender();
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
setSleepWhenIdle(sleep: boolean): void {
|
|
446
|
-
this.sleepWhenIdle = sleep;
|
|
447
|
-
this.wake();
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
setView(yaw: number, pitch: number, immediate = false): void {
|
|
451
|
-
if (immediate || this.reducedMotion) {
|
|
452
|
-
this.viewTarget = null;
|
|
453
|
-
this.view = { yaw, pitch };
|
|
454
|
-
} else {
|
|
455
|
-
this.viewTarget = { yaw, pitch };
|
|
456
|
-
}
|
|
457
|
-
this.scheduleRender();
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
setGaze(x: number, y: number): void {
|
|
461
|
-
this.scheduler.setGazeTarget(x, y);
|
|
462
|
-
this.wake();
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
poke(mood?: AvatarMood): AvatarMood {
|
|
466
|
-
this.beginBlend();
|
|
467
|
-
const picked = this.scheduler.poke(mood);
|
|
468
|
-
this.scheduleRender();
|
|
469
|
-
return picked;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
toSvg(px = 512): string {
|
|
473
|
-
return renderSvg(this.def, { ...this.currentRenderOptions(px), reducedMotion: true });
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
getDefinition(): AvatarDefinition {
|
|
477
|
-
return this.def;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
getState() {
|
|
481
|
-
return {
|
|
482
|
-
state: this.scheduler.state,
|
|
483
|
-
mood: this.scheduler.activeMood,
|
|
484
|
-
lockedMood: this.scheduler.lockedMood,
|
|
485
|
-
sleeping: this.sleeping,
|
|
486
|
-
visible: this.visible,
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
destroy(): void {
|
|
491
|
-
this.destroyed = true;
|
|
492
|
-
this.pauseLoop();
|
|
493
|
-
this.io?.disconnect();
|
|
494
|
-
for (const off of this.cleanupListeners) off();
|
|
495
|
-
this.cleanupListeners = [];
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
// ---------------------------------------------------------------------------
|
|
500
|
-
// 原生 Web Component:<grok-bot>
|
|
501
|
-
|
|
502
|
-
const ATTRS = [
|
|
503
|
-
'shape',
|
|
504
|
-
'color',
|
|
505
|
-
'palette',
|
|
506
|
-
'eye-color',
|
|
507
|
-
'state',
|
|
508
|
-
'mood',
|
|
509
|
-
'size',
|
|
510
|
-
'depth',
|
|
511
|
-
'clay',
|
|
512
|
-
'orbit',
|
|
513
|
-
'badge',
|
|
514
|
-
'follow',
|
|
515
|
-
'sleep-idle',
|
|
516
|
-
'reduced-motion',
|
|
517
|
-
] as const;
|
|
518
|
-
|
|
519
|
-
export class GrokBotElement extends HTMLElement {
|
|
520
|
-
static observedAttributes = [...ATTRS];
|
|
521
|
-
private bot: GrokBot | null = null;
|
|
522
|
-
private mountEl: HTMLDivElement | null = null;
|
|
523
|
-
|
|
524
|
-
connectedCallback(): void {
|
|
525
|
-
if (!this.shadowRoot) {
|
|
526
|
-
const root = this.attachShadow({ mode: 'open' });
|
|
527
|
-
const style = document.createElement('style');
|
|
528
|
-
style.textContent = `:host{display:inline-flex;align-items:center;justify-content:center;vertical-align:middle;user-select:none}`;
|
|
529
|
-
this.mountEl = document.createElement('div');
|
|
530
|
-
root.append(style, this.mountEl);
|
|
531
|
-
}
|
|
532
|
-
if (!this.bot && this.mountEl) {
|
|
533
|
-
this.bot = new GrokBot({
|
|
534
|
-
container: this.mountEl,
|
|
535
|
-
shape: (this.getAttribute('shape') as ShapeId) || 'circle',
|
|
536
|
-
palette: this.getAttribute('palette') ?? this.getAttribute('color') ?? 'coral',
|
|
537
|
-
eyeColor: this.getAttribute('eye-color') ?? undefined,
|
|
538
|
-
state: (this.getAttribute('state') as AvatarState) || 'idle',
|
|
539
|
-
mood: (this.getAttribute('mood') as AvatarMood | 'auto') || 'auto',
|
|
540
|
-
size: Number(this.getAttribute('size')) || 120,
|
|
541
|
-
depth: this.hasAttribute('depth') ? Number(this.getAttribute('depth')) : 6,
|
|
542
|
-
clay: this.getAttribute('clay') !== 'false',
|
|
543
|
-
orbit: this.hasAttribute('orbit') && this.getAttribute('orbit') !== 'false',
|
|
544
|
-
badge: (this.getAttribute('badge') as BadgeKind) || 'none',
|
|
545
|
-
followPointer: this.getAttribute('follow') !== 'false',
|
|
546
|
-
sleepWhenIdle: this.getAttribute('sleep-idle') !== 'false',
|
|
547
|
-
reducedMotion: this.hasAttribute('reduced-motion') && this.getAttribute('reduced-motion') !== 'false',
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
disconnectedCallback(): void {
|
|
553
|
-
this.bot?.destroy();
|
|
554
|
-
this.bot = null;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
attributeChangedCallback(name: string, _oldVal: string | null, newVal: string | null): void {
|
|
558
|
-
if (!this.bot) return;
|
|
559
|
-
switch (name) {
|
|
560
|
-
case 'shape':
|
|
561
|
-
if (newVal) this.bot.setShape(newVal as ShapeId);
|
|
562
|
-
break;
|
|
563
|
-
case 'color':
|
|
564
|
-
case 'palette':
|
|
565
|
-
if (newVal) this.bot.setPalette(newVal);
|
|
566
|
-
break;
|
|
567
|
-
case 'eye-color':
|
|
568
|
-
this.bot.setEyeColor(newVal ?? undefined);
|
|
569
|
-
break;
|
|
570
|
-
case 'state':
|
|
571
|
-
if (newVal) this.bot.setState(newVal as AvatarState);
|
|
572
|
-
break;
|
|
573
|
-
case 'mood':
|
|
574
|
-
this.bot.setMood((newVal as AvatarMood | 'auto' | null) ?? 'auto');
|
|
575
|
-
break;
|
|
576
|
-
case 'size':
|
|
577
|
-
if (newVal) this.bot.setSize(Number(newVal) || 120);
|
|
578
|
-
break;
|
|
579
|
-
case 'depth':
|
|
580
|
-
this.bot.setDepth(newVal === null ? 6 : Number(newVal));
|
|
581
|
-
break;
|
|
582
|
-
case 'clay':
|
|
583
|
-
this.bot.setClay(newVal !== 'false');
|
|
584
|
-
break;
|
|
585
|
-
case 'orbit':
|
|
586
|
-
this.bot.setOrbit(newVal !== null && newVal !== 'false');
|
|
587
|
-
break;
|
|
588
|
-
case 'badge':
|
|
589
|
-
this.bot.setBadge((newVal as BadgeKind) || 'none');
|
|
590
|
-
break;
|
|
591
|
-
case 'follow':
|
|
592
|
-
this.bot.setFollowPointer(newVal !== 'false');
|
|
593
|
-
break;
|
|
594
|
-
case 'sleep-idle':
|
|
595
|
-
this.bot.setSleepWhenIdle(newVal !== 'false');
|
|
596
|
-
break;
|
|
597
|
-
case 'reduced-motion':
|
|
598
|
-
this.bot.setReducedMotion(newVal !== null && newVal !== 'false');
|
|
599
|
-
break;
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
poke(mood?: AvatarMood): AvatarMood | undefined {
|
|
604
|
-
const res = this.bot?.poke(mood);
|
|
605
|
-
if (res) this.dispatchEvent(new CustomEvent('grok-poke', { detail: { mood: res } }));
|
|
606
|
-
return res;
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
setDefinition(def: AvatarDefinition): void {
|
|
610
|
-
this.bot?.setShape(def);
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
toSvg(px = 512): string {
|
|
614
|
-
return this.bot?.toSvg(px) ?? '';
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
get instance(): GrokBot | null {
|
|
618
|
-
return this.bot;
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
export function registerGrokBot(tagName = 'grok-bot'): void {
|
|
623
|
-
if (typeof customElements !== 'undefined' && !customElements.get(tagName)) {
|
|
624
|
-
customElements.define(tagName, GrokBotElement);
|
|
625
|
-
}
|
|
626
|
-
}
|
package/avatar/src/bot/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Grok 风格 Agent 头像引擎公共导出。
|
|
3
|
-
*
|
|
4
|
-
* - 纯数学与调度层(ring / spring / scheduler / shapes / palettes / effects)零 DOM 依赖,
|
|
5
|
-
* 适合直接嵌入 Web 应用、DeepSeek harness 或转译至 Android Canvas / Flutter。
|
|
6
|
-
* - GrokBot 与 <grok-bot> Web Component 可零依赖嵌入任意宿主。
|
|
7
|
-
*/
|
|
8
|
-
export * from './ring.ts';
|
|
9
|
-
export * from './spring.ts';
|
|
10
|
-
export * from './scheduler.ts';
|
|
11
|
-
export * from './shapes.ts';
|
|
12
|
-
export * from './palettes.ts';
|
|
13
|
-
export * from './effects.ts';
|
|
14
|
-
export * from './clay.ts';
|
|
15
|
-
export * from './grok-bot.ts';
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 12 套黏土主题色板(Base + Highlight + Inner Shadow + Eye Ink + Accent)。
|
|
3
|
-
* 也支持从任意 #rgb/#rrggbb 主色通过 OKLCH 自动派生配套的高光、内阴影和对比墨色。
|
|
4
|
-
*/
|
|
5
|
-
import { autoEyeColor, oklch, parseHex, toHex, toOklch, type Rgb } from '../core/color.ts';
|
|
6
|
-
|
|
7
|
-
export interface BotPalette {
|
|
8
|
-
id: string;
|
|
9
|
-
name: string;
|
|
10
|
-
/** 躯干底层主色。 */
|
|
11
|
-
base: string;
|
|
12
|
-
/** 左上角高光色(径向渐变起始)。 */
|
|
13
|
-
highlight: string;
|
|
14
|
-
/** 右下角内阴影色。 */
|
|
15
|
-
shadow: string;
|
|
16
|
-
/** 眼睛颜色(与主色高对比)。 */
|
|
17
|
-
eye: string;
|
|
18
|
-
/** 轨道粒子 / 强调色。 */
|
|
19
|
-
accent: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const PALETTES: readonly BotPalette[] = [
|
|
23
|
-
{ id: 'coral', name: '赤陶橙', base: '#ff6a1a', highlight: '#ffd4b8', shadow: '#8c2800', eye: '#141622', accent: '#ffaa66' },
|
|
24
|
-
{ id: 'cobalt', name: '钴蓝', base: '#2563eb', highlight: '#bfdbfe', shadow: '#132a73', eye: '#fafaff', accent: '#60a5fa' },
|
|
25
|
-
{ id: 'emerald', name: '薄荷翠', base: '#10b981', highlight: '#ccfbf1', shadow: '#064e3b', eye: '#141622', accent: '#5eead4' },
|
|
26
|
-
{ id: 'amber', name: '琥珀金', base: '#f59e0b', highlight: '#fef3c7', shadow: '#78350f', eye: '#141622', accent: '#fcd34d' },
|
|
27
|
-
{ id: 'crimson', name: '绯红', base: '#ef4444', highlight: '#fecaca', shadow: '#7f1d1d', eye: '#fafaff', accent: '#fca5a5' },
|
|
28
|
-
{ id: 'violet', name: '暮紫', base: '#8b5cf6', highlight: '#ede9fe', shadow: '#4c1d95', eye: '#fafaff', accent: '#c4b5fd' },
|
|
29
|
-
{ id: 'teal', name: '深海青', base: '#0d9488', highlight: '#ccfbf1', shadow: '#11423e', eye: '#fafaff', accent: '#2dd4bf' },
|
|
30
|
-
{ id: 'rose', name: '蔷薇粉', base: '#ec4899', highlight: '#fce7f3', shadow: '#831843', eye: '#fafaff', accent: '#f9a8d4' },
|
|
31
|
-
{ id: 'indigo', name: '靛青', base: '#4f46e5', highlight: '#e0e7ff', shadow: '#1e1b4b', eye: '#fafaff', accent: '#818cf8' },
|
|
32
|
-
{ id: 'lime', name: '青柠', base: '#84cc16', highlight: '#f7fee7', shadow: '#365314', eye: '#141622', accent: '#bef264' },
|
|
33
|
-
{ id: 'obsidian', name: '黑曜石', base: '#27272a', highlight: '#71717a', shadow: '#09090b', eye: '#fafaff', accent: '#a1a1aa' },
|
|
34
|
-
{ id: 'pearl', name: '陶瓷白', base: '#ede9e2', highlight: '#ffffff', shadow: '#9c9284', eye: '#141622', accent: '#ff6a1a' },
|
|
35
|
-
] as const;
|
|
36
|
-
|
|
37
|
-
export const DEFAULT_PALETTE = PALETTES[0];
|
|
38
|
-
|
|
39
|
-
/** 按 ID 或 hex 匹配预设色板。 */
|
|
40
|
-
export function findPalette(idOrColor: string | undefined): BotPalette | undefined {
|
|
41
|
-
if (!idOrColor) return undefined;
|
|
42
|
-
const key = idOrColor.trim().toLowerCase();
|
|
43
|
-
return PALETTES.find((p) => p.id === key || p.base.toLowerCase() === key);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** 从任意合法主色通过 OKLCH 空间自动派生黏土高光色、内阴影色和眼睛颜色。 */
|
|
47
|
-
export function derivePalette(baseColor: string, id = 'custom', name = 'Custom'): BotPalette {
|
|
48
|
-
const preset = findPalette(baseColor);
|
|
49
|
-
if (preset) return preset;
|
|
50
|
-
const rgb: Rgb = parseHex(baseColor) ?? (parseHex(DEFAULT_PALETTE.base) as Rgb);
|
|
51
|
-
const base = toHex(rgb);
|
|
52
|
-
const [L, C, H] = toOklch(rgb);
|
|
53
|
-
const hiL = Math.min(0.97, L + (1 - L) * 0.58);
|
|
54
|
-
const hiC = C * 0.45;
|
|
55
|
-
const shL = Math.max(0.16, L * 0.46);
|
|
56
|
-
const shC = Math.min(0.24, C * 0.95);
|
|
57
|
-
const highlight = toHex(oklch(hiL, hiC, (H + 8) % 360));
|
|
58
|
-
const shadow = toHex(oklch(shL, shC, (H - 12 + 360) % 360));
|
|
59
|
-
const accent = toHex(oklch(Math.min(0.84, L + 0.14), Math.max(0.1, C * 0.85), (H + 16) % 360));
|
|
60
|
-
const eye = toHex(autoEyeColor(rgb));
|
|
61
|
-
return { id, name, base, highlight, shadow, eye, accent };
|
|
62
|
-
}
|