The best way to control Claude Code, Codex and more from any computer or phone

main
ts 53 lines 1.93 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
5 import { Send, SendEmailError, type SendClient, type SendEmailMessage } from "./Send.ts";
6 import type { SendEmail } from "./SendEmail.ts";
7
8 export const SendBinding = Layer.effect(
9 Send,
10 Effect.gen(function* () {
11 const host = yield* Worker;
12 const env = yield* WorkerEnvironment;
13
14 return Effect.fn(function* (sender: SendEmail) {
15 if (!globalThis.__ALCHEMY_RUNTIME__) {
16 yield* host.bind(sender.name, {
17 bindings: [
18 {
19 type: "send_email",
20 name: sender.name,
21 destinationAddress: sender.destinationAddress,
22 allowedDestinationAddresses: sender.allowedDestinationAddresses,
23 allowedSenderAddresses: sender.allowedSenderAddresses,
24 },
25 ],
26 // Dev-only local-emulation opt-out, contributed as a parallel
27 // channel (like `hyperdrives`) so the wire binding stays pure.
28 ...(sender.devRemote ? { devRemote: { [sender.name]: true } } : {}),
29 });
30 }
31
32 const raw = Effect.sync(() => (env as Record<string, runtime.SendEmail>)[sender.name]!);
33
34 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, SendEmailError> =>
35 Effect.tryPromise({
36 try: fn,
37 catch: (error: any) =>
38 new SendEmailError({
39 message: error?.message ?? "Unknown send_email error",
40 cause: error,
41 }),
42 });
43
44 return {
45 raw,
46 send: (message: SendEmailMessage) =>
47 raw.pipe(Effect.flatMap((s) => tryPromise(() => s.send(message)))),
48 sendRaw: (message: runtime.EmailMessage) =>
49 raw.pipe(Effect.flatMap((s) => tryPromise(() => s.send(message)))),
50 } satisfies SendClient;
51 });
52 }),
53 );