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 bda from "@distilled.cloud/aws/bedrock-data-automation"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { readBdaTags, syncBdaTags, toBdaTagList, unredact } from "./internal.ts"; |
| 14 | |
| 15 | // Explicitly-typed pipeable repeat helper. Inlining `Effect.repeat` in a |
| 16 | // provider lifecycle op leaks its conditional return type into declaration |
| 17 | // emit and widens the provider layer for every consumer of `AWS.providers()`. |
| 18 | const repeatUntilLibraryGone = <E, R>( |
| 19 | self: Effect.Effect<bda.DataAutomationLibrary | undefined, E, R>, |
| 20 | ): Effect.Effect<bda.DataAutomationLibrary | undefined, E, R> => |
| 21 | Effect.repeat(self, { |
| 22 | schedule: Schedule.fixed("3 seconds"), |
| 23 | until: (library) => library === undefined, |
| 24 | times: 10, |
| 25 | }); |
| 26 | |
| 27 | export interface DataAutomationLibraryProps { |
| 28 | /** |
| 29 | * Name of the library. Changing the name replaces the library (the API has |
| 30 | * no rename operation). |
| 31 | * @default ${app}-${stage}-${id} |
| 32 | */ |
| 33 | libraryName?: string; |
| 34 | /** |
| 35 | * Human-readable description of the library. Mutable in place. |
| 36 | */ |
| 37 | libraryDescription?: string; |
| 38 | /** |
| 39 | * Customer-managed KMS encryption for the library. Create-only — changing |
| 40 | * the key replaces the library. When omitted, the service uses an |
| 41 | * AWS-owned key. |
| 42 | */ |
| 43 | encryptionConfiguration?: bda.EncryptionConfiguration; |
| 44 | /** |
| 45 | * Tags to apply to the library. Merged with internal Alchemy tags. |
| 46 | */ |
| 47 | tags?: Record<string, string>; |
| 48 | } |
| 49 | |
| 50 | export interface DataAutomationLibrary extends Resource< |
| 51 | "AWS.BedrockDataAutomation.DataAutomationLibrary", |
| 52 | DataAutomationLibraryProps, |
| 53 | { |
| 54 | /** |
| 55 | * The ARN of the library. |
| 56 | */ |
| 57 | libraryArn: string; |
| 58 | /** |
| 59 | * Name of the library. |
| 60 | */ |
| 61 | libraryName: string; |
| 62 | /** |
| 63 | * Current status of the library (`ACTIVE` or `DELETING`). |
| 64 | */ |
| 65 | status: string; |
| 66 | }, |
| 67 | never, |
| 68 | Providers |
| 69 | > {} |
| 70 | |
| 71 | /** |
| 72 | * An Amazon Bedrock Data Automation Library — a store of reusable entities |
| 73 | * (currently `VOCABULARY` entities: domain phrases with display forms) that |
| 74 | * data automation projects reference via their |
| 75 | * `dataAutomationLibraryConfiguration` to improve extraction accuracy. |
| 76 | * |
| 77 | * Entities are loaded into the library with ingestion jobs — see the |
| 78 | * `InvokeDataAutomationLibraryIngestionJob` binding. |
| 79 | * |
| 80 | * ### Creating Libraries |
| 81 | * **Example:** Library with a description |
| 82 | * ```typescript |
| 83 | * import * as BDA from "@alchemy.run/aws/BedrockDataAutomation"; |
| 84 | * |
| 85 | * const library = yield* BDA.DataAutomationLibrary("Vocab", { |
| 86 | * libraryDescription: "domain vocabulary for invoice extraction", |
| 87 | * }); |
| 88 | * ``` |
| 89 | * |
| 90 | * **Example:** Reference the library from a project |
| 91 | * ```typescript |
| 92 | * const project = yield* BDA.DataAutomationProject("Docs", { |
| 93 | * standardOutputConfiguration: {}, |
| 94 | * dataAutomationLibraryConfiguration: { |
| 95 | * libraries: [{ libraryArn: library.libraryArn }], |
| 96 | * }, |
| 97 | * }); |
| 98 | * ``` |
| 99 | * |
| 100 | * @resource |
| 101 | */ |
| 102 | export const DataAutomationLibrary = Resource<DataAutomationLibrary>( |
| 103 | "AWS.BedrockDataAutomation.DataAutomationLibrary", |
| 104 | ); |
| 105 | |
| 106 | /** |
| 107 | * Raised when a Data Automation library cannot be observed immediately after |
| 108 | * a successful create — a race with a concurrent delete. |
| 109 | */ |
| 110 | export class DataAutomationLibraryNotObservable extends Data.TaggedError( |
| 111 | "DataAutomationLibraryNotObservable", |
| 112 | )<{ libraryName: string; message: string }> {} |
| 113 | |
| 114 | export const DataAutomationLibraryProvider = () => |
| 115 | Provider.effect( |
| 116 | DataAutomationLibrary, |
| 117 | Effect.gen(function* () { |
| 118 | const createName = Effect.fn(function* ( |
| 119 | id: string, |
| 120 | props: Pick<DataAutomationLibraryProps, "libraryName">, |
| 121 | ) { |
| 122 | return props.libraryName ?? (yield* createPhysicalName({ id, maxLength: 128 })); |
| 123 | }); |
| 124 | |
| 125 | const toAttributes = (library: bda.DataAutomationLibrary) => ({ |
| 126 | libraryArn: library.libraryArn, |
| 127 | libraryName: unredact(library.libraryName), |
| 128 | status: library.status as string, |
| 129 | }); |
| 130 | |
| 131 | const observeLibrary = Effect.fn(function* (libraryArn: string) { |
| 132 | return yield* bda.getDataAutomationLibrary({ libraryArn }).pipe( |
| 133 | Effect.map((r) => r.library), |
| 134 | Effect.catchTag("ResourceNotFoundException", () => Effect.undefined), |
| 135 | ); |
| 136 | }); |
| 137 | |
| 138 | const findLibraryArn = Effect.fn(function* (name: string) { |
| 139 | const summaries = yield* bda.listDataAutomationLibraries.items({}).pipe(Stream.runCollect); |
| 140 | return Array.from(summaries).find( |
| 141 | (s) => s.libraryName !== undefined && unredact(s.libraryName) === name, |
| 142 | )?.libraryArn; |
| 143 | }); |
| 144 | |
| 145 | return DataAutomationLibrary.Provider.of({ |
| 146 | stables: ["libraryArn", "libraryName"], |
| 147 | |
| 148 | list: () => |
| 149 | Effect.gen(function* () { |
| 150 | const summaries = yield* bda.listDataAutomationLibraries |
| 151 | .items({}) |
| 152 | .pipe(Stream.runCollect); |
| 153 | const attrs = yield* Effect.forEach( |
| 154 | Array.from(summaries), |
| 155 | (s) => |
| 156 | observeLibrary(s.libraryArn).pipe( |
| 157 | Effect.map((library) => (library === undefined ? [] : [toAttributes(library)])), |
| 158 | ), |
| 159 | { concurrency: 5 }, |
| 160 | ); |
| 161 | return attrs.flat(); |
| 162 | }), |
| 163 | |
| 164 | read: Effect.fn(function* ({ id, olds, output }) { |
| 165 | const libraryArn = |
| 166 | output?.libraryArn ?? (yield* findLibraryArn(yield* createName(id, olds ?? {}))); |
| 167 | if (libraryArn === undefined) return undefined; |
| 168 | const found = yield* observeLibrary(libraryArn); |
| 169 | if (found === undefined) return undefined; |
| 170 | const attrs = toAttributes(found); |
| 171 | const tags = yield* readBdaTags(libraryArn); |
| 172 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 173 | }), |
| 174 | |
| 175 | diff: Effect.fn(function* ({ id, news, olds }) { |
| 176 | if (!isResolved(news)) return undefined; |
| 177 | const oldName = yield* createName(id, olds ?? {}); |
| 178 | const newName = yield* createName(id, news ?? {}); |
| 179 | if ( |
| 180 | oldName !== newName || |
| 181 | // updateDataAutomationLibrary only takes a description, so a KMS |
| 182 | // key change can only be honored by replacement. |
| 183 | olds?.encryptionConfiguration?.kmsKeyId !== news.encryptionConfiguration?.kmsKeyId |
| 184 | ) { |
| 185 | return { action: "replace" } as const; |
| 186 | } |
| 187 | // fall through: engine default update logic for mutable fields |
| 188 | }), |
| 189 | |
| 190 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 191 | const libraryName = output?.libraryName ?? (yield* createName(id, news)); |
| 192 | const internalTags = yield* createInternalTags(id); |
| 193 | const desiredTags = { ...news.tags, ...internalTags }; |
| 194 | |
| 195 | // 1. OBSERVE — cloud state is authoritative; output caches the ARN. |
| 196 | const cachedArn = output?.libraryArn ?? (yield* findLibraryArn(libraryName)); |
| 197 | let live = cachedArn === undefined ? undefined : yield* observeLibrary(cachedArn); |
| 198 | |
| 199 | // 2. ENSURE — create when missing; a concurrent create surfaces as |
| 200 | // the typed ConflictException, which we treat as a race and |
| 201 | // re-observe by name. Create returns only arn + status, so |
| 202 | // re-observe for the full shape. |
| 203 | if (live === undefined) { |
| 204 | const createdArn = yield* bda |
| 205 | .createDataAutomationLibrary({ |
| 206 | libraryName, |
| 207 | libraryDescription: news.libraryDescription, |
| 208 | encryptionConfiguration: news.encryptionConfiguration, |
| 209 | tags: toBdaTagList(desiredTags), |
| 210 | }) |
| 211 | .pipe( |
| 212 | Effect.map((r) => r.libraryArn), |
| 213 | Effect.catchTag("ConflictException", (conflict) => |
| 214 | Effect.gen(function* () { |
| 215 | const arn = yield* findLibraryArn(libraryName); |
| 216 | return arn === undefined ? yield* conflict : arn; |
| 217 | }), |
| 218 | ), |
| 219 | ); |
| 220 | live = createdArn === undefined ? undefined : yield* observeLibrary(createdArn); |
| 221 | } |
| 222 | if (live === undefined) { |
| 223 | return yield* new DataAutomationLibraryNotObservable({ |
| 224 | libraryName, |
| 225 | message: `Data Automation library '${libraryName}' was not observable after create`, |
| 226 | }); |
| 227 | } |
| 228 | |
| 229 | // 3. SYNC — diff the OBSERVED description against the desired one; |
| 230 | // apply the idempotent update only on drift. |
| 231 | if (unredact(live.libraryDescription ?? "") !== (news.libraryDescription ?? "")) { |
| 232 | yield* bda.updateDataAutomationLibrary({ |
| 233 | libraryArn: live.libraryArn, |
| 234 | libraryDescription: news.libraryDescription ?? "", |
| 235 | }); |
| 236 | live = (yield* observeLibrary(live.libraryArn)) ?? live; |
| 237 | } |
| 238 | |
| 239 | // 3b. SYNC TAGS against observed cloud tags. |
| 240 | yield* syncBdaTags(live.libraryArn, desiredTags); |
| 241 | |
| 242 | yield* session.note(libraryName); |
| 243 | return toAttributes(live); |
| 244 | }), |
| 245 | |
| 246 | delete: Effect.fn(function* ({ output }) { |
| 247 | yield* bda |
| 248 | .deleteDataAutomationLibrary({ libraryArn: output.libraryArn }) |
| 249 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 250 | // Deletion settles asynchronously (status ACTIVE → DELETING → gone) |
| 251 | // and library names are unique, so wait for the terminal |
| 252 | // disappearance — otherwise an immediate same-name re-create |
| 253 | // conflicts with the vanishing library. Bounded: 10 × 3s ≈ 30s. |
| 254 | yield* observeLibrary(output.libraryArn).pipe(repeatUntilLibraryGone); |
| 255 | }), |
| 256 | }); |
| 257 | }), |
| 258 | ); |