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
| 1 | import * as alerting from "@distilled.cloud/cloudflare/alerting"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | import type { AlertType, NotificationPolicyMechanisms } from "./NotificationPolicy.ts"; |
| 7 | |
| 8 | // Notification-policy plumbing shared by NotificationPolicy and CustomAlert. |
| 9 | |
| 10 | export interface ObservedPolicy { |
| 11 | readonly id: string; |
| 12 | readonly name?: string; |
| 13 | readonly alertType?: AlertType; |
| 14 | readonly enabled?: boolean; |
| 15 | readonly description?: string; |
| 16 | readonly alertInterval?: string; |
| 17 | readonly mechanisms?: unknown; |
| 18 | readonly filters?: unknown; |
| 19 | readonly created?: string; |
| 20 | readonly modified?: string; |
| 21 | } |
| 22 | |
| 23 | export const undef = <T>(v: T | null | undefined): T | undefined => (v == null ? undefined : v); |
| 24 | |
| 25 | export const narrowPolicy = (raw: { |
| 26 | id?: string | null; |
| 27 | name?: string | null; |
| 28 | alertType?: string | null; |
| 29 | enabled?: boolean | null; |
| 30 | description?: string | null; |
| 31 | alertInterval?: string | null; |
| 32 | mechanisms?: unknown; |
| 33 | filters?: unknown; |
| 34 | created?: string | null; |
| 35 | modified?: string | null; |
| 36 | }): ObservedPolicy | undefined => |
| 37 | raw.id == null |
| 38 | ? undefined |
| 39 | : { |
| 40 | id: raw.id, |
| 41 | name: undef(raw.name), |
| 42 | alertType: undef(raw.alertType) as AlertType | undefined, |
| 43 | enabled: undef(raw.enabled), |
| 44 | description: undef(raw.description), |
| 45 | alertInterval: undef(raw.alertInterval), |
| 46 | mechanisms: raw.mechanisms ?? undefined, |
| 47 | filters: raw.filters ?? undefined, |
| 48 | created: undef(raw.created), |
| 49 | modified: undef(raw.modified), |
| 50 | }; |
| 51 | |
| 52 | export const observePolicy = (accountId: string, policyId: string) => |
| 53 | alerting.getPolicy({ accountId, policyId }).pipe( |
| 54 | Effect.map((p) => narrowPolicy({ ...p, id: p.id ?? policyId })), |
| 55 | Effect.catchTag("PolicyNotFound", () => Effect.undefined), |
| 56 | ); |
| 57 | |
| 58 | export const findPolicyByName = (accountId: string, name: string) => |
| 59 | alerting.listPolicies.items({ accountId }).pipe( |
| 60 | Stream.filter((p) => p.name === name && p.id != null), |
| 61 | Stream.runHead, |
| 62 | Effect.map(Option.getOrUndefined), |
| 63 | Effect.map((p) => (p === undefined ? undefined : narrowPolicy(p))), |
| 64 | ); |
| 65 | |
| 66 | export const createPolicyName = (id: string, name: string | undefined) => |
| 67 | Effect.gen(function* () { |
| 68 | return name ?? (yield* createPhysicalName({ id, lowercase: true })); |
| 69 | }); |
| 70 | |
| 71 | export type PolicyBody = Omit<alerting.CreatePolicyRequest, "accountId">; |
| 72 | |
| 73 | /** |
| 74 | * Binding contract of a notification policy (`NotificationPolicy`, |
| 75 | * `CustomAlert`): destinations contributed by the runtimes that consume its |
| 76 | * notifications, e.g. `Cloudflare.Alerting.consumeNotifications`. |
| 77 | */ |
| 78 | export interface NotificationPolicyBinding { |
| 79 | /** Webhook destinations to notify, alongside the declared `mechanisms`. */ |
| 80 | webhooks?: ReadonlyArray<{ id: string }>; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * The declared mechanisms plus the webhooks bound onto the policy. The engine |
| 85 | * sid-sorts bindings, so the merged order is stable across deploys. |
| 86 | */ |
| 87 | export const boundMechanisms = ( |
| 88 | mechanisms: NotificationPolicyMechanisms | undefined, |
| 89 | bindings: ReadonlyArray<{ data: NotificationPolicyBinding }>, |
| 90 | ): PolicyBody["mechanisms"] => { |
| 91 | const bound = bindings.flatMap((b) => b.data.webhooks ?? []); |
| 92 | if (bound.length === 0) return mechanisms as PolicyBody["mechanisms"]; |
| 93 | const webhooks = [...(mechanisms?.webhooks ?? []), ...bound]; |
| 94 | return { |
| 95 | ...mechanisms, |
| 96 | webhooks: webhooks.filter((w, i) => webhooks.findIndex((x) => x.id === w.id) === i), |
| 97 | } as PolicyBody["mechanisms"]; |
| 98 | }; |
| 99 | |
| 100 | export const policyEqualsObserved = (desired: PolicyBody, observed: ObservedPolicy): boolean => |
| 101 | desired.name === (observed.name ?? "") && |
| 102 | desired.enabled === (observed.enabled ?? true) && |
| 103 | (desired.description ?? "") === (observed.description ?? "") && |
| 104 | (desired.alertInterval ?? undefined) === observed.alertInterval && |
| 105 | normalizedEquals(desired.mechanisms, observed.mechanisms) && |
| 106 | normalizedEquals(desired.filters, observed.filters); |
| 107 | |
| 108 | export const normalizedEquals = (a: unknown, b: unknown): boolean => |
| 109 | JSON.stringify(normalize(a)) === JSON.stringify(normalize(b)); |
| 110 | |
| 111 | /** Drop null/undefined members and sort object keys for stable comparison. */ |
| 112 | export const normalize = (value: unknown): unknown => { |
| 113 | if (value === null || value === undefined) return undefined; |
| 114 | if (Array.isArray(value)) return value.map(normalize); |
| 115 | if (typeof value === "object") { |
| 116 | const out: Record<string, unknown> = {}; |
| 117 | for (const key of Object.keys(value as Record<string, unknown>).sort()) { |
| 118 | const v = normalize((value as Record<string, unknown>)[key]); |
| 119 | if (v !== undefined) out[key] = v; |
| 120 | } |
| 121 | return Object.keys(out).length === 0 ? undefined : out; |
| 122 | } |
| 123 | return value; |
| 124 | }; |