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 Lambda from "@distilled.cloud/aws/lambda"; |
| 2 | import { deepEqual } from "alchemy/Diff"; |
| 3 | import { toWireSeconds } from "alchemy/Util/Duration"; |
| 4 | import type * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | |
| 8 | const DEFAULT_MAXIMUM_RETRY_ATTEMPTS = 2; |
| 9 | const DEFAULT_MAXIMUM_EVENT_AGE_SECONDS = 21_600; |
| 10 | |
| 11 | /** |
| 12 | * Asynchronous invocation settings for a Lambda function or alias. |
| 13 | * |
| 14 | * Configured via {@link FunctionProps.eventInvokeConfig} for the unqualified |
| 15 | * function, or {@link AliasProps.eventInvokeConfig} for a specific alias. |
| 16 | */ |
| 17 | export interface EventInvokeConfig { |
| 18 | /** |
| 19 | * Maximum number of times Lambda retries an asynchronous invocation. |
| 20 | * @default 2 |
| 21 | */ |
| 22 | maximumRetryAttempts?: number; |
| 23 | /** |
| 24 | * Maximum age that Lambda retains an asynchronous event (e.g. `"6 hours"`). |
| 25 | * Rounded to whole seconds on the wire. |
| 26 | * @default "6 hours" |
| 27 | */ |
| 28 | maximumEventAge?: Duration.Input; |
| 29 | /** |
| 30 | * Destinations for successful or failed asynchronous invocation records. |
| 31 | */ |
| 32 | destinationConfig?: Lambda.DestinationConfig; |
| 33 | } |
| 34 | |
| 35 | const retryOnConflict = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 36 | effect.pipe( |
| 37 | Effect.retry({ |
| 38 | while: (e) => e._tag === "ResourceConflictException", |
| 39 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]), |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | const normalizeDestinationConfig = ( |
| 44 | config: Lambda.DestinationConfig | undefined, |
| 45 | ): Lambda.DestinationConfig | undefined => { |
| 46 | const onSuccess = config?.OnSuccess?.Destination |
| 47 | ? { Destination: config.OnSuccess.Destination } |
| 48 | : undefined; |
| 49 | const onFailure = config?.OnFailure?.Destination |
| 50 | ? { Destination: config.OnFailure.Destination } |
| 51 | : undefined; |
| 52 | return onSuccess || onFailure |
| 53 | ? { |
| 54 | OnSuccess: onSuccess, |
| 55 | OnFailure: onFailure, |
| 56 | } |
| 57 | : undefined; |
| 58 | }; |
| 59 | |
| 60 | const observeConfig = (functionName: string, qualifier: string | undefined) => |
| 61 | Lambda.getFunctionEventInvokeConfig({ |
| 62 | FunctionName: functionName, |
| 63 | Qualifier: qualifier, |
| 64 | }).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 65 | |
| 66 | /** |
| 67 | * Converge the async invocation config of a function or alias to the desired |
| 68 | * state: put it when set, delete it when omitted. Diffs against the observed |
| 69 | * cloud config so a no-op deploy skips the write entirely. |
| 70 | */ |
| 71 | export const syncEventInvokeConfig = Effect.fn(function* ({ |
| 72 | functionName, |
| 73 | qualifier, |
| 74 | config, |
| 75 | }: { |
| 76 | functionName: string; |
| 77 | /** Alias name (or version) to scope the config to. Omit for `$LATEST`. */ |
| 78 | qualifier?: string; |
| 79 | config: EventInvokeConfig | undefined; |
| 80 | }) { |
| 81 | const observed = yield* observeConfig(functionName, qualifier); |
| 82 | |
| 83 | if (config === undefined) { |
| 84 | if (observed) { |
| 85 | yield* retryOnConflict( |
| 86 | Lambda.deleteFunctionEventInvokeConfig({ |
| 87 | FunctionName: functionName, |
| 88 | Qualifier: qualifier, |
| 89 | }), |
| 90 | ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 91 | } |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | const desired = { |
| 96 | maximumRetryAttempts: config.maximumRetryAttempts ?? DEFAULT_MAXIMUM_RETRY_ATTEMPTS, |
| 97 | maximumEventAgeSeconds: |
| 98 | toWireSeconds(config.maximumEventAge) ?? DEFAULT_MAXIMUM_EVENT_AGE_SECONDS, |
| 99 | destinationConfig: normalizeDestinationConfig(config.destinationConfig), |
| 100 | }; |
| 101 | |
| 102 | if ( |
| 103 | observed && |
| 104 | (observed.MaximumRetryAttempts ?? DEFAULT_MAXIMUM_RETRY_ATTEMPTS) === |
| 105 | desired.maximumRetryAttempts && |
| 106 | (observed.MaximumEventAgeInSeconds ?? DEFAULT_MAXIMUM_EVENT_AGE_SECONDS) === |
| 107 | desired.maximumEventAgeSeconds && |
| 108 | deepEqual(normalizeDestinationConfig(observed.DestinationConfig), desired.destinationConfig) |
| 109 | ) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | yield* Lambda.putFunctionEventInvokeConfig({ |
| 114 | FunctionName: functionName, |
| 115 | Qualifier: qualifier, |
| 116 | MaximumRetryAttempts: desired.maximumRetryAttempts, |
| 117 | MaximumEventAgeInSeconds: desired.maximumEventAgeSeconds, |
| 118 | DestinationConfig: desired.destinationConfig, |
| 119 | }).pipe( |
| 120 | Effect.retry({ |
| 121 | while: (e): e is Lambda.ResourceConflictException | Lambda.InvalidParameterValueException => |
| 122 | e._tag === "ResourceConflictException" || |
| 123 | // Destination validation races IAM policy propagation on the |
| 124 | // execution role — Lambda rejects the put until the role can reach |
| 125 | // the destination. |
| 126 | (e._tag === "InvalidParameterValueException" && |
| 127 | (e.message?.includes("The function execution role does not have permissions to call") ?? |
| 128 | false)), |
| 129 | schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]), |
| 130 | }), |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | /** |
| 135 | * The async invocation config of a function or alias, normalized, as |
| 136 | * observed (`undefined` when none is set). Function and Alias report it so |
| 137 | * `alchemy drift` detects out-of-band changes. |
| 138 | */ |
| 139 | export const observeEventInvokeConfig = Effect.fn(function* ( |
| 140 | functionName: string, |
| 141 | qualifier: string | undefined, |
| 142 | ) { |
| 143 | const observed = yield* observeConfig(functionName, qualifier); |
| 144 | if (observed === undefined) return undefined; |
| 145 | return { |
| 146 | maximumRetryAttempts: observed.MaximumRetryAttempts ?? DEFAULT_MAXIMUM_RETRY_ATTEMPTS, |
| 147 | maximumEventAgeSeconds: observed.MaximumEventAgeInSeconds ?? DEFAULT_MAXIMUM_EVENT_AGE_SECONDS, |
| 148 | destinationConfig: normalizeDestinationConfig(observed.DestinationConfig), |
| 149 | }; |
| 150 | }); |
| 151 | |
| 152 | /** The normalized async invocation config {@link observeEventInvokeConfig} reports. */ |
| 153 | export type ObservedEventInvokeConfig = Effect.Success<ReturnType<typeof observeEventInvokeConfig>>; |