The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as GitHub from "@alchemy.run/github"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | |
| 8 | export const owner = "alchemy-run-test"; |
| 9 | export const repository = "alchemy-pr-2519-notifications"; |
| 10 | |
| 11 | // Public and retained across runs: the gh token lacks delete_repo. |
| 12 | export const Repo = GitHub.Repository("Repo", { |
| 13 | owner, |
| 14 | name: repository, |
| 15 | visibility: "public", |
| 16 | description: "Retained deterministic fixture for alchemy PR #2519 (Cloudflare notifications)", |
| 17 | autoInit: true, |
| 18 | }); |
| 19 | |
| 20 | // Declares no mechanisms: the Worker's event source adds its webhook. |
| 21 | export const IssuePolicy = Cloudflare.Alerting.NotificationPolicy("IssuePolicy", { |
| 22 | alertType: "workers_observability_real_time_issue", |
| 23 | }); |
| 24 | |
| 25 | // Opens a GitHub issue for every notification of the policy an Issues |
| 26 | // automation scoped to this Worker notifies, so `/boom` makes Cloudflare report |
| 27 | // the Worker's own error to GitHub. |
| 28 | export default class NotificationWorker extends Cloudflare.Worker<NotificationWorker>()( |
| 29 | "NotificationWorker", |
| 30 | { |
| 31 | main: import.meta.url, |
| 32 | observability: { enabled: true, issues: { enabled: true } }, |
| 33 | }, |
| 34 | Effect.gen(function* () { |
| 35 | const policy = yield* IssuePolicy; |
| 36 | const createIssue = yield* GitHub.CreateIssue(yield* Repo); |
| 37 | |
| 38 | yield* Cloudflare.Alerting.consumeNotifications(policy, (notification) => |
| 39 | createIssue({ |
| 40 | title: notification.text.split("\n")[0], |
| 41 | body: notification.text, |
| 42 | }).pipe(Effect.orDie), |
| 43 | ); |
| 44 | |
| 45 | return { |
| 46 | fetch: Effect.gen(function* () { |
| 47 | const request = yield* HttpServerRequest; |
| 48 | if (new URL(request.url, "http://x").pathname === "/boom") { |
| 49 | return yield* Effect.die(new Error("alchemy notification event source test")); |
| 50 | } |
| 51 | return HttpServerResponse.text("ok"); |
| 52 | }), |
| 53 | }; |
| 54 | }).pipe( |
| 55 | Effect.provide( |
| 56 | Layer.mergeAll(Cloudflare.Alerting.NotificationEventSourceLive, GitHub.CreateIssueHttp), |
| 57 | ), |
| 58 | ), |
| 59 | ) {} |