@hmharness/environments 0.20.1 → 0.20.3

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.
@@ -0,0 +1,6 @@
1
+ /** 16 high-contrast colors indexed 0..15 (approximation, stable) */
2
+ export declare const ARC3_PALETTE: Array<[number, number, number]>;
3
+ /** Composite the frame's grids side-by-side (white gutter) into a PNG. */
4
+ export declare function renderFramePng(frame: unknown, scale?: number): Buffer | null;
5
+ /** Describe the grid compactly for text-only models (color-run encoding). */
6
+ export declare function gridSummary(grid: number[][]): string;
@@ -0,0 +1,127 @@
1
+ /**
2
+ * @hmharness/environments - ARC-AGI-3 frame renderer (blueprint §14)
3
+ *
4
+ * The live frame is [gridA, gridB] — typically before/after grids — each a
5
+ * jagged array of rows whose cells are palette indices. We composite them
6
+ * side by side into ONE truecolor PNG with a zero-dependency encoder
7
+ * (zlib deflate + hand-built chunks), cells upscaled so the vision model
8
+ * can actually SEE the game. The 16-color palette is a stable approximation
9
+ * chosen for visual separability (exact official RGBs are not published in
10
+ * the API docs); consistency across runs is what reasoning needs.
11
+ */
12
+ import { deflateSync } from 'node:zlib';
13
+ /** 16 high-contrast colors indexed 0..15 (approximation, stable) */
14
+ export const ARC3_PALETTE = [
15
+ [0, 0, 0], // 0 black
16
+ [0, 116, 255], // 1 blue
17
+ [255, 121, 0], // 2 orange
18
+ [156, 183, 41], // 3 green
19
+ [255, 0, 200], // 4 magenta
20
+ [0, 200, 255], // 5 sky
21
+ [128, 128, 128], // 6 gray
22
+ [230, 230, 230], // 7 silver
23
+ [165, 42, 42], // 8 brown
24
+ [255, 215, 0], // 9 gold
25
+ [0, 255, 140], // 10 mint
26
+ [255, 80, 80], // 11 coral
27
+ [80, 80, 255], // 12 indigo
28
+ [200, 255, 100], // 13 lime
29
+ [255, 160, 255], // 14 pink
30
+ [60, 60, 90], // 15 slate
31
+ ];
32
+ function cellColor(grid, x, y) {
33
+ const row = grid[y];
34
+ if (!Array.isArray(row))
35
+ return ARC3_PALETTE[0];
36
+ const v = Number(row[x]);
37
+ return ARC3_PALETTE[Number.isInteger(v) && v >= 0 && v <= 15 ? v : 0];
38
+ }
39
+ /** Composite the frame's grids side-by-side (white gutter) into a PNG. */
40
+ export function renderFramePng(frame, scale = 8) {
41
+ const grids = Array.isArray(frame) ? frame.filter((g) => Array.isArray(g)) : [];
42
+ if (grids.length === 0)
43
+ return null;
44
+ const heights = grids.map((g) => g.length);
45
+ const widths = grids.map((g) => Math.max(1, ...g.map((r) => (Array.isArray(r) ? r.length : 0))));
46
+ const cellH = Math.max(...heights, 1);
47
+ const gutter = 4; // gutter cells between grids
48
+ const totalCellsW = widths.reduce((s, w) => s + w, 0) + gutter * (grids.length - 1);
49
+ const W = totalCellsW * scale;
50
+ const H = cellH * scale;
51
+ const raw = Buffer.alloc(H * (1 + W * 3));
52
+ let o = 0;
53
+ for (let py = 0; py < H; py++) {
54
+ raw[o++] = 0; // filter none
55
+ const gy = Math.floor(py / scale);
56
+ for (let px = 0; px < W; px++) {
57
+ const gx = Math.floor(px / scale);
58
+ // which grid and local x?
59
+ let rem = gx;
60
+ let gi = 0;
61
+ while (gi < grids.length - 1 && rem >= widths[gi]) {
62
+ rem -= widths[gi] + gutter;
63
+ gi++;
64
+ }
65
+ const inGutter = rem >= widths[gi] || gi >= grids.length;
66
+ let c;
67
+ if (inGutter || gy >= heights[gi])
68
+ c = [255, 255, 255];
69
+ else
70
+ c = cellColor(grids[gi], rem, gy);
71
+ raw[o++] = c[0];
72
+ raw[o++] = c[1];
73
+ raw[o++] = c[2];
74
+ }
75
+ }
76
+ return encodePng(W, H, raw);
77
+ }
78
+ /** minimal zero-dependency truecolor PNG encoder */
79
+ function encodePng(width, height, rawData) {
80
+ const sig = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
81
+ const ihdr = Buffer.alloc(13);
82
+ ihdr.writeUInt32BE(width, 0);
83
+ ihdr.writeUInt32BE(height, 4);
84
+ ihdr[8] = 8; // bit depth
85
+ ihdr[9] = 2; // color type: truecolor
86
+ const chunks = [sig, chunk(0x49484452, ihdr), chunk(0x49444154, deflateSync(rawData)), chunk(0x49454e44, Buffer.alloc(0))];
87
+ return Buffer.concat(chunks);
88
+ }
89
+ function chunk(type, data) {
90
+ const len = Buffer.alloc(4);
91
+ len.writeUInt32BE(data.length, 0);
92
+ const t = Buffer.alloc(4);
93
+ t.writeUInt32BE(type, 0);
94
+ const crc = Buffer.alloc(4);
95
+ crc.writeUInt32BE(crc32(Buffer.concat([t, data])), 0);
96
+ return Buffer.concat([len, t, data, crc]);
97
+ }
98
+ const CRC_TABLE = (() => {
99
+ const table = new Uint32Array(256);
100
+ for (let n = 0; n < 256; n++) {
101
+ let c = n;
102
+ for (let k = 0; k < 8; k++)
103
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
104
+ table[n] = c >>> 0;
105
+ }
106
+ return table;
107
+ })();
108
+ function crc32(buf) {
109
+ let c = 0xffffffff;
110
+ for (let i = 0; i < buf.length; i++)
111
+ c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8);
112
+ return (c ^ 0xffffffff) >>> 0;
113
+ }
114
+ /** Describe the grid compactly for text-only models (color-run encoding). */
115
+ export function gridSummary(grid) {
116
+ const palette = new Map();
117
+ for (const row of grid) {
118
+ if (!Array.isArray(row))
119
+ continue;
120
+ for (const v of row)
121
+ palette.set(Number(v), (palette.get(Number(v)) ?? 0) + 1);
122
+ }
123
+ const rows = grid.length;
124
+ const cols = Math.max(1, ...grid.map((r) => (Array.isArray(r) ? r.length : 0)));
125
+ const colors = [...palette.entries()].sort((a, b) => b[1] - a[1]).map(([v, n]) => `${v}×${n}`);
126
+ return `${rows}×${cols} cells; colors: ${colors.join(' ')}`;
127
+ }
package/dist/index.d.ts CHANGED
@@ -14,5 +14,6 @@ export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from './adapt
14
14
  export type { BrowserEnvOptions, DesktopEnvOptions, Arc3EnvOptions } from './adapters.js';
