@womp/spark-agent 17.8.1 → 17.10.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.
- package/CHANGELOG.md +34 -0
- package/README.md +8 -12
- package/dist/agent-tools.js +1 -0
- package/dist/capabilities/scene.d.ts +23 -0
- package/dist/capabilities/scene.js +50 -16
- package/dist/capabilities.d.ts +6 -1
- package/dist/generated/scene-declarations.d.ts +1 -1
- package/dist/generated/scene-declarations.js +1 -1
- package/dist/misuse-bench-cases.d.ts +26 -0
- package/dist/misuse-bench-cases.js +420 -0
- package/dist/misuse-bench-cli.d.ts +2 -0
- package/dist/misuse-bench-cli.js +224 -0
- package/dist/misuse-bench.d.ts +63 -0
- package/dist/misuse-bench.js +73 -0
- package/dist/misuse-check.d.ts +90 -0
- package/dist/misuse-check.js +103 -0
- package/dist/quality-bench-cli.js +6 -13
- package/dist/quality-bench-sheet.js +2 -6
- package/dist/quality-bench.d.ts +6 -48
- package/dist/quality-bench.js +37 -266
- package/dist/server/standalone-assets-host.d.ts +12 -0
- package/dist/server/standalone-assets-host.js +39 -0
- package/dist/server/standalone.js +3 -0
- package/dist/silhouette-compare.d.ts +33 -0
- package/dist/silhouette-compare.js +113 -0
- package/package.json +10 -3
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/** Compares the outline of a build render with the outline of a reference picture taken from the same direction. */
|
|
2
|
+
const SIZE = 256;
|
|
3
|
+
/** Background: pixels connected to the border whose colour is within `tolerance` of the border's median colour. */
|
|
4
|
+
export function subjectMask(rgb, width, height, channels, tolerance = 18) {
|
|
5
|
+
const border = [[], [], []];
|
|
6
|
+
const at = (x, y) => (y * width + x) * channels;
|
|
7
|
+
for (let x = 0; x < width; x++)
|
|
8
|
+
for (const y of [0, height - 1])
|
|
9
|
+
for (let c = 0; c < 3; c++)
|
|
10
|
+
border[c].push(rgb[at(x, y) + c]);
|
|
11
|
+
for (let y = 0; y < height; y++)
|
|
12
|
+
for (const x of [0, width - 1])
|
|
13
|
+
for (let c = 0; c < 3; c++)
|
|
14
|
+
border[c].push(rgb[at(x, y) + c]);
|
|
15
|
+
const seed = border.map((values) => values.sort((a, b) => a - b)[values.length >> 1]);
|
|
16
|
+
const near = (i) => Math.max(...[0, 1, 2].map((c) => Math.abs(rgb[i * channels + c] - seed[c]))) <= tolerance;
|
|
17
|
+
const background = new Uint8Array(width * height);
|
|
18
|
+
const stack = [];
|
|
19
|
+
const push = (i) => { if (!background[i] && near(i)) {
|
|
20
|
+
background[i] = 1;
|
|
21
|
+
stack.push(i);
|
|
22
|
+
} };
|
|
23
|
+
for (let x = 0; x < width; x++) {
|
|
24
|
+
push(x);
|
|
25
|
+
push((height - 1) * width + x);
|
|
26
|
+
}
|
|
27
|
+
for (let y = 0; y < height; y++) {
|
|
28
|
+
push(y * width);
|
|
29
|
+
push(y * width + width - 1);
|
|
30
|
+
}
|
|
31
|
+
while (stack.length) {
|
|
32
|
+
const i = stack.pop(), x = i % width, y = (i / width) | 0;
|
|
33
|
+
if (x > 0)
|
|
34
|
+
push(i - 1);
|
|
35
|
+
if (x < width - 1)
|
|
36
|
+
push(i + 1);
|
|
37
|
+
if (y > 0)
|
|
38
|
+
push(i - width);
|
|
39
|
+
if (y < height - 1)
|
|
40
|
+
push(i + width);
|
|
41
|
+
}
|
|
42
|
+
const data = new Uint8Array(width * height);
|
|
43
|
+
for (let i = 0; i < data.length; i++)
|
|
44
|
+
data[i] = background[i] ? 0 : 1;
|
|
45
|
+
return { width, height, data };
|
|
46
|
+
}
|
|
47
|
+
/** The mask cropped to its bounds and resampled to SIZE rows, keeping its aspect, centred in a SIZE*2-wide frame. */
|
|
48
|
+
function normalise(mask) {
|
|
49
|
+
let x0 = mask.width, x1 = -1, y0 = mask.height, y1 = -1;
|
|
50
|
+
for (let y = 0; y < mask.height; y++)
|
|
51
|
+
for (let x = 0; x < mask.width; x++) {
|
|
52
|
+
if (!mask.data[y * mask.width + x])
|
|
53
|
+
continue;
|
|
54
|
+
if (x < x0)
|
|
55
|
+
x0 = x;
|
|
56
|
+
if (x > x1)
|
|
57
|
+
x1 = x;
|
|
58
|
+
if (y < y0)
|
|
59
|
+
y0 = y;
|
|
60
|
+
if (y > y1)
|
|
61
|
+
y1 = y;
|
|
62
|
+
}
|
|
63
|
+
if (x1 < 0)
|
|
64
|
+
throw new Error("No subject found: the image is all background.");
|
|
65
|
+
const h = y1 - y0 + 1, w = x1 - x0 + 1, scale = SIZE / h, width = SIZE * 2;
|
|
66
|
+
const out = new Uint8Array(width * SIZE);
|
|
67
|
+
const sw = Math.min(width, Math.round(w * scale)), offset = (width - sw) >> 1;
|
|
68
|
+
for (let y = 0; y < SIZE; y++)
|
|
69
|
+
for (let x = 0; x < sw; x++) {
|
|
70
|
+
const sx = x0 + Math.min(w - 1, Math.floor(x / scale)), sy = y0 + Math.min(h - 1, Math.floor(y / scale));
|
|
71
|
+
out[y * width + offset + x] = mask.data[sy * mask.width + sx];
|
|
72
|
+
}
|
|
73
|
+
return { rows: { width, height: SIZE, data: out }, aspect: w / h };
|
|
74
|
+
}
|
|
75
|
+
export function compareMasks(reference, build, bandCount = 10) {
|
|
76
|
+
const a = normalise(reference), b = normalise(build);
|
|
77
|
+
let inter = 0, union = 0;
|
|
78
|
+
for (let i = 0; i < a.rows.data.length; i++) {
|
|
79
|
+
inter += a.rows.data[i] & b.rows.data[i];
|
|
80
|
+
union += a.rows.data[i] | b.rows.data[i];
|
|
81
|
+
}
|
|
82
|
+
const rowStats = (m, y) => {
|
|
83
|
+
let l = -1, r = -1;
|
|
84
|
+
for (let x = 0; x < m.width; x++)
|
|
85
|
+
if (m.data[y * m.width + x]) {
|
|
86
|
+
if (l < 0)
|
|
87
|
+
l = x;
|
|
88
|
+
r = x;
|
|
89
|
+
}
|
|
90
|
+
return l < 0 ? null : { width: (r - l + 1) / SIZE, center: ((l + r) / 2 - m.width / 2) / SIZE };
|
|
91
|
+
};
|
|
92
|
+
const round = (v) => Math.round(v * 1000) / 1000;
|
|
93
|
+
const bands = [];
|
|
94
|
+
for (let k = 0; k < bandCount; k++) {
|
|
95
|
+
const y0 = Math.floor((k * SIZE) / bandCount), y1 = Math.floor(((k + 1) * SIZE) / bandCount);
|
|
96
|
+
const mean = (m) => {
|
|
97
|
+
const rows = Array.from({ length: y1 - y0 }, (_, i) => rowStats(m, y0 + i)).filter((s) => s !== null);
|
|
98
|
+
return rows.length ? { width: rows.reduce((s, r) => s + r.width, 0) / rows.length, center: rows.reduce((s, r) => s + r.center, 0) / rows.length } : { width: 0, center: 0 };
|
|
99
|
+
};
|
|
100
|
+
const ra = mean(a.rows), rb = mean(b.rows);
|
|
101
|
+
bands.push({ from: round(k / bandCount), to: round((k + 1) / bandCount), referenceWidth: round(ra.width), buildWidth: round(rb.width), referenceCenter: round(ra.center), buildCenter: round(rb.center) });
|
|
102
|
+
}
|
|
103
|
+
return { overlap: round(inter / Math.max(1, union)), aspect: { reference: round(a.aspect), build: round(b.aspect) }, bands };
|
|
104
|
+
}
|
|
105
|
+
/** Decodes both images (any format sharp reads) and compares their subjects' outlines. */
|
|
106
|
+
export async function compareSilhouettes(reference, build, bandCount = 10) {
|
|
107
|
+
const { default: sharp } = await import("sharp");
|
|
108
|
+
const decode = async (bytes) => {
|
|
109
|
+
const { data, info } = await sharp(bytes).flatten({ background: "#ffffff" }).removeAlpha().resize({ height: 512, withoutEnlargement: false }).raw().toBuffer({ resolveWithObject: true });
|
|
110
|
+
return subjectMask(new Uint8Array(data.buffer, data.byteOffset, data.length), info.width, info.height, info.channels);
|
|
111
|
+
};
|
|
112
|
+
return compareMasks(await decode(reference), await decode(build), bandCount);
|
|
113
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@womp/spark-agent",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.10.0",
|
|
4
4
|
"description": "Portable skills-based AI agent with local and durable chat transports for Womp",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"spark-agent": "dist/cli.js",
|
|
10
10
|
"agent-run": "dist/agent-run-cli.js",
|
|
11
11
|
"intent-bench": "dist/intent-bench-cli.js",
|
|
12
|
-
"quality-bench": "dist/quality-bench-cli.js"
|
|
12
|
+
"quality-bench": "dist/quality-bench-cli.js",
|
|
13
|
+
"misuse-bench": "dist/misuse-bench-cli.js"
|
|
13
14
|
},
|
|
14
15
|
"exports": {
|
|
15
16
|
"./browser": {
|
|
@@ -57,6 +58,11 @@
|
|
|
57
58
|
"import": "./dist/client.js",
|
|
58
59
|
"default": "./dist/client.js"
|
|
59
60
|
},
|
|
61
|
+
"./misuse": {
|
|
62
|
+
"types": "./dist/misuse-check.d.ts",
|
|
63
|
+
"import": "./dist/misuse-check.js",
|
|
64
|
+
"default": "./dist/misuse-check.js"
|
|
65
|
+
},
|
|
60
66
|
"./server": {
|
|
61
67
|
"types": "./dist/server/index.d.ts",
|
|
62
68
|
"import": "./dist/server/index.js",
|
|
@@ -101,11 +107,12 @@
|
|
|
101
107
|
"intent-bench": "tsx src/intent-bench-cli.ts",
|
|
102
108
|
"kakapo-mux": "tsx src/kakapo-mux-cli.ts",
|
|
103
109
|
"quality-bench": "tsx src/quality-bench-cli.ts",
|
|
110
|
+
"misuse-bench": "tsx src/misuse-bench-cli.ts",
|
|
104
111
|
"dev:demo-account": "yarn build && concurrently --kill-others \"yarn dev:server --profile demo-account\" \"yarn dev:web\"",
|
|
105
112
|
"dev:web": "vite --config web/vite.config.ts",
|
|
106
113
|
"start": "node dist/cli.js",
|
|
107
114
|
"web:build": "yarn build && tsc --noEmit -p web/tsconfig.json && vite build --config web/vite.config.ts",
|
|
108
|
-
"test": "yarn build && tsx --tsconfig tsconfig.json --test test/**/*.test.ts && node --input-type=module -e \"await Promise.all([import('@womp/spark-agent'), import('@womp/spark-agent/ui'), import('@womp/spark-agent/client'), import('@womp/spark-agent/server'), import('@womp/spark-agent/express'), import('@womp/spark-agent/kakapo'), import('@womp/spark-agent/geometry'), import('@womp/spark-agent/openscad')])\"",
|
|
115
|
+
"test": "yarn build && tsx --tsconfig tsconfig.json --test test/**/*.test.ts && node --input-type=module -e \"await Promise.all([import('@womp/spark-agent'), import('@womp/spark-agent/ui'), import('@womp/spark-agent/client'), import('@womp/spark-agent/server'), import('@womp/spark-agent/express'), import('@womp/spark-agent/kakapo'), import('@womp/spark-agent/geometry'), import('@womp/spark-agent/openscad'), import('@womp/spark-agent/misuse')])\"",
|
|
109
116
|
"test:live": "tsx --tsconfig tsconfig.json test/womp-scene-api.live.ts",
|
|
110
117
|
"test:live:materials": "tsx --tsconfig tsconfig.json test/materials-flowir.live.ts",
|
|
111
118
|
"prepack": "yarn build",
|