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 { InstanceProfile } from "@alchemy.run/aws/IAM/InstanceProfile"; |
| 2 | import { Role } from "@alchemy.run/aws/IAM/Role"; |
| 3 | import * as ImageBuilder from "@alchemy.run/aws/ImageBuilder"; |
| 4 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | export class ImageBuilderTestFunction extends Lambda.Function<Lambda.Function>()( |
| 16 | "ImageBuilderTestFunction", |
| 17 | ) {} |
| 18 | |
| 19 | const componentData = [ |
| 20 | "name: alchemy-imagebuilder-bindings-component", |
| 21 | "description: no-op component used by alchemy binding tests", |
| 22 | "schemaVersion: 1.0", |
| 23 | "phases:", |
| 24 | " - name: build", |
| 25 | " steps:", |
| 26 | " - name: hello", |
| 27 | " action: ExecuteBash", |
| 28 | " inputs:", |
| 29 | " commands:", |
| 30 | " - echo hello-from-alchemy-bindings", |
| 31 | ].join("\n"); |
| 32 | |
| 33 | export default ImageBuilderTestFunction.make( |
| 34 | { |
| 35 | main, |
| 36 | functionUrl: true, |
| 37 | timeout: Duration.seconds(30), |
| 38 | }, |
| 39 | Effect.gen(function* () { |
| 40 | // The pipeline the pipeline-scoped bindings are bound to. ENABLED but |
| 41 | // with no schedule, so it only builds when the Start binding fires. |
| 42 | const role = yield* Role("BindingsBuilderRole", { |
| 43 | assumeRolePolicyDocument: { |
| 44 | Version: "2012-10-17", |
| 45 | Statement: [ |
| 46 | { |
| 47 | Effect: "Allow", |
| 48 | Principal: { Service: "ec2.amazonaws.com" }, |
| 49 | Action: ["sts:AssumeRole"], |
| 50 | }, |
| 51 | ], |
| 52 | }, |
| 53 | managedPolicyArns: [ |
| 54 | "arn:aws:iam::aws:policy/EC2InstanceProfileForImageBuilder", |
| 55 | "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore", |
| 56 | ], |
| 57 | }); |
| 58 | const profile = yield* InstanceProfile("BindingsBuilderProfile", { |
| 59 | roleName: role.roleName, |
| 60 | }); |
| 61 | const component = yield* ImageBuilder.Component("BindingsComponent", { |
| 62 | platform: "Linux", |
| 63 | data: componentData, |
| 64 | }); |
| 65 | const recipe = yield* ImageBuilder.ImageRecipe("BindingsRecipe", { |
| 66 | // The testing account deploys to us-west-2 (region is fixed here |
| 67 | // because AWSEnvironment is deploy-only and this effect re-runs at |
| 68 | // Lambda init). |
| 69 | parentImage: "arn:aws:imagebuilder:us-west-2:aws:image/amazon-linux-2023-x86/x.x.x", |
| 70 | components: [{ componentArn: component.componentBuildVersionArn }], |
| 71 | }); |
| 72 | const infra = yield* ImageBuilder.InfrastructureConfiguration("BindingsInfra", { |
| 73 | instanceProfileName: profile.instanceProfileName, |
| 74 | instanceTypes: ["t3.micro"], |
| 75 | terminateInstanceOnFailure: true, |
| 76 | }); |
| 77 | const pipeline = yield* ImageBuilder.ImagePipeline("BindingsPipeline", { |
| 78 | imageRecipeArn: recipe.imageRecipeArn, |
| 79 | infrastructureConfigurationArn: infra.infrastructureConfigurationArn, |
| 80 | // Disable tests so a cancelled build never proceeds to a test phase. |
| 81 | imageTestsConfiguration: { imageTestsEnabled: false, timeout: "1 hour" }, |
| 82 | }); |
| 83 | |
| 84 | // Event source: subscribe the host to image state-change events. The |
| 85 | // deploy proves the EventBridge rule + invoke permission wiring. |
| 86 | yield* ImageBuilder.consumeImageEvents({ kinds: ["image-state-change"] }, (events) => |
| 87 | Stream.runForEach(events, (event) => |
| 88 | Effect.log(`image state change: ${event.resources[0]} -> ${event.detail.state?.status}`), |
| 89 | ), |
| 90 | ); |
| 91 | |
| 92 | const getPipeline = yield* ImageBuilder.GetImagePipeline(pipeline); |
| 93 | const listPipelineImages = yield* ImageBuilder.ListImagePipelineImages(pipeline); |
| 94 | const startBuild = yield* ImageBuilder.StartImagePipelineExecution(pipeline); |
| 95 | const getImage = yield* ImageBuilder.GetImage(); |
| 96 | const cancelBuild = yield* ImageBuilder.CancelImageCreation(); |
| 97 | const deleteImage = yield* ImageBuilder.DeleteImage(); |
| 98 | const listImages = yield* ImageBuilder.ListImages(); |
| 99 | const listWorkflowExecutions = yield* ImageBuilder.ListWorkflowExecutions(); |
| 100 | const listImageBuildVersions = yield* ImageBuilder.ListImageBuildVersions(); |
| 101 | const listImagePackages = yield* ImageBuilder.ListImagePackages(); |
| 102 | const listImageScanFindings = yield* ImageBuilder.ListImageScanFindings(); |
| 103 | const listImageScanFindingAggregations = yield* ImageBuilder.ListImageScanFindingAggregations(); |
| 104 | const getWorkflowExecution = yield* ImageBuilder.GetWorkflowExecution(); |
| 105 | const getWorkflowStepExecution = yield* ImageBuilder.GetWorkflowStepExecution(); |
| 106 | const listWorkflowStepExecutions = yield* ImageBuilder.ListWorkflowStepExecutions(); |
| 107 | const listWaitingWorkflowSteps = yield* ImageBuilder.ListWaitingWorkflowSteps(); |
| 108 | const sendWorkflowStepAction = yield* ImageBuilder.SendWorkflowStepAction(); |
| 109 | const retryImage = yield* ImageBuilder.RetryImage(); |
| 110 | |
| 111 | const bound = { |
| 112 | getPipeline, |
| 113 | listPipelineImages, |
| 114 | startBuild, |
| 115 | getImage, |
| 116 | cancelBuild, |
| 117 | deleteImage, |
| 118 | listImages, |
| 119 | listWorkflowExecutions, |
| 120 | listImageBuildVersions, |
| 121 | listImagePackages, |
| 122 | listImageScanFindings, |
| 123 | listImageScanFindingAggregations, |
| 124 | getWorkflowExecution, |
| 125 | getWorkflowStepExecution, |
| 126 | listWorkflowStepExecutions, |
| 127 | listWaitingWorkflowSteps, |
| 128 | // Approving needs a build paused on WAIT_FOR_ACTION and retrying needs |
| 129 | // a FAILED build, so the suite only proves registration/IAM wiring for |
| 130 | // these two. |
| 131 | sendWorkflowStepAction, |
| 132 | retryImage, |
| 133 | }; |
| 134 | |
| 135 | return { |
| 136 | fetch: Effect.gen(function* () { |
| 137 | const request = yield* HttpServerRequest; |
| 138 | const url = new URL(request.originalUrl); |
| 139 | const pathname = url.pathname; |
| 140 | const arn = url.searchParams.get("arn") ?? ""; |
| 141 | const id = url.searchParams.get("id") ?? ""; |
| 142 | |
| 143 | if (request.method === "GET" && pathname === "/bindings") { |
| 144 | return yield* HttpServerResponse.json({ |
| 145 | bound: Object.keys(bound), |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | // Pipeline-scoped read: the pipeline ARN is injected. |
| 150 | if (request.method === "GET" && pathname === "/pipeline") { |
| 151 | const { imagePipeline } = yield* getPipeline(); |
| 152 | return yield* HttpServerResponse.json({ |
| 153 | arn: imagePipeline?.arn, |
| 154 | name: imagePipeline?.name, |
| 155 | status: imagePipeline?.status, |
| 156 | timeoutMinutes: imagePipeline?.imageTestsConfiguration?.timeoutMinutes, |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | // Pipeline-scoped list of the builds this pipeline produced. |
| 161 | if (request.method === "GET" && pathname === "/pipeline-images") { |
| 162 | const { imageSummaryList } = yield* listPipelineImages(); |
| 163 | return yield* HttpServerResponse.json({ |
| 164 | arns: (imageSummaryList ?? []).map((image) => image.arn), |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | // Account-level image listing. |
| 169 | if (request.method === "GET" && pathname === "/images") { |
| 170 | const { imageVersionList } = yield* listImages({ owner: "Self" }); |
| 171 | return yield* HttpServerResponse.json({ |
| 172 | count: (imageVersionList ?? []).length, |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | // Kick off a build of the bound pipeline. |
| 177 | if (request.method === "POST" && pathname === "/build/start") { |
| 178 | const { imageBuildVersionArn } = yield* startBuild(); |
| 179 | return yield* HttpServerResponse.json({ imageBuildVersionArn }); |
| 180 | } |
| 181 | |
| 182 | // Cancel an in-flight build. Early build states can briefly reject |
| 183 | // cancellation — report the typed tag so the test can poll. |
| 184 | if (request.method === "POST" && pathname === "/build/cancel") { |
| 185 | const result = yield* cancelBuild({ |
| 186 | imageBuildVersionArn: arn, |
| 187 | }).pipe( |
| 188 | Effect.map(({ imageBuildVersionArn }) => ({ |
| 189 | imageBuildVersionArn, |
| 190 | })), |
| 191 | Effect.catchTag( |
| 192 | [ |
| 193 | "ResourceInUseException", |
| 194 | "InvalidRequestException", |
| 195 | // The build version is not visible immediately after start. |
| 196 | "ResourceNotFoundException", |
| 197 | ], |
| 198 | (error) => Effect.succeed({ reason: error._tag }), |
| 199 | ), |
| 200 | ); |
| 201 | return yield* HttpServerResponse.json(result); |
| 202 | } |
| 203 | |
| 204 | // Read a build version's state. |
| 205 | if (request.method === "GET" && pathname === "/build") { |
| 206 | const { image } = yield* getImage({ imageBuildVersionArn: arn }); |
| 207 | return yield* HttpServerResponse.json({ |
| 208 | status: image?.state?.status, |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | // Drill into the build's workflow runs. |
| 213 | if (request.method === "GET" && pathname === "/build/workflows") { |
| 214 | const { workflowExecutions } = yield* listWorkflowExecutions({ |
| 215 | imageBuildVersionArn: arn, |
| 216 | }); |
| 217 | return yield* HttpServerResponse.json({ |
| 218 | ids: (workflowExecutions ?? []).flatMap((execution) => |
| 219 | execution.workflowExecutionId !== undefined ? [execution.workflowExecutionId] : [], |
| 220 | ), |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | // List the build versions of an image version. |
| 225 | if (request.method === "GET" && pathname === "/build-versions") { |
| 226 | const { imageSummaryList } = yield* listImageBuildVersions({ |
| 227 | imageVersionArn: arn, |
| 228 | }); |
| 229 | return yield* HttpServerResponse.json({ |
| 230 | arns: (imageSummaryList ?? []).flatMap((image) => |
| 231 | image.arn !== undefined ? [image.arn] : [], |
| 232 | ), |
| 233 | }); |
| 234 | } |
| 235 | |
| 236 | // Packages only exist once a build is AVAILABLE; earlier states |
| 237 | // report the typed rejection so the test can assert it. |
| 238 | if (request.method === "GET" && pathname === "/build/packages") { |
| 239 | const result = yield* listImagePackages({ |
| 240 | imageBuildVersionArn: arn, |
| 241 | }).pipe( |
| 242 | Effect.map(({ imagePackageList }) => ({ |
| 243 | count: (imagePackageList ?? []).length, |
| 244 | })), |
| 245 | Effect.catchTag(["InvalidRequestException", "ResourceNotFoundException"], (error) => |
| 246 | Effect.succeed({ reason: error._tag }), |
| 247 | ), |
| 248 | ); |
| 249 | return yield* HttpServerResponse.json(result); |
| 250 | } |
| 251 | |
| 252 | // Account-level Inspector scan findings (empty unless scanning is |
| 253 | // enabled somewhere in the account). |
| 254 | if (request.method === "GET" && pathname === "/scan-findings") { |
| 255 | const { findings } = yield* listImageScanFindings(); |
| 256 | return yield* HttpServerResponse.json({ |
| 257 | count: (findings ?? []).length, |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | if (request.method === "GET" && pathname === "/scan-aggregations") { |
| 262 | const { responses } = yield* listImageScanFindingAggregations(); |
| 263 | return yield* HttpServerResponse.json({ |
| 264 | count: (responses ?? []).length, |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | // Steps across the account waiting on WAIT_FOR_ACTION. |
| 269 | if (request.method === "GET" && pathname === "/waiting-steps") { |
| 270 | const { steps } = yield* listWaitingWorkflowSteps(); |
| 271 | return yield* HttpServerResponse.json({ |
| 272 | count: (steps ?? []).length, |
| 273 | }); |
| 274 | } |
| 275 | |
| 276 | // Read one workflow execution by id. |
| 277 | if (request.method === "GET" && pathname === "/workflow-execution") { |
| 278 | const execution = yield* getWorkflowExecution({ |
| 279 | workflowExecutionId: id, |
| 280 | }); |
| 281 | return yield* HttpServerResponse.json({ |
| 282 | id: execution.workflowExecutionId, |
| 283 | type: execution.type, |
| 284 | status: execution.status, |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | // List the steps of one workflow execution. |
| 289 | if (request.method === "GET" && pathname === "/workflow-steps") { |
| 290 | const { steps } = yield* listWorkflowStepExecutions({ |
| 291 | workflowExecutionId: id, |
| 292 | }); |
| 293 | return yield* HttpServerResponse.json({ |
| 294 | ids: (steps ?? []).flatMap((step) => |
| 295 | step.stepExecutionId !== undefined ? [step.stepExecutionId] : [], |
| 296 | ), |
| 297 | }); |
| 298 | } |
| 299 | |
| 300 | // Read one workflow step by id. |
| 301 | if (request.method === "GET" && pathname === "/workflow-step") { |
| 302 | const step = yield* getWorkflowStepExecution({ |
| 303 | stepExecutionId: id, |
| 304 | }); |
| 305 | return yield* HttpServerResponse.json({ |
| 306 | name: step.name, |
| 307 | status: step.status, |
| 308 | }); |
| 309 | } |
| 310 | |
| 311 | // Prune a build version; not-yet-deletable states report why so the |
| 312 | // test can poll until the build settles. |
| 313 | if (request.method === "DELETE" && pathname === "/build") { |
| 314 | const result = yield* deleteImage({ |
| 315 | imageBuildVersionArn: arn, |
| 316 | }).pipe( |
| 317 | Effect.map(() => ({ deleted: true as const })), |
| 318 | // Already gone — deletion is idempotent. |
| 319 | Effect.catchTag("ResourceNotFoundException", () => |
| 320 | Effect.succeed({ deleted: true as const }), |
| 321 | ), |
| 322 | Effect.catchTag(["InvalidRequestException", "ResourceDependencyException"], (error) => |
| 323 | Effect.succeed({ deleted: false as const, reason: error._tag }), |
| 324 | ), |
| 325 | ); |
| 326 | return yield* HttpServerResponse.json(result); |
| 327 | } |
| 328 | |
| 329 | return yield* HttpServerResponse.json( |
| 330 | { error: "Not found", method: request.method, pathname }, |
| 331 | { status: 404 }, |
| 332 | ); |
| 333 | }).pipe(Effect.orDie), |
| 334 | }; |
| 335 | }).pipe( |
| 336 | Effect.provide( |
| 337 | Layer.mergeAll( |
| 338 | Lambda.EventSource, |
| 339 | ImageBuilder.GetImagePipelineHttp, |
| 340 | ImageBuilder.ListImagePipelineImagesHttp, |
| 341 | ImageBuilder.StartImagePipelineExecutionHttp, |
| 342 | ImageBuilder.GetImageHttp, |
| 343 | ImageBuilder.CancelImageCreationHttp, |
| 344 | ImageBuilder.DeleteImageHttp, |
| 345 | ImageBuilder.ListImagesHttp, |
| 346 | ImageBuilder.ListWorkflowExecutionsHttp, |
| 347 | ImageBuilder.ListImageBuildVersionsHttp, |
| 348 | ImageBuilder.ListImagePackagesHttp, |
| 349 | ImageBuilder.ListImageScanFindingsHttp, |
| 350 | ImageBuilder.ListImageScanFindingAggregationsHttp, |
| 351 | ImageBuilder.GetWorkflowExecutionHttp, |
| 352 | ImageBuilder.GetWorkflowStepExecutionHttp, |
| 353 | ImageBuilder.ListWorkflowStepExecutionsHttp, |
| 354 | ImageBuilder.ListWaitingWorkflowStepsHttp, |
| 355 | ImageBuilder.SendWorkflowStepActionHttp, |
| 356 | ImageBuilder.RetryImageHttp, |
| 357 | ), |
| 358 | ), |
| 359 | ), |
| 360 | ); |