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

main
ts 138 lines 5.07 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 Amazon SageMaker delivers to EventBridge. SageMaker
11 * mirrors the corresponding `Describe*` output in PascalCase; only the
12 * fields shared across the state-change event kinds are declared here, the
13 * rest stay open (the schema grows over time).
14 */
15 export interface SageMakerEventDetail {
16 /** Endpoint state changes: the endpoint's name. */
17 EndpointName?: string;
18 /** Endpoint state changes: the endpoint's status (e.g. `IN_SERVICE`, `FAILED`). */
19 EndpointStatus?: string;
20 /** Feature group state changes: the feature group's name. */
21 FeatureGroupName?: string;
22 /** Feature group state changes: the feature group's status. */
23 FeatureGroupStatus?: string;
24 /** Job state changes: the job's status (e.g. `Completed`, `Failed`). */
25 TrainingJobStatus?: string;
26 ProcessingJobStatus?: string;
27 TransformJobStatus?: string;
28 /** Failure detail accompanying `Failed` statuses. */
29 FailureReason?: string;
30 /** Additional event fields (the schema grows over time). */
31 [key: string]: unknown;
32 }
33
34 /** A SageMaker EventBridge event delivered to the handler. */
35 export type SageMakerEvent = EventRecord<SageMakerEventDetail>;
36
37 /** Which SageMaker notifications to subscribe to. */
38 export type SageMakerEventKind =
39 | "endpoint"
40 | "endpoint-deployment"
41 | "feature-group"
42 | "model-package"
43 | "training-job"
44 | "processing-job"
45 | "transform-job"
46 | "hyperparameter-tuning-job"
47 | "pipeline-execution"
48 | "pipeline-step"
49 | "image";
50
51 const DETAIL_TYPES: Record<SageMakerEventKind, string> = {
52 endpoint: "SageMaker Endpoint State Change",
53 "endpoint-deployment": "SageMaker Endpoint Deployment State Change",
54 "feature-group": "SageMaker Feature Group State Change",
55 "model-package": "SageMaker Model Package State Change",
56 "training-job": "SageMaker Training Job State Change",
57 "processing-job": "SageMaker Processing Job State Change",
58 "transform-job": "SageMaker Transform Job State Change",
59 "hyperparameter-tuning-job": "SageMaker HyperParameter Tuning Job State Change",
60 "pipeline-execution": "SageMaker Model Building Pipeline Execution Status Change",
61 "pipeline-step": "SageMaker Model Building Pipeline Execution Step Status Change",
62 image: "SageMaker Image State Change",
63 };
64
65 export interface SageMakerEventSourceProps extends EventRouteProps {
66 /**
67 * Logical id for the backing EventBridge rule.
68 * @default "SageMakerEvents"
69 */
70 id?: string;
71 /**
72 * Which SageMaker notifications to subscribe to: endpoint / endpoint
73 * deployment / feature group / model package state changes and the
74 * training, processing, transform, tuning, pipeline, and image job
75 * lifecycles.
76 * @default ["endpoint"]
77 */
78 kinds?: readonly SageMakerEventKind[];
79 /**
80 * Restrict to events about specific SageMaker resources (matched against
81 * the event's top-level `resources`, which carries the resource ARN).
82 */
83 resourceArns?: readonly string[];
84 }
85
86 /**
87 * Event source connecting Amazon SageMaker state changes to the hosting
88 * compute. SageMaker publishes endpoint, feature-group, model-package, and
89 * job lifecycle transitions to the account's default EventBridge bus
90 * (source `aws.sagemaker`); this subscribes the host Function to those
91 * events so it can react — e.g. alert when an endpoint drops out of
92 * `InService` or kick off downstream work when a training job completes.
93 *
94 * SageMaker publishes to EventBridge automatically — no additional resource
95 * is created besides the EventBridge rule targeting the host. Provide the
96 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
97 * Function effect.
98 *
99 * ### Consuming SageMaker Events
100 * **Example:** Alert When an Endpoint Fails
101 * ```typescript
102 * import * as AWS from "@alchemy.run/aws";
103 *
104 * export default AlertFunction.make(
105 * { main: import.meta.url },
106 * Effect.gen(function* () {
107 * yield* AWS.SageMaker.consumeSageMakerEvents(
108 * { kinds: ["endpoint"] },
109 * (events) =>
110 * Stream.runForEach(events, (event) =>
111 * event.detail.EndpointStatus === "FAILED"
112 * ? Effect.logError(
113 * `endpoint ${event.detail.EndpointName} failed: ${event.detail.FailureReason}`,
114 * )
115 * : Effect.void,
116 * ),
117 * );
118 * return {};
119 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
120 * );
121 * ```
122 */
123 export const consumeSageMakerEvents = <StreamReq = never, Req = never>(
124 props: SageMakerEventSourceProps,
125 process: (
126 events: Stream.Stream<SageMakerEvent, never, StreamReq>,
127 ) => Effect.Effect<void, never, Req>,
128 ) =>
129 consumeBusEvents(
130 props.id ?? "SageMakerEvents",
131 {
132 source: ["aws.sagemaker"],
133 "detail-type": (props.kinds ?? (["endpoint"] as const)).map((kind) => DETAIL_TYPES[kind]),
134 ...(props.resourceArns !== undefined ? { resources: [...props.resourceArns] } : {}),
135 },
136 { description: props.description, state: props.state },
137 process,
138 );