@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 84 lines 2.42 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.provideMerge(
27 Layer.effect(
28 JobNotifications,
29 Effect.gen(function* () {
30 const topic = yield* AWS.SNS.Topic("JobNotificationsTopic", {
31 attributes: {
32 DisplayName: "job-notifications",
33 },
34 });
35
36 const publish = yield* AWS.SNS.Publish(topic);
37
38 yield* AWS.SNS.consumeTopicNotifications(topic, (stream) =>
39 stream.pipe(
40 Stream.mapEffect((notification) =>
41 Effect.try({
42 try: () => JSON.parse(notification.Message) as JobNotification,
43 catch: (cause) =>
44 new NotifyJobError({
45 message: "Failed to parse SNS job notification",
46 cause,
47 }),
48 }).pipe(
49 Effect.flatMap((payload) =>
50 Effect.logInfo(`Job notification received: ${payload.type} (${payload.job.id})`),
51 ),
52 // Keep the example resilient to malformed demo messages.
53 Effect.catchTag("NotifyJobError", (error) => Effect.logWarning(error.message)),
54 ),
55 ),
56 Stream.runDrain,
57 ),
58 );
59
60 const notifyJobCreated = (job: Job) =>
61 publish({
62 Subject: "JobCreated",
63 Message: JSON.stringify({
64 type: "job.created",
65 job,
66 } satisfies JobNotification),
67 }).pipe(
68 Effect.asVoid,
69 Effect.mapError(
70 (cause) =>
71 new NotifyJobError({
72 message: `Failed to publish job notification for "${job.id}"`,
73 cause,
74 }),
75 ),
76 );
77
78 return JobNotifications.of({
79 notifyJobCreated,
80 });
81 }),
82 ),
83 Layer.mergeAll(AWS.Lambda.TopicEventSource, AWS.SNS.PublishHttp),
84 );