@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 357 lines 13.5 KB
Raw
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 { bdaConfigEquals, 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 repeatUntilProjectSettled = <E, R>(
19 self: Effect.Effect<bda.DataAutomationProject | undefined, E, R>,
20 ): Effect.Effect<bda.DataAutomationProject | undefined, E, R> =>
21 Effect.repeat(self, {
22 schedule: Schedule.fixed("3 seconds"),
23 until: (project) => project === undefined || project.status !== "IN_PROGRESS",
24 times: 25,
25 });
26
27 export interface DataAutomationProjectProps {
28 /**
29 * Name of the project. Must match `[a-zA-Z0-9-_]+` and be at most 128
30 * characters. Changing the name replaces the project.
31 * @default ${app}-${stage}-${id}
32 */
33 projectName?: string;
34 /**
35 * Human-readable description of the project.
36 */
37 projectDescription?: string;
38 /**
39 * The stage of the project: `DEVELOPMENT` or `LIVE`. Stages are distinct
40 * addressable copies, so changing the stage replaces the project.
41 * @default LIVE
42 */
43 projectStage?: bda.DataAutomationProjectStage;
44 /**
45 * Whether the project processes files asynchronously (`ASYNC`) or
46 * synchronously (`SYNC`). Immutable — changing it replaces the project.
47 * @default ASYNC
48 */
49 projectType?: bda.DataAutomationProjectType;
50 /**
51 * The standard (default) output the project produces per modality —
52 * document, image, video, and audio extraction settings. Pass `{}` to use
53 * the service defaults.
54 */
55 standardOutputConfiguration: bda.StandardOutputConfiguration;
56 /**
57 * Custom output configuration — the list of `Blueprint`s the project
58 * applies to matched files.
59 */
60 customOutputConfiguration?: bda.CustomOutputConfiguration;
61 /**
62 * Overrides for the project (e.g. document splitter and modality routing).
63 */
64 overrideConfiguration?: bda.OverrideConfiguration;
65 /**
66 * Data automation library (entity/vocabulary) configuration.
67 */
68 dataAutomationLibraryConfiguration?: bda.DataAutomationLibraryConfiguration;
69 /**
70 * Customer-managed KMS encryption for the project. When omitted, the
71 * service uses an AWS-owned key.
72 */
73 encryptionConfiguration?: bda.EncryptionConfiguration;
74 /**
75 * Tags to apply to the project. Merged with internal Alchemy tags.
76 */
77 tags?: Record<string, string>;
78 }
79
80 export interface DataAutomationProject extends Resource<
81 "AWS.BedrockDataAutomation.DataAutomationProject",
82 DataAutomationProjectProps,
83 {
84 /**
85 * The ARN of the project.
86 */
87 projectArn: string;
88 /**
89 * Name of the project.
90 */
91 projectName: string;
92 /**
93 * The stage (`DEVELOPMENT` or `LIVE`) of the project.
94 */
95 projectStage: string;
96 /**
97 * Current status of the project (e.g. `COMPLETED`).
98 */
99 status: string;
100 },
101 never,
102 Providers
103 > {}
104
105 /**
106 * An Amazon Bedrock Data Automation Project — the configuration unit that
107 * turns documents, images, audio, and video into structured output, with
108 * optional custom output driven by `Blueprint`s.
109 *
110 * ### Creating Projects
111 * **Example:** Project with default standard output
112 * ```typescript
113 * import * as BDA from "@alchemy.run/aws/BedrockDataAutomation";
114 *
115 * const project = yield* BDA.DataAutomationProject("Docs", {
116 * standardOutputConfiguration: {},
117 * });
118 * ```
119 *
120 * **Example:** Document project with granular extraction
121 * ```typescript
122 * const project = yield* BDA.DataAutomationProject("Docs", {
123 * projectDescription: "invoice pipeline",
124 * standardOutputConfiguration: {
125 * document: {
126 * extraction: {
127 * granularity: { types: ["DOCUMENT", "PAGE"] },
128 * boundingBox: { state: "DISABLED" },
129 * },
130 * generativeField: { state: "DISABLED" },
131 * outputFormat: {
132 * textFormat: { types: ["MARKDOWN"] },
133 * additionalFileFormat: { state: "DISABLED" },
134 * },
135 * },
136 * },
137 * });
138 * ```
139 *
140 * ### Custom Output
141 * **Example:** Attach blueprints for custom output
142 * ```typescript
143 * const blueprint = yield* BDA.Blueprint("InvoiceBlueprint", {
144 * type: "DOCUMENT",
145 * schema: invoiceSchemaJson,
146 * });
147 *
148 * const project = yield* BDA.DataAutomationProject("Docs", {
149 * standardOutputConfiguration: {},
150 * customOutputConfiguration: {
151 * blueprints: [{ blueprintArn: blueprint.blueprintArn }],
152 * },
153 * });
154 * ```
155 *
156 * @resource
157 */
158 export const DataAutomationProject = Resource<DataAutomationProject>(
159 "AWS.BedrockDataAutomation.DataAutomationProject",
160 );
161
162 /**
163 * Raised when a Data Automation project reaches the terminal `FAILED` status
164 * during provisioning or update.
165 */
166 export class DataAutomationProjectFailed extends Data.TaggedError("DataAutomationProjectFailed")<{
167 projectArn: string;
168 message: string;
169 }> {}
170
171 export const DataAutomationProjectProvider = () =>
172 Provider.effect(
173 DataAutomationProject,
174 Effect.gen(function* () {
175 const createName = Effect.fn(function* (
176 id: string,
177 props: Pick<DataAutomationProjectProps, "projectName">,
178 ) {
179 return props.projectName ?? (yield* createPhysicalName({ id, maxLength: 128 }));
180 });
181
182 const toAttributes = (project: bda.DataAutomationProject) => ({
183 projectArn: project.projectArn,
184 projectName: unredact(project.projectName),
185 projectStage: project.projectStage ?? "LIVE",
186 status: project.status as string,
187 });
188
189 const observeProject = Effect.fn(function* (
190 projectArn: string,
191 projectStage?: bda.DataAutomationProjectStage,
192 ) {
193 return yield* bda.getDataAutomationProject({ projectArn, projectStage }).pipe(
194 Effect.map((r) => r.project),
195 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
196 );
197 });
198
199 const findProjectArn = Effect.fn(function* (name: string) {
200 const summaries = yield* bda.listDataAutomationProjects
201 .items({ resourceOwner: "ACCOUNT" })
202 .pipe(Stream.runCollect);
203 return Array.from(summaries).find(
204 (s) => s.projectName !== undefined && unredact(s.projectName) === name,
205 )?.projectArn;
206 });
207
208 // Project create/update settle asynchronously (status IN_PROGRESS →
209 // COMPLETED | FAILED). Bounded poll: 25 × 3s ≈ 75s worst case.
210 const waitForSettled = Effect.fn(function* (
211 projectArn: string,
212 projectStage?: bda.DataAutomationProjectStage,
213 ) {
214 const settled = yield* observeProject(projectArn, projectStage).pipe(
215 repeatUntilProjectSettled,
216 );
217 if (settled !== undefined && settled.status === "FAILED") {
218 return yield* new DataAutomationProjectFailed({
219 projectArn,
220 message: `Data Automation project '${projectArn}' reached status FAILED`,
221 });
222 }
223 return settled;
224 });
225
226 return DataAutomationProject.Provider.of({
227 stables: ["projectArn", "projectName"],
228
229 list: () =>
230 Effect.gen(function* () {
231 const summaries = yield* bda.listDataAutomationProjects
232 .items({ resourceOwner: "ACCOUNT" })
233 .pipe(Stream.runCollect);
234 const attrs = yield* Effect.forEach(
235 Array.from(summaries),
236 (s) =>
237 observeProject(s.projectArn, s.projectStage).pipe(
238 Effect.map((project) => (project === undefined ? [] : [toAttributes(project)])),
239 ),
240 { concurrency: 5 },
241 );
242 return attrs.flat();
243 }),
244
245 read: Effect.fn(function* ({ id, olds, output }) {
246 const projectArn =
247 output?.projectArn ?? (yield* findProjectArn(yield* createName(id, olds ?? {})));
248 if (projectArn === undefined) return undefined;
249 const found = yield* observeProject(projectArn, olds?.projectStage);
250 if (found === undefined) return undefined;
251 const attrs = toAttributes(found);
252 const tags = yield* readBdaTags(projectArn);
253 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
254 }),
255
256 diff: Effect.fn(function* ({ id, news, olds }) {
257 if (!isResolved(news)) return undefined;
258 const oldName = yield* createName(id, olds ?? {});
259 const newName = yield* createName(id, news ?? {});
260 if (
261 oldName !== newName ||
262 olds?.projectStage !== news.projectStage ||
263 olds?.projectType !== news.projectType
264 ) {
265 return { action: "replace" } as const;
266 }
267 // fall through: engine default update logic for mutable fields
268 }),
269
270 reconcile: Effect.fn(function* ({ id, news, output, session }) {
271 const projectName = output?.projectName ?? (yield* createName(id, news));
272 const internalTags = yield* createInternalTags(id);
273 const desiredTags = { ...news.tags, ...internalTags };
274
275 // 1. OBSERVE — cloud state is authoritative; output caches the ARN.
276 const cachedArn = output?.projectArn ?? (yield* findProjectArn(projectName));
277 let live =
278 cachedArn === undefined
279 ? undefined
280 : yield* observeProject(cachedArn, news.projectStage);
281
282 // 2. ENSURE — create when missing; a concurrent create surfaces as
283 // the typed ConflictException, which we treat as a race and
284 // re-observe by name. Wait for the async provisioning to settle.
285 if (live === undefined) {
286 const projectArn = yield* bda
287 .createDataAutomationProject({
288 projectName,
289 projectDescription: news.projectDescription,
290 projectStage: news.projectStage,
291 projectType: news.projectType,
292 standardOutputConfiguration: news.standardOutputConfiguration,
293 customOutputConfiguration: news.customOutputConfiguration,
294 overrideConfiguration: news.overrideConfiguration,
295 dataAutomationLibraryConfiguration: news.dataAutomationLibraryConfiguration,
296 encryptionConfiguration: news.encryptionConfiguration,
297 tags: toBdaTagList(desiredTags),
298 })
299 .pipe(
300 Effect.map((r) => r.projectArn),
301 Effect.catchTag("ConflictException", (conflict) =>
302 Effect.gen(function* () {
303 const arn = yield* findProjectArn(projectName);
304 return arn === undefined ? yield* conflict : arn;
305 }),
306 ),
307 );
308 live = yield* waitForSettled(projectArn, news.projectStage);
309 }
310 if (live === undefined) {
311 return yield* new DataAutomationProjectFailed({
312 projectArn: cachedArn ?? projectName,
313 message: `Data Automation project '${projectName}' was not observable after create`,
314 });
315 }
316
317 // 3. SYNC — diff OBSERVED configuration against desired; apply the
318 // idempotent PUT only on drift. The server may fill defaulted
319 // fields, in which case the PUT re-applies harmlessly.
320 const drift =
321 unredact(live.projectDescription ?? "") !== (news.projectDescription ?? "") ||
322 !bdaConfigEquals(live.standardOutputConfiguration, news.standardOutputConfiguration) ||
323 (news.customOutputConfiguration !== undefined &&
324 !bdaConfigEquals(live.customOutputConfiguration, news.customOutputConfiguration)) ||
325 (news.overrideConfiguration !== undefined &&
326 !bdaConfigEquals(live.overrideConfiguration, news.overrideConfiguration)) ||
327 (news.encryptionConfiguration !== undefined &&
328 live.kmsKeyId !== news.encryptionConfiguration.kmsKeyId);
329 if (drift) {
330 yield* bda.updateDataAutomationProject({
331 projectArn: live.projectArn,
332 projectStage: news.projectStage,
333 projectDescription: news.projectDescription,
334 standardOutputConfiguration: news.standardOutputConfiguration,
335 customOutputConfiguration: news.customOutputConfiguration,
336 overrideConfiguration: news.overrideConfiguration,
337 dataAutomationLibraryConfiguration: news.dataAutomationLibraryConfiguration,
338 encryptionConfiguration: news.encryptionConfiguration,
339 });
340 live = (yield* waitForSettled(live.projectArn, news.projectStage)) ?? live;
341 }
342
343 // 3b. SYNC TAGS against observed cloud tags.
344 yield* syncBdaTags(live.projectArn, desiredTags);
345
346 yield* session.note(projectName);
347 return toAttributes(live);
348 }),
349
350 delete: Effect.fn(function* ({ output }) {
351 yield* bda
352 .deleteDataAutomationProject({ projectArn: output.projectArn })
353 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
354 }),
355 });
356 }),
357 );