dsh-quay-bot 1.0.2 → 1.0.4
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 +3 -2
- 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 +35 -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/avatar/src/core/types.ts
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 头像定义契约 v1。
|
|
3
|
-
*
|
|
4
|
-
* 定义只描述几何:一条 100×100 坐标系里的单色轮廓路径,以及一对眼睛的位置。
|
|
5
|
-
* 颜色、隐含体积、视角、凝视、眨眼、动效全部由运行时渲染器决定。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export interface AvatarEyes {
|
|
9
|
-
/** 眼睛对的中心,viewBox 单位。 */
|
|
10
|
-
x: number;
|
|
11
|
-
y: number;
|
|
12
|
-
/** 两眼中心距,viewBox 单位。 */
|
|
13
|
-
spacing: number;
|
|
14
|
-
/** 1 = 单只中性眼睛约宽 4、高 11。 */
|
|
15
|
-
scale: number;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface AvatarDefinition {
|
|
19
|
-
version: 1;
|
|
20
|
-
/** 小写 slug,例如 "controller"。 */
|
|
21
|
-
id: string;
|
|
22
|
-
/** 展示用名称。 */
|
|
23
|
-
name: string;
|
|
24
|
-
/** 一条填充路径,nonzero 填充;孔洞反向绕行。 */
|
|
25
|
-
body: { path: string };
|
|
26
|
-
eyes: AvatarEyes;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type AvatarState = 'idle' | 'thinking' | 'working' | 'waiting' | 'blocked' | 'done' | 'alert';
|
|
30
|
-
/**
|
|
31
|
-
* 表情(15 种)。它只改“脸”(眼睛形状、开合、闪烁、高光),以及一点点身体倾向;
|
|
32
|
-
* 状态决定“在干什么”,表情决定“看起来怎么样”。两者可以任意组合。
|
|
33
|
-
*/
|
|
34
|
-
export type AvatarMood =
|
|
35
|
-
| 'neutral'
|
|
36
|
-
| 'happy'
|
|
37
|
-
| 'joy'
|
|
38
|
-
| 'love'
|
|
39
|
-
| 'surprised'
|
|
40
|
-
| 'curious'
|
|
41
|
-
| 'skeptical'
|
|
42
|
-
| 'sleepy'
|
|
43
|
-
| 'bored'
|
|
44
|
-
| 'sad'
|
|
45
|
-
| 'angry'
|
|
46
|
-
| 'focused'
|
|
47
|
-
| 'confused'
|
|
48
|
-
| 'shy'
|
|
49
|
-
| 'smug';
|
|
50
|
-
|
|
51
|
-
export type BadgeKind = 'none' | 'red' | 'blue';
|
|
52
|
-
|
|
53
|
-
export interface RingPoint {
|
|
54
|
-
x: number;
|
|
55
|
-
y: number;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface RenderOptions {
|
|
59
|
-
/** 身体颜色 #rgb / #rrggbb,非法值回落到默认色。 */
|
|
60
|
-
color?: string;
|
|
61
|
-
/** 高光颜色(用于黏土质感左上角渐变)。 */
|
|
62
|
-
highlightColor?: string;
|
|
63
|
-
/** 阴影颜色(用于黏土质感右下角内阴影)。 */
|
|
64
|
-
shadowColor?: string;
|
|
65
|
-
/** 眼睛颜色,默认近黑墨色。 */
|
|
66
|
-
eyeColor?: string;
|
|
67
|
-
/** 渲染尺寸(CSS px);省略则为流式 SVG。 */
|
|
68
|
-
size?: number;
|
|
69
|
-
/** 体积量 0..14,默认 6;0 = 完全扁平。 */
|
|
70
|
-
depth?: number;
|
|
71
|
-
/** 是否叠加伪 3D 黏土质感(左上角高光 + 右下角内阴影)。默认 true。 */
|
|
72
|
-
clay?: boolean;
|
|
73
|
-
/** 在静止视角之上叠加的视角旋转(弧度)。 */
|
|
74
|
-
view?: { yaw: number; pitch: number };
|
|
75
|
-
/** 动画时钟(秒)。同样的输入永远得到同样的输出。 */
|
|
76
|
-
time?: number;
|
|
77
|
-
/** 注视目标,每轴 -1..1(x 向右,y 向下)。 */
|
|
78
|
-
gaze?: { x: number; y: number; weight?: number };
|
|
79
|
-
state?: AvatarState;
|
|
80
|
-
mood?: AvatarMood;
|
|
81
|
-
/** 状态开始后经过的秒数;`done` 用它驱动庆祝动作。 */
|
|
82
|
-
stateAge?: number;
|
|
83
|
-
/** 冻结身体动作、眨眼、彩带与标记,只保留凝视。 */
|
|
84
|
-
reducedMotion?: boolean;
|
|
85
|
-
/** 额外叠加一个表情(0..1 权重),用来做“表情闪现”或外部驱动的瞬时反应。 */
|
|
86
|
-
expression?: { mood: AvatarMood; weight?: number };
|
|
87
|
-
/**
|
|
88
|
-
* 从上一帧的姿态缓动到这一帧:`from` 通常是上一帧返回的 `frame.pose`,
|
|
89
|
-
* `t` 0..1。状态与表情的切换都靠它变得有过渡,而不是硬切。
|
|
90
|
-
*/
|
|
91
|
-
poseBlend?: { from: PoseLike; t: number };
|
|
92
|
-
/** 外部弹簧引擎输出的 48 点闭合眼环(左右眼,单位:eyes.scale = 1 时的 viewBox)。 */
|
|
93
|
-
eyeRings?: [RingPoint[], RingPoint[]];
|
|
94
|
-
/** 独立眨眼纵向缩放(1 = 睁开,0.08 = 闭眼),配合 `eyeRings` 使用。 */
|
|
95
|
-
blinkScale?: number;
|
|
96
|
-
/** 是否显示外围轨道粒子。 */
|
|
97
|
-
orbit?: boolean;
|
|
98
|
-
/** 通知角标(红点 / 蓝点)。 */
|
|
99
|
-
badge?: BadgeKind;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 眼睛怎么画,全部是数值,于是任意两种 look 都能插值成中间形状。
|
|
104
|
-
* 六个形状轴 + 眨眼/高光两个辅助量。
|
|
105
|
-
*/
|
|
106
|
-
export interface EyeLook {
|
|
107
|
-
/** 0 = 普通药丸,1 = 开心弧。 */
|
|
108
|
-
arc: number;
|
|
109
|
-
/** 0 = 普通药丸,1 = 等待时的扁平细缝。 */
|
|
110
|
-
slit: number;
|
|
111
|
-
/** 0..1,惊讶:眼睛更高更圆。 */
|
|
112
|
-
wide: number;
|
|
113
|
-
/** 0..1,闪光星。 */
|
|
114
|
-
star: number;
|
|
115
|
-
/** -1..1,眼轴倾斜(负 = 内角朝下)。 */
|
|
116
|
-
tilt: number;
|
|
117
|
-
/** 0..1,上眼皮下垂。 */
|
|
118
|
-
lid: number;
|
|
119
|
-
/** 0..1,高光。 */
|
|
120
|
-
glint: number;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 一帧姿态的全部数值。它是纯函数的结果,也可以作为 `poseBlend.from`
|
|
125
|
-
* 从“屏幕上正在显示的样子”继续缓动到新姿态。
|
|
126
|
-
*/
|
|
127
|
-
export interface PoseLike {
|
|
128
|
-
yaw: number;
|
|
129
|
-
pitch: number;
|
|
130
|
-
roll: number;
|
|
131
|
-
lift: number;
|
|
132
|
-
sx: number;
|
|
133
|
-
sy: number;
|
|
134
|
-
gazeX: number;
|
|
135
|
-
gazeY: number;
|
|
136
|
-
blink: number;
|
|
137
|
-
squintL: number;
|
|
138
|
-
squintR: number;
|
|
139
|
-
eyes: EyeLook;
|
|
140
|
-
ribbons: number;
|
|
141
|
-
cage: number;
|
|
142
|
-
sweep: number;
|
|
143
|
-
bang: number;
|
|
144
|
-
dots: number;
|
|
145
|
-
/** 完成态的粒子绽放强度,0..1。 */
|
|
146
|
-
bloom: number;
|
|
147
|
-
/** 粒子绽放已经进行了几秒(粒子自己的时间轴)。 */
|
|
148
|
-
bloomAge: number;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export type ValidationResult =
|
|
152
|
-
| { ok: true; value: AvatarDefinition }
|
|
153
|
-
| { ok: false; errors: string[] };
|
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 身体轮廓的分析契约:眼睛尺寸常量、子路径/孔洞判定、眼睛可放性与校验。
|
|
3
|
-
*
|
|
4
|
-
* `eyesFit` / `drawnEyesFit` 回答“眼睛画在这里会不会出界”,
|
|
5
|
-
* `gazeRange` 回答“在当前放大的眼睛下每个方向还能看多远”。
|
|
6
|
-
*/
|
|
7
|
-
import { bounds, edgeBands, filledArea, flattenPath, parsePath, PathError, serializePath, splitSubpaths, type Bounds, type Point } from './path.ts';
|
|
8
|
-
import type { AvatarEyes, ValidationResult } from './types.ts';
|
|
9
|
-
|
|
10
|
-
/** scale = 1 时单只中性眼睛的宽高,viewBox 单位。 */
|
|
11
|
-
export const EYE_W = 4;
|
|
12
|
-
export const EYE_H = 11;
|
|
13
|
-
/** 硬限制;契约中对身体的软要求是 8..92。 */
|
|
14
|
-
export const BODY_MIN = 6;
|
|
15
|
-
export const BODY_MAX = 94;
|
|
16
|
-
const EYE_MARGIN = 1.2;
|
|
17
|
-
/** 眼睛在脸内的最大平移(scale = 1 时)。刻意很小:一点点位移就读作“在看”。 */
|
|
18
|
-
export const GAZE_X = 1.5;
|
|
19
|
-
export const GAZE_Y = 1.1;
|
|
20
|
-
/** 开心眼弧线,按绘制眼睛尺寸的比例:从 (x ∓ halfWidth·w, y + base·h) 经 (x, y − peak·h) 的二次曲线。 */
|
|
21
|
-
export const HAPPY_ARC = { halfWidth: 0.72, base: 0.1, peak: 0.42, stroke: 0.6 } as const;
|
|
22
|
-
/** 渲染器在眼睛与轮廓之间保持的间隙。 */
|
|
23
|
-
export const EYE_CLEARANCE = 1.5;
|
|
24
|
-
|
|
25
|
-
const TOP_KEYS = ['version', 'id', 'name', 'body', 'eyes'];
|
|
26
|
-
const EYE_KEYS = ['x', 'y', 'spacing', 'scale'];
|
|
27
|
-
|
|
28
|
-
export interface BodyHole {
|
|
29
|
-
d: string;
|
|
30
|
-
area: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface BodySubpath {
|
|
34
|
-
d: string;
|
|
35
|
-
polys: Point[][];
|
|
36
|
-
/** 该子路径是挖孔而不是加料。 */
|
|
37
|
-
hole: boolean;
|
|
38
|
-
/** 子路径自身填充内的一点(孔洞则是挖空区域内的一点)。 */
|
|
39
|
-
probe: Point | null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface BodyAnalysis {
|
|
43
|
-
polys: Point[][];
|
|
44
|
-
bounds: Bounds;
|
|
45
|
-
area: number;
|
|
46
|
-
holes: BodyHole[];
|
|
47
|
-
subpaths: BodySubpath[];
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface GazeRange {
|
|
51
|
-
left: number;
|
|
52
|
-
right: number;
|
|
53
|
-
up: number;
|
|
54
|
-
down: number;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const analysisCache = new Map<string, BodyAnalysis>();
|
|
58
|
-
|
|
59
|
-
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
60
|
-
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function isNum(v: unknown): v is number {
|
|
64
|
-
return typeof v === 'number' && Number.isFinite(v);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** 单个眼睛药丸外扩 `margin` 后的轮廓点。 */
|
|
68
|
-
function eyeOutline(cx: number, cy: number, scale: number, margin: number): Point[] {
|
|
69
|
-
const hw = (EYE_W * scale) / 2 + margin;
|
|
70
|
-
const hh = (EYE_H * scale) / 2 + margin;
|
|
71
|
-
const pts: Point[] = [];
|
|
72
|
-
const straight = hh - hw;
|
|
73
|
-
for (let k = 0; k < 16; k++) {
|
|
74
|
-
const a = (k / 16) * Math.PI * 2;
|
|
75
|
-
const x = Math.cos(a) * hw;
|
|
76
|
-
const y = Math.sin(a) * hw + Math.sign(Math.sin(a)) * Math.max(0, straight);
|
|
77
|
-
pts.push([cx + x, cy + y]);
|
|
78
|
-
}
|
|
79
|
-
for (const f of [-0.5, 0, 0.5]) pts.push([cx - hw, cy + f * straight], [cx + hw, cy + f * straight]);
|
|
80
|
-
pts.push([cx, cy]);
|
|
81
|
-
return pts;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** 渲染器可能画出的每一种眼睛形状(药丸与开心弧)外扩 `margin` 后的轮廓点。 */
|
|
85
|
-
function drawnEyeOutline(cx: number, cy: number, scale: number, margin: number): Point[] {
|
|
86
|
-
const pts = eyeOutline(cx, cy, scale, margin);
|
|
87
|
-
const w = EYE_W * scale;
|
|
88
|
-
const h = EYE_H * scale;
|
|
89
|
-
const x0 = cx - HAPPY_ARC.halfWidth * w;
|
|
90
|
-
const x1 = cx + HAPPY_ARC.halfWidth * w;
|
|
91
|
-
const yb = cy + HAPPY_ARC.base * h;
|
|
92
|
-
const yc = cy - HAPPY_ARC.peak * h;
|
|
93
|
-
const r = (HAPPY_ARC.stroke * w) / 2 + margin;
|
|
94
|
-
for (let i = 0; i <= 8; i++) {
|
|
95
|
-
const t = i / 8;
|
|
96
|
-
const m = 1 - t;
|
|
97
|
-
const x = m * m * x0 + 2 * m * t * cx + t * t * x1;
|
|
98
|
-
const y = (m * m + t * t) * yb + 2 * m * t * yc;
|
|
99
|
-
for (let k = 0; k < 8; k++) {
|
|
100
|
-
const a = (k / 8) * Math.PI * 2;
|
|
101
|
-
pts.push([x + Math.cos(a) * r, y + Math.sin(a) * r]);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return pts;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function eyeCenters(eyes: AvatarEyes): [Point, Point] {
|
|
108
|
-
return [
|
|
109
|
-
[eyes.x - eyes.spacing / 2, eyes.y],
|
|
110
|
-
[eyes.x + eyes.spacing / 2, eyes.y],
|
|
111
|
-
];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function eyesFit(polys: Point[][], eyes: AvatarEyes, dx: number, dy: number, margin: number): Point | null {
|
|
115
|
-
const index = edgeBands(polys);
|
|
116
|
-
for (const [cx, cy] of eyeCenters(eyes)) {
|
|
117
|
-
for (const [x, y] of eyeOutline(cx + dx, cy + dy, eyes.scale, margin)) {
|
|
118
|
-
if (index.winding(x, y) === 0) return [x, y];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export function drawnEyesFit(polys: Point[][], eyes: AvatarEyes, dx: number, dy: number, margin: number): Point | null {
|
|
125
|
-
const index = edgeBands(polys);
|
|
126
|
-
for (const [cx, cy] of eyeCenters(eyes)) {
|
|
127
|
-
for (const [x, y] of drawnEyeOutline(cx + dx, cy + dy, eyes.scale, margin)) {
|
|
128
|
-
if (index.winding(x, y) === 0) return [x, y];
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** 静止时绘制眼睛到轮廓的最大间隙(单位 0.1),贴边时为 0。 */
|
|
135
|
-
export function eyeClearance(polys: Point[][], eyes: AvatarEyes): number {
|
|
136
|
-
if (drawnEyesFit(polys, eyes, 0, 0, 0)) return 0;
|
|
137
|
-
let lo = 0;
|
|
138
|
-
let hi = 20;
|
|
139
|
-
while (hi - lo > 0.05) {
|
|
140
|
-
const mid = (lo + hi) / 2;
|
|
141
|
-
if (drawnEyesFit(polys, eyes, 0, 0, mid)) hi = mid;
|
|
142
|
-
else lo = mid;
|
|
143
|
-
}
|
|
144
|
-
return Math.floor(lo * 10) / 10;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/** 绘制眼睛每个方向还能走多远(0..1 的 GAZE_X / GAZE_Y 比例)而保持 EYE_CLEARANCE。 */
|
|
148
|
-
export function gazeRange(polys: Point[][], eyes: AvatarEyes): GazeRange {
|
|
149
|
-
const reach = (ux: number, uy: number) => {
|
|
150
|
-
for (const k of [1, 0.8, 0.6, 0.4, 0.2]) {
|
|
151
|
-
if (!drawnEyesFit(polys, eyes, ux * k * GAZE_X * eyes.scale, uy * k * GAZE_Y * eyes.scale, EYE_CLEARANCE)) return k;
|
|
152
|
-
}
|
|
153
|
-
return 0;
|
|
154
|
-
};
|
|
155
|
-
return { left: reach(-1, 0), right: reach(1, 0), up: reach(0, -1), down: reach(0, 1) };
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** 解析并分析一条身体路径(带缓存)。 */
|
|
159
|
-
export function analyzeBody(path: string): BodyAnalysis {
|
|
160
|
-
const hit = analysisCache.get(path);
|
|
161
|
-
if (hit) return hit;
|
|
162
|
-
const segs = parsePath(path);
|
|
163
|
-
const polys = flattenPath(segs);
|
|
164
|
-
const b = bounds(polys);
|
|
165
|
-
const whole = edgeBands(polys);
|
|
166
|
-
const holes: BodyHole[] = [];
|
|
167
|
-
const subpaths: BodySubpath[] = [];
|
|
168
|
-
for (const sub of splitSubpaths(segs)) {
|
|
169
|
-
const own = flattenPath(sub);
|
|
170
|
-
if (!own.length) continue;
|
|
171
|
-
const ob = bounds(own);
|
|
172
|
-
let inside = 0;
|
|
173
|
-
let cut = 0;
|
|
174
|
-
let probe: Point | null = null;
|
|
175
|
-
let cutProbe: Point | null = null;
|
|
176
|
-
const step = (ob.maxX - ob.minX) * (ob.maxY - ob.minY) < 900 ? 0.5 : 1;
|
|
177
|
-
const alone = own.length === polys.length;
|
|
178
|
-
const xs: number[] = [];
|
|
179
|
-
for (let x = Math.floor(ob.minX) + step / 2; x < ob.maxX; x += step) xs.push(x);
|
|
180
|
-
const mine = new Uint8Array(xs.length);
|
|
181
|
-
const all = new Uint8Array(xs.length);
|
|
182
|
-
const self = edgeBands(own);
|
|
183
|
-
for (let y = Math.floor(ob.minY) + step / 2; y < ob.maxY; y += step) {
|
|
184
|
-
self.insideRow(y, xs, mine);
|
|
185
|
-
if (!alone) whole.insideRow(y, xs, all);
|
|
186
|
-
for (let k = 0; k < xs.length; k++) {
|
|
187
|
-
if (!mine[k]) continue;
|
|
188
|
-
inside++;
|
|
189
|
-
probe ??= [xs[k], y];
|
|
190
|
-
if (!alone && !all[k]) {
|
|
191
|
-
cut++;
|
|
192
|
-
cutProbe ??= [xs[k], y];
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
cut *= step * step * 4;
|
|
197
|
-
inside *= step * step * 4;
|
|
198
|
-
const d = serializePath(sub);
|
|
199
|
-
const hole = inside > 0 && cut / inside > 0.6;
|
|
200
|
-
if (hole) holes.push({ d, area: Math.round(cut / 4) });
|
|
201
|
-
subpaths.push({ d, polys: own, hole, probe: hole ? cutProbe : probe });
|
|
202
|
-
}
|
|
203
|
-
const analysis: BodyAnalysis = { polys, bounds: b, area: filledArea(polys, b), holes, subpaths };
|
|
204
|
-
if (analysisCache.size > 64) analysisCache.clear();
|
|
205
|
-
analysisCache.set(path, analysis);
|
|
206
|
-
return analysis;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const r1 = (n: number) => Math.round(n * 10) / 10;
|
|
210
|
-
|
|
211
|
-
export function validateAvatar(input: unknown): ValidationResult {
|
|
212
|
-
const errors: string[] = [];
|
|
213
|
-
if (!isRecord(input)) return { ok: false, errors: ['definition must be a JSON object'] };
|
|
214
|
-
|
|
215
|
-
for (const k of Object.keys(input)) if (!TOP_KEYS.includes(k)) errors.push(`unknown field "${k}" (allowed: ${TOP_KEYS.join(', ')})`);
|
|
216
|
-
if (input.version !== 1) errors.push('version must be 1');
|
|
217
|
-
|
|
218
|
-
const id = input.id;
|
|
219
|
-
if (typeof id !== 'string' || !/^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/.test(id)) {
|
|
220
|
-
errors.push('id must be a lowercase slug of 1-40 chars [a-z0-9-]');
|
|
221
|
-
}
|
|
222
|
-
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
|
223
|
-
if (!name || name.length > 40 || /[\u0000-\u001f<>]/.test(name)) {
|
|
224
|
-
errors.push('name must be 1-40 printable characters without < or >');
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
let path: string | null = null;
|
|
228
|
-
if (!isRecord(input.body)) errors.push('body must be an object { "path": "..." }');
|
|
229
|
-
else {
|
|
230
|
-
for (const k of Object.keys(input.body)) if (k !== 'path') errors.push(`unknown field "body.${k}" (only "path" is allowed)`);
|
|
231
|
-
if (typeof input.body.path !== 'string') errors.push('body.path must be a string');
|
|
232
|
-
else path = input.body.path;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
let eyes: AvatarEyes | null = null;
|
|
236
|
-
if (!isRecord(input.eyes)) errors.push('eyes must be an object { x, y, spacing, scale }');
|
|
237
|
-
else {
|
|
238
|
-
const e = input.eyes;
|
|
239
|
-
for (const k of Object.keys(e)) if (!EYE_KEYS.includes(k)) errors.push(`unknown field "eyes.${k}" (allowed: ${EYE_KEYS.join(', ')})`);
|
|
240
|
-
const bad = EYE_KEYS.filter((k) => !isNum(e[k]));
|
|
241
|
-
if (bad.length) errors.push(`eyes.${bad.join(', eyes.')} must be finite numbers`);
|
|
242
|
-
else {
|
|
243
|
-
eyes = { x: e.x as number, y: e.y as number, spacing: e.spacing as number, scale: e.scale as number };
|
|
244
|
-
if (eyes.scale < 0.6 || eyes.scale > 1.8) errors.push(`eyes.scale ${eyes.scale} out of range 0.6..1.8`);
|
|
245
|
-
const minSpacing = EYE_W * eyes.scale * 1.75;
|
|
246
|
-
if (eyes.spacing < minSpacing || eyes.spacing > 40) {
|
|
247
|
-
errors.push(`eyes.spacing ${eyes.spacing} out of range ${r1(minSpacing)}..40 for scale ${eyes.scale}`);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
let normalized = '';
|
|
253
|
-
let body: BodyAnalysis | null = null;
|
|
254
|
-
if (path !== null) {
|
|
255
|
-
try {
|
|
256
|
-
const segs = parsePath(path);
|
|
257
|
-
normalized = serializePath(segs);
|
|
258
|
-
body = analyzeBody(normalized);
|
|
259
|
-
} catch (err) {
|
|
260
|
-
if (err instanceof PathError) errors.push(`body.path: ${err.message}`);
|
|
261
|
-
else throw err;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (body) {
|
|
266
|
-
const b = body.bounds;
|
|
267
|
-
const out: string[] = [];
|
|
268
|
-
if (b.minX < BODY_MIN) out.push(`left edge x=${r1(b.minX)}`);
|
|
269
|
-
if (b.minY < BODY_MIN) out.push(`top edge y=${r1(b.minY)}`);
|
|
270
|
-
if (b.maxX > BODY_MAX) out.push(`right edge x=${r1(b.maxX)}`);
|
|
271
|
-
if (b.maxY > BODY_MAX) out.push(`bottom edge y=${r1(b.maxY)}`);
|
|
272
|
-
if (out.length) errors.push(`body.path leaves the safe area (${out.join(', ')}); keep geometry within 8..92`);
|
|
273
|
-
const w = b.maxX - b.minX;
|
|
274
|
-
const h = b.maxY - b.minY;
|
|
275
|
-
if (Math.max(w, h) < 40 || Math.min(w, h) < 14) {
|
|
276
|
-
errors.push(`body is too small (${r1(w)}x${r1(h)}); use at least 40 units on the long side and 14 on the short side`);
|
|
277
|
-
}
|
|
278
|
-
if (body.area < 600) errors.push(`body fill area ${body.area} is too thin to carry eyes; must be at least 600 units²`);
|
|
279
|
-
|
|
280
|
-
if (eyes) {
|
|
281
|
-
const miss = eyesFit(body.polys, eyes, 0, 0, EYE_MARGIN);
|
|
282
|
-
if (miss) {
|
|
283
|
-
errors.push(
|
|
284
|
-
`eyes must sit inside the body with ${EYE_MARGIN} units margin; point (${r1(miss[0])}, ${r1(miss[1])}) is outside or in a hole. ` +
|
|
285
|
-
'Move eyes.x/y toward the widest solid area, reduce spacing or scale.',
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
if (errors.length || !eyes) return { ok: false, errors };
|
|
292
|
-
return {
|
|
293
|
-
ok: true,
|
|
294
|
-
value: { version: 1, id: id as string, name, body: { path: normalized }, eyes: { ...eyes } },
|
|
295
|
-
};
|
|
296
|
-
}
|