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.
Files changed (61) hide show
  1. package/README.md +28 -105
  2. package/botscreen/requirements.txt +1 -1
  3. package/client.js +123 -53
  4. package/cordis.patch.yml +1 -1
  5. package/host/api.js +4 -0
  6. package/host/remote.js +285 -8
  7. package/host/screen.js +22 -86
  8. package/package.json +34 -32
  9. package/remote/bin/dsh-remote.mjs +435 -0
  10. package/remote/package.json +27 -0
  11. package/remote/src/config.mjs +183 -0
  12. package/remote/src/crypto.mjs +220 -0
  13. package/remote/src/keys.mjs +226 -0
  14. package/remote/src/proxy.mjs +1028 -0
  15. package/remote/src/sessions.mjs +163 -0
  16. package/remote/src/shim.js +838 -0
  17. package/remote/src/supervisor.mjs +207 -0
  18. package/remote/src/tunnel.mjs +533 -0
  19. package/remote/src/upstream.mjs +403 -0
  20. package/remote/src/util.mjs +149 -0
  21. package/remote/src/ws.mjs +313 -0
  22. package/LICENSE +0 -21
  23. package/avatar/build.mjs +0 -81
  24. package/avatar/gallery/droid.json +0 -14
  25. package/avatar/gallery/ghost.json +0 -14
  26. package/avatar/gallery/grok-pod.json +0 -14
  27. package/avatar/gallery/guardian.json +0 -14
  28. package/avatar/gallery/mecha.json +0 -14
  29. package/avatar/gallery/mochi.json +0 -14
  30. package/avatar/gallery/neko.json +0 -14
  31. package/avatar/gallery/orbiter.json +0 -14
  32. package/avatar/gallery/relay.json +0 -14
  33. package/avatar/gallery/spark.json +0 -14
  34. package/avatar/src/bot/clay.ts +0 -135
  35. package/avatar/src/bot/effects.ts +0 -100
  36. package/avatar/src/bot/grok-bot.ts +0 -626
  37. package/avatar/src/bot/index.ts +0 -15
  38. package/avatar/src/bot/palettes.ts +0 -62
  39. package/avatar/src/bot/ring.ts +0 -219
  40. package/avatar/src/bot/scheduler.ts +0 -239
  41. package/avatar/src/bot/shapes.ts +0 -277
  42. package/avatar/src/bot/spring.ts +0 -196
  43. package/avatar/src/core/color.ts +0 -92
  44. package/avatar/src/core/eyes.ts +0 -297
  45. package/avatar/src/core/field.ts +0 -482
  46. package/avatar/src/core/frame.ts +0 -655
  47. package/avatar/src/core/fx.ts +0 -375
  48. package/avatar/src/core/index.ts +0 -49
  49. package/avatar/src/core/paint.ts +0 -331
  50. package/avatar/src/core/path.ts +0 -495
  51. package/avatar/src/core/pose.ts +0 -581
  52. package/avatar/src/core/svg.ts +0 -98
  53. package/avatar/src/core/types.ts +0 -153
  54. package/avatar/src/core/validate.ts +0 -296
  55. package/avatar/src/core/volume.ts +0 -681
  56. package/avatar/src/entry.ts +0 -260
  57. package/bin/doctor.mjs +0 -148
  58. package/botscreen/requirements-offline.txt +0 -11
  59. package/market/index.json +0 -41
  60. package/vendor/pip/pip-25.1.1-py3-none-any.whl +0 -0
  61. package/vendor/pip/setuptools-78.1.1-py3-none-any.whl +0 -0
