@velmlabs/unstuck-core 0.2.5 → 0.2.7
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/dist/index.d.ts +1 -1
- package/dist/index.js +98 -23
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -80,7 +80,7 @@ declare function probeCapabilities(cdp: CdpClient, opts?: {
|
|
|
80
80
|
}): Promise<Capabilities>;
|
|
81
81
|
|
|
82
82
|
/** Replays one viewer input message into the page via CDP Input.* */
|
|
83
|
-
declare function applyInput(cdp: CdpClient, msg: ViewerInput, caps?: Capabilities): Promise<void>;
|
|
83
|
+
declare function applyInput(cdp: Pick<CdpClient, 'send'>, msg: ViewerInput, caps?: Capabilities): Promise<void>;
|
|
84
84
|
|
|
85
85
|
interface FrameSink {
|
|
86
86
|
send(b: Uint8Array): void;
|
package/dist/index.js
CHANGED
|
@@ -63,6 +63,16 @@ import {
|
|
|
63
63
|
parseViewerMessage
|
|
64
64
|
} from "@velmlabs/unstuck-protocol";
|
|
65
65
|
|
|
66
|
+
// src/stream/within.ts
|
|
67
|
+
var CAPTURE_TIMEOUT_MS = 5e3;
|
|
68
|
+
function within(p, ms, what) {
|
|
69
|
+
let timer;
|
|
70
|
+
const late = new Promise((_, reject) => {
|
|
71
|
+
timer = setTimeout(() => reject(new Error(`${what} did not answer within ${ms} ms`)), ms);
|
|
72
|
+
});
|
|
73
|
+
return Promise.race([p, late]).finally(() => clearTimeout(timer));
|
|
74
|
+
}
|
|
75
|
+
|
|
66
76
|
// src/stream/screencast.ts
|
|
67
77
|
function b64ToBytes(b64) {
|
|
68
78
|
const bin = atob(b64);
|
|
@@ -97,7 +107,7 @@ async function startScreencast(cdp, onFrame, opts = {}) {
|
|
|
97
107
|
const maxWidth = opts.maxWidth ?? 1600;
|
|
98
108
|
const maxHeight = opts.maxHeight ?? 1600;
|
|
99
109
|
await cdp.send("Page.startScreencast", { format: "jpeg", quality, maxWidth, maxHeight });
|
|
100
|
-
void captureFrame(cdp, quality, maxWidth, maxHeight).then(
|
|
110
|
+
void within(captureFrame(cdp, quality, maxWidth, maxHeight), CAPTURE_TIMEOUT_MS, "Page.captureScreenshot").then(
|
|
101
111
|
(f) => {
|
|
102
112
|
if (f && !framed && !stopped) onFrame(f);
|
|
103
113
|
},
|
|
@@ -122,8 +132,12 @@ async function startScreenshotPolling(cdp, onFrame, opts = {}) {
|
|
|
122
132
|
const tick = async () => {
|
|
123
133
|
const started = Date.now();
|
|
124
134
|
try {
|
|
125
|
-
const metrics = await
|
|
126
|
-
|
|
135
|
+
const metrics = await within(
|
|
136
|
+
cdp.send("Page.getLayoutMetrics"),
|
|
137
|
+
CAPTURE_TIMEOUT_MS,
|
|
138
|
+
"Page.getLayoutMetrics"
|
|
139
|
+
);
|
|
140
|
+
const shot = await within(cdp.send("Page.captureScreenshot", { format: "jpeg", quality }), CAPTURE_TIMEOUT_MS, "Page.captureScreenshot");
|
|
127
141
|
if (!stopped) {
|
|
128
142
|
const bin = atob(shot.data);
|
|
129
143
|
onFrame({
|
|
@@ -709,6 +723,8 @@ var HELLO_TIMEOUT_MS = 1e4;
|
|
|
709
723
|
var MAX_CODE_ATTEMPTS = 5;
|
|
710
724
|
var DRAIN_INTERVAL_MS = 50;
|
|
711
725
|
var STOP_TIMEOUT_MS = 2e3;
|
|
726
|
+
var MAX_PENDING_INPUT = 256;
|
|
727
|
+
var DROPPED = new Error("input dropped");
|
|
712
728
|
var FINAL_EVENT = {
|
|
713
729
|
resolved: "done",
|
|
714
730
|
cancelled: "cancel",
|
|
@@ -762,7 +778,11 @@ function runTakeover(deps, cdp, opts) {
|
|
|
762
778
|
} };
|
|
763
779
|
let offPopups = () => {
|
|
764
780
|
};
|
|
765
|
-
|
|
781
|
+
const pendingInput = [];
|
|
782
|
+
let pumping = false;
|
|
783
|
+
let inFlight = null;
|
|
784
|
+
let inputDropped = 0;
|
|
785
|
+
let pressed = null;
|
|
766
786
|
const helloTimers = /* @__PURE__ */ new Set();
|
|
767
787
|
let resolveResult;
|
|
768
788
|
const result = new Promise((r) => resolveResult = r);
|
|
@@ -814,20 +834,83 @@ function runTakeover(deps, cdp, opts) {
|
|
|
814
834
|
sendJson(viewer, { t: "state", status });
|
|
815
835
|
viewer.close(1e3, status);
|
|
816
836
|
}
|
|
837
|
+
inputDropped += pendingInput.splice(0).length;
|
|
838
|
+
const lastDispatch = inFlight;
|
|
817
839
|
const durationS = Math.round(now() - startedAt) / 1e3;
|
|
818
840
|
void (async () => {
|
|
841
|
+
await bounded((async () => {
|
|
842
|
+
await lastDispatch;
|
|
843
|
+
await releasePressed();
|
|
844
|
+
})());
|
|
819
845
|
await stopStream();
|
|
820
|
-
|
|
821
|
-
await Promise.race([Promise.all(locks.map((l) => l?.stop())), new Promise((r) => unlockTimer = setTimeout(r, STOP_TIMEOUT_MS))]);
|
|
822
|
-
clearTimeout(unlockTimer);
|
|
846
|
+
await bounded(Promise.all(locks.map((l) => l?.stop())));
|
|
823
847
|
if (published) await transport.unpublish(sessionId).catch(() => {
|
|
824
848
|
});
|
|
825
|
-
emit("finished", { status: final, durationS, inputCount, ...logged ?? (detail ? { detail } : {}) });
|
|
849
|
+
emit("finished", { status: final, durationS, inputCount, ...inputDropped ? { inputDropped } : {}, ...logged ?? (detail ? { detail } : {}) });
|
|
826
850
|
const fallbacks = [!caps.screencast && "screenshots", !caps.insertText && "per-key typing"].filter(Boolean).join(", ");
|
|
827
851
|
const shown = detail ?? (fallbacks ? `fallback: ${fallbacks}` : void 0);
|
|
828
852
|
resolveResult({ status: final, sessionId, url, durationS, ...shown ? { detail: shown } : {} });
|
|
829
853
|
})();
|
|
830
854
|
}
|
|
855
|
+
async function bounded(p) {
|
|
856
|
+
let timer;
|
|
857
|
+
await Promise.race([p, new Promise((r) => timer = setTimeout(r, STOP_TIMEOUT_MS))]);
|
|
858
|
+
clearTimeout(timer);
|
|
859
|
+
}
|
|
860
|
+
async function releasePressed() {
|
|
861
|
+
const p = pressed;
|
|
862
|
+
pressed = null;
|
|
863
|
+
if (!p) return;
|
|
864
|
+
const { x, y, button, clickCount } = p;
|
|
865
|
+
await p.target.cdp.send("Input.dispatchMouseEvent", { type: "mouseReleased", x, y, button, clickCount, modifiers: 0 }).catch(() => {
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
function enqueueInput(conn, input) {
|
|
869
|
+
if (isTerminal(status) || conn !== holder) return void inputDropped++;
|
|
870
|
+
if (pendingInput.length >= MAX_PENDING_INPUT) {
|
|
871
|
+
let i = pendingInput.findIndex((p) => p.input.t === "wheel" || p.input.t === "mouse" && p.input.action === "move");
|
|
872
|
+
if (i < 0 && isRelease(input)) i = pendingInput.findIndex((p) => !isRelease(p.input));
|
|
873
|
+
inputDropped++;
|
|
874
|
+
if (i < 0) return;
|
|
875
|
+
pendingInput.splice(i, 1);
|
|
876
|
+
}
|
|
877
|
+
pendingInput.push({ conn, input });
|
|
878
|
+
if (!pumping) void pumpInput();
|
|
879
|
+
}
|
|
880
|
+
async function pumpInput() {
|
|
881
|
+
pumping = true;
|
|
882
|
+
for (let next = pendingInput.shift(); next; next = pendingInput.shift()) {
|
|
883
|
+
inFlight = dispatchInput(next.conn, next.input);
|
|
884
|
+
await inFlight;
|
|
885
|
+
}
|
|
886
|
+
inFlight = null;
|
|
887
|
+
pumping = false;
|
|
888
|
+
}
|
|
889
|
+
async function dispatchInput(conn, input) {
|
|
890
|
+
const live = () => !isTerminal(status) && conn === holder;
|
|
891
|
+
if (!live()) return void inputDropped++;
|
|
892
|
+
const target = active();
|
|
893
|
+
const guarded = {
|
|
894
|
+
send: (method, params) => live() ? target.cdp.send(method, params) : Promise.reject(DROPPED)
|
|
895
|
+
};
|
|
896
|
+
inputCount++;
|
|
897
|
+
try {
|
|
898
|
+
const msgs = input.t === "key" ? translateShortcut(input, await browserPlatformIsMac(target.cdp)) : [input];
|
|
899
|
+
for (const m of msgs) {
|
|
900
|
+
await applyInput(guarded, m, target.caps);
|
|
901
|
+
if (m.t === "mouse") trackButton(target, m);
|
|
902
|
+
}
|
|
903
|
+
} catch (e) {
|
|
904
|
+
if (e === DROPPED) return;
|
|
905
|
+
return onTargetError(target, e);
|
|
906
|
+
}
|
|
907
|
+
if (live()) fieldFeed.onInput(input);
|
|
908
|
+
}
|
|
909
|
+
function trackButton(target, m) {
|
|
910
|
+
if (m.action === "down") pressed = { target, x: m.x, y: m.y, button: m.button, clickCount: m.clickCount };
|
|
911
|
+
else if (m.action === "up") pressed = null;
|
|
912
|
+
else if (pressed) pressed = { ...pressed, x: m.x, y: m.y };
|
|
913
|
+
}
|
|
831
914
|
async function attachViewer(conn, vn) {
|
|
832
915
|
const previous = holder;
|
|
833
916
|
holder = conn;
|
|
@@ -992,18 +1075,7 @@ function runTakeover(deps, cdp, opts) {
|
|
|
992
1075
|
}
|
|
993
1076
|
default: {
|
|
994
1077
|
const safe = safeInput(msg);
|
|
995
|
-
if (
|
|
996
|
-
inputCount++;
|
|
997
|
-
inputChain = inputChain.then(async () => {
|
|
998
|
-
const target = active();
|
|
999
|
-
try {
|
|
1000
|
-
const msgs = safe.t === "key" ? translateShortcut(safe, await browserPlatformIsMac(target.cdp)) : [safe];
|
|
1001
|
-
for (const m of msgs) await applyInput(target.cdp, m, target.caps);
|
|
1002
|
-
} catch (e) {
|
|
1003
|
-
return onTargetError(target, e);
|
|
1004
|
-
}
|
|
1005
|
-
fieldFeed.onInput(safe);
|
|
1006
|
-
});
|
|
1078
|
+
if (safe) enqueueInput(conn, safe);
|
|
1007
1079
|
}
|
|
1008
1080
|
}
|
|
1009
1081
|
}
|
|
@@ -1090,6 +1162,9 @@ function runTakeover(deps, cdp, opts) {
|
|
|
1090
1162
|
}
|
|
1091
1163
|
return { result, cancel: (detail) => finish("cancelled", detail), fail, joined };
|
|
1092
1164
|
}
|
|
1165
|
+
function isRelease(m) {
|
|
1166
|
+
return m.t === "mouse" && m.action === "up";
|
|
1167
|
+
}
|
|
1093
1168
|
function newCode() {
|
|
1094
1169
|
const buf = new Uint32Array(1);
|
|
1095
1170
|
do
|
|
@@ -1416,7 +1491,7 @@ function createCloudAlerter(o) {
|
|
|
1416
1491
|
const headers = { Authorization: `Bearer ${o.apiKey}` };
|
|
1417
1492
|
let cache = null;
|
|
1418
1493
|
let lastSet = null;
|
|
1419
|
-
async function
|
|
1494
|
+
async function within2(what, call) {
|
|
1420
1495
|
const ac = new AbortController();
|
|
1421
1496
|
let timer;
|
|
1422
1497
|
const timeout = new Promise((_, reject) => {
|
|
@@ -1433,7 +1508,7 @@ function createCloudAlerter(o) {
|
|
|
1433
1508
|
}
|
|
1434
1509
|
async function devices() {
|
|
1435
1510
|
if (cache && now() - cache.at < CACHE_MS) return cache.devices;
|
|
1436
|
-
const list = await
|
|
1511
|
+
const list = await within2("devices lookup", async (signal) => {
|
|
1437
1512
|
const res = await doFetch(`${base}/api/v1/agent/devices`, { headers, signal });
|
|
1438
1513
|
if (res.status === 403 && (await res.json().catch(() => null))?.error === "alerts_not_allowed") {
|
|
1439
1514
|
log("[unstuck] alerts are not enabled for this API key (turn them on in your unstuck.sh dashboard)");
|
|
@@ -1472,7 +1547,7 @@ function createCloudAlerter(o) {
|
|
|
1472
1547
|
log(`[unstuck] alert sealed to ${ok.length} phone(s): ${named}`);
|
|
1473
1548
|
}
|
|
1474
1549
|
const sealed = ok.map((r) => ({ device: r.d.id, blob: r.blob }));
|
|
1475
|
-
const res = await
|
|
1550
|
+
const res = await within2(
|
|
1476
1551
|
"notify",
|
|
1477
1552
|
(signal) => doFetch(`${base}/api/v1/agent/notify`, {
|
|
1478
1553
|
method: "POST",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@velmlabs/unstuck-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"description": "The Unstuck takeover engine core: screencast, input, sessions and transports, usable in Node or Cloudflare Workers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"tldts": "^7.5.2",
|
|
50
|
-
"@velmlabs/unstuck-protocol": "0.2.
|
|
50
|
+
"@velmlabs/unstuck-protocol": "0.2.7"
|
|
51
51
|
},
|
|
52
52
|
"publishConfig": {
|
|
53
53
|
"access": "public"
|