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 { Endpoint } from "@distilled.cloud/aws"; |
| 2 | import * as mgmt from "@distilled.cloud/aws/apigatewaymanagementapi"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import { |
| 7 | ManageConnections, |
| 8 | type DeleteConnectionRequest, |
| 9 | type GetConnectionRequest, |
| 10 | type PostToConnectionRequest, |
| 11 | } from "./ManageConnections.ts"; |
| 12 | import type { ApiGatewayV2Stage } from "./Stage.ts"; |
| 13 | |
| 14 | /** |
| 15 | * HTTP implementation of the {@link ManageConnections} binding. Signs |
| 16 | * `@connections` requests with the Lambda's IAM role against the stage's |
| 17 | * callback endpoint. |
| 18 | * |
| 19 | * Provide it on the hosting Lambda function's Effect so the binding is |
| 20 | * available at runtime: |
| 21 | * |
| 22 | * @example |
| 23 | * ```typescript |
| 24 | * export default MyFunction.make( |
| 25 | * { main: import.meta.url }, |
| 26 | * Effect.gen(function* () { |
| 27 | * const connections = yield* ApiGatewayV2.ManageConnections(stage); |
| 28 | * // ... register WebSocket routes that push via `connections` |
| 29 | * return {}; |
| 30 | * }).pipe(Effect.provide(ApiGatewayV2.ManageConnectionsHttp)), |
| 31 | * ); |
| 32 | * ``` |
| 33 | */ |
| 34 | export const ManageConnectionsHttp = Layer.effect( |
| 35 | ManageConnections, |
| 36 | Effect.gen(function* () { |
| 37 | const bind = yield* AWSBindings.Bind; |
| 38 | const postToConnection = yield* AWSBindings.withCredentials(mgmt.postToConnection); |
| 39 | const getConnection = yield* AWSBindings.withCredentials(mgmt.getConnection); |
| 40 | const deleteConnection = yield* AWSBindings.withCredentials(mgmt.deleteConnection); |
| 41 | |
| 42 | return Effect.fn(function* <S extends ApiGatewayV2Stage>(stage: S) { |
| 43 | const CallbackUrl = yield* stage.callbackUrl; |
| 44 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 45 | yield* bind`AWS.ApiGatewayV2.ManageConnections(${stage})`({ |
| 46 | policyStatements: [ |
| 47 | { |
| 48 | Effect: "Allow", |
| 49 | Action: ["execute-api:ManageConnections"], |
| 50 | Resource: [stage.connectionsArn], |
| 51 | }, |
| 52 | ], |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | // The management API is served from the stage's own callback |
| 57 | // endpoint rather than the service default, so each call overrides |
| 58 | // the distilled endpoint resolver with the bound URL. |
| 59 | const withEndpoint = <A, E>( |
| 60 | effect: Effect.Effect<A, E>, |
| 61 | endpoint: string, |
| 62 | ): Effect.Effect<A, E> => |
| 63 | Effect.provideService(effect, Endpoint.Endpoint, Effect.succeed(endpoint)); |
| 64 | |
| 65 | return { |
| 66 | postToConnection: Effect.fn(`AWS.ApiGatewayV2.PostToConnection(${stage.LogicalId})`)( |
| 67 | function* (request: PostToConnectionRequest) { |
| 68 | const endpoint = yield* CallbackUrl; |
| 69 | return yield* withEndpoint(postToConnection(request), endpoint); |
| 70 | }, |
| 71 | ), |
| 72 | getConnection: Effect.fn(`AWS.ApiGatewayV2.GetConnection(${stage.LogicalId})`)(function* ( |
| 73 | request: GetConnectionRequest, |
| 74 | ) { |
| 75 | const endpoint = yield* CallbackUrl; |
| 76 | return yield* withEndpoint(getConnection(request), endpoint); |
| 77 | }), |
| 78 | deleteConnection: Effect.fn(`AWS.ApiGatewayV2.DeleteConnection(${stage.LogicalId})`)( |
| 79 | function* (request: DeleteConnectionRequest) { |
| 80 | const endpoint = yield* CallbackUrl; |
| 81 | return yield* withEndpoint(deleteConnection(request), endpoint); |
| 82 | }, |
| 83 | ), |
| 84 | }; |
| 85 | }); |
| 86 | }), |
| 87 | ); |