@velmlabs/unstuck 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +23 -2
- package/dist/index.d.ts +59 -0
- package/dist/index.js +316 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @velmlabs/unstuck
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```bash
|
|
4
|
+
npm install @velmlabs/unstuck
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
Human takeover for AI browser agents. When your agent hits a CAPTCHA, a login wall or a 2FA prompt, `unstuck.takeover(page)` pauses it, gives a human a link to the live browser, and resolves when they're done.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Unstuck } from '@velmlabs/unstuck';
|
|
11
|
+
|
|
12
|
+
const unstuck = new Unstuck({ onLink: ({ url, reason }) => console.log(`${reason}: ${url}`) });
|
|
13
|
+
const result = await unstuck.takeover(page, { reason: 'Solve the CAPTCHA' }); // Playwright or Puppeteer page
|
|
14
|
+
if (result.status !== 'resolved') throw new Error(`human takeover ${result.status}`);
|
|
15
|
+
await unstuck.close();
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
`result.status` is `resolved`, `timed_out`, `cancelled` (the human tapped "Can't solve") or `failed` (for example the page closed). `takeover()` only rejects with `UnstuckEngineError` when the engine binary can't run.
|
|
19
|
+
|
|
20
|
+
The engine comes with the package: npm installs the build for your computer (macOS 13+ on Apple silicon or Intel, Linux with glibc, Windows x64). To use another binary, set `UNSTUCK_ENGINE_PATH` or the `enginePath` option.
|
|
21
|
+
|
|
22
|
+
Send the link through Unstuck Cloud, so it opens on any phone, by setting `UNSTUCK_API_KEY=uk_live_…`. Tutorials for Playwright, Puppeteer, MCP, browser-use, Stagehand and any Chromium: https://unstuck.sh/docs
|
|
23
|
+
|
|
24
|
+
License: Apache-2.0. The engine binary also contains the AGPL-3.0 viewer; see its package's NOTICE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { TakeoverResult } from '@velmlabs/unstuck-protocol';
|
|
2
|
+
export { TakeoverResult, TakeoverStatus } from '@velmlabs/unstuck-protocol';
|
|
3
|
+
|
|
4
|
+
interface TakeoverLink {
|
|
5
|
+
url: string;
|
|
6
|
+
reason: string;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
expiresAt: number;
|
|
9
|
+
/** The 6-digit code to give the human, when the takeover requires one. Never part of `url`. */
|
|
10
|
+
code?: string;
|
|
11
|
+
}
|
|
12
|
+
interface UnstuckOptions {
|
|
13
|
+
enginePath?: string;
|
|
14
|
+
host?: string;
|
|
15
|
+
port?: number;
|
|
16
|
+
/** Default takeover timeout in seconds (engine default: 600). */
|
|
17
|
+
timeoutS?: number;
|
|
18
|
+
/** Called when a human is needed. Default: print the link to stderr. */
|
|
19
|
+
onLink?: (link: TakeoverLink) => void;
|
|
20
|
+
/** Relay to use instead of the local server, e.g. wss://relay.example.com (or set UNSTUCK_RELAY_URL). */
|
|
21
|
+
relay?: string;
|
|
22
|
+
/** Key for that relay (or UNSTUCK_AGENT_KEY). With only UNSTUCK_API_KEY set, Unstuck Cloud is used. */
|
|
23
|
+
agentKey?: string;
|
|
24
|
+
/** Ask the human for a 6-digit code delivered with the link (default false). */
|
|
25
|
+
requireCode?: boolean;
|
|
26
|
+
}
|
|
27
|
+
declare class Unstuck {
|
|
28
|
+
private readonly opts;
|
|
29
|
+
private engine;
|
|
30
|
+
private readonly pages;
|
|
31
|
+
private inFlight;
|
|
32
|
+
constructor(opts?: UnstuckOptions);
|
|
33
|
+
/**
|
|
34
|
+
* Pauses your agent until a human finishes. Awaiting this IS the pause: don't drive the page meanwhile.
|
|
35
|
+
* Resolves with the outcome; rejects only with UnstuckEngineError if the engine can't run.
|
|
36
|
+
*/
|
|
37
|
+
takeover(page: unknown, opts: {
|
|
38
|
+
reason: string;
|
|
39
|
+
timeoutS?: number;
|
|
40
|
+
requireCode?: boolean;
|
|
41
|
+
}): Promise<TakeoverResult>;
|
|
42
|
+
/** Stops the engine (cancelling any running takeover). */
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
private ensureEngine;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The engine binary could not be found, started, or kept running. */
|
|
48
|
+
declare class UnstuckEngineError extends Error {
|
|
49
|
+
readonly enginePath?: string | undefined;
|
|
50
|
+
constructor(message: string, enginePath?: string | undefined);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The page was already closed when Unstuck tried to attach. */
|
|
54
|
+
declare class PageClosedError extends Error {
|
|
55
|
+
constructor();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { PageClosedError, Unstuck, UnstuckEngineError };
|
|
59
|
+
export type { TakeoverLink, UnstuckOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// src/unstuck.ts
|
|
2
|
+
import {
|
|
3
|
+
Methods as Methods2,
|
|
4
|
+
RpcCallError as RpcCallError2
|
|
5
|
+
} from "@velmlabs/unstuck-protocol";
|
|
6
|
+
|
|
7
|
+
// src/attach.ts
|
|
8
|
+
var PageClosedError = class extends Error {
|
|
9
|
+
constructor() {
|
|
10
|
+
super("page closed");
|
|
11
|
+
this.name = "PageClosedError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var counter = 0;
|
|
15
|
+
var newPageId = () => `page-${Date.now().toString(36)}-${(counter++).toString(36)}`;
|
|
16
|
+
async function attachPage(page) {
|
|
17
|
+
const p = page;
|
|
18
|
+
const isPlaywright = !!p && typeof p.context === "function" && typeof p.context()?.newCDPSession === "function";
|
|
19
|
+
const isPuppeteer = !!p && typeof p.createCDPSession === "function";
|
|
20
|
+
if (!isPlaywright && !isPuppeteer) throw new TypeError("Unsupported page object: pass a Playwright or Puppeteer Page");
|
|
21
|
+
const isClosed = () => typeof p.isClosed === "function" ? Boolean(p.isClosed()) : false;
|
|
22
|
+
if (isClosed()) throw new PageClosedError();
|
|
23
|
+
let session;
|
|
24
|
+
try {
|
|
25
|
+
session = isPlaywright ? await p.context().newCDPSession(p) : await p.createCDPSession();
|
|
26
|
+
} catch (e) {
|
|
27
|
+
if (isClosed()) throw new PageClosedError();
|
|
28
|
+
throw e;
|
|
29
|
+
}
|
|
30
|
+
const pageObj = p;
|
|
31
|
+
const closeCbs = [];
|
|
32
|
+
let closedReason = null;
|
|
33
|
+
const fire = (reason) => () => {
|
|
34
|
+
if (closedReason !== null) return;
|
|
35
|
+
closedReason = reason;
|
|
36
|
+
for (const cb of closeCbs) cb(reason);
|
|
37
|
+
};
|
|
38
|
+
const onPageClose = fire("page closed");
|
|
39
|
+
const onCrash = fire("page crashed");
|
|
40
|
+
pageObj.on("close", onPageClose);
|
|
41
|
+
pageObj.on("crash", onCrash);
|
|
42
|
+
return {
|
|
43
|
+
pageId: newPageId(),
|
|
44
|
+
url: () => pageObj.url?.() ?? "",
|
|
45
|
+
send: (method, params) => session.send(method, params),
|
|
46
|
+
subscribe: (event, cb) => void session.on(event, cb),
|
|
47
|
+
onClose: (cb) => {
|
|
48
|
+
if (closedReason !== null) cb(closedReason);
|
|
49
|
+
else closeCbs.push(cb);
|
|
50
|
+
},
|
|
51
|
+
async detach() {
|
|
52
|
+
pageObj.off?.("close", onPageClose);
|
|
53
|
+
pageObj.off?.("crash", onCrash);
|
|
54
|
+
await session.detach().catch(() => {
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/engine-process.ts
|
|
61
|
+
import { spawn } from "child_process";
|
|
62
|
+
import { createRequire } from "module";
|
|
63
|
+
import readline from "readline";
|
|
64
|
+
import { createRpcPeer, Methods, PROTOCOL_VERSION, RpcCallError } from "@velmlabs/unstuck-protocol";
|
|
65
|
+
|
|
66
|
+
// src/errors.ts
|
|
67
|
+
var UnstuckEngineError = class extends Error {
|
|
68
|
+
constructor(message, enginePath) {
|
|
69
|
+
super(message);
|
|
70
|
+
this.enginePath = enginePath;
|
|
71
|
+
this.name = "UnstuckEngineError";
|
|
72
|
+
}
|
|
73
|
+
enginePath;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/version.ts
|
|
77
|
+
var VERSION = "0.1.1";
|
|
78
|
+
|
|
79
|
+
// src/engine-process.ts
|
|
80
|
+
var requireFromHere = createRequire(import.meta.url);
|
|
81
|
+
function resolveViaEngine(id, base = requireFromHere, make = createRequire) {
|
|
82
|
+
try {
|
|
83
|
+
return make(base.resolve("@velmlabs/unstuck-engine/package.json")).resolve(id);
|
|
84
|
+
} catch {
|
|
85
|
+
return base.resolve(id);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function resolveEnginePath(opts, env = process.env, resolve = (id) => resolveViaEngine(id)) {
|
|
89
|
+
if (opts.enginePath) return opts.enginePath;
|
|
90
|
+
if (env.UNSTUCK_ENGINE_PATH) return env.UNSTUCK_ENGINE_PATH;
|
|
91
|
+
const pkg = `@velmlabs/unstuck-engine-${process.platform}-${process.arch}`;
|
|
92
|
+
const bin = `${pkg}/bin/unstuck-engine${process.platform === "win32" ? ".exe" : ""}`;
|
|
93
|
+
try {
|
|
94
|
+
return resolve(bin);
|
|
95
|
+
} catch {
|
|
96
|
+
throw new UnstuckEngineError(
|
|
97
|
+
`Unstuck engine not found. Tried: the enginePath option, UNSTUCK_ENGINE_PATH, and the package ${pkg}. Reinstall @velmlabs/unstuck without skipping optional dependencies, or set UNSTUCK_ENGINE_PATH to the unstuck-engine binary.`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
var HINT = "Set UNSTUCK_ENGINE_PATH to a working unstuck-engine binary.";
|
|
102
|
+
var HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
103
|
+
async function spawnEngine(path, opts) {
|
|
104
|
+
const args = ["--stdio"];
|
|
105
|
+
if (opts.host) args.push("--host", opts.host);
|
|
106
|
+
if (opts.port !== void 0) args.push("--port", String(opts.port));
|
|
107
|
+
const env = { ...process.env, ...opts.relay ? { UNSTUCK_RELAY_URL: opts.relay } : {}, ...opts.agentKey ? { UNSTUCK_AGENT_KEY: opts.agentKey } : {} };
|
|
108
|
+
const child = spawn(path, args, { stdio: ["pipe", "pipe", "inherit"], env });
|
|
109
|
+
const peer = createRpcPeer((line) => {
|
|
110
|
+
if (child.stdin.writable) child.stdin.write(line + "\n");
|
|
111
|
+
});
|
|
112
|
+
child.stdin.on("error", () => {
|
|
113
|
+
});
|
|
114
|
+
readline.createInterface({ input: child.stdout }).on("line", (l) => peer.receive(l));
|
|
115
|
+
const exited = new Promise((resolve) => {
|
|
116
|
+
child.once("exit", (code) => {
|
|
117
|
+
peer.close(`engine exited with code ${code}`);
|
|
118
|
+
resolve(code);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
const spawnError = new Promise(
|
|
122
|
+
(_, reject) => child.once(
|
|
123
|
+
"error",
|
|
124
|
+
(e) => reject(new UnstuckEngineError(`could not start the Unstuck engine at ${path}: ${e.message}. ${HINT}`, path))
|
|
125
|
+
)
|
|
126
|
+
);
|
|
127
|
+
const earlyExit = exited.then((code) => {
|
|
128
|
+
throw new UnstuckEngineError(`the Unstuck engine at ${path} exited with code ${code} before the handshake. ${HINT}`, path);
|
|
129
|
+
});
|
|
130
|
+
const timeoutMs = opts.handshakeTimeoutMs ?? HANDSHAKE_TIMEOUT_MS;
|
|
131
|
+
let handshakeTimer;
|
|
132
|
+
const handshakeTimeout = new Promise((_, reject) => {
|
|
133
|
+
handshakeTimer = setTimeout(
|
|
134
|
+
() => reject(new UnstuckEngineError(`the Unstuck engine at ${path} did not answer the handshake within ${timeoutMs / 1e3} s. ${HINT}`, path)),
|
|
135
|
+
timeoutMs
|
|
136
|
+
);
|
|
137
|
+
});
|
|
138
|
+
const handles = [child, child.stdin, child.stdout];
|
|
139
|
+
const setBusy = (busy) => handles.forEach((h) => busy ? h.ref() : h.unref());
|
|
140
|
+
let hello;
|
|
141
|
+
try {
|
|
142
|
+
hello = await Promise.race([
|
|
143
|
+
peer.request(Methods.hello, { sdk: "ts", sdkVersion: VERSION, protocolVersion: PROTOCOL_VERSION }),
|
|
144
|
+
spawnError,
|
|
145
|
+
earlyExit,
|
|
146
|
+
handshakeTimeout
|
|
147
|
+
]);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
child.kill();
|
|
150
|
+
if (e instanceof UnstuckEngineError) throw e;
|
|
151
|
+
if (e instanceof RpcCallError && e.code === "closed") {
|
|
152
|
+
throw new UnstuckEngineError(`the Unstuck engine at ${path} exited with code ${await exited} before the handshake. ${HINT}`, path);
|
|
153
|
+
}
|
|
154
|
+
throw new UnstuckEngineError(`the Unstuck engine at ${path} refused the handshake: ${e.message}. ${HINT}`, path);
|
|
155
|
+
} finally {
|
|
156
|
+
clearTimeout(handshakeTimer);
|
|
157
|
+
handshakeTimeout.catch(() => {
|
|
158
|
+
});
|
|
159
|
+
earlyExit.catch(() => {
|
|
160
|
+
});
|
|
161
|
+
spawnError.catch(() => {
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
setBusy(false);
|
|
165
|
+
return { peer, engineVersion: hello.engineVersion, exited, setBusy, kill: () => child.kill() };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/unstuck.ts
|
|
169
|
+
var defaultOnLink = (l) => process.stderr.write(`Unstuck: a human is needed (${l.reason}) \u2192 ${l.url}${l.code ? ` (code ${l.code})` : ""}
|
|
170
|
+
`);
|
|
171
|
+
function pageEvents(page) {
|
|
172
|
+
const p = page;
|
|
173
|
+
return typeof p?.on === "function" ? page : null;
|
|
174
|
+
}
|
|
175
|
+
function siteOf(url) {
|
|
176
|
+
try {
|
|
177
|
+
return new URL(url).hostname || void 0;
|
|
178
|
+
} catch {
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
var Unstuck = class {
|
|
183
|
+
constructor(opts = {}) {
|
|
184
|
+
this.opts = opts;
|
|
185
|
+
}
|
|
186
|
+
opts;
|
|
187
|
+
engine = null;
|
|
188
|
+
pages = /* @__PURE__ */ new Map();
|
|
189
|
+
inFlight = 0;
|
|
190
|
+
/**
|
|
191
|
+
* Pauses your agent until a human finishes. Awaiting this IS the pause: don't drive the page meanwhile.
|
|
192
|
+
* Resolves with the outcome; rejects only with UnstuckEngineError if the engine can't run.
|
|
193
|
+
*/
|
|
194
|
+
async takeover(page, opts) {
|
|
195
|
+
const pageClosed = { status: "failed", sessionId: "", url: "", durationS: 0, detail: "page_closed" };
|
|
196
|
+
let attached;
|
|
197
|
+
try {
|
|
198
|
+
attached = await attachPage(page);
|
|
199
|
+
} catch (e) {
|
|
200
|
+
if (e instanceof PageClosedError) return pageClosed;
|
|
201
|
+
throw e;
|
|
202
|
+
}
|
|
203
|
+
let engine;
|
|
204
|
+
const popups = [];
|
|
205
|
+
let onPopup = null;
|
|
206
|
+
let closedEarly = false;
|
|
207
|
+
let notifyClose = null;
|
|
208
|
+
let wakeOnClose;
|
|
209
|
+
const closedWhileStarting = new Promise((r) => wakeOnClose = () => r(null));
|
|
210
|
+
attached.onClose((reason) => {
|
|
211
|
+
if (notifyClose) notifyClose(reason);
|
|
212
|
+
else {
|
|
213
|
+
closedEarly = true;
|
|
214
|
+
wakeOnClose();
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
try {
|
|
218
|
+
engine = await Promise.race([this.ensureEngine(), closedWhileStarting]) ?? void 0;
|
|
219
|
+
if (closedEarly || !engine) return pageClosed;
|
|
220
|
+
const live = engine;
|
|
221
|
+
this.pages.set(attached.pageId, attached);
|
|
222
|
+
notifyClose = (reason) => live.peer.notify(Methods2.cdpClosed, { pageId: attached.pageId, reason });
|
|
223
|
+
onPopup = async (popupPage) => {
|
|
224
|
+
try {
|
|
225
|
+
const popup = await attachPage(popupPage);
|
|
226
|
+
popups.push(popup);
|
|
227
|
+
this.pages.set(popup.pageId, popup);
|
|
228
|
+
popup.onClose((reason) => live.peer.notify(Methods2.cdpClosed, { pageId: popup.pageId, reason }));
|
|
229
|
+
live.peer.notify(Methods2.pagePopup, { pageId: attached.pageId, popupPageId: popup.pageId });
|
|
230
|
+
} catch {
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
pageEvents(page)?.on("popup", onPopup);
|
|
234
|
+
engine.setBusy(++this.inFlight > 0);
|
|
235
|
+
const requireCode = opts.requireCode ?? this.opts.requireCode;
|
|
236
|
+
const params = {
|
|
237
|
+
pageId: attached.pageId,
|
|
238
|
+
reason: opts.reason,
|
|
239
|
+
timeoutS: opts.timeoutS ?? this.opts.timeoutS,
|
|
240
|
+
site: siteOf(attached.url()),
|
|
241
|
+
...requireCode !== void 0 ? { requireCode } : {}
|
|
242
|
+
};
|
|
243
|
+
const died = engine.exited.then((code) => {
|
|
244
|
+
throw new UnstuckEngineError(`engine exited with code ${code}`);
|
|
245
|
+
});
|
|
246
|
+
died.catch(() => {
|
|
247
|
+
});
|
|
248
|
+
try {
|
|
249
|
+
return await Promise.race([engine.peer.request(Methods2.takeover, params), died]);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
if (e instanceof RpcCallError2 && e.code === "closed") throw new UnstuckEngineError(`engine exited with code ${await engine.exited}`);
|
|
252
|
+
throw e;
|
|
253
|
+
}
|
|
254
|
+
} finally {
|
|
255
|
+
if (onPopup) pageEvents(page)?.off?.("popup", onPopup);
|
|
256
|
+
for (const popup of popups) {
|
|
257
|
+
this.pages.delete(popup.pageId);
|
|
258
|
+
await popup.detach();
|
|
259
|
+
}
|
|
260
|
+
this.pages.delete(attached.pageId);
|
|
261
|
+
await attached.detach();
|
|
262
|
+
if (engine && notifyClose) engine.setBusy(--this.inFlight > 0);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** Stops the engine (cancelling any running takeover). */
|
|
266
|
+
async close() {
|
|
267
|
+
const pending = this.engine;
|
|
268
|
+
this.engine = null;
|
|
269
|
+
if (!pending) return;
|
|
270
|
+
const engine = await pending.catch(() => null);
|
|
271
|
+
if (!engine) return;
|
|
272
|
+
engine.setBusy(true);
|
|
273
|
+
engine.peer.request(Methods2.shutdown, {}).catch(() => {
|
|
274
|
+
});
|
|
275
|
+
const timer = setTimeout(() => engine.kill(), 2e3);
|
|
276
|
+
await engine.exited;
|
|
277
|
+
clearTimeout(timer);
|
|
278
|
+
}
|
|
279
|
+
ensureEngine() {
|
|
280
|
+
if (this.engine) return this.engine;
|
|
281
|
+
const started = (async () => {
|
|
282
|
+
const engine = await spawnEngine(resolveEnginePath(this.opts), { host: this.opts.host, port: this.opts.port, relay: this.opts.relay, agentKey: this.opts.agentKey });
|
|
283
|
+
engine.peer.handle(Methods2.cdpSend, (raw) => {
|
|
284
|
+
const p = raw;
|
|
285
|
+
const page = this.pages.get(p.pageId);
|
|
286
|
+
if (!page) throw new Error(`unknown page ${p.pageId}`);
|
|
287
|
+
return page.send(p.method, p.params);
|
|
288
|
+
});
|
|
289
|
+
engine.peer.handle(Methods2.cdpSubscribe, (raw) => {
|
|
290
|
+
const p = raw;
|
|
291
|
+
const page = this.pages.get(p.pageId);
|
|
292
|
+
if (!page) throw new Error(`unknown page ${p.pageId}`);
|
|
293
|
+
for (const event of p.events) page.subscribe(event, (params) => engine.peer.notify(Methods2.cdpEvent, { pageId: p.pageId, method: event, params }));
|
|
294
|
+
return {};
|
|
295
|
+
});
|
|
296
|
+
engine.peer.onNotification(Methods2.takeoverLink, (raw) => {
|
|
297
|
+
const l = raw;
|
|
298
|
+
(this.opts.onLink ?? defaultOnLink)({ url: l.url, reason: l.reason, sessionId: l.sessionId, expiresAt: l.expiresAt, ...l.code ? { code: l.code } : {} });
|
|
299
|
+
});
|
|
300
|
+
void engine.exited.then(() => {
|
|
301
|
+
if (this.engine === started) this.engine = null;
|
|
302
|
+
});
|
|
303
|
+
return engine;
|
|
304
|
+
})();
|
|
305
|
+
this.engine = started;
|
|
306
|
+
started.catch(() => {
|
|
307
|
+
if (this.engine === started) this.engine = null;
|
|
308
|
+
});
|
|
309
|
+
return started;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
export {
|
|
313
|
+
PageClosedError,
|
|
314
|
+
Unstuck,
|
|
315
|
+
UnstuckEngineError
|
|
316
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@velmlabs/unstuck",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"ai",
|
|
7
|
+
"agent",
|
|
8
|
+
"browser",
|
|
9
|
+
"captcha",
|
|
10
|
+
"human-in-the-loop",
|
|
11
|
+
"playwright",
|
|
12
|
+
"puppeteer",
|
|
13
|
+
"cdp",
|
|
14
|
+
"mcp"
|
|
15
|
+
],
|
|
16
|
+
"homepage": "https://unstuck.sh/docs",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/velmlabs/unstuck/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/velmlabs/unstuck.git",
|
|
23
|
+
"directory": "sdk-ts"
|
|
24
|
+
},
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"author": "Velm Labs",
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=20"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@velmlabs/unstuck-engine": "0.1.1",
|
|
45
|
+
"@velmlabs/unstuck-protocol": "0.1.1"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsc -p tsconfig.json --noEmit",
|
|
52
|
+
"test": "vitest run --coverage",
|
|
53
|
+
"build:dist": "tsup"
|
|
54
|
+
}
|
|
6
55
|
}
|