@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 131 lines 4.24 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as Stream from "effect/Stream";
4
5 /**
6 * The JSON message delivered to the handler. This is exactly the payload
7 * produced by the rule's SQL `SELECT` — for a `SELECT *` rule it is the raw
8 * MQTT message body, and for enriched SQL it also carries the selected
9 * metadata (`topic()`, `timestamp()`, ...).
10 */
11 export type IoTMessage = Record<string, unknown>;
12
13 export interface TopicRuleEventSourceProps {
14 /**
15 * Explicit rule name. Rule names may only contain `[a-zA-Z0-9_]`.
16 * If omitted, a unique name is generated.
17 */
18 ruleName?: string;
19
20 /**
21 * Override the rule's SQL statement. When omitted, the rule uses
22 * `SELECT * FROM '${topicFilter}'`.
23 */
24 sql?: string;
25
26 /**
27 * The SQL rules engine version.
28 * @default "2016-03-23"
29 */
30 awsIotSqlVersion?: string;
31 }
32
33 type MessagesHandler<Req> = (stream: Stream.Stream<IoTMessage>) => Effect.Effect<void, never, Req>;
34
35 /**
36 * Invoke an Effect handler for every MQTT message matching an IoT topic
37 * filter, by creating an IoT {@link TopicRule} with a Lambda action targeting
38 * the current function.
39 *
40 * Provide the host-specific implementation layer on the Function —
41 * `AWS.Lambda.TopicRuleEventSource` creates the topic rule and dispatches
42 * matching invocations to the handler.
43 *
44 * @param topicFilter The MQTT topic (filter) to subscribe to, e.g.
45 * `sensors/+/telemetry`.
46 * @param props Optional rule configuration.
47 * @param process The handler invoked with a stream of matching messages.
48 *
49 * @example Consume MQTT messages in a Lambda
50 * ```typescript
51 * export default IngestFunction.make(
52 * { main: import.meta.url },
53 * Effect.gen(function* () {
54 * const table = yield* Table;
55 * const putItem = yield* AWS.DynamoDB.PutItem(table);
56 *
57 * yield* AWS.IoT.consumeTopicMessages("sensors/+/telemetry", (messages) =>
58 * messages.pipe(
59 * Stream.runForEach((message) =>
60 * putItem({
61 * Item: { pk: { S: String(message.deviceId) } },
62 * }),
63 * ),
64 * Effect.orDie,
65 * ),
66 * );
67 *
68 * return {};
69 * }).pipe(
70 * Effect.provide(
71 * Layer.mergeAll(AWS.Lambda.TopicRuleEventSource, AWS.DynamoDB.PutItemHttp),
72 * ),
73 * ),
74 * );
75 * ```
76 *
77 * @example Enrich the payload with rule SQL
78 * ```typescript
79 * yield* AWS.IoT.consumeTopicMessages(
80 * "sensors/+/telemetry",
81 * { sql: "SELECT *, topic(2) AS deviceId FROM 'sensors/+/telemetry'" },
82 * (messages) =>
83 * messages.pipe(
84 * Stream.runForEach((message) => Effect.log(message.deviceId)),
85 * Effect.orDie,
86 * ),
87 * );
88 * ```
89 */
90 export function consumeTopicMessages<Req = never>(
91 topicFilter: string,
92 process: MessagesHandler<Req>,
93 ): Effect.Effect<void, never, TopicRuleEventSource>;
94 export function consumeTopicMessages<Req = never>(
95 topicFilter: string,
96 props: TopicRuleEventSourceProps,
97 process: MessagesHandler<Req>,
98 ): Effect.Effect<void, never, TopicRuleEventSource>;
99 export function consumeTopicMessages<Req = never>(
100 topicFilter: string,
101 propsOrProcess: TopicRuleEventSourceProps | MessagesHandler<Req>,
102 maybeProcess?: MessagesHandler<Req>,
103 ): Effect.Effect<void, never, TopicRuleEventSource> {
104 const [props, process] =
105 typeof propsOrProcess === "function"
106 ? [{} as TopicRuleEventSourceProps, propsOrProcess]
107 : [propsOrProcess, maybeProcess!];
108 return TopicRuleEventSource.use((source) => source(topicFilter, props, process));
109 }
110
111 /**
112 * Event source connecting an IoT topic filter to the hosting compute.
113 *
114 * The contract is a Context service consumed via
115 * {@link consumeTopicMessages}; the Lambda implementation layer is
116 * `AWS.Lambda.TopicRuleEventSource`, which deploys an IoT {@link TopicRule}
117 * with a Lambda action (plus the invoke permission) and streams matching
118 * messages into the registered handler.
119 *
120 * @binding
121 */
122 export class TopicRuleEventSource extends Context.Service<
123 TopicRuleEventSource,
124 TopicRuleEventSourceService
125 >()("AWS.IoT.TopicRuleEventSource") {}
126
127 export type TopicRuleEventSourceService = <Req = never>(
128 topicFilter: string,
129 props: TopicRuleEventSourceProps,
130 process: (stream: Stream.Stream<IoTMessage>) => Effect.Effect<void, never, Req>,
131 ) => Effect.Effect<void, never, never>;