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 FIS from "@alchemy.run/aws/FIS"; |
| 2 | import { Role } from "@alchemy.run/aws/IAM/Role"; |
| 3 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import path from "pathe"; |
| 11 | |
| 12 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 13 | |
| 14 | export class FisTestFunction extends Lambda.Function<Lambda.Function>()("FisTestFunction") {} |
| 15 | |
| 16 | export default FisTestFunction.make( |
| 17 | { |
| 18 | main, |
| 19 | functionUrl: true, |
| 20 | timeout: Duration.seconds(30), |
| 21 | }, |
| 22 | Effect.gen(function* () { |
| 23 | // The experiment template the template-scoped bindings are bound to. A |
| 24 | // single target-less aws:fis:wait action, so starting an experiment |
| 25 | // disrupts nothing — it just idles until it completes or is stopped. |
| 26 | const role = yield* Role("FisBindingsRole", { |
| 27 | assumeRolePolicyDocument: { |
| 28 | Version: "2012-10-17", |
| 29 | Statement: [ |
| 30 | { |
| 31 | Effect: "Allow", |
| 32 | Principal: { Service: "fis.amazonaws.com" }, |
| 33 | Action: ["sts:AssumeRole"], |
| 34 | }, |
| 35 | ], |
| 36 | }, |
| 37 | }); |
| 38 | const template = yield* FIS.ExperimentTemplate("BindingsWaitTemplate", { |
| 39 | description: "alchemy fis bindings fixture template", |
| 40 | roleArn: role.roleArn, |
| 41 | actions: { |
| 42 | Wait: { |
| 43 | actionId: "aws:fis:wait", |
| 44 | parameters: { duration: "PT3M" }, |
| 45 | }, |
| 46 | }, |
| 47 | }); |
| 48 | |
| 49 | // Event source: subscribe the host to experiment state changes. The |
| 50 | // deploy proves the EventBridge rule + invoke permission wiring. |
| 51 | yield* FIS.consumeExperimentEvents({}, (events) => |
| 52 | Stream.runForEach(events, (event) => |
| 53 | Effect.log( |
| 54 | `fis experiment ${event.detail["experiment-id"]} -> ` + |
| 55 | `${event.detail["new-state"]?.status}`, |
| 56 | ), |
| 57 | ), |
| 58 | ); |
| 59 | |
| 60 | const startExperiment = yield* FIS.StartExperiment(template); |
| 61 | const getExperimentTemplate = yield* FIS.GetExperimentTemplate(template); |
| 62 | const getExperiment = yield* FIS.GetExperiment(); |
| 63 | const stopExperiment = yield* FIS.StopExperiment(); |
| 64 | const listExperiments = yield* FIS.ListExperiments(); |
| 65 | const listExperimentResolvedTargets = yield* FIS.ListExperimentResolvedTargets(); |
| 66 | const listExperimentTemplates = yield* FIS.ListExperimentTemplates(); |
| 67 | const getAction = yield* FIS.GetAction(); |
| 68 | const listActions = yield* FIS.ListActions(); |
| 69 | const getTargetResourceType = yield* FIS.GetTargetResourceType(); |
| 70 | const listTargetResourceTypes = yield* FIS.ListTargetResourceTypes(); |
| 71 | const getSafetyLever = yield* FIS.GetSafetyLever(); |
| 72 | const updateSafetyLeverState = yield* FIS.UpdateSafetyLeverState(); |
| 73 | const getExperimentTargetAccountConfiguration = |
| 74 | yield* FIS.GetExperimentTargetAccountConfiguration(); |
| 75 | const listExperimentTargetAccountConfigurations = |
| 76 | yield* FIS.ListExperimentTargetAccountConfigurations(); |
| 77 | |
| 78 | const bound = { |
| 79 | startExperiment, |
| 80 | getExperimentTemplate, |
| 81 | getExperiment, |
| 82 | stopExperiment, |
| 83 | listExperiments, |
| 84 | listExperimentResolvedTargets, |
| 85 | listExperimentTemplates, |
| 86 | getAction, |
| 87 | listActions, |
| 88 | getTargetResourceType, |
| 89 | listTargetResourceTypes, |
| 90 | getSafetyLever, |
| 91 | // Engaging the lever halts every experiment in the account, so the |
| 92 | // suite only proves registration/IAM wiring and never calls it. |
| 93 | updateSafetyLeverState, |
| 94 | // Multi-account reads; registration-only on a single-account fixture. |
| 95 | getExperimentTargetAccountConfiguration, |
| 96 | listExperimentTargetAccountConfigurations, |
| 97 | }; |
| 98 | |
| 99 | return { |
| 100 | fetch: Effect.gen(function* () { |
| 101 | const request = yield* HttpServerRequest; |
| 102 | const url = new URL(request.originalUrl); |
| 103 | const pathname = url.pathname; |
| 104 | const id = url.searchParams.get("id") ?? ""; |
| 105 | |
| 106 | if (request.method === "GET" && pathname === "/bindings") { |
| 107 | return yield* HttpServerResponse.json({ |
| 108 | bound: Object.keys(bound), |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | // Template-scoped read: the template id is injected by the binding. |
| 113 | if (request.method === "GET" && pathname === "/template") { |
| 114 | const { experimentTemplate } = yield* getExperimentTemplate(); |
| 115 | return yield* HttpServerResponse.json({ |
| 116 | id: experimentTemplate?.id, |
| 117 | actions: Object.keys(experimentTemplate?.actions ?? {}), |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "GET" && pathname === "/templates") { |
| 122 | const { experimentTemplates } = yield* listExperimentTemplates(); |
| 123 | return yield* HttpServerResponse.json({ |
| 124 | ids: (experimentTemplates ?? []).map((t) => t.id), |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | // Start a (harmless, wait-only) experiment from the bound template. |
| 129 | if (request.method === "POST" && pathname === "/experiments") { |
| 130 | const { experiment } = yield* startExperiment({ |
| 131 | tags: { "alchemy-test": "fis-bindings" }, |
| 132 | }); |
| 133 | return yield* HttpServerResponse.json({ |
| 134 | id: experiment?.id, |
| 135 | status: experiment?.state?.status, |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | if (request.method === "GET" && pathname === "/experiment") { |
| 140 | const { experiment } = yield* getExperiment({ id }); |
| 141 | return yield* HttpServerResponse.json({ |
| 142 | id: experiment?.id, |
| 143 | templateId: experiment?.experimentTemplateId, |
| 144 | status: experiment?.state?.status, |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | if (request.method === "GET" && pathname === "/experiments") { |
| 149 | const templateId = url.searchParams.get("templateId") ?? undefined; |
| 150 | const { experiments } = yield* listExperiments({ |
| 151 | experimentTemplateId: templateId, |
| 152 | }); |
| 153 | return yield* HttpServerResponse.json({ |
| 154 | ids: (experiments ?? []).map((e) => e.id), |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | if (request.method === "POST" && pathname === "/stop") { |
| 159 | const { experiment } = yield* stopExperiment({ id }); |
| 160 | return yield* HttpServerResponse.json({ |
| 161 | status: experiment?.state?.status, |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | if (request.method === "GET" && pathname === "/resolved-targets") { |
| 166 | const { resolvedTargets } = yield* listExperimentResolvedTargets({ |
| 167 | experimentId: id, |
| 168 | }); |
| 169 | return yield* HttpServerResponse.json({ |
| 170 | count: (resolvedTargets ?? []).length, |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | // Catalog reads. |
| 175 | if (request.method === "GET" && pathname === "/action") { |
| 176 | const { action } = yield* getAction({ id }); |
| 177 | return yield* HttpServerResponse.json({ id: action?.id }); |
| 178 | } |
| 179 | |
| 180 | if (request.method === "GET" && pathname === "/actions") { |
| 181 | const { actions } = yield* listActions(); |
| 182 | return yield* HttpServerResponse.json({ |
| 183 | ids: (actions ?? []).map((a) => a.id), |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | if (request.method === "GET" && pathname === "/target-resource-type") { |
| 188 | const { targetResourceType } = yield* getTargetResourceType({ |
| 189 | resourceType: url.searchParams.get("type") ?? "", |
| 190 | }); |
| 191 | return yield* HttpServerResponse.json({ |
| 192 | resourceType: targetResourceType?.resourceType, |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | if (request.method === "GET" && pathname === "/target-resource-types") { |
| 197 | const { targetResourceTypes } = yield* listTargetResourceTypes(); |
| 198 | return yield* HttpServerResponse.json({ |
| 199 | resourceTypes: (targetResourceTypes ?? []).map((t) => t.resourceType), |
| 200 | }); |
| 201 | } |
| 202 | |
| 203 | // The account's safety lever has the well-known id `default`. |
| 204 | if (request.method === "GET" && pathname === "/safety-lever") { |
| 205 | const { safetyLever } = yield* getSafetyLever({ id: "default" }); |
| 206 | return yield* HttpServerResponse.json({ |
| 207 | id: safetyLever?.id, |
| 208 | status: safetyLever?.state?.status, |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | return yield* HttpServerResponse.json( |
| 213 | { error: "Not found", method: request.method, pathname }, |
| 214 | { status: 404 }, |
| 215 | ); |
| 216 | }).pipe(Effect.orDie), |
| 217 | }; |
| 218 | }).pipe( |
| 219 | Effect.provide( |
| 220 | Layer.mergeAll( |
| 221 | Lambda.EventSource, |
| 222 | FIS.StartExperimentHttp, |
| 223 | FIS.GetExperimentTemplateHttp, |
| 224 | FIS.GetExperimentHttp, |
| 225 | FIS.StopExperimentHttp, |
| 226 | FIS.ListExperimentsHttp, |
| 227 | FIS.ListExperimentResolvedTargetsHttp, |
| 228 | FIS.ListExperimentTemplatesHttp, |
| 229 | FIS.GetActionHttp, |
| 230 | FIS.ListActionsHttp, |
| 231 | FIS.GetTargetResourceTypeHttp, |
| 232 | FIS.ListTargetResourceTypesHttp, |
| 233 | FIS.GetSafetyLeverHttp, |
| 234 | FIS.UpdateSafetyLeverStateHttp, |
| 235 | FIS.GetExperimentTargetAccountConfigurationHttp, |
| 236 | FIS.ListExperimentTargetAccountConfigurationsHttp, |
| 237 | ), |
| 238 | ), |
| 239 | ), |
| 240 | ); |