15
15
  export { Arc3RestBridge, ARC3_BASE } from './arc3-rest.js';
16
16
  export type { Arc3Frame, Arc3Game } from './arc3-rest.js';
17
+ export { renderFramePng, gridSummary, ARC3_PALETTE } from './arc3-render.js';
17
18
  export { browserActBridge, desktopActBridge } from './bridges.js';
18
19
  export type { ToolExecute } from './bridges.js';
package/dist/index.js CHANGED
@@ -10,4 +10,5 @@ export { TerminalEnvironment, runShell } from "./terminal.js";
10
10
  export { HarmonyOsEnvironment } from "./harmonyos.js";
11
11
  export { BrowserEnvironment, DesktopEnvironment, Arc3Environment } from "./adapters.js";
12
12
  export { Arc3RestBridge, ARC3_BASE } from "./arc3-rest.js";
13
+ export { renderFramePng, gridSummary, ARC3_PALETTE } from "./arc3-render.js";
13
14
  export { browserActBridge, desktopActBridge } from "./bridges.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/environments",
3
- "version": "0.20.1",
3
+ "version": "0.20.3",
4
4
  "description": "HMH Cognitive OS environment adapters: Terminal (native), HarmonyOS (via domain-harmony), Browser/Desktop (via host automation bridge), ARC3 (external adapter). Every adapter implements the @hmharness/cognitive Environment protocol and must pass the shared conformance test.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -26,7 +26,7 @@
26
26
  "node": ">=22"
27
27
  },
28
28
  "dependencies": {
29
- "@hmharness/kernel": "0.20.1",
30
- "@hmharness/cognitive": "0.20.1"
29
+ "@hmharness/kernel": "0.20.3",
30
+ "@hmharness/cognitive": "0.20.3"
31
31
  }
32
32
  }