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 dax from "@distilled.cloud/aws/dax"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export class ParameterGroupUnexpectedResponse extends Data.TaggedError( |
| 12 | "ParameterGroupUnexpectedResponse", |
| 13 | )<{ |
| 14 | readonly message: string; |
| 15 | readonly cause?: unknown; |
| 16 | }> {} |
| 17 | |
| 18 | export interface ParameterGroupProps { |
| 19 | /** |
| 20 | * Name of the parameter group. If omitted, a deterministic physical name |
| 21 | * is generated. Changing the name replaces the parameter group. |
| 22 | */ |
| 23 | parameterGroupName?: string; |
| 24 | /** |
| 25 | * Human-readable description of the parameter group. |
| 26 | */ |
| 27 | description?: string; |
| 28 | /** |
| 29 | * Parameter overrides, e.g. `{ "query-ttl-millis": "60000" }`. DAX exposes |
| 30 | * two tunable parameters: `query-ttl-millis` and `record-ttl-millis`. |
| 31 | * DAX has no reset-to-default API, so a key removed from this map keeps |
| 32 | * its last applied value. |
| 33 | */ |
| 34 | parameters?: Record<string, string>; |
| 35 | } |
| 36 | |
| 37 | export interface ParameterGroup extends Resource< |
| 38 | "AWS.DAX.ParameterGroup", |
| 39 | ParameterGroupProps, |
| 40 | { |
| 41 | /** Name of the parameter group. */ |
| 42 | parameterGroupName: string; |
| 43 | /** Description of the parameter group. */ |
| 44 | description: string | undefined; |
| 45 | /** Current non-default parameter values, keyed by parameter name. */ |
| 46 | parameters: Record<string, string>; |
| 47 | }, |
| 48 | never, |
| 49 | Providers |
| 50 | > {} |
| 51 | |
| 52 | /** |
| 53 | * A DAX parameter group — a named set of DAX engine parameters (item and |
| 54 | * query cache TTLs) that can be attached to one or more DAX |
| 55 | * {@link Cluster}s. |
| 56 | * |
| 57 | * Parameter groups are free and provision instantly. DAX does not support |
| 58 | * tags on parameter groups. |
| 59 | * ### Creating a Parameter Group |
| 60 | * **Example:** Parameter Group with Custom Cache TTLs |
| 61 | * ```typescript |
| 62 | * const params = yield* ParameterGroup("DaxParams", { |
| 63 | * description: "5 minute item and query TTLs", |
| 64 | * parameters: { |
| 65 | * "query-ttl-millis": "300000", |
| 66 | * "record-ttl-millis": "300000", |
| 67 | * }, |
| 68 | * }); |
| 69 | * ``` |
| 70 | * |
| 71 | * ### Attaching to a Cluster |
| 72 | * **Example:** Cluster Using the Parameter Group |
| 73 | * ```typescript |
| 74 | * const cluster = yield* Cluster("Cache", { |
| 75 | * nodeType: "dax.t3.small", |
| 76 | * replicationFactor: 1, |
| 77 | * iamRoleArn: role.roleArn, |
| 78 | * parameterGroupName: params.parameterGroupName, |
| 79 | * }); |
| 80 | * ``` |
| 81 | * |
| 82 | * @resource |
| 83 | */ |
| 84 | export const ParameterGroup = Resource<ParameterGroup>("AWS.DAX.ParameterGroup"); |
| 85 | |
| 86 | export const ParameterGroupProvider = () => |
| 87 | Provider.effect( |
| 88 | ParameterGroup, |
| 89 | Effect.gen(function* () { |
| 90 | const toName = (id: string, props: ParameterGroupProps) => |
| 91 | props.parameterGroupName |
| 92 | ? Effect.succeed(props.parameterGroupName) |
| 93 | : createPhysicalName({ id, maxLength: 255, lowercase: true }); |
| 94 | |
| 95 | const readGroup = Effect.fn(function* (name: string) { |
| 96 | const response = yield* dax |
| 97 | .describeParameterGroups({ ParameterGroupNames: [name] }) |
| 98 | .pipe(Effect.catchTag("ParameterGroupNotFoundFault", () => Effect.undefined)); |
| 99 | return response?.ParameterGroups?.[0]; |
| 100 | }); |
| 101 | |
| 102 | // Observed parameter values for the group (bounded pagination — DAX |
| 103 | // has no paginated distilled ops). |
| 104 | const readParameters = Effect.fn(function* (name: string) { |
| 105 | const values: Record<string, string> = {}; |
| 106 | let nextToken: string | undefined; |
| 107 | for (let page = 0; page < 20; page++) { |
| 108 | const response = yield* dax.describeParameters({ |
| 109 | ParameterGroupName: name, |
| 110 | NextToken: nextToken, |
| 111 | }); |
| 112 | for (const parameter of response.Parameters ?? []) { |
| 113 | if (parameter.ParameterName !== undefined && parameter.ParameterValue !== undefined) { |
| 114 | values[parameter.ParameterName] = parameter.ParameterValue; |
| 115 | } |
| 116 | } |
| 117 | nextToken = response.NextToken; |
| 118 | if (!nextToken) break; |
| 119 | } |
| 120 | return values; |
| 121 | }); |
| 122 | |
| 123 | const toAttrs = ( |
| 124 | group: dax.ParameterGroup, |
| 125 | parameters: Record<string, string>, |
| 126 | overrides: Record<string, string> | undefined, |
| 127 | ) => ({ |
| 128 | parameterGroupName: group.ParameterGroupName ?? "", |
| 129 | description: group.Description, |
| 130 | // Report only the user's overridden keys with their observed values, |
| 131 | // not the entire engine parameter list. |
| 132 | parameters: Object.fromEntries( |
| 133 | Object.keys(overrides ?? {}).map((key) => [key, parameters[key] ?? ""]), |
| 134 | ), |
| 135 | }); |
| 136 | |
| 137 | return { |
| 138 | stables: ["parameterGroupName"], |
| 139 | |
| 140 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 141 | if (!isResolved(news)) return undefined; |
| 142 | if ((yield* toName(id, olds ?? {})) !== (yield* toName(id, news ?? {}))) { |
| 143 | return { action: "replace" } as const; |
| 144 | } |
| 145 | }), |
| 146 | |
| 147 | read: Effect.fn(function* ({ id, olds, output }) { |
| 148 | const name = output?.parameterGroupName ?? (yield* toName(id, olds ?? {})); |
| 149 | const group = yield* readGroup(name); |
| 150 | if (group?.ParameterGroupName === undefined) return undefined; |
| 151 | const parameters = yield* readParameters(name); |
| 152 | // DAX parameter groups do not support tags, so ownership cannot be |
| 153 | // verified — return the attributes directly. |
| 154 | return toAttrs(group, parameters, olds?.parameters); |
| 155 | }), |
| 156 | |
| 157 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 158 | const props = news ?? {}; |
| 159 | const name = output?.parameterGroupName ?? (yield* toName(id, props)); |
| 160 | |
| 161 | // 1. Observe — cloud state is authoritative. |
| 162 | let observed = yield* readGroup(name); |
| 163 | |
| 164 | // 2. Ensure — create if missing; tolerate AlreadyExists as a race. |
| 165 | if (observed === undefined) { |
| 166 | yield* dax |
| 167 | .createParameterGroup({ |
| 168 | ParameterGroupName: name, |
| 169 | Description: props.description, |
| 170 | }) |
| 171 | .pipe(Effect.catchTag("ParameterGroupAlreadyExistsFault", () => Effect.void)); |
| 172 | observed = yield* readGroup(name); |
| 173 | } |
| 174 | if (observed === undefined) { |
| 175 | return yield* new ParameterGroupUnexpectedResponse({ |
| 176 | message: `DAX parameter group '${name}' not found after create`, |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | // 3. Sync parameters — diff desired overrides against OBSERVED |
| 181 | // values and apply only the delta. (Description is create-only in |
| 182 | // the DAX API — UpdateParameterGroup accepts only parameter |
| 183 | // values.) |
| 184 | const observedParameters = yield* readParameters(name); |
| 185 | const delta = Object.entries(props.parameters ?? {}).filter( |
| 186 | ([key, value]) => observedParameters[key] !== value, |
| 187 | ); |
| 188 | if (delta.length > 0) { |
| 189 | yield* dax.updateParameterGroup({ |
| 190 | ParameterGroupName: name, |
| 191 | ParameterNameValues: delta.map(([ParameterName, ParameterValue]) => ({ |
| 192 | ParameterName, |
| 193 | ParameterValue, |
| 194 | })), |
| 195 | }); |
| 196 | for (const [key, value] of delta) { |
| 197 | observedParameters[key] = value; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | yield* session.note(name); |
| 202 | return toAttrs(observed, observedParameters, props.parameters); |
| 203 | }), |
| 204 | |
| 205 | delete: Effect.fn(function* ({ output }) { |
| 206 | // A parameter group still attached to a cluster rejects deletion |
| 207 | // with InvalidParameterGroupStateFault — retry (bounded) while the |
| 208 | // cluster releases it. NotFound is success (idempotent delete). |
| 209 | yield* dax |
| 210 | .deleteParameterGroup({ |
| 211 | ParameterGroupName: output.parameterGroupName, |
| 212 | }) |
| 213 | .pipe( |
| 214 | Effect.catchTag("ParameterGroupNotFoundFault", () => Effect.void), |
| 215 | Effect.retry({ |
| 216 | while: (e) => e._tag === "InvalidParameterGroupStateFault", |
| 217 | schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(10)]), |
| 218 | }), |
| 219 | ); |
| 220 | }), |
| 221 | |
| 222 | list: () => |
| 223 | Effect.gen(function* () { |
| 224 | // Bounded hand-rolled pagination (no distilled paginator). |
| 225 | const groups: dax.ParameterGroup[] = []; |
| 226 | let nextToken: string | undefined; |
| 227 | for (let page = 0; page < 20; page++) { |
| 228 | const response = yield* dax.describeParameterGroups({ |
| 229 | NextToken: nextToken, |
| 230 | }); |
| 231 | groups.push(...(response.ParameterGroups ?? [])); |
| 232 | nextToken = response.NextToken; |
| 233 | if (!nextToken) break; |
| 234 | } |
| 235 | return groups |
| 236 | .filter( |
| 237 | (group) => |
| 238 | group.ParameterGroupName !== undefined && |
| 239 | // AWS-managed default parameter groups (`default.dax1.0`, |
| 240 | // …) always exist and can never be deleted — keep them out |
| 241 | // of enumeration for account-wide teardown (nuke). |
| 242 | !group.ParameterGroupName.startsWith("default."), |
| 243 | ) |
| 244 | .map((group) => toAttrs(group, {}, undefined)); |
| 245 | }), |
| 246 | }; |
| 247 | }), |
| 248 | ); |