@womp/spark-agent 17.9.0 → 17.11.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.
@@ -2,11 +2,37 @@ import { fitSceneCamera } from "./capabilities/scene.js";
2
2
  import { INTERNAL_UNITS_PER_MM, sceneUnitMultiplier, toEnginePosition } from "./coordinates.js";
3
3
  /** Width / height of the last capture from each engine; the render size follows the viewer, so it is learned, not assumed. */
4
4
  const captureAspects = new WeakMap();
5
+ /** A flat chroma-green backdrop; silhouette-compare keys it with `matteMask`. */
6
+ export const MATTE_BACKDROP = {
7
+ backdrop_color1: { x: 0, y: 1, z: 0 },
8
+ backdrop_color2: { x: 0, y: 1, z: 0 },
9
+ backdrop_multiplier: 1,
10
+ use_image_background: false,
11
+ };
12
+ /** Runs `capture` with the scene's backdrop swapped for MATTE_BACKDROP, then restores the scene's own values. */
13
+ async function withMatte(api, capture) {
14
+ const keys = Object.keys(MATTE_BACKDROP);
15
+ const saved = (await api.accessScene((scene) => {
16
+ const properties = scene.getSceneProperties();
17
+ scene.patchSceneProperties(keys.map((key) => ({ op: "add", path: `/${key}`, value: MATTE_BACKDROP[key] })), { returnScene: false });
18
+ return Object.fromEntries(keys.map((key) => [key, properties[key]]));
19
+ })).value;
20
+ try {
21
+ return await capture();
22
+ }
23
+ finally {
24
+ await api.accessScene((scene) => scene.patchSceneProperties(keys.map((key) => saved[key] === undefined
25
+ ? { op: "remove", path: `/${key}` }
26
+ : { op: "add", path: `/${key}`, value: saved[key] }), { returnScene: false }));
27
+ }
28
+ }
5
29
  /**
6
30
  * Convert explicit cameras at the native boundary; never fit or move the default view. A fitted camera is refitted to
7
31
  * the engine's capture aspect, and captured again if the image comes back at another aspect.
8
32
  */
9
33
  export async function captureSdkSceneScreenshot(api, input) {
34
+ if (input.matte)
35
+ return withMatte(api, () => captureSdkSceneScreenshot(api, { ...input, matte: false }));
10
36
  const remember = (image) => {
11
37
  if (image.width > 0 && image.height > 0)
12
38
  captureAspects.set(api, image.width / image.height);
@@ -27,7 +27,12 @@ type Mask = {
27
27
  };
28
28
  /** Background: pixels connected to the border whose colour is within `tolerance` of the border's median colour. */
29
29
  export declare function subjectMask(rgb: Uint8Array, width: number, height: number, channels: number, tolerance?: number): Mask;
30
+ /** Subject of a capture on a flat green matte: everything but strongly green pixels connected to the border (the
31
+ * matte, its floor lines and shadows); green-lit subject colours keep red or blue above 70 and stay subject. */
32
+ export declare function matteMask(rgb: Uint8Array, width: number, height: number, channels: number): Mask;
30
33
  export declare function compareMasks(reference: Mask, build: Mask, bandCount?: number): SilhouetteComparison;
31
34
  /** Decodes both images (any format sharp reads) and compares their subjects' outlines. */
32
- export declare function compareSilhouettes(reference: Uint8Array, build: Uint8Array, bandCount?: number): Promise<SilhouetteComparison>;
35
+ export declare function compareSilhouettes(reference: Uint8Array, build: Uint8Array, bandCount?: number, options?: {
36
+ matteBuild?: boolean;
37
+ }): Promise<SilhouetteComparison>;
33
38
  export {};
@@ -44,6 +44,40 @@ export function subjectMask(rgb, width, height, channels, tolerance = 18) {
44
44
  data[i] = background[i] ? 0 : 1;
45
45
  return { width, height, data };
46
46
  }
47
+ /** Subject of a capture on a flat green matte: everything but strongly green pixels connected to the border (the
48
+ * matte, its floor lines and shadows); green-lit subject colours keep red or blue above 70 and stay subject. */
49
+ export function matteMask(rgb, width, height, channels) {
50
+ const green = (i) => { const r = rgb[i * channels], g = rgb[i * channels + 1], b = rgb[i * channels + 2]; return g > 120 && Math.max(r, b) < 70; };
51
+ const background = new Uint8Array(width * height);
52
+ const stack = [];
53
+ const push = (i) => { if (!background[i] && green(i)) {
54
+ background[i] = 1;
55
+ stack.push(i);
56
+ } };
57
+ for (let x = 0; x < width; x++) {
58
+ push(x);
59
+ push((height - 1) * width + x);
60
+ }
61
+ for (let y = 0; y < height; y++) {
62
+ push(y * width);
63
+ push(y * width + width - 1);
64
+ }
65
+ while (stack.length) {
66
+ const i = stack.pop(), x = i % width, y = (i / width) | 0;
67
+ if (x > 0)
68
+ push(i - 1);
69
+ if (x < width - 1)
70
+ push(i + 1);
71
+ if (y > 0)
72
+ push(i - width);
73
+ if (y < height - 1)
74
+ push(i + width);
75
+ }
76
+ const data = new Uint8Array(width * height);
77
+ for (let i = 0; i < data.length; i++)
78
+ data[i] = background[i] ? 0 : 1;
79
+ return { width, height, data };
80
+ }
47
81
  /** The mask cropped to its bounds and resampled to SIZE rows, keeping its aspect, centred in a SIZE*2-wide frame. */
48
82
  function normalise(mask) {
49
83
  let x0 = mask.width, x1 = -1, y0 = mask.height, y1 = -1;
@@ -103,11 +137,12 @@ export function compareMasks(reference, build, bandCount = 10) {
103
137
  return { overlap: round(inter / Math.max(1, union)), aspect: { reference: round(a.aspect), build: round(b.aspect) }, bands };
104
138
  }
105
139
  /** Decodes both images (any format sharp reads) and compares their subjects' outlines. */
106
- export async function compareSilhouettes(reference, build, bandCount = 10) {
140
+ export async function compareSilhouettes(reference, build, bandCount = 10, options = {}) {
107
141
  const { default: sharp } = await import("sharp");
108
- const decode = async (bytes) => {
142
+ const decode = async (bytes, matte) => {
109
143
  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);
144
+ const rgb = new Uint8Array(data.buffer, data.byteOffset, data.length);
145
+ return (matte ? matteMask : subjectMask)(rgb, info.width, info.height, info.channels);
111
146
  };
112
- return compareMasks(await decode(reference), await decode(build), bandCount);
147
+ return compareMasks(await decode(reference, false), await decode(build, options.matteBuild === true), bandCount);
113
148
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@womp/spark-agent",
3
- "version": "17.9.0",
3
+ "version": "17.11.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",