@effect-agent/platform-cloudflare 0.1.0-beta.12 → 0.1.0-beta.120
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/Alarm.d.mts +243 -0
- package/dist/Alarm.mjs +882 -0
- package/dist/Alarm.mjs.map +1 -0
- package/dist/BrowserRestCapture.d.mts +34 -0
- package/dist/BrowserRestCapture.mjs +238 -0
- package/dist/BrowserRestCapture.mjs.map +1 -0
- package/dist/BrowserRestCrawl.d.mts +16 -0
- package/dist/BrowserRestCrawl.mjs +244 -0
- package/dist/BrowserRestCrawl.mjs.map +1 -0
- package/dist/CloudflareAiGateway.d.mts +64 -0
- package/dist/CloudflareAiGateway.mjs +70 -0
- package/dist/CloudflareAiGateway.mjs.map +1 -0
- package/dist/CloudflareBindings.d.mts +115 -0
- package/dist/CloudflareBindings.mjs +113 -0
- package/dist/CloudflareBindings.mjs.map +1 -0
- package/dist/CloudflareBrowser-DV6kya1H.mjs +495 -0
- package/dist/CloudflareBrowser-DV6kya1H.mjs.map +1 -0
- package/dist/CloudflareBrowser-Doe1M7R5.d.mts +80 -0
- package/dist/CloudflareBrowser.d.mts +2 -0
- package/dist/CloudflareBrowser.mjs +2 -0
- package/dist/CloudflareCodeMode.d.mts +44 -0
- package/dist/CloudflareCodeMode.mjs +616 -0
- package/dist/CloudflareCodeMode.mjs.map +1 -0
- package/dist/CloudflareConfig-DKXGo8L3.d.mts +106 -0
- package/dist/CloudflareConfig.d.mts +2 -0
- package/dist/CloudflareConfig.mjs +128 -0
- package/dist/CloudflareConfig.mjs.map +1 -0
- package/dist/CloudflareMemory.d.mts +142 -0
- package/dist/CloudflareMemory.mjs +202 -0
- package/dist/CloudflareMemory.mjs.map +1 -0
- package/dist/CloudflareScheduling.d.mts +52 -0
- package/dist/CloudflareScheduling.mjs +396 -0
- package/dist/CloudflareScheduling.mjs.map +1 -0
- package/dist/CloudflareSubscriptions.d.mts +87 -0
- package/dist/CloudflareSubscriptions.mjs +530 -0
- package/dist/CloudflareSubscriptions.mjs.map +1 -0
- package/dist/CloudflareThreadClient.d.mts +1697 -0
- package/dist/CloudflareThreadClient.mjs +382 -0
- package/dist/CloudflareThreadClient.mjs.map +1 -0
- package/dist/InteractiveBrowser-BQo7QKaP.d.mts +228 -0
- package/dist/InteractiveBrowser-CBUFAECo.mjs +1749 -0
- package/dist/InteractiveBrowser-CBUFAECo.mjs.map +1 -0
- package/dist/InteractiveBrowser.d.mts +2 -0
- package/dist/InteractiveBrowser.mjs +2 -0
- package/dist/ProtectedBrowser.d.mts +128 -0
- package/dist/ProtectedBrowser.mjs +1201 -0
- package/dist/ProtectedBrowser.mjs.map +1 -0
- package/dist/ThreadObject-9IVibJM7.d.mts +839 -0
- package/dist/ThreadObject-DaC-IhCm.mjs +849 -0
- package/dist/ThreadObject-DaC-IhCm.mjs.map +1 -0
- package/dist/ThreadObject.d.mts +2 -0
- package/dist/ThreadObject.mjs +3 -0
- package/dist/WakeScheduler.d.mts +23 -0
- package/dist/WakeScheduler.mjs +57 -0
- package/dist/WakeScheduler.mjs.map +1 -0
- package/dist/boundary-BguazVkh.mjs +42 -0
- package/dist/boundary-BguazVkh.mjs.map +1 -0
- package/dist/index.d.mts +13 -1548
- package/dist/index.mjs +13 -1636
- package/dist/prepared-admission-DU0_T-ev.mjs +73 -0
- package/dist/prepared-admission-DU0_T-ev.mjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +1 -56
- package/src/Alarm.ts +1830 -0
- package/src/BrowserRestCapture.ts +477 -0
- package/src/BrowserRestCrawl.ts +540 -0
- package/src/CloudflareAiGateway.ts +170 -0
- package/src/CloudflareBindings.ts +199 -0
- package/src/CloudflareBrowser.ts +15 -0
- package/src/{code-mode-executor.ts → CloudflareCodeMode.ts} +372 -204
- package/src/{config.ts → CloudflareConfig.ts} +19 -12
- package/src/CloudflareMemory.ts +420 -0
- package/src/CloudflareScheduling.ts +747 -0
- package/src/CloudflareSubscriptions.ts +1041 -0
- package/src/CloudflareThreadClient.ts +809 -0
- package/src/InteractiveBrowser.ts +3241 -0
- package/src/ProtectedBrowser.ts +9 -0
- package/src/ThreadObject.ts +1190 -0
- package/src/{wake-scheduler.ts → WakeScheduler.ts} +32 -26
- package/src/index.ts +12 -23
- package/src/internal/boundary.ts +55 -0
- package/src/internal/browser-binding.ts +82 -0
- package/src/internal/browser-failure.ts +92 -0
- package/src/internal/browser-file-selection.ts +126 -0
- package/src/internal/browser-quick-action.ts +857 -0
- package/src/internal/browser-session-lifecycle.ts +193 -0
- package/src/internal/layers.ts +773 -0
- package/src/internal/message-delivery.ts +131 -0
- package/src/internal/prepared-admission.ts +116 -0
- package/src/internal/progress-wait.ts +121 -0
- package/src/internal/transport.ts +44 -0
- package/src/protected-browser/binding.ts +354 -0
- package/src/protected-browser/host.ts +382 -0
- package/src/protected-browser/inspect-frame.ts +140 -0
- package/src/protected-browser/native.ts +485 -0
- package/src/protected-browser/policy.ts +872 -0
- package/dist/index.mjs.map +0 -1
- package/src/alarm.ts +0 -334
- package/src/bindings.ts +0 -145
- package/src/client.ts +0 -646
- package/src/conversation-object.ts +0 -768
- package/src/layers.ts +0 -376
- package/src/transport.ts +0 -38
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import type { ConversationId } from "@effect-agent/core";
|
|
2
|
-
import { WakeScheduler } from "@effect-agent/session";
|
|
3
1
|
import { Effect, Layer, PubSub, Schema, Stream } from "effect";
|
|
2
|
+
import { type ThreadId } from "effect-agent/identifiers";
|
|
3
|
+
import { makeWakeSubscriptionHub, WakeScheduler } from "effect-agent/wake-scheduler";
|
|
4
4
|
|
|
5
|
-
import { DurableAlarmService } from "./
|
|
6
|
-
import {
|
|
5
|
+
import { DurableAlarmService } from "./Alarm.ts";
|
|
6
|
+
import {
|
|
7
|
+
callThreadObject,
|
|
8
|
+
ThreadObjectPlacement,
|
|
9
|
+
ThreadObjectNamespace,
|
|
10
|
+
} from "./CloudflareBindings.ts";
|
|
11
|
+
import { safeCauseMessage } from "./internal/boundary.ts";
|
|
7
12
|
|
|
8
13
|
/**
|
|
9
14
|
* Bounded in-memory wake buffer for same-incarnation `awaitSettlement` subscribers. Wake
|
|
@@ -15,7 +20,7 @@ const WAKE_BUFFER_CAPACITY = 1_024;
|
|
|
15
20
|
|
|
16
21
|
/** A remote wake stub call failed; always swallowed and logged (hints are droppable). */
|
|
17
22
|
class RemoteWakeDropped extends Schema.TaggedError<RemoteWakeDropped>()("RemoteWakeDropped", {
|
|
18
|
-
|
|
23
|
+
threadId: Schema.String,
|
|
19
24
|
message: Schema.String,
|
|
20
25
|
cause: Schema.optionalKey(Schema.Defect()),
|
|
21
26
|
}) {}
|
|
@@ -35,17 +40,20 @@ class RemoteWakeDropped extends Schema.TaggedError<RemoteWakeDropped>()("RemoteW
|
|
|
35
40
|
export const cloudflareWakeSchedulerLayer: Layer.Layer<
|
|
36
41
|
WakeScheduler,
|
|
37
42
|
never,
|
|
38
|
-
DurableAlarmService |
|
|
43
|
+
DurableAlarmService | ThreadObjectPlacement | ThreadObjectNamespace
|
|
39
44
|
> = Layer.effect(WakeScheduler)(
|
|
40
45
|
Effect.gen(function* () {
|
|
41
46
|
const alarm = yield* DurableAlarmService;
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
const hints = yield* PubSub.sliding<
|
|
47
|
+
const placement = yield* ThreadObjectPlacement;
|
|
48
|
+
const namespace = yield* ThreadObjectNamespace;
|
|
49
|
+
const hints = yield* PubSub.sliding<ThreadId>(WAKE_BUFFER_CAPACITY);
|
|
50
|
+
const progress = yield* makeWakeSubscriptionHub;
|
|
51
|
+
|
|
45
52
|
yield* Effect.addFinalizer(() => PubSub.shutdown(hints));
|
|
46
53
|
|
|
47
|
-
const notifyLocal = (
|
|
48
|
-
|
|
54
|
+
const notifyLocal = (threadId: ThreadId) =>
|
|
55
|
+
progress.notify(threadId).pipe(
|
|
56
|
+
Effect.andThen(PubSub.publish(hints, threadId)),
|
|
49
57
|
Effect.andThen(alarm.scheduleNow),
|
|
50
58
|
Effect.catch((error) =>
|
|
51
59
|
// `notify` never fails by contract; a failed alarm write degrades to "hint lost"
|
|
@@ -55,30 +63,28 @@ export const cloudflareWakeSchedulerLayer: Layer.Layer<
|
|
|
55
63
|
Effect.asVoid,
|
|
56
64
|
);
|
|
57
65
|
|
|
58
|
-
const notifyRemote = (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
const notifyRemote = (threadId: ThreadId) =>
|
|
67
|
+
callThreadObject(
|
|
68
|
+
threadId,
|
|
69
|
+
(target) => target.wake(),
|
|
70
|
+
(cause) =>
|
|
62
71
|
RemoteWakeDropped.make({
|
|
63
|
-
|
|
64
|
-
message: cause
|
|
72
|
+
threadId,
|
|
73
|
+
message: safeCauseMessage(cause, "The remote wake failed without a diagnostic"),
|
|
65
74
|
cause,
|
|
66
75
|
}),
|
|
67
|
-
|
|
76
|
+
).pipe(
|
|
77
|
+
Effect.provideService(ThreadObjectNamespace, namespace),
|
|
68
78
|
Effect.catch((error) =>
|
|
69
|
-
Effect.logWarning(
|
|
70
|
-
`CloudflareWakeScheduler: remote wake of ${conversationId} dropped`,
|
|
71
|
-
error,
|
|
72
|
-
),
|
|
79
|
+
Effect.logWarning(`CloudflareWakeScheduler: remote wake of ${threadId} dropped`, error),
|
|
73
80
|
),
|
|
74
81
|
Effect.asVoid,
|
|
75
82
|
);
|
|
76
83
|
|
|
77
84
|
return WakeScheduler.of({
|
|
78
|
-
notify: (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
: notifyRemote(conversationId),
|
|
85
|
+
notify: (threadId) =>
|
|
86
|
+
placement.ownsThread(threadId) ? notifyLocal(threadId) : notifyRemote(threadId),
|
|
87
|
+
subscribe: progress.subscribe,
|
|
82
88
|
wakes: Stream.fromPubSub(hints),
|
|
83
89
|
});
|
|
84
90
|
}),
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* to import the `cloudflare:workers` runtime module.
|
|
14
|
-
*/
|
|
15
|
-
export * from "./bindings.ts";
|
|
16
|
-
export * from "./config.ts";
|
|
17
|
-
export * from "./alarm.ts";
|
|
18
|
-
export * from "./wake-scheduler.ts";
|
|
19
|
-
export * from "./transport.ts";
|
|
20
|
-
export * from "./layers.ts";
|
|
21
|
-
export * from "./conversation-object.ts";
|
|
22
|
-
export * from "./client.ts";
|
|
23
|
-
export * from "./code-mode-executor.ts";
|
|
1
|
+
export * as Alarm from "./Alarm.ts";
|
|
2
|
+
export * as CloudflareBindings from "./CloudflareBindings.ts";
|
|
3
|
+
export * as CloudflareBrowser from "./CloudflareBrowser.ts";
|
|
4
|
+
export * as CloudflareCodeMode from "./CloudflareCodeMode.ts";
|
|
5
|
+
export * as CloudflareConfig from "./CloudflareConfig.ts";
|
|
6
|
+
export * as CloudflareMemory from "./CloudflareMemory.ts";
|
|
7
|
+
export * as CloudflareScheduling from "./CloudflareScheduling.ts";
|
|
8
|
+
export * as CloudflareSubscriptions from "./CloudflareSubscriptions.ts";
|
|
9
|
+
export * as CloudflareThreadClient from "./CloudflareThreadClient.ts";
|
|
10
|
+
export * as ThreadObject from "./ThreadObject.ts";
|
|
11
|
+
export * as WakeScheduler from "./WakeScheduler.ts";
|
|
12
|
+
export * as CloudflareAiGateway from "./CloudflareAiGateway.ts";
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Predicate } from "effect";
|
|
2
|
+
|
|
3
|
+
const MAX_FOREIGN_DIAGNOSTIC_LENGTH = 8_192;
|
|
4
|
+
|
|
5
|
+
const boundForeignDiagnostic = (message: string): string =>
|
|
6
|
+
message.slice(0, MAX_FOREIGN_DIAGNOSTIC_LENGTH);
|
|
7
|
+
|
|
8
|
+
/** Render a foreign failure without trusting accessors or coercion hooks on the value. */
|
|
9
|
+
export const safeCauseMessage = (cause: unknown, fallback: string): string => {
|
|
10
|
+
try {
|
|
11
|
+
const message = cause instanceof Error ? cause.message : cause;
|
|
12
|
+
|
|
13
|
+
return boundForeignDiagnostic(typeof message === "string" ? message : String(message));
|
|
14
|
+
} catch {
|
|
15
|
+
return boundForeignDiagnostic(fallback);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Include an Error name when worker-failure classification needs it. */
|
|
20
|
+
export const safeCauseDiagnostic = (cause: unknown, fallback: string): string => {
|
|
21
|
+
try {
|
|
22
|
+
return cause instanceof Error
|
|
23
|
+
? boundForeignDiagnostic(`${cause.name}: ${cause.message}`)
|
|
24
|
+
: safeCauseMessage(cause, fallback);
|
|
25
|
+
} catch {
|
|
26
|
+
return boundForeignDiagnostic(fallback);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export interface CloudflareFailureSignals {
|
|
31
|
+
readonly retryable?: boolean | undefined;
|
|
32
|
+
readonly overloaded?: boolean | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Read Cloudflare RPC classifications without letting a hostile proxy defect the client. */
|
|
36
|
+
export const cloudflareFailureSignals = (cause: unknown): CloudflareFailureSignals => {
|
|
37
|
+
if (!Predicate.isObjectKeyword(cause)) return {};
|
|
38
|
+
try {
|
|
39
|
+
const retryableValue = Reflect.get(cause, "retryable");
|
|
40
|
+
const overloadedValue = Reflect.get(cause, "overloaded");
|
|
41
|
+
const resetValue = Reflect.get(cause, "durableObjectReset");
|
|
42
|
+
|
|
43
|
+
const retryable =
|
|
44
|
+
typeof retryableValue === "boolean" ? retryableValue : resetValue === true ? true : undefined;
|
|
45
|
+
|
|
46
|
+
const overloaded = typeof overloadedValue === "boolean" ? overloadedValue : undefined;
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
...(retryable === undefined ? {} : { retryable }),
|
|
50
|
+
...(overloaded === undefined ? {} : { overloaded }),
|
|
51
|
+
};
|
|
52
|
+
} catch {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import puppeteer, { type Browser } from "@cloudflare/puppeteer";
|
|
3
|
+
import { Context, Layer, Schema } from "effect";
|
|
4
|
+
|
|
5
|
+
import { browserFailure, BrowserRunFailure } from "./browser-failure.ts";
|
|
6
|
+
|
|
7
|
+
const Acquired = Schema.Struct({ sessionId: Schema.String.check(Schema.isUUID()) });
|
|
8
|
+
|
|
9
|
+
/** Native transport port shared by both scoped adapters; the Layer owns the foreign binding. */
|
|
10
|
+
export class BrowserRunBinding extends Context.Service<
|
|
11
|
+
BrowserRunBinding,
|
|
12
|
+
{
|
|
13
|
+
readonly acquire: (
|
|
14
|
+
keepAliveMillis: number,
|
|
15
|
+
operation: "protected.acquire" | "interactive.acquire",
|
|
16
|
+
) => Promise<string>;
|
|
17
|
+
readonly connect: (sessionId: string, operation: string) => Promise<Browser>;
|
|
18
|
+
}
|
|
19
|
+
>()("@effect-agent/platform-cloudflare/internal/BrowserRunBinding") {
|
|
20
|
+
static layer(browser: Pick<BrowserRun, "fetch">) {
|
|
21
|
+
return Layer.succeed(this)({
|
|
22
|
+
// One allocation path, with recording explicitly disabled for private sessions.
|
|
23
|
+
acquire: async (keepAliveMillis, operation) => {
|
|
24
|
+
const response = await browser.fetch(
|
|
25
|
+
`https://browser-rendering.cloudflare.com/v1/devtools/browser?keep_alive=${keepAliveMillis}&recording=false`,
|
|
26
|
+
{ method: "POST" },
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
if (response.status !== 200) {
|
|
30
|
+
const failure = new BrowserRunFailure({
|
|
31
|
+
operation,
|
|
32
|
+
reason: "provider",
|
|
33
|
+
status: response.status,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
await response.body?.cancel();
|
|
38
|
+
} catch {
|
|
39
|
+
// Body cancellation cannot replace the acquisition refusal.
|
|
40
|
+
}
|
|
41
|
+
throw failure;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return Schema.decodeUnknownSync(Acquired)(await response.json()).sessionId;
|
|
45
|
+
},
|
|
46
|
+
// The SDK embeds bodies and session IDs in errors; retain status before that loss.
|
|
47
|
+
connect: async (sessionId, operation) => {
|
|
48
|
+
let failure: BrowserRunFailure | undefined;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
return await puppeteer.connect(
|
|
52
|
+
{
|
|
53
|
+
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
54
|
+
try {
|
|
55
|
+
const response = await browser.fetch(input, init);
|
|
56
|
+
|
|
57
|
+
if (response.status !== 101) {
|
|
58
|
+
failure = new BrowserRunFailure({
|
|
59
|
+
operation,
|
|
60
|
+
reason: "provider",
|
|
61
|
+
status: response.status,
|
|
62
|
+
});
|
|
63
|
+
await response.body?.cancel();
|
|
64
|
+
throw failure;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return response;
|
|
68
|
+
} catch (cause) {
|
|
69
|
+
failure ??= browserFailure(operation, cause);
|
|
70
|
+
throw failure;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
sessionId,
|
|
75
|
+
);
|
|
76
|
+
} catch (cause) {
|
|
77
|
+
throw failure ?? browserFailure(operation, cause);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Effect } from "effect";
|
|
2
|
+
import { Cause, Data, ErrorReporter, Predicate, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
/** Source-authored stages and classifications only; never retain SDK text or session capabilities. */
|
|
5
|
+
export class BrowserRunFailure extends Data.TaggedError("BrowserRunFailure")<{
|
|
6
|
+
readonly operation: string;
|
|
7
|
+
readonly reason:
|
|
8
|
+
| "provider"
|
|
9
|
+
| "timeout"
|
|
10
|
+
| "malformed"
|
|
11
|
+
| "configuration"
|
|
12
|
+
| "authorization"
|
|
13
|
+
| "rate-limited"
|
|
14
|
+
| "pending";
|
|
15
|
+
readonly status?: number;
|
|
16
|
+
readonly cause?: BrowserRunFailure | { readonly name: string };
|
|
17
|
+
}> {
|
|
18
|
+
readonly [ErrorReporter.severity] = "Error" as const;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A public projection of an already reported private failure must not produce another issue. */
|
|
22
|
+
export const reportedBrowserError = <A extends object>(error: A): A =>
|
|
23
|
+
Object.assign(error, { [ErrorReporter.ignore]: true });
|
|
24
|
+
|
|
25
|
+
export const inheritBrowserReport = <A extends object>(original: unknown, error: A): A =>
|
|
26
|
+
ErrorReporter.isIgnored(original) ? reportedBrowserError(error) : error;
|
|
27
|
+
|
|
28
|
+
const reasonSchema = Schema.Literals([
|
|
29
|
+
"provider",
|
|
30
|
+
"timeout",
|
|
31
|
+
"malformed",
|
|
32
|
+
"configuration",
|
|
33
|
+
"authorization",
|
|
34
|
+
"rate-limited",
|
|
35
|
+
"pending",
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
const statusSchema = Schema.Int.check(Schema.isBetween({ minimum: 100, maximum: 599 }));
|
|
39
|
+
|
|
40
|
+
const failureNames = new Set([
|
|
41
|
+
"TypeError",
|
|
42
|
+
"RangeError",
|
|
43
|
+
"SyntaxError",
|
|
44
|
+
"TimeoutError",
|
|
45
|
+
"SchemaError",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
export const browserFailure = (operation: string, cause?: unknown): BrowserRunFailure =>
|
|
49
|
+
cause instanceof BrowserRunFailure
|
|
50
|
+
? cause
|
|
51
|
+
: new BrowserRunFailure({
|
|
52
|
+
operation,
|
|
53
|
+
reason:
|
|
54
|
+
Predicate.isObject(cause) && "reason" in cause && Schema.is(reasonSchema)(cause.reason)
|
|
55
|
+
? cause.reason
|
|
56
|
+
: "provider",
|
|
57
|
+
...(Predicate.isObject(cause) && "status" in cause && Schema.is(statusSchema)(cause.status)
|
|
58
|
+
? { status: cause.status }
|
|
59
|
+
: {}),
|
|
60
|
+
...(Predicate.isObject(cause) &&
|
|
61
|
+
"cause" in cause &&
|
|
62
|
+
cause.cause instanceof BrowserRunFailure
|
|
63
|
+
? { cause: cause.cause }
|
|
64
|
+
: Predicate.isObject(cause) &&
|
|
65
|
+
"name" in cause &&
|
|
66
|
+
typeof cause.name === "string" &&
|
|
67
|
+
failureNames.has(cause.name)
|
|
68
|
+
? { cause: { name: cause.name } }
|
|
69
|
+
: {}),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/** Keep mixed defects and interruptions distinct while discarding all foreign diagnostic content. */
|
|
73
|
+
export const reportBrowserCause = <E>(
|
|
74
|
+
operation: string,
|
|
75
|
+
cause: Cause.Cause<E>,
|
|
76
|
+
): Effect.Effect<void> =>
|
|
77
|
+
ErrorReporter.report(
|
|
78
|
+
Cause.fromReasons(
|
|
79
|
+
cause.reasons
|
|
80
|
+
.filter((reason) => !Cause.isFailReason(reason) || !ErrorReporter.isIgnored(reason.error))
|
|
81
|
+
.map((reason) => {
|
|
82
|
+
switch (reason._tag) {
|
|
83
|
+
case "Fail":
|
|
84
|
+
return Cause.makeFailReason(browserFailure(operation, reason.error));
|
|
85
|
+
case "Die":
|
|
86
|
+
return Cause.makeDieReason(browserFailure(operation, reason.defect));
|
|
87
|
+
case "Interrupt":
|
|
88
|
+
return reason;
|
|
89
|
+
}
|
|
90
|
+
}),
|
|
91
|
+
),
|
|
92
|
+
);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { Page } from "@cloudflare/puppeteer";
|
|
2
|
+
import { Encoding } from "effect";
|
|
3
|
+
import type { BrowserSelectFileRequest } from "effect-agent/interactive-browser";
|
|
4
|
+
|
|
5
|
+
type Element = NonNullable<Awaited<ReturnType<Page["$"]>>>;
|
|
6
|
+
|
|
7
|
+
// Both calls run this fixed function in Chromium. No caller-provided code or
|
|
8
|
+
// filesystem path is evaluated, and none of the file bytes enter diagnostics.
|
|
9
|
+
function setFileSelection(element: object, encoded: string, name: string, type: string): void {
|
|
10
|
+
if (
|
|
11
|
+
Reflect.get(element, "type") !== "file" ||
|
|
12
|
+
Reflect.get(element, "disabled") ||
|
|
13
|
+
Reflect.get(element, "webkitdirectory")
|
|
14
|
+
) {
|
|
15
|
+
throw new Error("The target is not an enabled file input");
|
|
16
|
+
}
|
|
17
|
+
const bytes = Uint8Array.from(atob(encoded), (character) => character.charCodeAt(0));
|
|
18
|
+
const Transfer = Reflect.get(globalThis, "DataTransfer");
|
|
19
|
+
const transfer = new Transfer();
|
|
20
|
+
|
|
21
|
+
transfer.items.add(new File([bytes], name, { type }));
|
|
22
|
+
Reflect.set(element, "files", transfer.files);
|
|
23
|
+
const selected = Reflect.get(element, "files");
|
|
24
|
+
|
|
25
|
+
if (
|
|
26
|
+
selected?.length !== 1 ||
|
|
27
|
+
selected[0]?.size !== bytes.length ||
|
|
28
|
+
selected[0]?.name !== name ||
|
|
29
|
+
selected[0]?.type !== type
|
|
30
|
+
) {
|
|
31
|
+
throw new Error("The input did not retain the selected file");
|
|
32
|
+
}
|
|
33
|
+
const dispatch = Reflect.get(element, "dispatchEvent");
|
|
34
|
+
|
|
35
|
+
Reflect.apply(dispatch, element, [new Event("input", { bubbles: true })]);
|
|
36
|
+
Reflect.apply(dispatch, element, [new Event("change", { bubbles: true })]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const makeFileSelection =
|
|
40
|
+
(page: Page) => async (request: BrowserSelectFileRequest, signal: AbortSignal) => {
|
|
41
|
+
const encoded = Encoding.encodeBase64(request.bytes);
|
|
42
|
+
const session = request.target === "chooser" ? await page.createCDPSession() : undefined;
|
|
43
|
+
|
|
44
|
+
if (session !== undefined) {
|
|
45
|
+
try {
|
|
46
|
+
await session.send("Page.enable");
|
|
47
|
+
await session.send("Page.setInterceptFileChooserDialog", { enabled: true });
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
await session.detach();
|
|
50
|
+
throw cause;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const select = async (element: Element): Promise<void> => {
|
|
55
|
+
if (session === undefined)
|
|
56
|
+
return element.evaluate(setFileSelection, encoded, request.fileName, request.mediaType);
|
|
57
|
+
let onOpened: (event: { backendNodeId?: number }) => void = () => {};
|
|
58
|
+
let onAbort: () => void = () => {};
|
|
59
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
60
|
+
|
|
61
|
+
const opened = new Promise<number>((resolve, reject) => {
|
|
62
|
+
onOpened = (event) =>
|
|
63
|
+
event.backendNodeId === undefined
|
|
64
|
+
? reject(new Error("The chooser has no file input"))
|
|
65
|
+
: resolve(event.backendNodeId);
|
|
66
|
+
onAbort = () => reject(new Error("File selection interrupted"));
|
|
67
|
+
session.on("Page.fileChooserOpened", onOpened);
|
|
68
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
69
|
+
timer = setTimeout(() => reject(new Error("No file chooser opened")), 5_000);
|
|
70
|
+
if (signal.aborted) onAbort();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const [backendNodeId] = await Promise.all([opened, element.click()]);
|
|
75
|
+
|
|
76
|
+
if (signal.aborted) throw new Error("File selection interrupted");
|
|
77
|
+
const resolved = await session.send("DOM.resolveNode", { backendNodeId });
|
|
78
|
+
|
|
79
|
+
const objectId = resolved.object.objectId;
|
|
80
|
+
|
|
81
|
+
if (objectId === undefined) throw new Error("The chooser has no file input");
|
|
82
|
+
if (signal.aborted) throw new Error("File selection interrupted");
|
|
83
|
+
|
|
84
|
+
const result = await session.send("Runtime.callFunctionOn", {
|
|
85
|
+
objectId,
|
|
86
|
+
functionDeclaration: `function(encoded, name, type) { (${setFileSelection.toString()})(this, encoded, name, type); }`,
|
|
87
|
+
arguments: [
|
|
88
|
+
{ value: encoded },
|
|
89
|
+
{ value: request.fileName },
|
|
90
|
+
{ value: request.mediaType },
|
|
91
|
+
],
|
|
92
|
+
returnByValue: true,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (result.exceptionDetails !== undefined)
|
|
96
|
+
throw new Error("The chooser did not select the file");
|
|
97
|
+
} finally {
|
|
98
|
+
clearTimeout(timer);
|
|
99
|
+
session.off("Page.fileChooserOpened", onOpened);
|
|
100
|
+
signal.removeEventListener("abort", onAbort);
|
|
101
|
+
// Detaching the owned session releases all remote object handles, including
|
|
102
|
+
// after timeout, interruption, or a navigation during change handlers.
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
validate: async (element: Element): Promise<boolean> =>
|
|
108
|
+
request.target === "chooser" ||
|
|
109
|
+
(await element.evaluate(
|
|
110
|
+
(input) =>
|
|
111
|
+
Reflect.get(input, "type") === "file" &&
|
|
112
|
+
!Reflect.get(input, "disabled") &&
|
|
113
|
+
!Reflect.get(input, "webkitdirectory"),
|
|
114
|
+
)),
|
|
115
|
+
select,
|
|
116
|
+
close: async (): Promise<void> => {
|
|
117
|
+
if (session !== undefined) {
|
|
118
|
+
try {
|
|
119
|
+
await session.send("Page.setInterceptFileChooserDialog", { enabled: false });
|
|
120
|
+
} finally {
|
|
121
|
+
await session.detach();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
};
|