@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 110 lines 4.19 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * The `detail` payload Bedrock Data Automation delivers to EventBridge when
11 * an asynchronous job changes state. Emitted only for jobs invoked with
12 * `notificationConfiguration.eventBridgeConfiguration.eventBridgeEnabled`.
13 */
14 export interface DataAutomationJobEventDetail {
15 /** The job id (the trailing segment of the invocation ARN). */
16 job_id?: string;
17 /** The job status, e.g. `SUCCESS`, `CLIENT_ERROR`, `SERVICE_ERROR`. */
18 job_status?: string;
19 /** The detected modality, e.g. `Document`, `Image`, `Audio`, `Video`. */
20 semantic_modality?: string;
21 /** The S3 input object the job processed. */
22 input_s3_object?: { s3_bucket?: string; name?: string };
23 /** The S3 location the job wrote results to. */
24 output_s3_location?: { s3_bucket?: string; name?: string };
25 /** The error message for failed jobs (empty on success). */
26 error_message?: string;
27 /** Additional event fields (the schema grows over time). */
28 [key: string]: unknown;
29 }
30
31 /** A Bedrock Data Automation EventBridge event delivered to the handler. */
32 export type DataAutomationJobEvent = EventRecord<DataAutomationJobEventDetail>;
33
34 /** Which Bedrock Data Automation job state-change events to subscribe to. */
35 export type DataAutomationJobEventKind = "created" | "succeeded" | "client-error" | "service-error";
36
37 const DETAIL_TYPES: Record<DataAutomationJobEventKind, string> = {
38 created: "Bedrock Data Automation Job Created",
39 succeeded: "Bedrock Data Automation Job Succeeded",
40 "client-error": "Bedrock Data Automation Job Failed With Client Error",
41 "service-error": "Bedrock Data Automation Job Failed With Service Error",
42 };
43
44 export interface DataAutomationJobEventSourceProps extends EventRouteProps {
45 /**
46 * Logical id for the backing EventBridge rule.
47 * @default "DataAutomationJobEvents"
48 */
49 id?: string;
50 /**
51 * Which job state-change events to subscribe to.
52 * @default all kinds
53 */
54 kinds?: readonly DataAutomationJobEventKind[];
55 }
56
57 /**
58 * Event source connecting Bedrock Data Automation job state changes to the
59 * hosting compute. Jobs started via `InvokeDataAutomationAsync` with
60 * `notificationConfiguration.eventBridgeConfiguration.eventBridgeEnabled`
61 * publish created / succeeded / failed events to the account's default
62 * EventBridge bus (source `aws.bedrock`); this subscribes the host Function
63 * to those events so it can pick up results the moment a job settles.
64 *
65 * Bedrock publishes to EventBridge directly — no additional resource is
66 * created besides the EventBridge rule targeting the host. Provide the
67 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
68 * Function effect.
69 *
70 * ### Consuming Job Events
71 * **Example:** Process Results When A Job Succeeds
72 * ```typescript
73 * import * as AWS from "@alchemy.run/aws";
74 *
75 * export default ResultsFunction.make(
76 * { main: import.meta.url },
77 * Effect.gen(function* () {
78 * yield* AWS.BedrockDataAutomation.consumeDataAutomationJobEvents(
79 * { kinds: ["succeeded", "client-error", "service-error"] },
80 * (events) =>
81 * Stream.runForEach(events, (event) =>
82 * event.detail.job_status === "SUCCESS"
83 * ? Effect.log(
84 * `job ${event.detail.job_id} wrote ${event.detail.output_s3_location?.name}`,
85 * )
86 * : Effect.log(`job failed: ${event.detail.error_message}`),
87 * ),
88 * );
89 * return {};
90 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
91 * );
92 * ```
93 */
94 export const consumeDataAutomationJobEvents = <StreamReq = never, Req = never>(
95 props: DataAutomationJobEventSourceProps,
96 process: (
97 events: Stream.Stream<DataAutomationJobEvent, never, StreamReq>,
98 ) => Effect.Effect<void, never, Req>,
99 ) =>
100 consumeBusEvents(
101 props.id ?? "DataAutomationJobEvents",
102 {
103 source: ["aws.bedrock"],
104 "detail-type": (
105 props.kinds ?? (Object.keys(DETAIL_TYPES) as DataAutomationJobEventKind[])
106 ).map((kind) => DETAIL_TYPES[kind]),
107 },
108 { description: props.description, state: props.state },
109 process,
110 );