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

main
ts 169 lines 5.34 KB
Raw
1 import * as Context from "effect/Context";
2 import type * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Stream from "effect/Stream";
5 import type { Broker } from "./Broker.ts";
6
7 /**
8 * A single message delivered by an Amazon MQ event source. The shape differs
9 * slightly between ActiveMQ (`aws:mq`) and RabbitMQ (`aws:rmq`) — the common
10 * fields are typed here and the raw payload is available for engine-specific
11 * access.
12 */
13 export interface MQMessage {
14 /** Base64-encoded message body. */
15 readonly data?: string;
16 /** ActiveMQ message id. */
17 readonly messageID?: string;
18 /** ActiveMQ message type (e.g. `jms/text-message`). */
19 readonly messageType?: string;
20 /** ActiveMQ destination the message was published to. */
21 readonly destination?: { readonly physicalName?: string };
22 /** Whether the message was redelivered. */
23 readonly redelivered?: boolean;
24 /** RabbitMQ basic properties. */
25 readonly basicProperties?: Record<string, unknown>;
26 /** Any additional engine-specific fields. */
27 readonly [key: string]: unknown;
28 }
29
30 /**
31 * The raw ActiveMQ (`aws:mq`) event Lambda receives.
32 */
33 export interface ActiveMQEvent {
34 readonly eventSource: "aws:mq";
35 readonly eventSourceArn: string;
36 readonly messages: MQMessage[];
37 }
38
39 /**
40 * The raw RabbitMQ (`aws:rmq`) event Lambda receives.
41 */
42 export interface RabbitMQEvent {
43 readonly eventSource: "aws:rmq";
44 readonly eventSourceArn: string;
45 readonly rmqMessagesByQueue: { readonly [queue: string]: MQMessage[] };
46 }
47
48 export type MQEvent = ActiveMQEvent | RabbitMQEvent;
49
50 export interface BrokerEventSourceProps {
51 /**
52 * Destination queue name(s) on the broker to consume. ActiveMQ supports a
53 * single queue; RabbitMQ supports multiple.
54 */
55 queues: string[];
56 /**
57 * ARN of a Secrets Manager secret holding the broker credentials as
58 * `{ "username": "...", "password": "..." }`. Lambda uses these
59 * (`BASIC_AUTH`) to connect to the broker.
60 */
61 credentialsSecretArn: string;
62 /**
63 * The maximum number of messages in each batch that Lambda pulls from the
64 * broker.
65 * @default 100
66 */
67 batchSize?: number;
68 /**
69 * The maximum time Lambda spends gathering records before invoking the
70 * function (e.g. `"5 seconds"`). Rounded to whole seconds on the wire.
71 */
72 maximumBatchingWindow?: Duration.Input;
73 /**
74 * Whether the event source mapping is active.
75 * @default true
76 */
77 enabled?: boolean;
78 }
79
80 type MessagesHandler<Req> = (stream: Stream.Stream<MQMessage>) => Effect.Effect<void, never, Req>;
81
82 /**
83 * Subscribe an Effect handler to messages produced by an Amazon MQ
84 * {@link Broker}. Lambda polls the named broker queue(s) using the supplied
85 * Secrets Manager credentials and invokes the handler with a stream of
86 * messages.
87 *
88 * Call it in the function's init phase and provide the host implementation
89 * layer (`Lambda.BrokerEventSource`) on the function Effect — the
90 * event-source mapping, IAM grants, and runtime dispatch are registered
91 * automatically.
92 *
93 * @example
94 * ```typescript
95 * // init — inside the Lambda function's setup Effect
96 * const broker = yield* MQ.Broker("Orders", {
97 * engineType: "ACTIVEMQ",
98 * engineVersion: "5.18",
99 * hostInstanceType: "mq.t3.micro",
100 * users: [{ username: "app", password: appPassword }],
101 * });
102 *
103 * yield* MQ.consumeBrokerMessages(
104 * broker,
105 * {
106 * queues: ["orders"],
107 * credentialsSecretArn: secret.secretArn,
108 * batchSize: 10,
109 * },
110 * (messages) =>
111 * messages.pipe(
112 * Stream.runForEach((message) =>
113 * // message bodies arrive base64-encoded
114 * Effect.log(atob(message.data ?? "")),
115 * ),
116 * ),
117 * );
118 * ```
119 *
120 * Provide the Lambda implementation layer on the function:
121 * ```typescript
122 * export default OrdersFunction.make(
123 * { main },
124 * Effect.gen(function* () {
125 * // ... consumeBrokerMessages(...) as above
126 * return { fetch: Effect.succeed(HttpServerResponse.text("ok")) };
127 * }).pipe(Effect.provide(Lambda.BrokerEventSource)),
128 * );
129 * ```
130 *
131 * @param broker The Amazon MQ broker to consume from.
132 * @param props Queues, credentials secret, and batching configuration.
133 * @param process The handler invoked with a stream of MQ messages.
134 */
135 export function consumeBrokerMessages<B extends Broker, Req = never>(
136 broker: B,
137 props: BrokerEventSourceProps,
138 process: MessagesHandler<Req>,
139 ): Effect.Effect<void, never, BrokerEventSource> {
140 return BrokerEventSource.use((source) => source(broker, props, process));
141 }
142
143 /**
144 * Event source connecting an Amazon MQ {@link Broker} to the hosting Lambda
145 * function. Prefer the {@link consumeBrokerMessages} helper; this service is
146 * the underlying contract, implemented by the `Lambda.BrokerEventSource`
147 * layer (which registers the event-source mapping, IAM grants, and runtime
148 * dispatch).
149 *
150 * **Example:** Example
151 * ```typescript
152 * // equivalent to consumeBrokerMessages(broker, props, process)
153 * yield* BrokerEventSource.use((source) =>
154 * source(broker, props, process),
155 * );
156 * ```
157 *
158 * @binding
159 */
160 export class BrokerEventSource extends Context.Service<
161 BrokerEventSource,
162 BrokerEventSourceService
163 >()("AWS.MQ.BrokerEventSource") {}
164
165 export type BrokerEventSourceService = <Req = never>(
166 broker: Broker,
167 props: BrokerEventSourceProps,
168 process: (stream: Stream.Stream<MQMessage>) => Effect.Effect<void, never, Req>,
169 ) => Effect.Effect<void, never, never>;