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

main
ts 81 lines 2.32 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Context from "effect/Context";
3 import * as Data from "effect/Data";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Stream from "effect/Stream";
7 import type { Job } from "./Job.ts";
8
9 export class NotifyJobError extends Data.TaggedError("NotifyJobError")<{
10 readonly message: string;
11 readonly cause?: unknown;
12 }> {}
13
14 type JobNotification = {
15 type: "job.created";
16 job: Job;
17 };
18
19 export class JobNotifications extends Context.Service<
20 JobNotifications,
21 {
22 notifyJobCreated(job: Job): Effect.Effect<void, NotifyJobError>;
23 }
24 >()("JobNotifications") {}
25
26 export const JobNotificationsSNS = Layer.effect(
27 JobNotifications,
28 Effect.gen(function* () {
29 const topic = yield* AWS.SNS.Topic("JobNotificationsTopic", {
30 attributes: {
31 DisplayName: "job-notifications",
32 },
33 });
34
35 const publish = yield* AWS.SNS.Publish(topic);
36
37 yield* AWS.SNS.consumeTopicNotifications(topic, (stream) =>
38 stream.pipe(
39 Stream.mapEffect((notification) =>
40 Effect.try({
41 try: () => JSON.parse(notification.Message) as JobNotification,
42 catch: (cause) =>
43 new NotifyJobError({
44 message: "Failed to parse SNS job notification",
45 cause,
46 }),
47 }).pipe(
48 Effect.flatMap((payload) =>
49 Effect.logInfo(`Job notification received: ${payload.type} (${payload.job.id})`),
50 ),
51 // Keep the example resilient to malformed demo messages.
52 Effect.catchTag("NotifyJobError", (error) => Effect.logWarning(error.message)),
53 ),
54 ),
55 Stream.runDrain,
56 ),
57 );
58
59 const notifyJobCreated = (job: Job) =>
60 publish({
61 Subject: "JobCreated",
62 Message: JSON.stringify({
63 type: "job.created",
64 job,
65 } satisfies JobNotification),
66 }).pipe(
67 Effect.asVoid,
68 Effect.mapError(
69 (cause) =>
70 new NotifyJobError({
71 message: `Failed to publish job notification for "${job.id}"`,
72 cause,
73 }),
74 ),
75 );
76
77 return JobNotifications.of({
78 notifyJobCreated,
79 });
80 }),
81 ).pipe(Layer.provideMerge(Layer.mergeAll(AWS.Lambda.TopicEventSource, AWS.SNS.PublishHttp)));