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, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | |
| 10 | // Bindings fixture: a personal contact with a deferred-activation email |
| 11 | // channel and a daily rotation, plus a Lambda that exercises the SSM |
| 12 | // Contacts runtime bindings against them. Requires the account's Incident |
| 13 | // Manager replication set (gated behind AWS_TEST_INCIDENT_MANAGER). |
| 14 | export class ContactsBindingsFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 15 | "ContactsBindingsFunction", |
| 16 | ) {} |
| 17 | |
| 18 | export class BoundContacts extends Context.Service< |
| 19 | BoundContacts, |
| 20 | { |
| 21 | oncall: AWS.SSMContacts.Contact; |
| 22 | email: AWS.SSMContacts.ContactChannel; |
| 23 | rotation: AWS.SSMContacts.Rotation; |
| 24 | } |
| 25 | >()("BoundContacts") {} |
| 26 | |
| 27 | export const BoundContactsLive = Layer.effect( |
| 28 | BoundContacts, |
| 29 | Effect.gen(function* () { |
| 30 | const oncall = yield* AWS.SSMContacts.Contact("BindingsOncall", { |
| 31 | type: "PERSONAL", |
| 32 | displayName: "Bindings Fixture On-Call", |
| 33 | }); |
| 34 | const email = yield* AWS.SSMContacts.ContactChannel("BindingsEmail", { |
| 35 | contactId: oncall.contactArn, |
| 36 | type: "EMAIL", |
| 37 | deliveryAddress: { SimpleAddress: "oncall@example.com" }, |
| 38 | deferActivation: true, |
| 39 | }); |
| 40 | const rotation = yield* AWS.SSMContacts.Rotation("BindingsRotation", { |
| 41 | contactIds: [oncall.contactArn], |
| 42 | timeZoneId: "America/Los_Angeles", |
| 43 | startTime: "2030-01-01T00:00:00Z", |
| 44 | recurrence: { |
| 45 | NumberOfOnCalls: 1, |
| 46 | RecurrenceMultiplier: 1, |
| 47 | DailySettings: [{ HourOfDay: 9, MinuteOfHour: 0 }], |
| 48 | }, |
| 49 | }); |
| 50 | return { oncall, email, rotation }; |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | export default ContactsBindingsFunction.make( |
| 55 | { |
| 56 | main: import.meta.url, |
| 57 | functionUrl: true, |
| 58 | timeout: Duration.seconds(60), |
| 59 | }, |
| 60 | Effect.gen(function* () { |
| 61 | const { oncall, email, rotation } = yield* BoundContacts; |
| 62 | |
| 63 | const startEngagement = yield* AWS.SSMContacts.StartEngagement(oncall); |
| 64 | const listPagesByContact = yield* AWS.SSMContacts.ListPagesByContact(oncall); |
| 65 | const stopEngagement = yield* AWS.SSMContacts.StopEngagement(); |
| 66 | const describeEngagement = yield* AWS.SSMContacts.DescribeEngagement(); |
| 67 | const listEngagements = yield* AWS.SSMContacts.ListEngagements(); |
| 68 | const acceptPage = yield* AWS.SSMContacts.AcceptPage(); |
| 69 | const describePage = yield* AWS.SSMContacts.DescribePage(); |
| 70 | const listPageReceipts = yield* AWS.SSMContacts.ListPageReceipts(); |
| 71 | const listPageResolutions = yield* AWS.SSMContacts.ListPageResolutions(); |
| 72 | const listPagesByEngagement = yield* AWS.SSMContacts.ListPagesByEngagement(); |
| 73 | const sendActivationCode = yield* AWS.SSMContacts.SendActivationCode(email); |
| 74 | const activateContactChannel = yield* AWS.SSMContacts.ActivateContactChannel(email); |
| 75 | const deactivateContactChannel = yield* AWS.SSMContacts.DeactivateContactChannel(email); |
| 76 | const listRotationShifts = yield* AWS.SSMContacts.ListRotationShifts(rotation); |
| 77 | const listPreviewRotationShifts = yield* AWS.SSMContacts.ListPreviewRotationShifts(); |
| 78 | const createRotationOverride = yield* AWS.SSMContacts.CreateRotationOverride(rotation); |
| 79 | const getRotationOverride = yield* AWS.SSMContacts.GetRotationOverride(rotation); |
| 80 | const deleteRotationOverride = yield* AWS.SSMContacts.DeleteRotationOverride(rotation); |
| 81 | const listRotationOverrides = yield* AWS.SSMContacts.ListRotationOverrides(rotation); |
| 82 | |
| 83 | const bound = { |
| 84 | startEngagement, |
| 85 | listPagesByContact, |
| 86 | stopEngagement, |
| 87 | describeEngagement, |
| 88 | listEngagements, |
| 89 | acceptPage, |
| 90 | describePage, |
| 91 | listPageReceipts, |
| 92 | listPageResolutions, |
| 93 | listPagesByEngagement, |
| 94 | sendActivationCode, |
| 95 | activateContactChannel, |
| 96 | deactivateContactChannel, |
| 97 | listRotationShifts, |
| 98 | listPreviewRotationShifts, |
| 99 | createRotationOverride, |
| 100 | getRotationOverride, |
| 101 | deleteRotationOverride, |
| 102 | listRotationOverrides, |
| 103 | }; |
| 104 | |
| 105 | // Accessor for the contact's ARN — an Accessor<string> at init scope, |
| 106 | // resolved with a second yield inside the runtime fetch handler. |
| 107 | const oncallArn = yield* oncall.contactArn; |
| 108 | |
| 109 | return { |
| 110 | fetch: Effect.gen(function* () { |
| 111 | const request = yield* HttpServerRequest; |
| 112 | const url = new URL(request.originalUrl); |
| 113 | const pathname = url.pathname; |
| 114 | |
| 115 | if (request.method === "GET" && pathname === "/bindings") { |
| 116 | return yield* HttpServerResponse.json({ |
| 117 | bound: Object.keys(bound), |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/shifts") { |
| 122 | const week = 7 * 24 * 60 * 60 * 1000; |
| 123 | const { RotationShifts } = yield* listRotationShifts({ |
| 124 | EndTime: new Date(Date.now() + week), |
| 125 | }); |
| 126 | const preview = yield* listPreviewRotationShifts({ |
| 127 | EndTime: new Date(Date.now() + week), |
| 128 | Members: [yield* oncallArn], |
| 129 | TimeZoneId: "America/Los_Angeles", |
| 130 | Recurrence: { |
| 131 | NumberOfOnCalls: 1, |
| 132 | RecurrenceMultiplier: 1, |
| 133 | DailySettings: [{ HourOfDay: 9, MinuteOfHour: 0 }], |
| 134 | }, |
| 135 | }); |
| 136 | return yield* HttpServerResponse.json({ |
| 137 | shifts: (RotationShifts ?? []).length, |
| 138 | previewShifts: (preview.RotationShifts ?? []).length, |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | if (request.method === "GET" && pathname === "/override") { |
| 143 | const hour = 60 * 60 * 1000; |
| 144 | const { RotationOverrideId } = yield* createRotationOverride({ |
| 145 | NewContactIds: [yield* oncallArn], |
| 146 | StartTime: new Date(Date.now() + hour), |
| 147 | EndTime: new Date(Date.now() + 2 * hour), |
| 148 | }); |
| 149 | const override = yield* getRotationOverride({ RotationOverrideId }); |
| 150 | const { RotationOverrides } = yield* listRotationOverrides({ |
| 151 | StartTime: new Date(), |
| 152 | EndTime: new Date(Date.now() + 3 * hour), |
| 153 | }); |
| 154 | yield* deleteRotationOverride({ RotationOverrideId }); |
| 155 | return yield* HttpServerResponse.json({ |
| 156 | overrideId: RotationOverrideId, |
| 157 | newContactIds: override.NewContactIds, |
| 158 | listed: (RotationOverrides ?? []).length, |
| 159 | }); |
| 160 | } |
| 161 | |
| 162 | if (request.method === "GET" && pathname === "/engage") { |
| 163 | const { EngagementArn } = yield* startEngagement({ |
| 164 | Sender: "alchemy-bindings-fixture", |
| 165 | Subject: "alchemy bindings fixture engagement", |
| 166 | Content: "Exercising the SSM Contacts runtime bindings.", |
| 167 | }); |
| 168 | const engagement = yield* describeEngagement({ |
| 169 | EngagementId: EngagementArn, |
| 170 | }); |
| 171 | // Pages materialize asynchronously — poll briefly (bounded). |
| 172 | const pages = yield* listPagesByEngagement({ |
| 173 | EngagementId: EngagementArn, |
| 174 | }).pipe( |
| 175 | Effect.map((r) => r.Pages ?? []), |
| 176 | Effect.repeat({ |
| 177 | schedule: Schedule.spaced("2 seconds"), |
| 178 | until: (pages): boolean => pages.length > 0, |
| 179 | times: 10, |
| 180 | }), |
| 181 | ); |
| 182 | let receipts = 0; |
| 183 | let resolutions = 0; |
| 184 | const pageArn = pages[0]?.PageArn; |
| 185 | if (pageArn !== undefined) { |
| 186 | yield* describePage({ PageId: pageArn }); |
| 187 | receipts = yield* listPageReceipts({ PageId: pageArn }).pipe( |
| 188 | Effect.map((r) => (r.Receipts ?? []).length), |
| 189 | ); |
| 190 | resolutions = yield* listPageResolutions({ PageId: pageArn }).pipe( |
| 191 | Effect.map((r) => (r.PageResolutions ?? []).length), |
| 192 | ); |
| 193 | } |
| 194 | yield* stopEngagement({ |
| 195 | EngagementId: EngagementArn, |
| 196 | Reason: "fixture done", |
| 197 | }); |
| 198 | return yield* HttpServerResponse.json({ |
| 199 | engagementArn: EngagementArn, |
| 200 | subject: engagement.Subject, |
| 201 | pages: pages.length, |
| 202 | receipts, |
| 203 | resolutions, |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | if (request.method === "GET" && pathname === "/pages") { |
| 208 | const { Engagements } = yield* listEngagements(); |
| 209 | const { Pages } = yield* listPagesByContact(); |
| 210 | return yield* HttpServerResponse.json({ |
| 211 | engagements: (Engagements ?? []).length, |
| 212 | pages: (Pages ?? []).length, |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | if (request.method === "GET" && pathname === "/channel") { |
| 217 | yield* sendActivationCode(); |
| 218 | yield* deactivateContactChannel(); |
| 219 | return yield* HttpServerResponse.json({ ok: true }); |
| 220 | } |
| 221 | |
| 222 | return yield* HttpServerResponse.json( |
| 223 | { error: "Not found", method: request.method, pathname }, |
| 224 | { status: 404 }, |
| 225 | ); |
| 226 | }).pipe(Effect.orDie), |
| 227 | }; |
| 228 | }).pipe( |
| 229 | Effect.provide( |
| 230 | Layer.mergeAll( |
| 231 | AWS.SSMContacts.StartEngagementHttp, |
| 232 | AWS.SSMContacts.ListPagesByContactHttp, |
| 233 | AWS.SSMContacts.StopEngagementHttp, |
| 234 | AWS.SSMContacts.DescribeEngagementHttp, |
| 235 | AWS.SSMContacts.ListEngagementsHttp, |
| 236 | AWS.SSMContacts.AcceptPageHttp, |
| 237 | AWS.SSMContacts.DescribePageHttp, |
| 238 | AWS.SSMContacts.ListPageReceiptsHttp, |
| 239 | AWS.SSMContacts.ListPageResolutionsHttp, |
| 240 | AWS.SSMContacts.ListPagesByEngagementHttp, |
| 241 | AWS.SSMContacts.SendActivationCodeHttp, |
| 242 | AWS.SSMContacts.ActivateContactChannelHttp, |
| 243 | AWS.SSMContacts.DeactivateContactChannelHttp, |
| 244 | AWS.SSMContacts.ListRotationShiftsHttp, |
| 245 | AWS.SSMContacts.ListPreviewRotationShiftsHttp, |
| 246 | AWS.SSMContacts.CreateRotationOverrideHttp, |
| 247 | AWS.SSMContacts.GetRotationOverrideHttp, |
| 248 | AWS.SSMContacts.DeleteRotationOverrideHttp, |
| 249 | AWS.SSMContacts.ListRotationOverridesHttp, |
| 250 | BoundContactsLive, |
| 251 | ), |
| 252 | ), |
| 253 | ), |
| 254 | ); |