@velmlabs/unstuck 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.
@@ -213,7 +213,7 @@ function startKeepAlive(cdp, intervalMs = 6e4) {
213
213
  }
214
214
 
215
215
  // src/version.ts
216
- var VERSION = "0.2.5";
216
+ var VERSION = "0.2.7";
217
217
 
218
218
  // src/in-process.ts
219
219
  var NO_RELAY_EDGE = "in a Worker, Unstuck needs a relay: set apiKey or relay + agentKey";
@@ -225,6 +225,11 @@ function defaultOnLink(l, alert, alerts) {
225
225
  else if (alerts !== "required") full();
226
226
  });
227
227
  }
228
+ function newSignal() {
229
+ let fire;
230
+ const promise = new Promise((r) => fire = r);
231
+ return { promise, fire };
232
+ }
228
233
  function siteOf(url) {
229
234
  try {
230
235
  return new URL(url).hostname || void 0;
@@ -250,6 +255,8 @@ var InProcessUnstuck = class {
250
255
  engine = null;
251
256
  /** Bumped by close(): a takeover that was still attaching when it ran must not start a new engine. */
252
257
  generation = 0;
258
+ /** Resolved by close(): a page still attaching (createCDPSession may never answer) must not hold close() up. */
259
+ closing = newSignal();
253
260
  inFlight = /* @__PURE__ */ new Set();
254
261
  async takeover(page, o) {
255
262
  const run = this.run(page, o);
@@ -268,16 +275,25 @@ var InProcessUnstuck = class {
268
275
  if (requireCode && !this.opts.onLink)
269
276
  throw new TypeError("requireCode needs an onLink handler that sends the 6-digit code by a different channel than the link (printed together, the code protects nothing)");
270
277
  const generation = this.generation;
278
+ const engineClosed = { status: "cancelled", sessionId: "", url: "", durationS: 0, detail: "engine_closed" };
271
279
  let attached;
272
280
  try {
273
- attached = await attachPage(page);
281
+ const attaching = attachPage(page);
282
+ const first = await Promise.race([attaching, this.closing.promise.then(() => null)]);
283
+ if (!first) {
284
+ attaching.then((a) => a.detach(), () => {
285
+ }).catch(() => {
286
+ });
287
+ return engineClosed;
288
+ }
289
+ attached = first;
274
290
  } catch (e) {
275
291
  if (e instanceof PageClosedError) return { status: "failed", sessionId: "", url: "", durationS: 0, detail: "page_closed" };
276
292
  throw e;
277
293
  }
278
294
  if (generation !== this.generation) {
279
295
  await attached.detach();
280
- return { status: "cancelled", sessionId: "", url: "", durationS: 0, detail: "engine_closed" };
296
+ return engineClosed;
281
297
  }
282
298
  const cdp = toCdpClient(attached);
283
299
  const stopKeepAlive = startKeepAlive(cdp);
@@ -329,10 +345,14 @@ var InProcessUnstuck = class {
329
345
  }
330
346
  /**
331
347
  * Cancels running takeovers (`engine_closed`), also those still attaching their page, closes the relay sockets and
332
- * resolves once they have all ended. A later takeover starts a new engine.
348
+ * resolves once they have all ended. A later takeover starts a new engine. Safe to call (and to await) from inside
349
+ * onLink: the takeover that called it ends with `engine_closed` like the others.
333
350
  */
334
351
  async close() {
335
352
  this.generation++;
353
+ const closing = this.closing;
354
+ this.closing = newSignal();
355
+ closing.fire();
336
356
  const engine = this.engine;
337
357
  this.engine = null;
338
358
  await engine?.close();
package/dist/edge.d.ts CHANGED
@@ -7,7 +7,7 @@ declare class Unstuck {
7
7
  private readonly inner;
8
8
  constructor(opts?: UnstuckOptions);
9
9
  takeover(page: unknown, o: TakeoverOptions): Promise<TakeoverResult>;
10
- /** Waits for in-flight takeovers: do not await it from inside onLink. */
10
+ /** Cancels running takeovers (`engine_closed`) and resolves once they have ended; safe to call from inside onLink. */
11
11
  close(): Promise<void>;
12
12
  }
13
13
 
package/dist/edge.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  UnstuckConfigError,
5
5
  UnstuckEngineError,
6
6
  validateOptions
7
- } from "./chunk-MHTC2ZLV.js";
7
+ } from "./chunk-Q4IMKIP6.js";
8
8
 
9
9
  // src/edge.ts
10
10
  var Unstuck = class {
@@ -17,7 +17,7 @@ var Unstuck = class {
17
17
  takeover(page, o) {
18
18
  return this.inner.takeover(page, o);
19
19
  }
20
- /** Waits for in-flight takeovers: do not await it from inside onLink. */
20
+ /** Cancels running takeovers (`engine_closed`) and resolves once they have ended; safe to call from inside onLink. */
21
21
  close() {
22
22
  return this.inner.close();
23
23
  }
package/dist/index.js CHANGED
@@ -8,7 +8,7 @@ import {
8
8
  callUserOnLink,
9
9
  optionEnv,
10
10
  validateOptions
11
- } from "./chunk-MHTC2ZLV.js";
11
+ } from "./chunk-Q4IMKIP6.js";
12
12
 
13
13
  // src/unstuck.ts
14
14
  import {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velmlabs/unstuck",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Human takeover for AI browser agents: when your agent gets stuck, a person takes over the live browser and hands it back.",
5
5
  "keywords": [
6
6
  "ai",
@@ -53,9 +53,9 @@
53
53
  "node": ">=20"
54
54
  },
55
55
  "dependencies": {
56
- "@velmlabs/unstuck-engine": "0.2.5",
57
- "@velmlabs/unstuck-protocol": "0.2.5",
58
- "@velmlabs/unstuck-core": "0.2.5"
56
+ "@velmlabs/unstuck-engine": "0.2.7",
57
+ "@velmlabs/unstuck-protocol": "0.2.7",
58
+ "@velmlabs/unstuck-core": "0.2.7"
59
59
  },
60
60
  "publishConfig": {
61
61
  "access": "public"
@@ -63,10 +63,11 @@
63
63
  "devDependencies": {
64
64
  "@cloudflare/vitest-plugin": "^1.8.1",
65
65
  "@cloudflare/workers-types": "^5.20261112.1",
66
- "wrangler": "^4.161.0"
66
+ "wrangler": "^4.161.0",
67
+ "@velmlabs/unstuck-relay-cloudflare": "0.0.0"
67
68
  },
68
69
  "scripts": {
69
- "build": "tsc -p tsconfig.json --noEmit",
70
+ "build": "tsc -p tsconfig.json --noEmit && tsc -p test-workers/tsconfig.json --noEmit",
70
71
  "test": "vitest run --coverage",
71
72
  "build:dist": "tsup",
72
73
  "test:workers": "vitest run --config vitest.workers.config.ts"