effect-browserbase 0.2.0-beta.9 → 0.3.0-beta.10
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/README.md +277 -319
- package/dist/Browserbase-CaPKwEY9.mjs +544 -0
- package/dist/Browserbase-CaPKwEY9.mjs.map +1 -0
- package/dist/Browserbase.d.mts +110 -0
- package/dist/Browserbase.mjs +3 -0
- package/dist/BrowserbaseClient-CtAQ2rg4.d.mts +195 -0
- package/dist/BrowserbaseClient.d.mts +2 -0
- package/dist/BrowserbaseClient.mjs +190 -0
- package/dist/BrowserbaseClient.mjs.map +1 -0
- package/dist/BrowserbaseError-D1EOrLNc.d.mts +79 -0
- package/dist/BrowserbaseError.d.mts +2 -0
- package/dist/BrowserbaseError.mjs +101 -0
- package/dist/BrowserbaseError.mjs.map +1 -0
- package/dist/ContextLease.d.mts +42 -0
- package/dist/ContextLease.mjs +80 -0
- package/dist/ContextLease.mjs.map +1 -0
- package/dist/index.d.mts +5 -27
- package/dist/index.mjs +5 -26
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/dist/testing/index.d.mts +136 -0
- package/dist/testing/index.mjs +440 -0
- package/dist/testing/index.mjs.map +1 -0
- package/package.json +51 -121
- package/src/Browserbase.ts +729 -0
- package/src/BrowserbaseClient.ts +559 -0
- package/src/BrowserbaseError.ts +108 -0
- package/src/ContextLease.ts +118 -0
- package/src/index.ts +7 -0
- package/src/internal/captureConnection.ts +214 -0
- package/src/testing/BrowserbaseContract.ts +170 -0
- package/src/testing/TestBrowserbase.ts +509 -0
- package/src/testing/index.ts +7 -0
- package/dist/Account.d.mts +0 -32
- package/dist/Account.mjs +0 -36
- package/dist/Account.mjs.map +0 -1
- package/dist/Acquisition-BOABu_qJ.mjs +0 -244
- package/dist/Acquisition-BOABu_qJ.mjs.map +0 -1
- package/dist/Agents-CPaTOH_g.d.mts +0 -156
- package/dist/Agents.d.mts +0 -3
- package/dist/Agents.mjs +0 -292
- package/dist/Agents.mjs.map +0 -1
- package/dist/Allocation.d.mts +0 -97
- package/dist/Allocation.mjs +0 -27
- package/dist/Allocation.mjs.map +0 -1
- package/dist/Authorization-Ll5f_XNM.mjs +0 -12
- package/dist/Authorization-Ll5f_XNM.mjs.map +0 -1
- package/dist/Browser-ByrWJ0ss.mjs +0 -274
- package/dist/Browser-ByrWJ0ss.mjs.map +0 -1
- package/dist/Browser.d.mts +0 -83
- package/dist/Browser.mjs +0 -3
- package/dist/BrowserBinding.d.mts +0 -13
- package/dist/BrowserBinding.mjs +0 -31
- package/dist/BrowserBinding.mjs.map +0 -1
- package/dist/Certificates-D4NQmyeA.d.mts +0 -34
- package/dist/Certificates.d.mts +0 -3
- package/dist/Certificates.mjs +0 -85
- package/dist/Certificates.mjs.map +0 -1
- package/dist/Cleanup-DMTDBir8.d.mts +0 -23
- package/dist/Cleanup.d.mts +0 -3
- package/dist/Cleanup.mjs +0 -43
- package/dist/Cleanup.mjs.map +0 -1
- package/dist/Client-BEC4Q8ZS.mjs +0 -564
- package/dist/Client-BEC4Q8ZS.mjs.map +0 -1
- package/dist/Client-B_f6bBLT.d.mts +0 -52
- package/dist/Client.d.mts +0 -3
- package/dist/Client.mjs +0 -2
- package/dist/Connection-sxyxtwlk.mjs +0 -56
- package/dist/Connection-sxyxtwlk.mjs.map +0 -1
- package/dist/ContextCoordination.d.mts +0 -29
- package/dist/ContextCoordination.mjs +0 -94
- package/dist/ContextCoordination.mjs.map +0 -1
- package/dist/ContextWriter-CiEma24z.mjs +0 -204
- package/dist/ContextWriter-CiEma24z.mjs.map +0 -1
- package/dist/Contexts-DqdL7cyX.d.mts +0 -31
- package/dist/Contexts.d.mts +0 -3
- package/dist/Contexts.mjs +0 -97
- package/dist/Contexts.mjs.map +0 -1
- package/dist/Deadline-CrViaogr.mjs +0 -13
- package/dist/Deadline-CrViaogr.mjs.map +0 -1
- package/dist/Downloads-CX52GOFJ.d.mts +0 -43
- package/dist/Downloads.d.mts +0 -3
- package/dist/Downloads.mjs +0 -137
- package/dist/Downloads.mjs.map +0 -1
- package/dist/Errors.d.mts +0 -100
- package/dist/Errors.mjs +0 -194
- package/dist/Errors.mjs.map +0 -1
- package/dist/Extensions-BTUn9zhK.mjs +0 -231
- package/dist/Extensions-BTUn9zhK.mjs.map +0 -1
- package/dist/Extensions-DBXuVM1f.d.mts +0 -43
- package/dist/Extensions.d.mts +0 -3
- package/dist/Extensions.mjs +0 -2
- package/dist/Functions-CsKMMUuZ.d.mts +0 -226
- package/dist/Functions.d.mts +0 -3
- package/dist/Functions.mjs +0 -318
- package/dist/Functions.mjs.map +0 -1
- package/dist/Issued-DejqoMYL.mjs +0 -15
- package/dist/Issued-DejqoMYL.mjs.map +0 -1
- package/dist/Launch-cAdE2rrZ.mjs +0 -115
- package/dist/Launch-cAdE2rrZ.mjs.map +0 -1
- package/dist/Launch.d.mts +0 -127
- package/dist/Launch.mjs +0 -97
- package/dist/Launch.mjs.map +0 -1
- package/dist/PageFetch-CjAM_0DS.d.mts +0 -40
- package/dist/PageFetch.d.mts +0 -3
- package/dist/PageFetch.mjs +0 -59
- package/dist/PageFetch.mjs.map +0 -1
- package/dist/Projects-tm5bTDH0.d.mts +0 -38
- package/dist/Projects.d.mts +0 -3
- package/dist/Projects.mjs +0 -65
- package/dist/Projects.mjs.map +0 -1
- package/dist/Recordings-B9k5aODB.d.mts +0 -27
- package/dist/Recordings.d.mts +0 -3
- package/dist/Recordings.mjs +0 -144
- package/dist/Recordings.mjs.map +0 -1
- package/dist/References-DOH3Wu44.d.mts +0 -34
- package/dist/References.d.mts +0 -3
- package/dist/References.mjs +0 -33
- package/dist/References.mjs.map +0 -1
- package/dist/Replays-mV93WGtx.d.mts +0 -27
- package/dist/Replays.d.mts +0 -3
- package/dist/Replays.mjs +0 -107
- package/dist/Replays.mjs.map +0 -1
- package/dist/Resource-J7Kkk5mu.mjs +0 -54
- package/dist/Resource-J7Kkk5mu.mjs.map +0 -1
- package/dist/Search-B64e7g-h.d.mts +0 -37
- package/dist/Search.d.mts +0 -3
- package/dist/Search.mjs +0 -46
- package/dist/Search.mjs.map +0 -1
- package/dist/SessionData-5xYZDshJ.d.mts +0 -38
- package/dist/SessionData.d.mts +0 -3
- package/dist/SessionData.mjs +0 -48
- package/dist/SessionData.mjs.map +0 -1
- package/dist/Sessions-D2H5m6rF.mjs +0 -260
- package/dist/Sessions-D2H5m6rF.mjs.map +0 -1
- package/dist/Sessions-oJEUmZth.d.mts +0 -69
- package/dist/Sessions.d.mts +0 -4
- package/dist/Sessions.mjs +0 -3
- package/dist/Testing.d.mts +0 -119
- package/dist/Testing.mjs +0 -206
- package/dist/Testing.mjs.map +0 -1
- package/dist/TransferPolicy-DQ06i7SZ.mjs +0 -25
- package/dist/TransferPolicy-DQ06i7SZ.mjs.map +0 -1
- package/dist/Transfers-GVqzrDWl.d.mts +0 -89
- package/dist/Transfers.d.mts +0 -3
- package/dist/Transfers.mjs +0 -85
- package/dist/Transfers.mjs.map +0 -1
- package/dist/Uploads-C2_bACgv.d.mts +0 -28
- package/dist/Uploads.d.mts +0 -3
- package/dist/Uploads.mjs +0 -105
- package/dist/Uploads.mjs.map +0 -1
- package/dist/Webhooks-BoeRAxzG.d.mts +0 -59
- package/dist/Webhooks.d.mts +0 -3
- package/dist/Webhooks.mjs +0 -134
- package/dist/Webhooks.mjs.map +0 -1
- package/dist/WriterFacts-BBJRDZYY.d.mts +0 -54
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `Browser` running on Browserbase.
|
|
3
|
+
*
|
|
4
|
+
* `open` creates a session and connects to it over CDP. When its scope closes it disconnects,
|
|
5
|
+
* releases the session and confirms that Browserbase reports it ended, so billing stops then
|
|
6
|
+
* rather than at the session's timeout. `release` does the same sooner and says how it went:
|
|
7
|
+
* `Settled`, or `Unconfirmed` when Browserbase still reported the session running, or could not
|
|
8
|
+
* be asked, at the deadline. Each create carries a nonce of its own in its user metadata, so a
|
|
9
|
+
* session made by a create whose answer was lost is found and ended too, rather than billing
|
|
10
|
+
* unseen until its timeout.
|
|
11
|
+
*
|
|
12
|
+
* A session that persists to a stored context saves to it when the session ends, so two at once
|
|
13
|
+
* can lose one's changes. `open` holds the context through the `ContextLease` the application
|
|
14
|
+
* provides, from before it creates such a session until Browserbase reports the session ended and
|
|
15
|
+
* `contextSettle` has passed, since the save lands later and nothing acknowledges it. A writer that
|
|
16
|
+
* leaves a session that saves to the context possibly running, its release `Unconfirmed`, its
|
|
17
|
+
* create's answer lost or its session kept, leaves the context unsettled: the next writer first ends
|
|
18
|
+
* the context's sessions, which `open` labels in their user metadata, and fails with `ContextHeld`
|
|
19
|
+
* while they can't be confirmed ended. `reconcile` ends them without opening, and `verifyContext`
|
|
20
|
+
* reads a context back under the same hold, from a session that saves nothing.
|
|
21
|
+
*
|
|
22
|
+
* `attach` connects to a session that already runs, as from another process: its pages keep their
|
|
23
|
+
* ids, so a page stored before is found again. `supervise` keeps a session open across losses and
|
|
24
|
+
* session ends, as `Supervisor` generations, and with `keep`, past its own scope, for the next
|
|
25
|
+
* `supervise` to adopt.
|
|
26
|
+
*
|
|
27
|
+
* @since 0.3.0
|
|
28
|
+
*/
|
|
29
|
+
import {
|
|
30
|
+
DateTime,
|
|
31
|
+
Duration,
|
|
32
|
+
Effect,
|
|
33
|
+
Exit,
|
|
34
|
+
Layer,
|
|
35
|
+
Option,
|
|
36
|
+
Random,
|
|
37
|
+
Redacted,
|
|
38
|
+
Schedule,
|
|
39
|
+
Schema,
|
|
40
|
+
Scope,
|
|
41
|
+
} from "effect";
|
|
42
|
+
import * as Browser from "effect-browser/Browser";
|
|
43
|
+
import { BrowserError, Closed } from "effect-browser/BrowserError";
|
|
44
|
+
import * as Cdp from "effect-browser/Cdp";
|
|
45
|
+
import * as Supervisor from "effect-browser/Supervisor";
|
|
46
|
+
|
|
47
|
+
import {
|
|
48
|
+
BrowserbaseClient,
|
|
49
|
+
type Service,
|
|
50
|
+
type Session,
|
|
51
|
+
type SessionOptions,
|
|
52
|
+
} from "./BrowserbaseClient.ts";
|
|
53
|
+
import { BrowserbaseError, Decode, InvalidRequest, isTransient } from "./BrowserbaseError.ts";
|
|
54
|
+
import { ContextHeld, ContextLease } from "./ContextLease.ts";
|
|
55
|
+
import * as CaptureConnection from "./internal/captureConnection.ts";
|
|
56
|
+
|
|
57
|
+
export interface Options extends Browser.Options {
|
|
58
|
+
readonly session?: SessionOptions | undefined;
|
|
59
|
+
/** Bound on connecting to the session. Defaults to 30 seconds. */
|
|
60
|
+
readonly connectTimeoutMillis?: number | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* How long a persisting session's context stays held after the session ends, for its save to
|
|
63
|
+
* land. Defaults to 10 seconds, after which hosted checks have read a save back.
|
|
64
|
+
*/
|
|
65
|
+
readonly contextSettle?: Duration.Input | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Run the pages' screencasts on a second connection to the session, which carries nothing else,
|
|
68
|
+
* so that a frame never waits behind a large message, such as an upload or a read's answer, on
|
|
69
|
+
* the connection that drives the page. Defaults to true.
|
|
70
|
+
*/
|
|
71
|
+
readonly captureConnection?: boolean | undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A browser on a Browserbase session. */
|
|
75
|
+
export interface Hosted {
|
|
76
|
+
readonly browser: Browser.Service;
|
|
77
|
+
readonly session: Session;
|
|
78
|
+
/**
|
|
79
|
+
* Release the session and confirm it ended, once: `open`'s scope runs it as it closes, while
|
|
80
|
+
* `attach`'s only disconnects. It disconnects `browser` first, which is lost as `released`, so a
|
|
81
|
+
* call in flight or a capture's reader is told so. Asked again, it reports the same outcome.
|
|
82
|
+
*/
|
|
83
|
+
readonly release: Effect.Effect<Supervisor.Released>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The user metadata key naming the stored context a persisting session writes to. */
|
|
87
|
+
const contextLabel = "persistsContext";
|
|
88
|
+
|
|
89
|
+
/** The user metadata key holding each create's own nonce, which finds a session made unseen. */
|
|
90
|
+
const createLabel = "createNonce";
|
|
91
|
+
|
|
92
|
+
/** The user metadata key naming what a supervisor keeps its sessions under, past its scope. */
|
|
93
|
+
const keepLabel = "keptAs";
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* How long a create whose answer was lost has its session looked for: Browserbase can list a new
|
|
97
|
+
* session late, or still be making it when the client stops waiting.
|
|
98
|
+
*/
|
|
99
|
+
const orphanSearch = Duration.seconds(30);
|
|
100
|
+
|
|
101
|
+
/** How long a release waits for Browserbase to report the session ended. */
|
|
102
|
+
const releaseDeadline = Duration.minutes(1);
|
|
103
|
+
|
|
104
|
+
const ended = (session: Session) => session.status !== "PENDING" && session.status !== "RUNNING";
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* End the session and confirm Browserbase reports it ended. The release request and the status
|
|
108
|
+
* reads are each tried again a second apart until the deadline; a session still running then, or
|
|
109
|
+
* one Browserbase could not be asked about, is `Unconfirmed`. Once it has ended, wait `settle`
|
|
110
|
+
* longer, for its context's save to land. It runs to the end once started.
|
|
111
|
+
*/
|
|
112
|
+
const confirm = (client: Service, id: string, settle: Duration.Duration | undefined) =>
|
|
113
|
+
Effect.gen(function* () {
|
|
114
|
+
// What Browserbase last said, for an end it never confirmed.
|
|
115
|
+
let last = "Browserbase did not answer";
|
|
116
|
+
|
|
117
|
+
const heard = (error: BrowserbaseError) => Effect.sync(() => (last = error.message));
|
|
118
|
+
|
|
119
|
+
const read = client.getSession(id).pipe(
|
|
120
|
+
Effect.tapError(heard),
|
|
121
|
+
Effect.tap((session) => Effect.sync(() => (last = `the session was ${session.status}`))),
|
|
122
|
+
Effect.retry(Schedule.spaced("1 second")),
|
|
123
|
+
Effect.repeat({ until: ended, schedule: Schedule.spaced("1 second") }),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// A release that keeps failing leaves the status reads to tell whether the session ended.
|
|
127
|
+
const confirmed = yield* client.releaseSession(id).pipe(
|
|
128
|
+
Effect.tapError(heard),
|
|
129
|
+
Effect.retry({ while: isTransient, schedule: Schedule.spaced("1 second") }),
|
|
130
|
+
Effect.exit,
|
|
131
|
+
Effect.andThen(read),
|
|
132
|
+
Effect.as(true),
|
|
133
|
+
Effect.timeoutOrElse({ duration: releaseDeadline, orElse: () => Effect.succeed(false) }),
|
|
134
|
+
// The reads are retried until the deadline, so a failure here is the same unconfirmed end.
|
|
135
|
+
Effect.orElseSucceed(() => false),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
if (!confirmed)
|
|
139
|
+
return new Supervisor.Unconfirmed({
|
|
140
|
+
detail: `${last} ${Duration.format(releaseDeadline)} after its release`,
|
|
141
|
+
});
|
|
142
|
+
if (settle !== undefined) yield* Effect.sleep(settle);
|
|
143
|
+
|
|
144
|
+
return new Supervisor.Settled();
|
|
145
|
+
}).pipe(
|
|
146
|
+
Effect.tap((released) => Effect.annotateCurrentSpan({ released: released._tag })),
|
|
147
|
+
Effect.withSpan(
|
|
148
|
+
"Browserbase.release",
|
|
149
|
+
{ attributes: { session: id, settle: settle !== undefined } },
|
|
150
|
+
{ captureStackTrace: false },
|
|
151
|
+
),
|
|
152
|
+
Effect.uninterruptible,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
/** A search for the sessions with `value` under the user metadata key `label`. */
|
|
156
|
+
const labelled = (label: string, value: string) => `user_metadata['${label}']:'${value}'`;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The sessions a search finds that have not ended. Pending ones are listed first, since one listed
|
|
160
|
+
* as pending may be running by the second query.
|
|
161
|
+
*/
|
|
162
|
+
const unended = (client: Service, query: string) =>
|
|
163
|
+
Effect.forEach(["PENDING", "RUNNING"], (status) => client.listSessions({ status, query })).pipe(
|
|
164
|
+
Effect.map((found) => [
|
|
165
|
+
...new Map(found.flat().map((session) => [session.id, session])).values(),
|
|
166
|
+
]),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* End the sessions and confirm they ended; then wait `settle`, if given, for the last save to land.
|
|
171
|
+
* One still running at its release's deadline makes the outcome `Unconfirmed`.
|
|
172
|
+
*/
|
|
173
|
+
const endAll = (
|
|
174
|
+
client: Service,
|
|
175
|
+
sessions: ReadonlyArray<string>,
|
|
176
|
+
settle: Duration.Duration | undefined,
|
|
177
|
+
) =>
|
|
178
|
+
Effect.forEach(sessions, (session) => confirm(client, session, undefined), {
|
|
179
|
+
concurrency: 4,
|
|
180
|
+
}).pipe(
|
|
181
|
+
Effect.flatMap((outcomes) => {
|
|
182
|
+
const unconfirmed = outcomes.find((outcome) => outcome._tag === "Unconfirmed");
|
|
183
|
+
|
|
184
|
+
return unconfirmed === undefined
|
|
185
|
+
? Effect.sleep(settle ?? Duration.zero).pipe(
|
|
186
|
+
Effect.as<Supervisor.Released>(new Supervisor.Settled()),
|
|
187
|
+
)
|
|
188
|
+
: Effect.succeed<Supervisor.Released>(unconfirmed);
|
|
189
|
+
}),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* End the session a create may have made though its answer was lost, found in any state by the
|
|
194
|
+
* create's own label, and then wait `settle`, if given, for its save. Browserbase can list a new
|
|
195
|
+
* session late, so it is looked for again, more and more seldom, until it is found or
|
|
196
|
+
* `orphanSearch` has passed. A search Browserbase never answered is `Unconfirmed`, as is a session
|
|
197
|
+
* still running at its release's deadline.
|
|
198
|
+
*/
|
|
199
|
+
const endOrphan = (client: Service, nonce: string, settle: Duration.Duration | undefined) =>
|
|
200
|
+
Effect.gen(function* () {
|
|
201
|
+
let answered = false;
|
|
202
|
+
|
|
203
|
+
const found = yield* client.listSessions({ query: labelled(createLabel, nonce) }).pipe(
|
|
204
|
+
Effect.tap(() =>
|
|
205
|
+
Effect.sync(() => {
|
|
206
|
+
answered = true;
|
|
207
|
+
}),
|
|
208
|
+
),
|
|
209
|
+
Effect.retry(Schedule.exponential("1 second")),
|
|
210
|
+
Effect.repeat({
|
|
211
|
+
schedule: Schedule.exponential("1 second"),
|
|
212
|
+
until: (sessions) => sessions.length > 0,
|
|
213
|
+
}),
|
|
214
|
+
Effect.timeoutOption(orphanSearch),
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
if (Option.isNone(found) && !answered)
|
|
218
|
+
return new Supervisor.Unconfirmed({
|
|
219
|
+
detail: `Browserbase did not answer a search for it within ${Duration.format(orphanSearch)}`,
|
|
220
|
+
});
|
|
221
|
+
const running = Option.getOrElse(found, () => []).filter((session) => !ended(session));
|
|
222
|
+
|
|
223
|
+
return yield* endAll(
|
|
224
|
+
client,
|
|
225
|
+
running.map((session) => session.id),
|
|
226
|
+
settle,
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Hold the stored context through the lease, as its only writer, until the scope closes or the
|
|
232
|
+
* session's end is known; the hold goes once, saying how this writer leaves the context.
|
|
233
|
+
*/
|
|
234
|
+
const holdContext = Effect.fnUntraced(function* (
|
|
235
|
+
client: Service,
|
|
236
|
+
lease: ContextLease["Service"],
|
|
237
|
+
id: string,
|
|
238
|
+
settle: Duration.Duration,
|
|
239
|
+
) {
|
|
240
|
+
const scope = yield* Scope.fork(yield* Effect.scope);
|
|
241
|
+
|
|
242
|
+
// A second writer waits here for the first one's whole session and the save after it.
|
|
243
|
+
const hold = yield* lease
|
|
244
|
+
.hold(id)
|
|
245
|
+
.pipe(
|
|
246
|
+
Scope.provide(scope),
|
|
247
|
+
Effect.withSpan("Browserbase.holdContext", {}, { captureStackTrace: false }),
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
// Whether a session that saves to the context may run, as this writer would leave it now.
|
|
251
|
+
let unsettled = hold.unsettled;
|
|
252
|
+
|
|
253
|
+
const set = (now: boolean) =>
|
|
254
|
+
Effect.sync(() => {
|
|
255
|
+
unsettled = now;
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
yield* Scope.addFinalizer(
|
|
259
|
+
scope,
|
|
260
|
+
Effect.suspend(() => hold.leave(unsettled)),
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
const letGo = (now: boolean) => set(now).pipe(Effect.andThen(Scope.close(scope, Exit.void)));
|
|
264
|
+
|
|
265
|
+
/** End the context's sessions but `except`, found by the label `open` gives them. */
|
|
266
|
+
const end = (except: ReadonlyArray<string>) =>
|
|
267
|
+
unended(client, labelled(contextLabel, id)).pipe(
|
|
268
|
+
Effect.flatMap((found) =>
|
|
269
|
+
endAll(
|
|
270
|
+
client,
|
|
271
|
+
found.map((session) => session.id).filter((session) => !except.includes(session)),
|
|
272
|
+
settle,
|
|
273
|
+
),
|
|
274
|
+
),
|
|
275
|
+
Effect.onExit((exit) => set(!(Exit.isSuccess(exit) && exit.value._tag === "Settled"))),
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const refuse = (outcome: Supervisor.Released) =>
|
|
279
|
+
outcome._tag === "Settled"
|
|
280
|
+
? Effect.void
|
|
281
|
+
: Effect.fail(new ContextHeld({ context: id, detail: outcome.detail }));
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
end,
|
|
285
|
+
/**
|
|
286
|
+
* End the sessions, but `except`, that a writer before may have left saving to the context,
|
|
287
|
+
* failing with `ContextHeld` while they can't be confirmed ended.
|
|
288
|
+
*/
|
|
289
|
+
clear: (except: ReadonlyArray<string>) =>
|
|
290
|
+
Effect.suspend(() => (unsettled ? Effect.flatMap(end(except), refuse) : Effect.void)),
|
|
291
|
+
/** A session that saves to the context runs from now. */
|
|
292
|
+
writing: set(true),
|
|
293
|
+
/** How the writer's session ended: unconfirmed, it may still save to the context. */
|
|
294
|
+
released: (outcome: Supervisor.Released) => letGo(outcome._tag === "Unconfirmed"),
|
|
295
|
+
/** How ending the session a lost create may have made went: unconfirmed, the open fails. */
|
|
296
|
+
lost: (outcome: Supervisor.Released) =>
|
|
297
|
+
letGo(outcome._tag === "Unconfirmed").pipe(Effect.andThen(refuse(outcome))),
|
|
298
|
+
/** The writer's session runs on past its scope, saving to the context when it ends. */
|
|
299
|
+
kept: letGo(true),
|
|
300
|
+
};
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
/** A create that failed so may still have made its session: its answer is lost or unreadable. */
|
|
304
|
+
const mayHaveCreated = (error: BrowserbaseError) =>
|
|
305
|
+
error.reason._tag === "Transport" ||
|
|
306
|
+
error.reason._tag === "Decode" ||
|
|
307
|
+
(error.reason._tag === "Status" && (error.reason.status >= 500 || error.reason.status === 408));
|
|
308
|
+
|
|
309
|
+
/** A create's own nonce: two random draws, which no other create draws in practice. */
|
|
310
|
+
const nonce = Effect.map(Effect.all([Random.nextInt, Random.nextInt]), (draws) =>
|
|
311
|
+
draws.map((draw) => Math.abs(draw).toString(36)).join("-"),
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* The create request, labelled with its own nonce, a persisting session with its context too, so
|
|
316
|
+
* either can be found, and a kept one with what it is kept under, kept alive past disconnecting.
|
|
317
|
+
*/
|
|
318
|
+
const withLabels = (
|
|
319
|
+
session: SessionOptions | undefined,
|
|
320
|
+
own: string,
|
|
321
|
+
context: string | undefined,
|
|
322
|
+
keep: string | undefined,
|
|
323
|
+
) => ({
|
|
324
|
+
...session,
|
|
325
|
+
...(keep === undefined ? {} : { keepAlive: true }),
|
|
326
|
+
userMetadata: {
|
|
327
|
+
...session?.userMetadata,
|
|
328
|
+
[createLabel]: own,
|
|
329
|
+
...(context === undefined ? {} : { [contextLabel]: context }),
|
|
330
|
+
...(keep === undefined ? {} : { [keepLabel]: keep }),
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* A release by the browser's owner: it closes the browser first, so that whatever fails then, a
|
|
336
|
+
* capture's reader or a call in flight, says the session was released, and not that the
|
|
337
|
+
* connection Browserbase cuts as the session ends was lost.
|
|
338
|
+
*/
|
|
339
|
+
const releasing = (browser: Scope.Closeable, end: Effect.Effect<Supervisor.Released>) =>
|
|
340
|
+
Scope.close(browser, Exit.void).pipe(Effect.andThen(end));
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* A browser over the session's DevTools address, its pages' screencasts on a capture connection of
|
|
344
|
+
* their own. A DevTools server's HTTP address, as a local Chromium gives in tests, keeps them on
|
|
345
|
+
* each page's own session: there the connection is loopback, and fast. A session that has ended
|
|
346
|
+
* fails `Closed` by the session, with no connect.
|
|
347
|
+
*/
|
|
348
|
+
const connect = Effect.fnUntraced(function* (
|
|
349
|
+
operation: string,
|
|
350
|
+
session: Session,
|
|
351
|
+
options: Options,
|
|
352
|
+
) {
|
|
353
|
+
const endpoint = session.connectUrl;
|
|
354
|
+
|
|
355
|
+
if (ended(session))
|
|
356
|
+
return yield* new BrowserError({
|
|
357
|
+
operation,
|
|
358
|
+
reason: new Closed({ cause: "session" }),
|
|
359
|
+
dispatched: false,
|
|
360
|
+
});
|
|
361
|
+
if (endpoint === undefined)
|
|
362
|
+
return yield* new BrowserbaseError({
|
|
363
|
+
operation,
|
|
364
|
+
reason: new Decode({ detail: `session ${session.id} has no connectUrl` }),
|
|
365
|
+
});
|
|
366
|
+
const apart = options.captureConnection !== false && /^wss?:/i.test(Redacted.value(endpoint));
|
|
367
|
+
|
|
368
|
+
return yield* Cdp.open({
|
|
369
|
+
...options,
|
|
370
|
+
endpoint,
|
|
371
|
+
id: session.id,
|
|
372
|
+
provider: "browserbase",
|
|
373
|
+
expiresAt: Option.getOrUndefined(DateTime.make(session.expiresAt)),
|
|
374
|
+
capture: apart ? yield* CaptureConnection.make(endpoint) : undefined,
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
/** How a supervisor keeps its sessions past its scope: under `name`, adopting one first if `adopt`. */
|
|
379
|
+
interface Keeping {
|
|
380
|
+
readonly name: string;
|
|
381
|
+
readonly adopt: boolean;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const keepName = /^[\w.:-]{1,64}$/;
|
|
385
|
+
|
|
386
|
+
const newestFirst = (sessions: ReadonlyArray<Session>) => {
|
|
387
|
+
const created = (session: Session) =>
|
|
388
|
+
Option.getOrElse(Option.map(DateTime.make(session.createdAt), DateTime.toEpochMillis), () => 0);
|
|
389
|
+
|
|
390
|
+
return sessions.toSorted((a, b) => created(b) - created(a));
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* The newest session kept under `name` that saves where this open would, to adopt. Every other
|
|
395
|
+
* session kept under it is to end, since nothing else adopts it: those that save to this open's
|
|
396
|
+
* context are its writers, for the context's hold to end, as `writers` says, and the rest end here.
|
|
397
|
+
*/
|
|
398
|
+
const adoptable = Effect.fnUntraced(function* (
|
|
399
|
+
client: Service,
|
|
400
|
+
name: string,
|
|
401
|
+
persisting: string | undefined,
|
|
402
|
+
) {
|
|
403
|
+
const [newest, ...older] = newestFirst(yield* unended(client, labelled(keepLabel, name)));
|
|
404
|
+
const fits = newest !== undefined && newest.userMetadata?.[contextLabel] === persisting;
|
|
405
|
+
const others = [...(fits || newest === undefined ? [] : [newest]), ...older];
|
|
406
|
+
|
|
407
|
+
const writes = (session: Session) =>
|
|
408
|
+
persisting !== undefined && session.userMetadata?.[contextLabel] === persisting;
|
|
409
|
+
|
|
410
|
+
const rest = others.filter((session) => !writes(session));
|
|
411
|
+
|
|
412
|
+
if (rest.length > 0) {
|
|
413
|
+
const outcome = yield* endAll(
|
|
414
|
+
client,
|
|
415
|
+
rest.map((session) => session.id),
|
|
416
|
+
undefined,
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
yield* Effect.annotateCurrentSpan({ endedKept: rest.length, endedKeptAs: outcome._tag });
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return { adoptee: fits ? newest : undefined, writers: others.some(writes) };
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Open a browser on a new session, or, when keeping, adopt one a supervisor kept before. A kept
|
|
427
|
+
* session's scope only disconnects, unless it closes on a failure: `release` ends it.
|
|
428
|
+
*/
|
|
429
|
+
const start = Effect.fnUntraced(function* (options: Options, keeping: Keeping | undefined) {
|
|
430
|
+
const client = yield* BrowserbaseClient;
|
|
431
|
+
const lease = yield* ContextLease;
|
|
432
|
+
const context = options.session?.browserSettings?.context;
|
|
433
|
+
const persisting = context?.persist === true ? context.id : undefined;
|
|
434
|
+
const local = yield* Scope.fork(yield* Effect.scope);
|
|
435
|
+
const settle = Duration.fromInputUnsafe(options.contextSettle ?? Duration.seconds(10));
|
|
436
|
+
|
|
437
|
+
return yield* Effect.gen(function* () {
|
|
438
|
+
if (keeping !== undefined && !keepName.test(keeping.name))
|
|
439
|
+
return yield* new BrowserbaseError({
|
|
440
|
+
operation: "supervise",
|
|
441
|
+
reason: new InvalidRequest({
|
|
442
|
+
detail: `keep must be 1 to 64 letters, digits, "_", ".", ":" or "-"`,
|
|
443
|
+
}),
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
const writing =
|
|
447
|
+
persisting === undefined ? undefined : yield* holdContext(client, lease, persisting, settle);
|
|
448
|
+
|
|
449
|
+
const adoption = keeping?.adopt
|
|
450
|
+
? yield* adoptable(client, keeping.name, persisting)
|
|
451
|
+
: undefined;
|
|
452
|
+
|
|
453
|
+
const adopted = adoption?.adoptee;
|
|
454
|
+
|
|
455
|
+
// Another writer kept under the name may still save to the context: clear it as any other.
|
|
456
|
+
if (adoption?.writers === true) yield* writing?.writing ?? Effect.void;
|
|
457
|
+
yield* writing?.clear(adopted === undefined ? [] : [adopted.id]) ?? Effect.void;
|
|
458
|
+
const connected = yield* Scope.fork(local);
|
|
459
|
+
const own = yield* nonce;
|
|
460
|
+
|
|
461
|
+
// Releasing ends the session; leaving, as a kept session's scope does, only disconnects.
|
|
462
|
+
const ending = (session: Session) =>
|
|
463
|
+
Effect.gen(function* () {
|
|
464
|
+
let asked = false;
|
|
465
|
+
|
|
466
|
+
const release = yield* Effect.cached(
|
|
467
|
+
Effect.suspend(() => {
|
|
468
|
+
asked = true;
|
|
469
|
+
|
|
470
|
+
return releasing(connected, confirm(client, session.id, writing && settle)).pipe(
|
|
471
|
+
Effect.tap((outcome) => writing?.released(outcome) ?? Effect.void),
|
|
472
|
+
);
|
|
473
|
+
}),
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
const leave = Effect.suspend(() =>
|
|
477
|
+
asked
|
|
478
|
+
? Effect.void
|
|
479
|
+
: Scope.close(connected, Exit.void).pipe(Effect.andThen(writing?.kept ?? Effect.void)),
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
return { session, release, leave };
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
const made = yield* Effect.acquireRelease(
|
|
486
|
+
Effect.gen(function* () {
|
|
487
|
+
const session =
|
|
488
|
+
adopted === undefined
|
|
489
|
+
? yield* client.createSession(
|
|
490
|
+
withLabels(options.session, own, persisting, keeping?.name),
|
|
491
|
+
)
|
|
492
|
+
: yield* client.getSession(adopted.id);
|
|
493
|
+
|
|
494
|
+
yield* writing?.writing ?? Effect.void;
|
|
495
|
+
|
|
496
|
+
return yield* ending(session);
|
|
497
|
+
}),
|
|
498
|
+
({ release, leave }, exit) =>
|
|
499
|
+
keeping !== undefined && Exit.isSuccess(exit) ? leave : Effect.asVoid(release),
|
|
500
|
+
).pipe(
|
|
501
|
+
// A session nobody can see bills until its timeout, and one that saves to the context would
|
|
502
|
+
// write beside the next writer: end it, found by its create's own label.
|
|
503
|
+
Effect.tapError((error) =>
|
|
504
|
+
adopted === undefined && mayHaveCreated(error)
|
|
505
|
+
? endOrphan(client, own, writing && settle).pipe(
|
|
506
|
+
Effect.flatMap((outcome) => writing?.lost(outcome) ?? Effect.void),
|
|
507
|
+
)
|
|
508
|
+
: Effect.void,
|
|
509
|
+
),
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
const { session, release } = made;
|
|
513
|
+
|
|
514
|
+
yield* Effect.annotateCurrentSpan({
|
|
515
|
+
session: session.id,
|
|
516
|
+
region: session.region,
|
|
517
|
+
adopted: adopted !== undefined,
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
const browser = yield* connect(
|
|
521
|
+
adopted === undefined ? "open" : "attach",
|
|
522
|
+
session,
|
|
523
|
+
options,
|
|
524
|
+
).pipe(Scope.provide(connected));
|
|
525
|
+
|
|
526
|
+
return { browser, session, release } satisfies Hosted;
|
|
527
|
+
}).pipe(
|
|
528
|
+
Scope.provide(local),
|
|
529
|
+
Effect.onError((cause) => Scope.close(local, Exit.failCause(cause))),
|
|
530
|
+
);
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Create a session and open a `Browser` on it, for as long as the scope is open. If opening
|
|
535
|
+
* fails, the session and context are released then, so a retry in the same scope can take them.
|
|
536
|
+
* A persisting open holds its context through the `ContextLease`; a session that may still save
|
|
537
|
+
* to it, as one whose release was left `Unconfirmed` or whose create's answer was lost, is ended
|
|
538
|
+
* first, and the open fails with `ContextHeld` while that can't be confirmed. A lost create's own
|
|
539
|
+
* open looks for its session by the create's nonce, within `orphanSearch`, ends it, then fails with
|
|
540
|
+
* the create's error. `release` disconnects the browser before it asks Browserbase to end the
|
|
541
|
+
* session, so what fails meanwhile says it was released.
|
|
542
|
+
*/
|
|
543
|
+
export const open = Effect.fn("Browserbase.open")(function* (options: Options = {}) {
|
|
544
|
+
return yield* start(options, undefined);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Open a `Browser` on a session that already runs, such as a `keepAlive` session another process
|
|
549
|
+
* opened. This is resume: its pages keep their ids, their CDP target ids, so `browser.page(id)`
|
|
550
|
+
* finds a page stored before. Closing the scope disconnects and leaves the session running;
|
|
551
|
+
* `release` ends it. A session that has ended fails `Closed` by the session. It holds no context.
|
|
552
|
+
*/
|
|
553
|
+
export const attach = Effect.fn("Browserbase.attach")(function* (
|
|
554
|
+
sessionId: string,
|
|
555
|
+
options: Options = {},
|
|
556
|
+
) {
|
|
557
|
+
const client = yield* BrowserbaseClient;
|
|
558
|
+
const session = yield* client.getSession(sessionId);
|
|
559
|
+
|
|
560
|
+
yield* Effect.annotateCurrentSpan({ session: session.id, region: session.region });
|
|
561
|
+
|
|
562
|
+
const connected = yield* Scope.fork(yield* Effect.scope);
|
|
563
|
+
const browser = yield* connect("attach", session, options).pipe(Scope.provide(connected));
|
|
564
|
+
|
|
565
|
+
const release = yield* Effect.cached(
|
|
566
|
+
releasing(connected, confirm(client, session.id, undefined)),
|
|
567
|
+
);
|
|
568
|
+
|
|
569
|
+
return { browser, session, release } satisfies Hosted;
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* End a stored context's sessions, the ones `open` labelled as writing to it, and let the context
|
|
574
|
+
* go once they are confirmed ended and its save has settled. `open` does this itself when a writer
|
|
575
|
+
* before it may still run; `reconcile` does it without opening, as for sessions another process
|
|
576
|
+
* left running. It holds the context through the `ContextLease`, so it waits for a writer. A
|
|
577
|
+
* session still running at the deadline makes it `Unconfirmed`, and leaves the context unsettled,
|
|
578
|
+
* as a failure does.
|
|
579
|
+
*/
|
|
580
|
+
export const reconcile = Effect.fn("Browserbase.reconcile")(function* (
|
|
581
|
+
contextId: string,
|
|
582
|
+
options: { readonly contextSettle?: Duration.Input | undefined } = {},
|
|
583
|
+
) {
|
|
584
|
+
const client = yield* BrowserbaseClient;
|
|
585
|
+
const lease = yield* ContextLease;
|
|
586
|
+
const settle = Duration.fromInputUnsafe(options.contextSettle ?? Duration.seconds(10));
|
|
587
|
+
|
|
588
|
+
// Reading the context first refuses an id that is not one before it reaches the query.
|
|
589
|
+
yield* client.getContext(contextId);
|
|
590
|
+
const hold = yield* holdContext(client, lease, contextId, settle);
|
|
591
|
+
|
|
592
|
+
return yield* hold.end([]);
|
|
593
|
+
}, Effect.scoped);
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Read a stored context back, as a login: a session that loads it and saves nothing runs `check`
|
|
597
|
+
* on its browser, and is released. The context is held through the `ContextLease` meanwhile, so
|
|
598
|
+
* the read never runs beside a writer, which a site could sign out, and it reads what the last
|
|
599
|
+
* writer saved, once that save settled. A session a writer before may have left saving to it is
|
|
600
|
+
* ended first, as for `open`.
|
|
601
|
+
*/
|
|
602
|
+
export const verifyContext = Effect.fn("Browserbase.verifyContext")(function* <A, E, R>(
|
|
603
|
+
contextId: string,
|
|
604
|
+
check: (browser: Browser.Service) => Effect.Effect<A, E, R>,
|
|
605
|
+
options: Options = {},
|
|
606
|
+
) {
|
|
607
|
+
const client = yield* BrowserbaseClient;
|
|
608
|
+
const lease = yield* ContextLease;
|
|
609
|
+
const settle = Duration.fromInputUnsafe(options.contextSettle ?? Duration.seconds(10));
|
|
610
|
+
const reading = yield* holdContext(client, lease, contextId, settle);
|
|
611
|
+
|
|
612
|
+
yield* reading.clear([]);
|
|
613
|
+
|
|
614
|
+
const { browser } = yield* start(
|
|
615
|
+
{
|
|
616
|
+
...options,
|
|
617
|
+
session: {
|
|
618
|
+
...options.session,
|
|
619
|
+
browserSettings: {
|
|
620
|
+
...options.session?.browserSettings,
|
|
621
|
+
context: { id: contextId, persist: false },
|
|
622
|
+
},
|
|
623
|
+
},
|
|
624
|
+
},
|
|
625
|
+
undefined,
|
|
626
|
+
);
|
|
627
|
+
|
|
628
|
+
return yield* check(browser);
|
|
629
|
+
}, Effect.scoped);
|
|
630
|
+
|
|
631
|
+
export interface SuperviseOptions<A = Session, E = never, R = never>
|
|
632
|
+
extends
|
|
633
|
+
Options,
|
|
634
|
+
Pick<
|
|
635
|
+
Supervisor.Options<
|
|
636
|
+
BrowserError | BrowserbaseError | ContextHeld | E,
|
|
637
|
+
BrowserbaseClient | ContextLease | R,
|
|
638
|
+
A
|
|
639
|
+
>,
|
|
640
|
+
"reopen" | "rotateBefore" | "waitTimeout" | "handover"
|
|
641
|
+
> {
|
|
642
|
+
/**
|
|
643
|
+
* Keep the session past the supervisor's scope, under this name: 1 to 64 letters, digits, `_`,
|
|
644
|
+
* `.`, `:` or `-`. Each session is created with `keepAlive` and labelled `keptAs: <keep>` in its
|
|
645
|
+
* user metadata. Closing the scope only disconnects the current one, published as `Kept`, and
|
|
646
|
+
* the first generation of the next `supervise` under the same name adopts it, with its pages,
|
|
647
|
+
* rather than creating a session; other sessions kept under it are ended. `retire` releases. A
|
|
648
|
+
* kept session bills until it is adopted and released, or until its timeout ends it.
|
|
649
|
+
*/
|
|
650
|
+
readonly keep?: string | undefined;
|
|
651
|
+
/** Build the generation's value in its scope. Without a builder, its value is its Session. */
|
|
652
|
+
readonly build?: ((hosted: Hosted) => Effect.Effect<A, E, R>) | undefined;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Browserbase refused the request: no new try mends a refused key, request or id. A context held
|
|
657
|
+
* is not among them, since each try ends its sessions again.
|
|
658
|
+
*/
|
|
659
|
+
const refused = (error: unknown) =>
|
|
660
|
+
Schema.is(BrowserbaseError)(error) && !isTransient(error) && error.reason._tag !== "Decode";
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Keep Browserbase sessions open as `Supervisor` generations, each a new session made from
|
|
664
|
+
* `session`. Sessions that persist to a stored context are exclusive: a rotation releases the
|
|
665
|
+
* current one, and its context's save settles, before the next one opens. An open Browserbase
|
|
666
|
+
* refused, as for a bad key or an invalid request, is `Down` at once. With `keep`, closing the
|
|
667
|
+
* scope leaves the current session running for the next `supervise` under that name to adopt.
|
|
668
|
+
*/
|
|
669
|
+
export function supervise<A, E, R>(
|
|
670
|
+
options: SuperviseOptions<A, E, R> & {
|
|
671
|
+
readonly build: (hosted: Hosted) => Effect.Effect<A, E, R>;
|
|
672
|
+
},
|
|
673
|
+
): Effect.Effect<
|
|
674
|
+
Supervisor.Supervisor<A>,
|
|
675
|
+
never,
|
|
676
|
+
Scope.Scope | BrowserbaseClient | ContextLease | Exclude<R, Scope.Scope>
|
|
677
|
+
>;
|
|
678
|
+
|
|
679
|
+
export function supervise<A = Session, E = never, R = never>(
|
|
680
|
+
options?: SuperviseOptions<A, E, R>,
|
|
681
|
+
): Effect.Effect<
|
|
682
|
+
Supervisor.Supervisor<A | Session>,
|
|
683
|
+
never,
|
|
684
|
+
Scope.Scope | BrowserbaseClient | ContextLease | Exclude<R, Scope.Scope>
|
|
685
|
+
>;
|
|
686
|
+
|
|
687
|
+
export function supervise<A = Session, E = never, R = never>(
|
|
688
|
+
options: SuperviseOptions<A, E, R> = {},
|
|
689
|
+
) {
|
|
690
|
+
return Effect.suspend(() => {
|
|
691
|
+
const { keep } = options;
|
|
692
|
+
// Only the first generation adopts a kept session: a later one would find its own.
|
|
693
|
+
let opened = false;
|
|
694
|
+
|
|
695
|
+
return Supervisor.make({
|
|
696
|
+
open: Effect.gen(function* () {
|
|
697
|
+
const hosted = yield* start(
|
|
698
|
+
options,
|
|
699
|
+
keep === undefined ? undefined : { name: keep, adopt: !opened },
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
opened = true;
|
|
703
|
+
// The generation owns resources acquired by build, including on failure or cancellation.
|
|
704
|
+
const value = options.build === undefined ? hosted.session : yield* options.build(hosted);
|
|
705
|
+
|
|
706
|
+
return { ...hosted, value };
|
|
707
|
+
}).pipe(Effect.withSpan("Browserbase.open", {}, { captureStackTrace: false })),
|
|
708
|
+
exclusive: options.session?.browserSettings?.context?.persist === true,
|
|
709
|
+
keep: keep !== undefined,
|
|
710
|
+
reopen: options.reopen,
|
|
711
|
+
definite: refused,
|
|
712
|
+
rotateBefore: options.rotateBefore,
|
|
713
|
+
waitTimeout: options.waitTimeout,
|
|
714
|
+
handover: options.handover,
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
export const layer = (
|
|
720
|
+
options: Options = {},
|
|
721
|
+
): Layer.Layer<
|
|
722
|
+
Browser.Browser,
|
|
723
|
+
BrowserError | BrowserbaseError | ContextHeld,
|
|
724
|
+
BrowserbaseClient | ContextLease
|
|
725
|
+
> =>
|
|
726
|
+
Layer.effect(
|
|
727
|
+
Browser.Browser,
|
|
728
|
+
Effect.map(open(options), ({ browser }) => browser),
|
|
729
|
+
);
|