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 appsync from "@distilled.cloud/aws/appsync"; |
| 2 | import { deepEqual, isResolved } from "alchemy/Diff"; |
| 3 | import type { Input } from "alchemy/Input"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { retryConcurrentModification, sanitizeAppSyncName } from "./common.ts"; |
| 11 | import type { AppSyncDataSource } from "./DataSource.ts"; |
| 12 | import type { GraphqlApi } from "./GraphqlApi.ts"; |
| 13 | import { APPSYNC_JS } from "./Resolver.ts"; |
| 14 | |
| 15 | export interface FunctionProps { |
| 16 | /** |
| 17 | * ID of the GraphQL API. Usually derived from `api.apiId` by the |
| 18 | * {@link Function} wrapper. |
| 19 | */ |
| 20 | apiId: string; |
| 21 | /** |
| 22 | * Name of the function (`[_A-Za-z][_0-9A-Za-z]*` — no dashes). If |
| 23 | * omitted, a deterministic name is generated (dashes sanitized to |
| 24 | * underscores). Names are mutable. |
| 25 | */ |
| 26 | name?: string; |
| 27 | /** Description of the function. */ |
| 28 | description?: string; |
| 29 | /** Name of the data source this function targets. */ |
| 30 | dataSourceName: string; |
| 31 | /** |
| 32 | * `APPSYNC_JS` function code exporting `request(ctx)` and |
| 33 | * `response(ctx)`. When set, the runtime defaults to APPSYNC_JS 1.0.0. |
| 34 | */ |
| 35 | code?: string; |
| 36 | /** VTL request mapping template (legacy alternative to `code`). */ |
| 37 | requestMappingTemplate?: string; |
| 38 | /** VTL response mapping template (legacy alternative to `code`). */ |
| 39 | responseMappingTemplate?: string; |
| 40 | /** |
| 41 | * The VTL function version — only used with mapping templates. |
| 42 | * @default "2018-05-29" (when templates are used) |
| 43 | */ |
| 44 | functionVersion?: string; |
| 45 | /** Maximum batch size for batched Lambda invocations (0–2000). */ |
| 46 | maxBatchSize?: number; |
| 47 | } |
| 48 | |
| 49 | export interface AppSyncFunction extends Resource< |
| 50 | "AWS.AppSync.Function", |
| 51 | FunctionProps, |
| 52 | { |
| 53 | /** The API this function belongs to. */ |
| 54 | apiId: string; |
| 55 | /** The immutable function ID (referenced by pipeline resolvers). */ |
| 56 | functionId: string; |
| 57 | /** The function ARN. */ |
| 58 | functionArn: string; |
| 59 | /** The function name. */ |
| 60 | name: string; |
| 61 | /** The data source this function targets. */ |
| 62 | dataSourceName: string; |
| 63 | }, |
| 64 | never, |
| 65 | Providers |
| 66 | > {} |
| 67 | |
| 68 | /** |
| 69 | * An AppSync pipeline function — a reusable step composed by `PIPELINE` |
| 70 | * resolvers. |
| 71 | * ### Creating Pipeline Functions |
| 72 | * **Example:** JavaScript pipeline function over a Lambda data source |
| 73 | * ```typescript |
| 74 | * const step = yield* AppSync.Function("InvokeStep", { |
| 75 | * api, |
| 76 | * dataSource: lambdaDS, |
| 77 | * code: ` |
| 78 | * export function request(ctx) { |
| 79 | * return { operation: "Invoke", payload: { args: ctx.args } }; |
| 80 | * } |
| 81 | * export function response(ctx) { |
| 82 | * return ctx.result; |
| 83 | * } |
| 84 | * `, |
| 85 | * }); |
| 86 | * // reference from a PIPELINE resolver: |
| 87 | * // pipelineFunctionIds: [step.functionId] |
| 88 | * ``` |
| 89 | * |
| 90 | * @resource |
| 91 | */ |
| 92 | export const FunctionResource = Resource<AppSyncFunction>("AWS.AppSync.Function"); |
| 93 | |
| 94 | export interface FunctionInputProps extends Omit< |
| 95 | { |
| 96 | [K in keyof FunctionProps]?: Input<FunctionProps[K]>; |
| 97 | }, |
| 98 | "apiId" | "dataSourceName" |
| 99 | > { |
| 100 | /** |
| 101 | * The `GraphqlApi` this function belongs to (preferred). Alternatively |
| 102 | * pass a raw `apiId`. |
| 103 | */ |
| 104 | api?: GraphqlApi; |
| 105 | apiId?: Input<string>; |
| 106 | /** |
| 107 | * The data source this function targets (preferred). Alternatively pass |
| 108 | * a raw `dataSourceName`. |
| 109 | */ |
| 110 | dataSource?: AppSyncDataSource; |
| 111 | dataSourceName?: Input<string>; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * User-facing wrapper for the pipeline Function resource. Accepts |
| 116 | * `api: GraphqlApi` and `dataSource: DataSource` directly. |
| 117 | */ |
| 118 | export const Function = (id: string, props: FunctionInputProps) => |
| 119 | Effect.gen(function* () { |
| 120 | const { api, dataSource, ...rest } = props; |
| 121 | const apiId = rest.apiId ?? api?.apiId; |
| 122 | if (!apiId) { |
| 123 | return yield* Effect.die( |
| 124 | "AppSync.Function requires either `api` (preferred) or an explicit `apiId`.", |
| 125 | ); |
| 126 | } |
| 127 | const dataSourceName = rest.dataSourceName ?? dataSource?.name; |
| 128 | if (!dataSourceName) { |
| 129 | return yield* Effect.die( |
| 130 | "AppSync.Function requires either `dataSource` (preferred) or an explicit `dataSourceName`.", |
| 131 | ); |
| 132 | } |
| 133 | return yield* FunctionResource(id, { |
| 134 | ...rest, |
| 135 | apiId, |
| 136 | dataSourceName, |
| 137 | } as any); |
| 138 | }); |
| 139 | |
| 140 | export const FunctionProvider = () => |
| 141 | Provider.effect( |
| 142 | FunctionResource, |
| 143 | Effect.gen(function* () { |
| 144 | const createName = Effect.fn(function* (id: string, props: Pick<FunctionProps, "name">) { |
| 145 | return props.name ?? sanitizeAppSyncName(yield* createPhysicalName({ id, maxLength: 64 })); |
| 146 | }); |
| 147 | |
| 148 | const getFunctionSafe = (apiId: string, functionId: string) => |
| 149 | appsync.getFunction({ apiId, functionId }).pipe( |
| 150 | Effect.map((response) => response.functionConfiguration), |
| 151 | Effect.catchTag("NotFoundException", () => Effect.undefined), |
| 152 | ); |
| 153 | |
| 154 | /** Find a function by name (fallback when no functionId is cached). */ |
| 155 | const findFunctionByName = Effect.fn(function* (apiId: string, name: string) { |
| 156 | const pages = yield* appsync.listFunctions.pages({ apiId }).pipe( |
| 157 | Stream.runCollect, |
| 158 | Effect.catchTag("NotFoundException", () => Effect.succeed([])), |
| 159 | ); |
| 160 | return Array.from(pages) |
| 161 | .flatMap((page) => page.functions ?? []) |
| 162 | .find((fn) => fn.name === name); |
| 163 | }); |
| 164 | |
| 165 | const desiredWire = (news: FunctionProps, name: string) => ({ |
| 166 | name, |
| 167 | description: news.description, |
| 168 | dataSourceName: news.dataSourceName, |
| 169 | code: news.code, |
| 170 | runtime: news.code !== undefined ? APPSYNC_JS : undefined, |
| 171 | requestMappingTemplate: news.requestMappingTemplate, |
| 172 | responseMappingTemplate: news.responseMappingTemplate, |
| 173 | functionVersion: |
| 174 | news.functionVersion ?? (news.code === undefined ? "2018-05-29" : undefined), |
| 175 | maxBatchSize: news.maxBatchSize, |
| 176 | }); |
| 177 | |
| 178 | const surface = (fn: { |
| 179 | name?: string; |
| 180 | description?: string; |
| 181 | dataSourceName?: string; |
| 182 | code?: string; |
| 183 | runtime?: appsync.AppSyncRuntime; |
| 184 | requestMappingTemplate?: string; |
| 185 | responseMappingTemplate?: string; |
| 186 | maxBatchSize?: number; |
| 187 | }) => |
| 188 | JSON.parse( |
| 189 | JSON.stringify({ |
| 190 | name: fn.name, |
| 191 | description: fn.description, |
| 192 | dataSourceName: fn.dataSourceName, |
| 193 | code: fn.code, |
| 194 | runtime: fn.runtime, |
| 195 | requestMappingTemplate: fn.requestMappingTemplate, |
| 196 | responseMappingTemplate: fn.responseMappingTemplate, |
| 197 | maxBatchSize: fn.maxBatchSize ?? 0, |
| 198 | }), |
| 199 | ); |
| 200 | |
| 201 | const toAttributes = ( |
| 202 | apiId: string, |
| 203 | fn: appsync.FunctionConfiguration, |
| 204 | ): AppSyncFunction["Attributes"] => ({ |
| 205 | apiId, |
| 206 | functionId: fn.functionId!, |
| 207 | functionArn: fn.functionArn!, |
| 208 | name: fn.name!, |
| 209 | dataSourceName: fn.dataSourceName!, |
| 210 | }); |
| 211 | |
| 212 | return FunctionResource.Provider.of({ |
| 213 | stables: ["apiId", "functionId", "functionArn"], |
| 214 | |
| 215 | // Sub-resource keyed entirely by its GraphQL API (apiId) with no global |
| 216 | // enumeration API of its own — nuke reaches it through the parent's |
| 217 | // deletion, so enumeration returns empty per the ProviderService |
| 218 | // doctrine. |
| 219 | list: () => Effect.succeed([]), |
| 220 | |
| 221 | read: Effect.fn(function* ({ id, olds, output }) { |
| 222 | const apiId = output?.apiId ?? olds?.apiId; |
| 223 | if (apiId === undefined) return undefined; |
| 224 | const fn = |
| 225 | output?.functionId !== undefined |
| 226 | ? yield* getFunctionSafe(apiId, output.functionId) |
| 227 | : yield* findFunctionByName(apiId, yield* createName(id, olds ?? {})); |
| 228 | if (fn?.functionId == null) return undefined; |
| 229 | return toAttributes(apiId, fn); |
| 230 | }), |
| 231 | |
| 232 | diff: Effect.fn(function* ({ news, olds }) { |
| 233 | if (!isResolved(news)) return undefined; |
| 234 | if (news.apiId !== olds.apiId) { |
| 235 | return { action: "replace" } as const; |
| 236 | } |
| 237 | // name/code/data source converge via updateFunction |
| 238 | }), |
| 239 | |
| 240 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 241 | const apiId = output?.apiId ?? news.apiId; |
| 242 | const name = news.name ?? (yield* createName(id, news)); |
| 243 | const desired = desiredWire(news, name); |
| 244 | |
| 245 | // 1. OBSERVE — functionId cache first, then name scan. |
| 246 | let observed = |
| 247 | output?.functionId !== undefined |
| 248 | ? yield* getFunctionSafe(apiId, output.functionId) |
| 249 | : yield* findFunctionByName(apiId, name); |
| 250 | |
| 251 | if (observed?.functionId == null) { |
| 252 | // 2. ENSURE |
| 253 | const created = yield* retryConcurrentModification( |
| 254 | appsync.createFunction({ apiId, ...desired }), |
| 255 | ); |
| 256 | observed = created.functionConfiguration!; |
| 257 | yield* session.note(`Created pipeline function ${name}`); |
| 258 | } else if (!deepEqual(surface(observed), surface(desired))) { |
| 259 | // 3. SYNC |
| 260 | const updated = yield* retryConcurrentModification( |
| 261 | appsync.updateFunction({ |
| 262 | apiId, |
| 263 | functionId: observed.functionId, |
| 264 | ...desired, |
| 265 | }), |
| 266 | ); |
| 267 | observed = updated.functionConfiguration ?? observed; |
| 268 | yield* session.note(`Updated pipeline function ${name}`); |
| 269 | } |
| 270 | |
| 271 | yield* session.note(name); |
| 272 | return toAttributes(apiId, observed); |
| 273 | }), |
| 274 | |
| 275 | delete: Effect.fn(function* ({ output }) { |
| 276 | yield* retryConcurrentModification( |
| 277 | appsync |
| 278 | .deleteFunction({ |
| 279 | apiId: output.apiId, |
| 280 | functionId: output.functionId, |
| 281 | }) |
| 282 | .pipe(Effect.catchTag("NotFoundException", () => Effect.void)), |
| 283 | ); |
| 284 | }), |
| 285 | }); |
| 286 | }), |
| 287 | ); |