@@ -1,581 +0,0 @@
1
- /**
2
- * 确定性姿态模型。姿态是 (seed, time, state, mood, gaze, reducedMotion) 的纯函数:
3
- * 没有墙上时钟、没有 Math.random,因此任何一帧都能被精确复现(测试、SSR、拖动进度)。
4
- *
5
- * 状态(state)决定“在干什么”:空闲、思考、工作、等待、阻塞、完成。
6
- * 表情(mood)决定“看起来怎么样”:中性、开心、困倦、惊讶、好奇、不爽、爱心。
7
- * 两者正交:`sleepy` 的困倦可以叠在 `working` 的忙碌上。
8
- */
9
- import type { AvatarMood, AvatarState, EyeLook, PoseLike, RenderOptions } from './types.ts';
10
-
11
- export type { EyeLook } from './types.ts';
12
-
13
- const REST_LOOK: EyeLook = { arc: 0, slit: 0, wide: 0, star: 0, tilt: 0, lid: 0, glint: 0.35 };
14
-
15
- function look(patch: Partial<EyeLook>): EyeLook {
16
- return { ...REST_LOOK, ...patch };
17
- }
18
-
19
- /**
20
- * 每个表情的眼睛配方(15 种)。0.35 的默认高光是“活着”的最低成本:一点反光就够。
21
- */
22
- const MOOD_LOOK: Record<AvatarMood, EyeLook> = {
23
- neutral: look({}),
24
- happy: look({ arc: 1, glint: 0.45 }),
25
- joy: look({ arc: 1, wide: 0.25, glint: 0.7 }),
26
- love: look({ star: 1, glint: 0.95 }),
27
- surprised: look({ wide: 1, glint: 0.9 }),
28
- curious: look({ wide: 0.35, tilt: 0.4, glint: 0.6 }),
29
- skeptical: look({ lid: 0.32, tilt: 0.22, glint: 0.4 }),
30
- sleepy: look({ lid: 0.55, glint: 0.12 }),
31
- bored: look({ lid: 0.36, slit: 0.25, glint: 0.15 }),
32
- sad: look({ lid: 0.3, tilt: 0.5, glint: 0.2 }),
33
- angry: look({ slit: 0.78, tilt: -0.8, glint: 0.1 }),
34
- focused: look({ slit: 0.42, wide: 0.1, glint: 0.5 }),
35
- confused: look({ wide: 0.2, tilt: 0.6, glint: 0.4 }),
36
- shy: look({ lid: 0.28, glint: 0.3 }),
37
- smug: look({ slit: 0.32, lid: 0.28, tilt: -0.25, glint: 0.45 }),
38
- };
39
-
40
- export function moodLook(mood: AvatarMood): EyeLook {
41
- return { ...(MOOD_LOOK[mood] ?? REST_LOOK) };
42
- }
43
-
44
- export type Pose = PoseLike;
45
-
46
- export const STATES: readonly AvatarState[] = ['idle', 'thinking', 'working', 'waiting', 'blocked', 'done', 'alert'];
47
- export const MOODS: readonly AvatarMood[] = [
48
- 'neutral',
49
- 'happy',
50
- 'joy',
51
- 'love',
52
- 'surprised',
53
- 'curious',
54
- 'skeptical',
55
- 'sleepy',
56
- 'bored',
57
- 'sad',
58
- 'angry',
59
- 'focused',
60
- 'confused',
61
- 'shy',
62
- 'smug',
63
- ];
64
-
65
- export function isState(value: unknown): value is AvatarState {
66
- return STATES.includes(value as AvatarState);
67
- }
68
-
69
- export function isMood(value: unknown): value is AvatarMood {
70
- return MOODS.includes(value as AvatarMood);
71
- }
72
-
73
- export function resolveLook(given: { state?: unknown; mood?: unknown }): { state: AvatarState; mood: AvatarMood } {
74
- const m = given.mood === 'grumpy' ? 'angry' : given.mood;
75
- return {
76
- state: isState(given.state) ? given.state : 'idle',
77
- mood: isMood(m) ? m : 'neutral',
78
- };
79
- }
80
-
81
- /** 静止视角:稍微侧一点、稍微俯一点,体积感能被读到但不抢戏。 */
82
- export const BASE_YAW = -0.3;
83
- export const BASE_PITCH = 0.2;
84
- /** 跟随注视目标时,身体最多转这么多(弧度),眼睛只用其中一部分。 */
85
- export const LOOK_YAW = 0.32;
86
- export const LOOK_PITCH = 0.18;
87
- export const LOOK_EYES = 0.7;
88
-
89
- const TAU = Math.PI * 2;
90
-
91
- export function hashString(s: string): number {
92
- let h = 2166136261;
93
- for (let i = 0; i < s.length; i++) {
94
- h ^= s.charCodeAt(i);
95
- h = Math.imul(h, 16777619);
96
- }
97
- return h >>> 0;
98
- }
99
-
100
- /** 无状态 hash 噪声,[0, 1)。 */
101
- export function noise(seed: number, n: number): number {
102
- let h = (seed ^ Math.imul(n | 0, 0x9e3779b1)) >>> 0;
103
- h = Math.imul(h ^ (h >>> 16), 0x85ebca6b);
104
- h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35);
105
- h ^= h >>> 16;
106
- return (h >>> 0) / 4294967296;
107
- }
108
-
109
- const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));
110
- const smooth = (u: number) => {
111
- const x = clamp(u, 0, 1);
112
- return x * x * (3 - 2 * x);
113
- };
114
- const mix = (a: number, b: number, k: number) => a + (b - a) * k;
115
-
116
- /**
117
- * 眨眼闭合 0..1:默认每约 4.2 s 一个槽位,相位由 seed 决定,偶尔连眨两下。
118
- * 节奏可以按表情改:困的时候槽位更长、闭合更久;惊讶时更短促。
119
- */
120
- export function blinkAt(seed: number, t: number, rhythm: { slot?: number; dur?: number; double?: number } = {}): number {
121
- const SLOT = rhythm.slot ?? 4.2;
122
- const DUR = rhythm.dur ?? 0.19;
123
- const doubleChance = rhythm.double ?? 0.8;
124
- const slot = Math.floor(t / SLOT);
125
- let closure = 0;
126
- for (const s of [slot - 1, slot]) {
127
- const start = s * SLOT + 0.5 + noise(seed, s * 2) * (SLOT - 1.3);
128
- const starts = noise(seed, s * 2 + 1) > doubleChance ? [start, start + DUR * 1.8] : [start];
129
- for (const b of starts) {
130
- const u = (t - b) / DUR;
131
- if (u > 0 && u < 1) closure = Math.max(closure, Math.sin(Math.PI * u));
132
- }
133
- }
134
- return closure;
135
- }
136
-
137
- /** 空闲时的视线游走:短暂停留之间快速扫视,多数时候居中。 */
138
- export function wanderAt(seed: number, t: number, reach = 1): [number, number] {
139
- const SLOT = 1.9;
140
- const s = Math.floor(t / SLOT);
141
- const target = (k: number): [number, number] => {
142
- if (noise(seed, 1000 + k * 3) < 0.4) return [0, 0];
143
- return [(noise(seed, 1001 + k * 3) * 2 - 1) * 0.6 * reach, (noise(seed, 1002 + k * 3) * 2 - 1) * 0.35 * reach];
144
- };
145
- const a = target(s - 1);
146
- const b = target(s);
147
- const e = smooth((t - s * SLOT) / 0.16);
148
- return [a[0] + (b[0] - a[0]) * e, a[1] + (b[1] - a[1]) * e];
149
- }
150
-
151
- /** 工作时:视线在几件事之间快速跳动,多数时候朝下看。 */
152
- function dartAt(seed: number, t: number): [number, number] {
153
- const SLOT = 0.62;
154
- const s = Math.floor(t / SLOT);
155
- const target = (k: number): [number, number] => [(noise(seed, 2001 + k * 3) * 2 - 1) * 0.8, 0.1 + noise(seed, 2002 + k * 3) * 0.5];
156
- const a = target(s - 1);
157
- const b = target(s);
158
- const e = smooth((t - s * SLOT) / 0.12);
159
- return [mix(a[0], b[0], e), mix(a[1], b[1], e)];
160
- }
161
-
162
- /**
163
- * 微表情:空闲时每隔几秒闪一下别的表情(惊讶一瞬、眯眼一瞬),
164
- * 让它看起来“在想事情”。返回一个 0..1 的权重与对应表情。
165
- */
166
- export function microAt(seed: number, t: number): { mood: AvatarMood; weight: number } | null {
167
- const SLOT = 3.4;
168
- const s = Math.floor(t / SLOT);
169
- if (noise(seed, 3000 + s) > 0.42) return null;
170
- const start = s * SLOT + 0.6 + noise(seed, 3100 + s) * 2.1;
171
- const dur = 0.26 + noise(seed, 3200 + s) * 0.3;
172
- const u = (t - start) / dur;
173
- if (u <= 0 || u >= 1) return null;
174
- const pick: AvatarMood[] = ['surprised', 'curious', 'angry', 'sleepy', 'skeptical', 'shy'];
175
- const mood = pick[Math.floor(noise(seed, 3300 + s) * pick.length) % pick.length];
176
- return { mood, weight: Math.sin(Math.PI * u) * 0.8 };
177
- }
178
-
179
- export interface PoseInput {
180
- seed: number;
181
- time: number;
182
- state?: AvatarState;
183
- mood?: AvatarMood;
184
- /** 额外叠加的表情(瞬时反应),按权重混进 mood 的眼睛配方。 */
185
- expression?: { mood: AvatarMood; weight?: number };
186
- stateAge?: number;
187
- gaze?: { x: number; y: number; weight?: number };
188
- reducedMotion: boolean;
189
- view?: { yaw: number; pitch: number };
190
- }
191
-
192
- /** 两个姿态之间的一段(同一时刻取两边,于是各自的动效仍在跑)。 */
193
- export function blendPoses(a: Pose, b: Pose, t: number): Pose {
194
- const k = clamp(t, 0, 1);
195
- const m = (x: number, y: number) => mix(x, y, k);
196
- const sweep = a.ribbons > 0 && b.ribbons > 0 ? m(a.sweep, b.sweep) : a.ribbons > 0 ? a.sweep : b.sweep;
197
- const cage = a.ribbons > 0 && b.ribbons > 0 ? m(a.cage, b.cage) : a.ribbons > 0 ? a.cage : b.cage;
198
- return {
199
- yaw: m(a.yaw, b.yaw),
200
- pitch: m(a.pitch, b.pitch),
201
- roll: m(a.roll, b.roll),
202
- lift: m(a.lift, b.lift),
203
- sx: m(a.sx, b.sx),
204
- sy: m(a.sy, b.sy),
205
- gazeX: m(a.gazeX, b.gazeX),
206
- gazeY: m(a.gazeY, b.gazeY),
207
- blink: m(a.blink, b.blink),
208
- squintL: m(a.squintL, b.squintL),
209
- squintR: m(a.squintR, b.squintR),
210
- eyes: {
211
- arc: m(a.eyes.arc, b.eyes.arc),
212
- slit: m(a.eyes.slit, b.eyes.slit),
213
- wide: m(a.eyes.wide, b.eyes.wide),
214
- star: m(a.eyes.star, b.eyes.star),
215
- tilt: m(a.eyes.tilt, b.eyes.tilt),
216
- lid: m(a.eyes.lid, b.eyes.lid),
217
- glint: m(a.eyes.glint, b.eyes.glint),
218
- },
219
- ribbons: m(a.ribbons, b.ribbons),
220
- cage,
221
- sweep,
222
- bang: m(a.bang, b.bang),
223
- dots: m(a.dots, b.dots),
224
- bloom: m(a.bloom, b.bloom),
225
- // 粒子的时间轴不插值:新的一侧在场就用它的,否则粒子的运动会“倒放”。
226
- bloomAge: b.bloom > 0 ? b.bloomAge : a.bloomAge,
227
- };
228
- }
229
-
230
- /** 完成时的庆祝节拍:3.87 s 转完并收回,整轮 6.2 s 起,每个头像略微不同。 */
231
- export const DONE_TURN_S = 3.87;
232
- export const DONE_CYCLE_S = 6.2;
233
- const DONE_SWEEPS = 9;
234
-
235
- const WORK_FIRST_S = 1.2;
236
- const WORK_FIRST_JITTER_S = 1.2;
237
- const WORK_GAP_S = 7.5;
238
- const WORK_JITTER_S = 1.5;
239
- const WORK_TURN_S = 1.5;
240
- const WORK_EASE = 6.2;
241
-
242
- /** 工作状态下 `age` 秒时,正处于第几秒的绕圈中;两圈之间为 null。 */
243
- function workTurn(seed: number, age: number): number | null {
244
- const first = WORK_FIRST_S + WORK_FIRST_JITTER_S * noise(seed, 41);
245
- if (age < first) return null;
246
- const n = Math.floor((age - first) / WORK_GAP_S);
247
- for (const k of [n, n - 1]) {
248
- if (k < 0) continue;
249
- const start = first + k * WORK_GAP_S + (k ? WORK_JITTER_S * noise(seed, 60 + k) : 0);
250
- if (age >= start && age < start + WORK_TURN_S) return age - start;
251
- }
252
- return null;
253
- }
254
-
255
- const near = (angle: number) => angle - TAU * Math.round(angle / TAU);
256
-
257
- const CELEBRATION_END_S = 3.79;
258
- const CELEBRATION_SPEED = (DONE_SWEEPS * TAU + 0.5) / 2.4625;
259
-
260
- /** 庆祝时彩带在 `t` 秒走过了多少弧度:先反向蓄 0.24 s,加速,再从 2.54 s 减速落在整数圈。 */
261
- function celebration(t: number): number {
262
- const v = CELEBRATION_SPEED;
263
- if (t <= 0) return 0;
264
- if (t < 0.24) return -0.25 * (1 - Math.cos((Math.PI * t) / 0.24));
265
- if (t < 0.54) return -0.5 + (v * (t - 0.24) ** 2) / 0.6;
266
- if (t < 2.54) return -0.5 + v * (t - 0.39);
267
- if (t < CELEBRATION_END_S) return -0.5 + v * 2.15 + (1.25 * v * (1 - (1 - (t - 2.54) / 1.25) ** 4)) / 4;
268
- return DONE_SWEEPS * TAU;
269
- }
270
-
271
- /** 表情对姿态的额外影响:身体倾向、呼吸节奏、视线偏好。 */
272
- interface MoodBody {
273
- /** 呼吸节奏倍数(<1 更慢)。 */
274
- pace: number;
275
- /** 视线偏好(叠加在基础游走上)。 */
276
- gazeBias: [number, number];
277
- /** 基础倾角(度)。 */
278
- roll: number;
279
- /** 眨眼节奏。 */
280
- blink: { slot?: number; dur?: number; double?: number };
281
- }
282
-
283
- const MOOD_BODY: Record<AvatarMood, MoodBody> = {
284
- neutral: { pace: 1, gazeBias: [0, 0], roll: 0, blink: {} },
285
- happy: { pace: 1.15, gazeBias: [0, -0.05], roll: 0, blink: { slot: 3.6 } },
286
- joy: { pace: 1.3, gazeBias: [0, -0.08], roll: 0, blink: { slot: 3.1 } },
287
- love: { pace: 1.25, gazeBias: [0, -0.15], roll: 0, blink: { slot: 3.1, dur: 0.16 } },
288
- surprised: { pace: 1.5, gazeBias: [0, 0], roll: 0, blink: { slot: 2.4, dur: 0.13 } },
289
- curious: { pace: 1.05, gazeBias: [0.18, -0.1], roll: 5, blink: { slot: 3.2 } },
290
- skeptical: { pace: 0.95, gazeBias: [0.2, -0.05], roll: 3.5, blink: { slot: 4.8 } },
291
- sleepy: { pace: 0.55, gazeBias: [-0.05, 0.35], roll: -3, blink: { slot: 7.4, dur: 0.46, double: 1.4 } },
292
- bored: { pace: 0.72, gazeBias: [0.22, 0.15], roll: -4, blink: { slot: 6.0, dur: 0.3 } },
293
- sad: { pace: 0.78, gazeBias: [0, 0.38], roll: -4.5, blink: { slot: 5.5, dur: 0.28 } },
294
- angry: { pace: 0.9, gazeBias: [-0.3, 0.1], roll: -2.5, blink: { slot: 5.6, dur: 0.14 } },
295
- focused: { pace: 1.2, gazeBias: [0, 0.05], roll: 0, blink: { slot: 5.2, dur: 0.14 } },
296
- confused: { pace: 0.95, gazeBias: [-0.2, -0.15], roll: 6.5, blink: { slot: 3.5 } },
297
- shy: { pace: 0.9, gazeBias: [-0.28, 0.24], roll: -4, blink: { slot: 3.0, double: 0.65 } },
298
- smug: { pace: 0.95, gazeBias: [0.24, -0.08], roll: 3, blink: { slot: 4.4 } },
299
- };
300
-
301
- function moodBody(mood: AvatarMood): MoodBody {
302
- return MOOD_BODY[mood] ?? MOOD_BODY.neutral;
303
- }
304
-
305
- /** 把两个表情按权重叠进一个 look(用于 `expression` 需求)。 */
306
- function mixLook(a: EyeLook, b: EyeLook, k: number): EyeLook {
307
- const m = (x: number, y: number) => mix(x, y, k);
308
- return {
309
- arc: m(a.arc, b.arc),
310
- slit: m(a.slit, b.slit),
311
- wide: m(a.wide, b.wide),
312
- star: m(a.star, b.star),
313
- tilt: m(a.tilt, b.tilt),
314
- lid: m(a.lid, b.lid),
315
- glint: m(a.glint, b.glint),
316
- };
317
- }
318
-
319
- export function computePose(input: PoseInput): Pose {
320
- const { seed, time, gaze, reducedMotion, view } = input;
321
- const { state, mood } = resolveLook(input);
322
- const body = moodBody(mood);
323
- const t = reducedMotion ? 0 : time + noise(seed, 7) * 10;
324
- const live = reducedMotion ? 0 : 1;
325
- const wave = (period: number, phase = 0) => (reducedMotion ? 0 : Math.sin((TAU * t) / period + phase));
326
- const given = gaze && Number.isFinite(gaze.x) && Number.isFinite(gaze.y) ? ([clamp(gaze.x, -1, 1), clamp(gaze.y, -1, 1)] as [number, number]) : null;
327
- const weight = given ? (Number.isFinite(gaze?.weight) ? clamp(gaze?.weight as number, 0, 1) : 1) : 0;
328
- const sway = 1 - 0.7 * weight;
329
-
330
- let eyes = moodLook(mood);
331
- if (input.expression) {
332
- const extra = (input.expression.mood as string) === 'grumpy' ? 'angry' : input.expression.mood;
333
- if (isMood(extra)) {
334
- const w = Number.isFinite(input.expression.weight) ? clamp(input.expression.weight as number, 0, 1) : 1;
335
- eyes = mixLook(eyes, moodLook(extra), w);
336
- }
337
- }
338
-
339
- const pose: Pose = {
340
- yaw: BASE_YAW,
341
- pitch: BASE_PITCH,
342
- roll: 0,
343
- lift: 0,
344
- sx: 1,
345
- sy: 1,
346
- gazeX: 0,
347
- gazeY: 0,
348
- blink: 0,
349
- squintL: 0,
350
- squintR: 0,
351
- eyes,
352
- ribbons: 0,
353
- cage: 0,
354
- sweep: 0,
355
- bang: 0,
356
- dots: 0,
357
- bloom: 0,
358
- bloomAge: 0,
359
- };
360
-
361
- /** 安静时的呼吸、摇摆、眨眼与游走。 */
362
- const rest = (paceScale: number) => {
363
- const pace = body.pace * paceScale;
364
- pose.yaw += 0.07 * sway * wave(6.1 * pace);
365
- pose.pitch += 0.04 * sway * wave(4.7 * pace, 1);
366
- pose.roll += 1.6 * wave(5.3 * pace, 2) + body.roll * 0.35;
367
- pose.lift = reducedMotion ? 0 : -0.45 * (1 - Math.cos((TAU * t) / (3.4 * pace)));
368
- const breath = wave(3.4 * pace, -Math.PI / 2);
369
- pose.sy = 1 + 0.012 * breath;
370
- pose.sx = 1 - 0.008 * breath;
371
- if (!reducedMotion) {
372
- pose.blink = blinkAt(seed, t, body.blink);
373
- const [wx, wy] = wanderAt(seed, t, mood === 'curious' ? 1.35 : mood === 'sleepy' ? 0.5 : 1);
374
- pose.gazeX = wx + body.gazeBias[0];
375
- pose.gazeY = wy + body.gazeBias[1];
376
- }
377
- };
378
-
379
- // 困倦:偶尔“点头”——慢慢垂下去再惊醒一下。
380
- const nodOff = () => {
381
- if (reducedMotion || mood !== 'sleepy') return;
382
- const p = (((t % 9) + 9) % 9) / 9;
383
- const drop = p < 0.62 ? smooth(p / 0.62) : 1 - smooth((p - 0.62) / 0.12);
384
- pose.pitch += 0.16 * drop;
385
- pose.roll += 3.5 * drop;
386
- pose.lift += 0.6 * drop;
387
- pose.eyes.lid = clamp(pose.eyes.lid + 0.35 * drop, 0, 1);
388
- };
389
-
390
- // 惊讶:一下急停,然后微微发抖。
391
- const startle = () => {
392
- if (reducedMotion || mood !== 'surprised') return;
393
- pose.pitch -= 0.05;
394
- pose.lift -= 0.5 * Math.abs(wave(0.9));
395
- pose.roll += 0.8 * wave(0.42);
396
- };
397
-
398
- if (state === 'idle' && (mood === 'happy' || mood === 'joy')) {
399
- // 没事可做的开心 bot 会跳。
400
- const period = mood === 'joy' ? 0.76 : 0.92;
401
- const p = reducedMotion ? 0.5 : (((t % period) + period) % period) / period;
402
- const air = 4 * p * (1 - p);
403
- const land = reducedMotion ? 0 : clamp(1 - Math.min(p, 1 - p) / 0.12, 0, 1);
404
- pose.lift = reducedMotion ? 0 : -(mood === 'joy' ? 4.0 : 3.2) * air;
405
- pose.sy = 1 - 0.09 * land + 0.03 * air * live;
406
- pose.sx = 1 + 0.07 * land - 0.015 * air * live;
407
- pose.roll = 4.5 * wave(period * 2);
408
- pose.yaw += 0.1 * wave(period * 2, 0.6);
409
- if (!reducedMotion) {
410
- pose.blink = blinkAt(seed, t, body.blink);
411
- [pose.gazeX, pose.gazeY] = wanderAt(seed, t, 1.1);
412
- }
413
- } else if (state === 'idle' && mood === 'love') {
414
- // 爱心:小幅弹跳 + 身体轻微搏动,像心跳。
415
- const beat = reducedMotion ? 0 : (1 - Math.cos((TAU * t) / 0.76)) / 2;
416
- pose.lift = reducedMotion ? 0 : -1.6 * beat;
417
- pose.sy = 1 + 0.035 * beat;
418
- pose.sx = 1 - 0.02 * beat;
419
- pose.roll += 2.2 * wave(4.4);
420
- pose.yaw += 0.06 * wave(6.2, 0.4);
421
- if (!reducedMotion) {
422
- pose.blink = blinkAt(seed, t, body.blink);
423
- [pose.gazeX, pose.gazeY] = wanderAt(seed, t, 0.8);
424
- pose.gazeY -= 0.12;
425
- }
426
- } else if (state === 'idle') {
427
- rest(1);
428
- if (mood === 'sleepy') nodOff();
429
- if (mood === 'surprised') startle();
430
- if (mood === 'curious' || mood === 'confused') {
431
- // 好奇/困惑:头歪一点,视线在两边之间来回打量。
432
- pose.roll += (mood === 'confused' ? 6 : 4) + 2.5 * wave(5.1);
433
- pose.yaw += 0.08 * wave(4.2);
434
- }
435
- if (mood === 'angry') {
436
- pose.sy *= 0.985;
437
- pose.roll -= 1.2;
438
- }
439
- if (mood === 'sad') {
440
- pose.pitch += 0.1;
441
- pose.lift += 0.6;
442
- }
443
- if (!reducedMotion && mood === 'neutral') {
444
- // 微表情抖动:每隔几秒闪一下别的表情。
445
- const micro = microAt(seed, t);
446
- if (micro) {
447
- const w = micro.weight;
448
- pose.eyes = mixLook(pose.eyes, moodLook(micro.mood), w);
449
- if (micro.mood === 'curious') pose.roll += 3 * w;
450
- if (micro.mood === 'surprised') pose.lift -= 0.8 * w;
451
- if (micro.mood === 'sleepy') pose.pitch += 0.06 * w;
452
- }
453
- }
454
- } else if (state === 'thinking') {
455
- pose.yaw += 0.12 + 0.05 * sway * wave(7.3);
456
- pose.pitch += 0.08;
457
- pose.roll = -5 + 1.4 * wave(4.1);
458
- pose.lift = -0.5 * (1 - Math.cos((TAU * t) / 5.2)) * live;
459
- pose.gazeX = 0.55 + 0.3 * wave(5.6);
460
- pose.gazeY = -0.8;
461
- pose.squintL = 0.22;
462
- pose.squintR = 0.32 + 0.1 * wave(3.1);
463
- if (!reducedMotion) pose.blink = blinkAt(seed, t * 0.7, body.blink);
464
- pose.dots = 1;
465
- } else if (state === 'working') {
466
- const beat = wave(0.7);
467
- pose.yaw += 0.08 * sway * wave(2.3, 0.4);
468
- pose.pitch -= 0.05;
469
- pose.roll = 3 + 0.6 * beat;
470
- pose.lift = -0.3 * (0.5 + 0.5 * beat) * live;
471
- pose.sy = 0.99 - 0.008 * beat;
472
- pose.sx = 1.005 + 0.005 * beat;
473
- pose.squintL = 0.25;
474
- pose.squintR = 0.25;
475
- if (reducedMotion) {
476
- pose.gazeX = 0.15;
477
- pose.gazeY = 0.3;
478
- } else {
479
- [pose.gazeX, pose.gazeY] = dartAt(seed, t);
480
- pose.blink = blinkAt(seed, t * 1.3, body.blink);
481
- }
482
- const age = Number.isFinite(input.stateAge) ? Math.max(0, input.stateAge as number) : Math.max(0, time);
483
- const into = reducedMotion ? null : workTurn(seed, age);
484
- if (into !== null) {
485
- const e = WORK_EASE * into;
486
- const turn = TAU * (1 - (1 + e) * Math.exp(-e));
487
- const swell = Math.sin(turn / 2);
488
- pose.ribbons = clamp(into / 0.16, 0, 1) * clamp((WORK_TURN_S - into) / 0.5, 0, 1);
489
- pose.sweep = turn;
490
- pose.yaw += 0.22 * swell;
491
- pose.roll += 3 * swell;
492
- pose.lift -= 1.2 * swell;
493
- pose.gazeX = clamp(pose.gazeX * (1 - swell) - 0.85 * Math.sin(turn), -1, 1);
494
- pose.gazeY = pose.gazeY * (1 - swell) - 0.3 * swell;
495
- }
496
- } else if (state === 'waiting') {
497
- const p = reducedMotion ? 0 : (((t % 6.5) + 6.5) % 6.5) / 6.5;
498
- const sigh = Math.sin(TAU * clamp(p / 0.33, 0, 1));
499
- pose.yaw += 0.04 * sway * wave(7.4);
500
- pose.pitch += 0.02 + 0.03 * sigh;
501
- pose.roll = 7 + 1.2 * wave(6.4, 2);
502
- pose.lift = -0.3 * (1 - Math.cos((TAU * t) / 6.5)) * live;
503
- pose.sy = 1 + 0.025 * sigh;
504
- pose.sx = 1 - 0.012 * sigh;
505
- pose.gazeX = 0.5 + 0.15 * wave(8.2);
506
- pose.gazeY = 0.1;
507
- // 等待天生半眯着眼;开心之类的表情会用弧线覆盖掉细缝。
508
- pose.eyes = mixLook(pose.eyes, look({ slit: 1 }), 1 - pose.eyes.arc);
509
- if (!reducedMotion) pose.blink = blinkAt(seed, t * 0.6, body.blink);
510
- } else if (state === 'blocked') {
511
- pose.sy = 1.02;
512
- pose.sx = 0.99;
513
- pose.bang = 1;
514
- } else if (state === 'alert') {
515
- // 警觉 / 通知态:身体保持警醒,轻微高频脉冲 + 抬头注意。
516
- const pulse = reducedMotion ? 0 : (1 - Math.cos((TAU * t) / 1.1)) / 2;
517
- pose.yaw += 0.05 * sway * wave(3.2);
518
- pose.pitch -= 0.06;
519
- pose.lift = -0.9 * pulse;
520
- pose.sy = 1 + 0.025 * pulse;
521
- pose.sx = 1 - 0.015 * pulse;
522
- pose.roll = 2.2 * wave(1.6);
523
- if (!reducedMotion) {
524
- pose.blink = blinkAt(seed, t * 1.1, { slot: 2.6, dur: 0.14 });
525
- [pose.gazeX, pose.gazeY] = dartAt(seed, t * 0.7);
526
- }
527
- } else if (state === 'done') {
528
- rest(1.25);
529
- const age = !reducedMotion && Number.isFinite(input.stateAge) ? Math.max(0, input.stateAge as number) : DONE_TURN_S;
530
- const length = DONE_CYCLE_S * (1 + 0.15 * noise(seed, 31));
531
- const cycle = Math.floor(age / length);
532
- const since = age - cycle * length;
533
- if (since < DONE_TURN_S) {
534
- const swell = Math.sin((Math.PI * since) / DONE_TURN_S);
535
- const way = cycle % 2 ? -1 : 1;
536
- const sweep = celebration(since) * way;
537
- pose.yaw += near(sweep / 3);
538
- pose.lift -= 2.2 * swell;
539
- pose.sy += 0.04 * swell;
540
- // 完成态用粒子绽放,而不是彩带笼:粒子有自己的一条时间轴(bloomAge)。
541
- pose.bloom = clamp(since / 0.12, 0, 1) * clamp((DONE_TURN_S + 0.6 - since) / 1.1, 0, 1);
542
- pose.bloomAge = since;
543
- }
544
- } else {
545
- rest(1);
546
- }
547
-
548
- // 眨眼关掉高光:闭上的眼睛不该有反光。
549
- if (pose.blink > 0.55) pose.eyes.glint *= clamp((1 - pose.blink) / 0.45, 0, 1);
550
-
551
- if (given && weight > 0) {
552
- const share = reducedMotion ? 1 : LOOK_EYES;
553
- const follow = weight * (1 - pose.eyes.arc * 0.6);
554
- pose.gazeX = mix(pose.gazeX, given[0] * share, follow);
555
- pose.gazeY = mix(pose.gazeY, given[1] * share, follow);
556
- if (!reducedMotion) {
557
- pose.yaw += weight * (given[0] * LOOK_YAW - BASE_YAW);
558
- pose.pitch += weight * (-given[1] * LOOK_PITCH - BASE_PITCH / 2);
559
- }
560
- }
561
- if (view && Number.isFinite(view.yaw) && Number.isFinite(view.pitch)) {
562
- pose.yaw += view.yaw;
563
- pose.pitch += view.pitch;
564
- }
565
- return pose;
566
- }
567
-
568
- /** `RenderOptions` 里与姿态有关的部分。 */
569
- export function poseFromOptions(seed: number, options: RenderOptions): Pose {
570
- return computePose({
571
- seed,
572
- time: Number.isFinite(options.time) ? (options.time as number) : 0,
573
- state: options.state,
574
- mood: options.mood,
575
- expression: options.expression,
576
- stateAge: options.stateAge,
577
- gaze: options.gaze,
578
- reducedMotion: options.reducedMotion === true,
579
- view: options.view,
580
- });
581
- }
@@ -1,98 +0,0 @@
1
- /**
2
- * 静帧 SVG:把一帧扁平数据序列化成独立文件,适合缩略图、导出与分享卡。
3
- * 只由数字和渲染器自己的标记构成。
4
- */
5
- import { computeFrame, type EyeMark, type Frame, type FramePath } from './frame.ts';
6
- import type { AvatarDefinition, RenderOptions } from './types.ts';
7
- import { svgClayFilter } from '../bot/clay.ts';
8
-
9
- const f = (n: number): string => {
10
- const r = Math.round(n * 100) / 100;
11
- return (Object.is(r, -0) ? 0 : r).toString();
12
- };
13
-
14
- const esc = (s: string): string => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
15
-
16
- function matrix(m: Frame['root']): string {
17
- return `matrix(${m.map((v, i) => (i < 4 ? String(Math.round(v * 10000) / 10000) : f(v))).join(' ')})`;
18
- }
19
-
20
- function eyeSvg(mark: EyeMark, ink: string): string {
21
- if (mark.shape === 'path') return `<path d="${mark.d}" fill="${ink}"/>`;
22
- if (mark.shape === 'arc') return `<path d="${mark.d}" fill="none" stroke="${ink}" stroke-width="${f(mark.strokeWidth)}" stroke-linecap="round"/>`;
23
- return `<rect x="${f(mark.x)}" y="${f(mark.y)}" width="${f(mark.width)}" height="${f(mark.height)}" rx="${f(mark.radius)}" fill="${ink}"/>`;
24
- }
25
-
26
- function gradientSvg(id: string, g: { x1: number; y1: number; x2: number; y2: number; stops: string[] }): string {
27
- const stops = g.stops.map((c, i) => `<stop offset="${f(i / Math.max(1, g.stops.length - 1))}" stop-color="${c}"/>`).join('');
28
- return `<linearGradient id="${id}" gradientUnits="userSpaceOnUse" x1="${f(g.x1)}" y1="${f(g.y1)}" x2="${f(g.x2)}" y2="${f(g.y2)}">${stops}</linearGradient>`;
29
- }
30
-
31
- function pathDefs(paths: FramePath[]): string {
32
- return paths.map((p) => `<path id="${p.id}" d="${p.d}"${p.evenOdd ? ' fill-rule="evenodd"' : ''}/>`).join('');
33
- }
34
-
35
- /** 渲染一条定义为独立 SVG 字符串。定义非法时抛错。 */
36
- export function renderSvg(definition: AvatarDefinition, options: RenderOptions = {}): string {
37
- const frame = computeFrame(definition, options);
38
- const gradients = new Map<string, string>();
39
- const hasSlices = frame.shapes.some((s) => s.kind === 'slice');
40
- const useClay = frame.clay && hasSlices;
41
- const clayFilterId = useClay ? `clay-${frame.color.slice(1)}` : '';
42
-
43
- const chunks: string[] = [];
44
- let inSliceGroup = false;
45
- const closeSliceGroup = () => {
46
- if (inSliceGroup) {
47
- chunks.push('</g>');
48
- inSliceGroup = false;
49
- }
50
- };
51
-
52
- for (const s of frame.shapes) {
53
- if (s.kind === 'slice') {
54
- if (useClay && !inSliceGroup) {
55
- chunks.push(`<g filter="url(#${clayFilterId})">`);
56
- inSliceGroup = true;
57
- }
58
- chunks.push(`<use href="#${s.path}" transform="${matrix(s.matrix)}"/>`);
59
- continue;
60
- }
61
- closeSliceGroup();
62
- if (s.kind === 'mark') chunks.push(`<use href="#${s.id}" transform="${matrix(s.matrix)}"/>`);
63
- else if (s.kind === 'eye') chunks.push(`<g transform="${matrix(s.matrix)}">${eyeSvg(s.mark, frame.ink)}</g>`);
64
- else if (s.kind === 'particle') chunks.push(`<g transform="${matrix(s.matrix)}"><path d="${s.d}" fill="${s.fill}"/></g>`);
65
- else {
66
- let id = gradients.get(s.key);
67
- if (!id) {
68
- id = `g-${s.key.replace(/[^a-z0-9-]/gi, '')}`;
69
- gradients.set(s.key, id);
70
- }
71
- chunks.push(`<g transform="${matrix(s.matrix)}">${gradientSvg(id, s.gradient)}<path d="${s.d}" fill="url(#${id})"/></g>`);
72
- }
73
- }
74
- closeSliceGroup();
75
-
76
- const body = chunks.join('');
77
- const size = Number.isFinite(options.size) && (options.size as number) > 0 ? Math.min(4096, Math.max(8, options.size as number)) : null;
78
- const dims = size ? ` width="${f(size)}" height="${f(size)}"` : '';
79
- const label = esc(frame.name);
80
- const filterDef = useClay ? svgClayFilter(clayFilterId, frame.highlight, frame.shadow) : '';
81
- return (
82
- `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"${dims} role="img" aria-label="${label}" overflow="visible">` +
83
- `<title>${label}</title>` +
84
- `<defs>${filterDef}${pathDefs(frame.paths)}</defs>` +
85
- `<g transform="${matrix(frame.root)}" fill="${frame.color}">${body}</g>` +
86
- `</svg>`
87
- );
88
- }
89
-
90
- /** 还没算体积时的轻量占位(缩略图列表首屏用):真实渲染会替换它。 */
91
- export function placeholderSvg(definition: AvatarDefinition, size = 64): string {
92
- const label = esc(definition.name);
93
- return (
94
- `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 100 100" role="img" aria-label="${label}">` +
95
- `<path d="${definition.body.path}" fill="#ff6a1a" fill-rule="nonzero"/>` +
96
- `</svg>`
97
- );
98
- }