dsh-apk-vm 0.10.2 → 0.11.1

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/lib/instances.js CHANGED
@@ -247,6 +247,14 @@ export class ExtraVmInstance {
247
247
  return this.stream.getClipboard();
248
248
  }
249
249
 
250
+ /** Inject arbitrary UTF-8 text through the scrcpy control stream. */
251
+ async sendText(text) {
252
+ this.touch();
253
+ if (!this.stream) throw new Error('H.264 stream is disabled for this instance');
254
+ const messages = await this.stream.sendText(String(text));
255
+ return { ok: true, chars: String(text).length, messages };
256
+ }
257
+
250
258
  handleVideoClient(client) {
251
259
  this.touch();
252
260
  if (!this.stream) {
@@ -327,9 +335,9 @@ export class ExtraVmInstance {
327
335
  this.touch();
328
336
  return this.vm.pullFile(remote, local);
329
337
  }
330
- reboot(mode) {
338
+ reboot(mode, options) {
331
339
  this.touch();
332
- return this.vm.reboot(mode);
340
+ return this.vm.reboot(mode, options);
333
341
  }
334
342
  forceStop(pkg) {
335
343
  this.touch();
@@ -0,0 +1,39 @@
1
+ /**
2
+ * JSON-safe conversion for values crossing the Agent tool boundary.
3
+ *
4
+ * DSH rejects tool results that do not survive a lossless JSON round-trip.
5
+ * Android diagnostics commonly contain `undefined`, `NaN`, BigInt, Buffers or
6
+ * cycles; convert those into explicit JSON values instead of failing the whole
7
+ * action. The original value is never mutated.
8
+ */
9
+
10
+ export function losslessJson(value, seen = new WeakSet()) {
11
+ if (value === undefined) return null;
12
+ if (value === null) return value;
13
+ const type = typeof value;
14
+ if (type === 'string' || type === 'boolean') return value;
15
+ if (type === 'number') return Number.isFinite(value) ? value : null;
16
+ if (type === 'bigint') return value.toString();
17
+ if (type === 'function' || type === 'symbol') return null;
18
+ if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
19
+ return { type: 'bytes', length: value.length };
20
+ }
21
+ if (value instanceof Date) return value.toISOString();
22
+ if (value instanceof Error) {
23
+ return { name: value.name, message: value.message, code: value.code || null };
24
+ }
25
+ if (type !== 'object') return String(value);
26
+ if (seen.has(value)) return '[Circular]';
27
+ seen.add(value);
28
+ if (Array.isArray(value)) {
29
+ const result = value.map((item) => losslessJson(item, seen));
30
+ seen.delete(value);
31
+ return result;
32
+ }
33
+ const result = {};
34
+ for (const key of Object.keys(value)) {
35
+ result[key] = losslessJson(value[key], seen);
36
+ }
37
+ seen.delete(value);
38
+ return result;
39
+ }
@@ -0,0 +1,114 @@
1
+ import zlib from 'node:zlib';
2
+ import { decodePng } from './image-features.js';
3
+
4
+ const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
5
+
6
+ const CRC_TABLE = (() => {
7
+ const table = new Uint32Array(256);
8
+ for (let n = 0; n < 256; n += 1) {
9
+ let c = n;
10
+ for (let k = 0; k < 8; k += 1) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
11
+ table[n] = c >>> 0;
12
+ }
13
+ return table;
14
+ })();
15
+
16
+ function crc32(buffer) {
17
+ let crc = 0xffffffff;
18
+ for (let i = 0; i < buffer.length; i += 1) crc = CRC_TABLE[(crc ^ buffer[i]) & 0xff] ^ (crc >>> 8);
19
+ return (crc ^ 0xffffffff) >>> 0;
20
+ }
21
+
22
+ function chunk(type, data) {
23
+ const name = Buffer.from(type, 'ascii');
24
+ const length = Buffer.alloc(4);
25
+ length.writeUInt32BE(data.length, 0);
26
+ const checksum = Buffer.alloc(4);
27
+ checksum.writeUInt32BE(crc32(Buffer.concat([name, data])), 0);
28
+ return Buffer.concat([length, name, data, checksum]);
29
+ }
30
+
31
+ /** Encode RGBA pixels as an 8-bit, non-interlaced PNG. */
32
+ export function encodePng({ width, height, pixels }) {
33
+ if (!width || !height || !pixels || pixels.length !== width * height * 4) {
34
+ throw new Error('png-annotate: invalid RGBA pixels');
35
+ }
36
+ const ihdr = Buffer.alloc(13);
37
+ ihdr.writeUInt32BE(width, 0);
38
+ ihdr.writeUInt32BE(height, 4);
39
+ ihdr[8] = 8;
40
+ ihdr[9] = 6;
41
+ ihdr[10] = 0;
42
+ ihdr[11] = 0;
43
+ ihdr[12] = 0;
44
+ const raw = Buffer.alloc((width * 4 + 1) * height);
45
+ for (let y = 0; y < height; y += 1) {
46
+ const target = y * (width * 4 + 1);
47
+ raw[target] = 0;
48
+ pixels.copy ? pixels.copy(raw, target + 1, y * width * 4, (y + 1) * width * 4) : Buffer.from(pixels.buffer, pixels.byteOffset + y * width * 4, width * 4).copy(raw, target + 1);
49
+ }
50
+ const idat = zlib.deflateSync(raw, { level: 6 });
51
+ return Buffer.concat([PNG_SIGNATURE, chunk('IHDR', ihdr), chunk('IDAT', idat), chunk('IEND', Buffer.alloc(0))]);
52
+ }
53
+
54
+ function drawRect(pixels, width, height, rect, color) {
55
+ const x1 = Math.max(0, Math.min(width - 1, Math.round(Number(rect.x1) || 0)));
56
+ const y1 = Math.max(0, Math.min(height - 1, Math.round(Number(rect.y1) || 0)));
57
+ const x2 = Math.max(x1, Math.min(width - 1, Math.round(Number(rect.x2) || x1)));
58
+ const y2 = Math.max(y1, Math.min(height - 1, Math.round(Number(rect.y2) || y1)));
59
+ const draw = (x, y) => {
60
+ const index = (y * width + x) * 4;
61
+ pixels[index] = color[0];
62
+ pixels[index + 1] = color[1];
63
+ pixels[index + 2] = color[2];
64
+ pixels[index + 3] = 230;
65
+ };
66
+ for (let t = 0; t < 2; t += 1) {
67
+ for (let x = x1; x <= x2 && x < width; x += 1) {
68
+ if (y1 + t < height) draw(x, y1 + t);
69
+ if (y2 - t >= 0) draw(x, y2 - t);
70
+ }
71
+ for (let y = y1; y <= y2 && y < height; y += 1) {
72
+ if (x1 + t < width) draw(x1 + t, y);
73
+ if (x2 - t >= 0) draw(x2 - t, y);
74
+ }
75
+ }
76
+ return { x1, y1, x2, y2 };
77
+ }
78
+
79
+ /**
80
+ * Draw UIAutomator bounds over a PNG screencap.
81
+ * @param {Buffer} png screenshot bytes
82
+ * @param {Array<{bounds?:object,text?:string,contentDesc?:string,resourceId?:string}>} nodes
83
+ */
84
+ export function annotateScreenshot(png, nodes, options = {}) {
85
+ const decoded = decodePng(png);
86
+ const pixels = Buffer.from(decoded.pixels);
87
+ const boxed = [];
88
+ const colors = [
89
+ [255, 71, 87],
90
+ [255, 184, 48],
91
+ [46, 213, 115],
92
+ ];
93
+ const list = (Array.isArray(nodes) ? nodes : []).slice(0, Math.max(1, Number(options.maxNodes) || 80));
94
+ for (let i = 0; i < list.length; i += 1) {
95
+ const node = list[i];
96
+ if (!node || !node.bounds) continue;
97
+ const rect = node.bounds;
98
+ const color = colors[i % colors.length];
99
+ const box = drawRect(pixels, decoded.width, decoded.height, rect, color);
100
+ boxed.push({
101
+ ...box,
102
+ color,
103
+ label: node.text || node.contentDesc || node.resourceId || node.className || null,
104
+ });
105
+ }
106
+ return {
107
+ buffer: encodePng({ width: decoded.width, height: decoded.height, pixels }),
108
+ width: decoded.width,
109
+ height: decoded.height,
110
+ boxes: boxed,
111
+ };
112
+ }
113
+
114
+ export { PNG_SIGNATURE };
@@ -271,6 +271,14 @@ export class ScrcpyStream {
271
271
  return session.requestClipboard();
272
272
  }
273
273
 
274
+ /** Inject arbitrary UTF-8 text through the scrcpy control socket. */
275
+ async sendText(text) {
276
+ await this.ensure();
277
+ const session = this.state.session;
278
+ if (!session || session.state !== 'running') throw new Error('H.264 control stream is not running');
279
+ return session.sendText(String(text));
280
+ }
281
+
274
282
  handleClient(client) {
275
283
  this.clients.add(client);
276
284
  this._sendInit();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-apk-vm",
3
- "version": "0.10.2",
3
+ "version": "0.11.1",
4
4
  "type": "module",
5
5
  "description": "Local AOSP Android VM for DeepSeek Harness: preview rooted APKs at up to 60 fps, automate UIAutomator flows, and give each Agent an isolated VM.",
6
6
  "license": "Apache-2.0",
@@ -25,9 +25,9 @@
25
25
  "registry": "https://registry.npmjs.org/"
26
26
  },
27
27
  "scripts": {
28
- "check": "node --check index.js && node --check client.js && node --check lib/android.js && node --check lib/apk-bundle.js && node --check lib/flows.js && node --check lib/storage.js && node --check lib/ui-inspector.js && node --check lib/x509.js && node --check lib/scrcpy.js && node --check lib/ws.js && node --check lib/setup.js && node --check lib/instances.js && node --check lib/vm-ownership.js && node --check lib/http-owner.js && node --check lib/video-stream.js && node --check lib/aab.js && node --check lib/har.js && node --check lib/ui-tests.js && node --check lib/remote.js && node --check lib/image-features.js && node --check lib/control-codebook.js && node --check lib/app-skills.js",
28
+ "check": "node --check index.js && node --check client.js && node --check lib/android.js && node --check lib/apk-bundle.js && node --check lib/flows.js && node --check lib/storage.js && node --check lib/ui-inspector.js && node --check lib/x509.js && node --check lib/scrcpy.js && node --check lib/ws.js && node --check lib/setup.js && node --check lib/instances.js && node --check lib/vm-ownership.js && node --check lib/http-owner.js && node --check lib/video-stream.js && node --check lib/aab.js && node --check lib/har.js && node --check lib/ui-tests.js && node --check lib/remote.js && node --check lib/cdp.js && node --check lib/json-safe.js && node --check lib/png-annotate.js && node --check lib/image-features.js && node --check lib/control-codebook.js && node --check lib/app-skills.js",
29
29
  "privacy": "node scripts/privacy-scan.mjs",
30
- "test": "node spike/ui-smoke.cjs && node spike/ui-better-sidebar-smoke.cjs && node spike/setup-smoke.mjs && node spike/android/mock-device.mjs && node spike/android/launch-plan.mjs && node spike/instances-smoke.mjs && node spike/ownership-test.mjs && node spike/features.mjs && node spike/aab-test.mjs && node spike/har-test.mjs && node spike/ui-tests-test.mjs && node spike/remote-test.mjs && node spike/skill-test.mjs"
30
+ "test": "node spike/ui-smoke.cjs && node spike/ui-better-sidebar-smoke.cjs && node spike/setup-smoke.mjs && node spike/android/mock-device.mjs && node spike/android/launch-plan.mjs && node spike/instances-smoke.mjs && node spike/ownership-test.mjs && node spike/cdp-test.mjs && node spike/features.mjs && node spike/aab-test.mjs && node spike/har-test.mjs && node spike/ui-tests-test.mjs && node spike/remote-test.mjs && node spike/skill-test.mjs"
31
31
  },
32
32
  "exports": {
33
33
  ".": "./index.js",
@@ -57,6 +57,9 @@
57
57
  "lib/har.js",
58
58
  "lib/ui-tests.js",
59
59
  "lib/remote.js",
60
+ "lib/cdp.js",
61
+ "lib/json-safe.js",
62
+ "lib/png-annotate.js",
60
63
  "lib/image-features.js",
61
64
  "lib/control-codebook.js",
62
65
  "lib/app-skills.js",