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

main
ts 155 lines 5.31 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 * A package version state, as reported in the EventBridge event detail.
11 */
12 export type PackageVersionState =
13 | "Unfinished"
14 | "Published"
15 | "Unlisted"
16 | "Archived"
17 | "Disposed"
18 | "Deleted";
19
20 /**
21 * The `detail` payload CodeArtifact delivers to EventBridge on every package
22 * version change (`CodeArtifact Package Version State Change`).
23 */
24 export interface PackageVersionStateChangeDetail {
25 /** Name of the domain containing the repository. */
26 domainName: string;
27 /** AWS account ID that owns the domain. */
28 domainOwner?: string;
29 /** Name of the repository the package version changed in. */
30 repositoryName: string;
31 /** Package format (`npm`, `pypi`, `maven`, `nuget`, `generic`, …). */
32 packageFormat: string;
33 /** Namespace of the package, for formats that support one. */
34 packageNamespace?: string;
35 /** Name of the package. */
36 packageName: string;
37 /** Version of the package that changed. */
38 packageVersion: string;
39 /** The state the package version is now in. */
40 packageVersionState: PackageVersionState;
41 /** Revision of the package version after the change. */
42 packageVersionRevision?: string;
43 /** What changed on the version (assets added/removed, status, metadata). */
44 changes?: {
45 assetsAdded?: number;
46 assetsRemoved?: number;
47 assetsUpdated?: number;
48 metadataUpdated?: boolean;
49 statusChanged?: boolean;
50 };
51 /** Whether the version was `Created`, `Updated`, or `Deleted`. */
52 operationType?: "Created" | "Updated" | "Deleted";
53 /** Monotonic sequence number of changes to this package version. */
54 sequenceNumber?: number;
55 /** Deduplication id — identical for retried deliveries of one change. */
56 eventDeduplicationId?: string;
57 /** Additional event fields (the schema grows over time). */
58 [key: string]: unknown;
59 }
60
61 /**
62 * A CodeArtifact package version state change EventBridge event delivered to
63 * the handler.
64 */
65 export type PackageVersionStateChangeEvent = EventRecord<PackageVersionStateChangeDetail>;
66
67 export interface PackageVersionStateChangesProps extends EventRouteProps {
68 /**
69 * Logical id for the backing EventBridge rule.
70 * @default "CodeArtifactPackageVersionStateChanges"
71 */
72 id?: string;
73 /**
74 * Only deliver events for these domains (by name).
75 * @default all domains
76 */
77 domains?: string[];
78 /**
79 * Only deliver events for these repositories (by name).
80 * @default all repositories
81 */
82 repositories?: string[];
83 /**
84 * Only deliver events for these package formats (e.g. `["npm"]`).
85 * @default all formats
86 */
87 formats?: string[];
88 /**
89 * Only deliver transitions into these states (e.g. `["Published"]`).
90 * @default all states
91 */
92 states?: PackageVersionState[];
93 }
94
95 /**
96 * Event source connecting CodeArtifact package version changes to the hosting
97 * compute. CodeArtifact publishes an event to the account's default
98 * EventBridge bus (source `aws.codeartifact`, detail-type `CodeArtifact
99 * Package Version State Change`) every time a package version is created,
100 * changes state (published, archived, disposed, …), or is deleted — including
101 * versions ingested from upstream and external connections; this subscribes
102 * the host Function to those events so it can react without polling.
103 *
104 * CodeArtifact publishes to EventBridge automatically — no additional
105 * resource is created besides the EventBridge rule targeting the host.
106 * Provide the host-specific implementation layer (e.g.
107 * `AWS.Lambda.EventSource`) on the Function effect.
108 *
109 * ### Consuming Package Version Events
110 * **Example:** React to Published Versions
111 * ```typescript
112 * import * as AWS from "@alchemy.run/aws";
113 *
114 * export default ReleaseBot.make(
115 * { main: import.meta.url },
116 * Effect.gen(function* () {
117 * yield* AWS.CodeArtifact.consumePackageVersionStateChanges(
118 * { states: ["Published"] },
119 * (events) =>
120 * Stream.runForEach(events, (event) =>
121 * Effect.log(
122 * `${event.detail.packageName}@${event.detail.packageVersion} → ${event.detail.packageVersionState}`,
123 * ),
124 * ),
125 * );
126 * return {};
127 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
128 * );
129 * ```
130 */
131 export const consumePackageVersionStateChanges = <StreamReq = never, Req = never>(
132 props: PackageVersionStateChangesProps,
133 process: (
134 events: Stream.Stream<PackageVersionStateChangeEvent, never, StreamReq>,
135 ) => Effect.Effect<void, never, Req>,
136 ) =>
137 consumeBusEvents(
138 props.id ?? "CodeArtifactPackageVersionStateChanges",
139 {
140 source: ["aws.codeartifact"],
141 "detail-type": ["CodeArtifact Package Version State Change"],
142 ...(props.domains || props.repositories || props.formats || props.states
143 ? {
144 detail: {
145 ...(props.domains ? { domainName: [...props.domains] } : {}),
146 ...(props.repositories ? { repositoryName: [...props.repositories] } : {}),
147 ...(props.formats ? { packageFormat: [...props.formats] } : {}),
148 ...(props.states ? { packageVersionState: [...props.states] } : {}),
149 },
150 }
151 : {}),
152 },
153 { description: props.description, state: props.state },
154 process,
155 );