The best way to control Claude Code, Codex and more from any computer or phone

main
ts 236 lines 8.44 KB
Raw
1 import * 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 /** S3 object attributes attached to B2BI EventBridge event details. */
10 export interface TransformationFileS3Attributes {
11 /** The bucket containing the object. */
12 bucket?: string;
13 /** The object's key. */
14 "object-key"?: string;
15 /** The object's size in bytes. */
16 "object-size-bytes"?: number;
17 }
18
19 /**
20 * The `detail` payload B2BI delivers to EventBridge when a transformation
21 * finishes (`Transformation Completed` / `Transformation Failed`). Split
22 * transformations additionally carry `split-attributes`.
23 */
24 export interface TransformationEventDetail {
25 /** The system-generated identifier of the transformer run. */
26 "transformer-job-id"?: string;
27 /** The system-generated identifier of the trading partner. */
28 "trading-partner-id"?: string;
29 /** When the transformation began processing. */
30 "start-timestamp"?: string;
31 /** When the transformation finished processing. */
32 "end-timestamp"?: string;
33 /** The X12 transaction set of the document, e.g. `X12_850`. */
34 "x12-transaction-set"?: string;
35 /** The X12 version of the document, e.g. `VERSION_4010`. */
36 "x12-version"?: string;
37 /** Location and size of the input file. */
38 "input-file-s3-attributes"?: TransformationFileS3Attributes;
39 /** Location and size of the output file. */
40 "output-file-s3-attributes"?: TransformationFileS3Attributes;
41 /** For failed transformations, why the transformation failed. */
42 "failure-message"?: string;
43 /** For failed transformations, the failure reason code. */
44 "failure-code"?: string;
45 /**
46 * Status of acknowledgement generation (`NOT_ATTEMPTED`, `COMPLETED`, or
47 * `FAILED`); only populated when the transformation generates one.
48 */
49 "ack-generation-status"?: string;
50 /** Whether an error code was detected while generating the acknowledgement. */
51 "ack-error-code-detected"?: boolean;
52 /** Source format (`JSON` or `XML`); outbound transformations only. */
53 "input-format"?: string;
54 /** Output format (`X12`); outbound transformations only. */
55 "output-format"?: string;
56 /** Validation outcome: `SUCCEEDED`, `FAILED`, or `NOT_ATTEMPTED`. */
57 "validation-status"?: string;
58 /** Where the validation report is stored in S3. */
59 "validation-report-s3-location"?: {
60 bucket?: string;
61 "object-key"?: string;
62 };
63 /** Split-file details; only present for split transformations. */
64 "split-attributes"?: {
65 /** 1-indexed number of this split. */
66 "split-number"?: number;
67 /** Total number of splits. */
68 "total-split-count"?: number;
69 /** Whether this split is valid per the X12 standard. */
70 "split-is-valid"?: boolean;
71 };
72 }
73
74 /**
75 * The `detail` payload B2BI delivers to EventBridge when acknowledgement
76 * generation finishes (`Acknowledgement Completed` / `Acknowledgement
77 * Failed`).
78 */
79 export interface AcknowledgementEventDetail {
80 /** The system-generated identifier of the transformer run. */
81 "transformer-job-id"?: string;
82 /** The system-generated identifier of the trading partner. */
83 "trading-partner-id"?: string;
84 /** When the acknowledgement began processing. */
85 "start-timestamp"?: string;
86 /** When the acknowledgement finished processing. */
87 "end-timestamp"?: string;
88 /** The X12 transaction set of the input file. */
89 "input-x12-transaction-set"?: string;
90 /** The X12 version of the input file. */
91 "input-x12-version"?: string;
92 /** Location and size of the input file. */
93 "input-file-s3-attributes"?: TransformationFileS3Attributes;
94 /** X12 type of the acknowledgement, e.g. `997`. */
95 "ack-x12-type"?: string;
96 /** X12 version of the acknowledgement. */
97 "ack-x12-version"?: string;
98 /** Location and size of the acknowledgement file (completed events only). */
99 "ack-file-s3-attributes"?: TransformationFileS3Attributes;
100 /** Whether an error code was detected (completed events only). */
101 "ack-error-code-detected"?: boolean;
102 /** For failed acknowledgements, why the acknowledgement failed. */
103 "failure-message"?: string;
104 /** For failed acknowledgements, the failure reason code. */
105 "failure-code"?: string;
106 }
107
108 /** A transformation EventBridge event delivered to the handler. */
109 export type TransformationEvent = EventRecord<TransformationEventDetail>;
110
111 /** An acknowledgement EventBridge event delivered to the handler. */
112 export type AcknowledgementEvent = EventRecord<AcknowledgementEventDetail>;
113
114 export type TransformationEventType = "Transformation Completed" | "Transformation Failed";
115
116 export type AcknowledgementEventType = "Acknowledgement Completed" | "Acknowledgement Failed";
117
118 export interface TransformationEventSourceProps extends EventRouteProps {
119 /**
120 * Logical id prefix for the backing EventBridge rule.
121 * @default "B2BI"
122 */
123 id?: string;
124 /**
125 * Only deliver these event types.
126 * @default both `Transformation Completed` and `Transformation Failed`
127 */
128 events?: TransformationEventType[];
129 }
130
131 export interface AcknowledgementEventSourceProps extends EventRouteProps {
132 /**
133 * Logical id prefix for the backing EventBridge rule.
134 * @default "B2BI"
135 */
136 id?: string;
137 /**
138 * Only deliver these event types.
139 * @default both `Acknowledgement Completed` and `Acknowledgement Failed`
140 */
141 events?: AcknowledgementEventType[];
142 }
143
144 /**
145 * Deliver B2BI transformation results (`aws.b2bi` / `"Transformation
146 * Completed"` and `"Transformation Failed"` EventBridge events) to the host
147 * Function — B2BI emits one for every transformer run, whether started by a
148 * capability's S3 trigger or {@link StartTransformerJob}.
149 *
150 * The EventBridge pattern matches every transformation in the account and
151 * region (the pattern must be literal — `Output` values do not resolve
152 * inside the deployed bundle); inspect `event.detail["trading-partner-id"]`
153 * or the resource ARNs in `event.resources` in the handler if multiple
154 * partnerships share the Function. Provide `AWS.Lambda.EventSource` on the
155 * Function effect to implement the subscription.
156 *
157 * ### Reacting to Transformations
158 * **Example:** Post-Process Completed Transformations
159 * ```typescript
160 * yield* AWS.B2BI.consumeTransformationEvents(
161 * { events: ["Transformation Completed"] },
162 * (events) =>
163 * Stream.runForEach(events, (event) =>
164 * Effect.log(
165 * `transformed -> s3://${event.detail["output-file-s3-attributes"]?.bucket}/${event.detail["output-file-s3-attributes"]?.["object-key"]}`,
166 * ),
167 * ),
168 * );
169 * ```
170 */
171 export const consumeTransformationEvents = <StreamReq = never, Req = never>(
172 props: TransformationEventSourceProps,
173 process: (
174 events: Stream.Stream<TransformationEvent, never, StreamReq>,
175 ) => Effect.Effect<void, never, Req>,
176 ) =>
177 Effect.gen(function* () {
178 const { id, events, ...routeProps } = props;
179
180 yield* consumeBusEvents(
181 `${id ?? "B2BI"}-TransformationEvents`,
182 {
183 source: ["aws.b2bi"],
184 "detail-type":
185 events && events.length > 0
186 ? events
187 : ["Transformation Completed", "Transformation Failed"],
188 },
189 routeProps,
190 process,
191 );
192 });
193
194 /**
195 * Deliver B2BI acknowledgement results (`aws.b2bi` / `"Acknowledgement
196 * Completed"` and `"Acknowledgement Failed"` EventBridge events) to the host
197 * Function — emitted when B2BI generates (or fails to generate) an X12
198 * acknowledgement such as a 997 for an inbound document.
199 *
200 * Provide `AWS.Lambda.EventSource` on the Function effect to implement the
201 * subscription.
202 *
203 * ### Reacting to Acknowledgements
204 * **Example:** Alert on Failed Acknowledgements
205 * ```typescript
206 * yield* AWS.B2BI.consumeAcknowledgementEvents(
207 * { events: ["Acknowledgement Failed"] },
208 * (events) =>
209 * Stream.runForEach(events, (event) =>
210 * Effect.log(`ack failed: ${event.detail["failure-message"]}`),
211 * ),
212 * );
213 * ```
214 */
215 export const consumeAcknowledgementEvents = <StreamReq = never, Req = never>(
216 props: AcknowledgementEventSourceProps,
217 process: (
218 events: Stream.Stream<AcknowledgementEvent, never, StreamReq>,
219 ) => Effect.Effect<void, never, Req>,
220 ) =>
221 Effect.gen(function* () {
222 const { id, events, ...routeProps } = props;
223
224 yield* consumeBusEvents(
225 `${id ?? "B2BI"}-AcknowledgementEvents`,
226 {
227 source: ["aws.b2bi"],
228 "detail-type":
229 events && events.length > 0
230 ? events
231 : ["Acknowledgement Completed", "Acknowledgement Failed"],
232 },
233 routeProps,
234 process,
235 );
236 });