@edlsh/browse-core 0.2.1 → 0.2.2
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/session.js +39 -12
- package/package.json +1 -1
package/dist/session.js
CHANGED
|
@@ -185,6 +185,21 @@ function messageOf(error) {
|
|
|
185
185
|
* @browserbasehq/stagehand.
|
|
186
186
|
*/
|
|
187
187
|
async function resetStagehandRuntime(connectUrl, signal) {
|
|
188
|
+
await withRawCdp(connectUrl, signal, async (send) => {
|
|
189
|
+
const { targetInfos } = await send("Target.getTargets");
|
|
190
|
+
const workers = targetInfos
|
|
191
|
+
.filter(target => target.type === "service_worker" && target.url.startsWith("chrome-extension://") && target.url.includes("service-worker.js"));
|
|
192
|
+
// The reply reaches whichever client is attached next, whose request ids count up from 1: use one it never reaches.
|
|
193
|
+
const close = JSON.stringify(JSON.stringify({ jsonrpc: "2.0", id: Number.MAX_SAFE_INTEGER, method: "stagehand.close", params: {} }));
|
|
194
|
+
for (const worker of workers) {
|
|
195
|
+
const { sessionId } = await send("Target.attachToTarget", { targetId: worker.targetId, flatten: true });
|
|
196
|
+
// Optional call: only the Stagehand runtime defines this receiver.
|
|
197
|
+
await send("Runtime.evaluate", { expression: `void globalThis.__stagehandReceiveFromHost?.(${close}); true`, returnByValue: true }, sessionId);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** Runs `use` over a short-lived raw CDP connection to a browser-level endpoint, then closes it. */
|
|
202
|
+
async function withRawCdp(connectUrl, signal, use) {
|
|
188
203
|
const socket = new WebSocket(connectUrl);
|
|
189
204
|
const pending = new Map();
|
|
190
205
|
let nextId = 0;
|
|
@@ -208,26 +223,31 @@ async function resetStagehandRuntime(connectUrl, signal) {
|
|
|
208
223
|
const send = (method, params = {}, sessionId) => {
|
|
209
224
|
const id = ++nextId;
|
|
210
225
|
const reply = new Promise((resolve, reject) => pending.set(id, { resolve, reject }));
|
|
211
|
-
socket.send(JSON.stringify({ id, method, params,
|
|
226
|
+
socket.send(JSON.stringify(sessionId ? { id, method, params, sessionId } : { id, method, params }));
|
|
212
227
|
return abortable(Promise.race([reply, closed]), signal);
|
|
213
228
|
};
|
|
214
229
|
try {
|
|
215
230
|
await abortable(Promise.race([new Promise(resolve => socket.addEventListener("open", resolve, { once: true })), closed]), signal);
|
|
216
|
-
|
|
217
|
-
const workers = targetInfos
|
|
218
|
-
.filter(target => target.type === "service_worker" && target.url.startsWith("chrome-extension://") && target.url.includes("service-worker.js"));
|
|
219
|
-
// The reply reaches whichever client is attached next, whose request ids count up from 1: use one it never reaches.
|
|
220
|
-
const close = JSON.stringify(JSON.stringify({ jsonrpc: "2.0", id: Number.MAX_SAFE_INTEGER, method: "stagehand.close", params: {} }));
|
|
221
|
-
for (const worker of workers) {
|
|
222
|
-
const { sessionId } = await send("Target.attachToTarget", { targetId: worker.targetId, flatten: true });
|
|
223
|
-
// Optional call: only the Stagehand runtime defines this receiver.
|
|
224
|
-
await send("Runtime.evaluate", { expression: `void globalThis.__stagehandReceiveFromHost?.(${close}); true`, returnByValue: true }, sessionId);
|
|
225
|
-
}
|
|
231
|
+
return await use(send);
|
|
226
232
|
}
|
|
227
233
|
finally {
|
|
228
234
|
socket.close();
|
|
229
235
|
}
|
|
230
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Opens a window with a blank tab in a running browser that has no tab, and returns its id; else undefined. macOS
|
|
239
|
+
* Chrome keeps running after its last window closes, and in that state no client can create a tab, so Stagehand cannot
|
|
240
|
+
* attach. The tab belongs to whoever attaches, whose close removes it again.
|
|
241
|
+
*/
|
|
242
|
+
async function ensurePageTarget(connectUrl, signal) {
|
|
243
|
+
return withRawCdp(connectUrl, signal, async (send) => {
|
|
244
|
+
const { targetInfos } = await send("Target.getTargets");
|
|
245
|
+
if (targetInfos.some(target => target.type === "page"))
|
|
246
|
+
return undefined;
|
|
247
|
+
const { targetId } = await send("Target.createTarget", { url: "about:blank", newWindow: true });
|
|
248
|
+
return z.string().parse(targetId);
|
|
249
|
+
});
|
|
250
|
+
}
|
|
231
251
|
/** Gives recurring Stagehand failures a code and a next step; anything else passes through unchanged. */
|
|
232
252
|
function translate(error) {
|
|
233
253
|
if (error instanceof BrowseToolError)
|
|
@@ -958,6 +978,8 @@ export class BrowseSession {
|
|
|
958
978
|
let browser;
|
|
959
979
|
let guard;
|
|
960
980
|
let quitPort;
|
|
981
|
+
/** A tab opened in an attached browser that had none; the first conversation takes it as its own. */
|
|
982
|
+
let createdTab;
|
|
961
983
|
if (attach) {
|
|
962
984
|
// Stagehand's close() would send Browser.close to an attached browser: connect through a guard that drops it.
|
|
963
985
|
const opened = await guardEndpoint(attach.cdpUrl, context.signal, attach.headers).catch(error => {
|
|
@@ -966,6 +988,11 @@ export class BrowseSession {
|
|
|
966
988
|
throw new BrowseToolError("attach_failed", `Could not reach the DevTools endpoint: ${messageOf(error).slice(0, 200)}. Check cdpUrl and any headers it needs.`);
|
|
967
989
|
});
|
|
968
990
|
guard = opened;
|
|
991
|
+
createdTab = await ensurePageTarget(opened.url, context.signal).catch(error => {
|
|
992
|
+
if (context.signal.aborted)
|
|
993
|
+
throw error;
|
|
994
|
+
return undefined;
|
|
995
|
+
});
|
|
969
996
|
// Stagehand's in-browser extension would follow the client to the loopback guard, which a browser elsewhere cannot reach.
|
|
970
997
|
const extensionCdpUrl = attach.extensionCdpUrl ?? (!LOOPBACK.has(new URL(attach.cdpUrl).hostname) ? opened.browserUrl : undefined);
|
|
971
998
|
try {
|
|
@@ -1023,7 +1050,7 @@ export class BrowseSession {
|
|
|
1023
1050
|
context.signal.throwIfAborted();
|
|
1024
1051
|
return {
|
|
1025
1052
|
key, browser, stagehand, quitPort, debugPort: quitPort, guard, cdpUrl: attach?.cdpUrl, endpoint: p.endpoint,
|
|
1026
|
-
headless: attach ? undefined : p.headless ?? false, domainPolicy, users: new Set(), closedTabs: new Set(), firstTab: attach ?
|
|
1053
|
+
headless: attach ? undefined : p.headless ?? false, domainPolicy, users: new Set(), closedTabs: new Set(), firstTab: attach ? createdTab : active?.pageId,
|
|
1027
1054
|
inference, aiTurn: Promise.resolve(),
|
|
1028
1055
|
};
|
|
1029
1056
|
}
|
package/package.json
CHANGED