@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 59 lines 2.31 KB
Raw
1 import type * as Firehose from "@distilled.cloud/aws/firehose";
2 import * as Output from "alchemy/Output";
3 import { makeBatchedSink } from "alchemy/Util/BatchedSink";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as AWSBindings from "../AWSBindings.ts";
7 import type { DeliveryStream } from "./DeliveryStream.ts";
8 import { DeliveryStreamSink, type DeliveryStreamSinkRecord } from "./DeliveryStreamSink.ts";
9 import { PutRecordBatch } from "./PutRecordBatch.ts";
10
11 /**
12 * HTTP implementation of {@link DeliveryStreamSink}. At deploy time it grants
13 * `firehose:PutRecordBatch` on the bound delivery stream; at runtime it
14 * batches stream elements into `PutRecordBatch` calls (500 records / 4 MiB)
15 * with bounded retry of transient per-record failures. Provide this layer on
16 * the Function using the sink.
17 */
18 export const DeliveryStreamSinkHttp = Layer.effect(
19 DeliveryStreamSink,
20 Effect.gen(function* () {
21 const bind = yield* AWSBindings.Bind;
22 const putRecordBatch = yield* PutRecordBatch;
23
24 return Effect.fn(function* (deliveryStream: DeliveryStream) {
25 if (!globalThis.__ALCHEMY_RUNTIME__) {
26 yield* bind`AWS.Firehose.DeliveryStreamSink(${deliveryStream})`({
27 policyStatements: [
28 {
29 Effect: "Allow",
30 Action: ["firehose:PutRecordBatch"],
31 Resource: [Output.interpolate`${deliveryStream.deliveryStreamArn}`],
32 },
33 ],
34 });
35 }
36 const send = yield* putRecordBatch(deliveryStream);
37 return makeBatchedSink<
38 DeliveryStreamSinkRecord,
39 Firehose.PutRecordBatchOutput,
40 Firehose.PutRecordBatchError
41 >({
42 maxRecords: 500,
43 maxBytes: 4 * 1024 * 1024,
44 sizeOf: (record) => record.Data.length,
45 send: (batch) => send({ Records: [...batch] }),
46 // PutRecordBatch failures are positional: RequestResponses[i] mirrors
47 // the request order and carries an ErrorCode (ServiceUnavailable /
48 // internal failure) — all transient, so re-submit them on the
49 // bounded schedule.
50 unprocessed: (out, batch) => {
51 if (!out.FailedPutCount) {
52 return [];
53 }
54 return batch.filter((_, index) => out.RequestResponses[index]?.ErrorCode !== undefined);
55 },
56 });
57 });
58 }),
59 );