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

main
ts 48 lines 1.78 KB
Raw
1 import type * as Kinesis from "@distilled.cloud/aws/kinesis";
2 import { makeBatchedSink } from "alchemy/Util/BatchedSink";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as AWSBindings from "../AWSBindings.ts";
6 import { PutRecords } from "./PutRecords.ts";
7 import type { Stream } from "./Stream.ts";
8 import { StreamSink, type StreamSinkRecord } from "./StreamSink.ts";
9
10 const encoder = new TextEncoder();
11
12 export const StreamSinkHttp = Layer.effect(
13 StreamSink,
14 Effect.gen(function* () {
15 const bind = yield* AWSBindings.Bind;
16 const putRecords = yield* PutRecords;
17
18 return Effect.fn(function* (stream: Stream) {
19 if (!globalThis.__ALCHEMY_RUNTIME__) {
20 yield* bind`AWS.Kinesis.StreamSink(${stream})`({
21 policyStatements: [
22 {
23 Effect: "Allow",
24 Action: ["kinesis:PutRecords"],
25 Resource: [stream.streamArn],
26 },
27 ],
28 });
29 }
30 const publish = yield* putRecords(stream);
31 return makeBatchedSink<StreamSinkRecord, Kinesis.PutRecordsOutput, Kinesis.PutRecordsError>({
32 maxRecords: 500,
33 maxBytes: 5 * 1024 * 1024,
34 sizeOf: (record) => record.Data.length + encoder.encode(record.PartitionKey).length,
35 send: (batch) => publish({ Records: [...batch] }),
36 // PutRecords failures are positional: Records[i] mirrors the request
37 // order and carries an ErrorCode (throughput exceeded / internal
38 // failure) — all transient, so re-submit them on the bounded schedule.
39 unprocessed: (out, batch) => {
40 if (!out.FailedRecordCount) {
41 return [];
42 }
43 return batch.filter((_, index) => out.Records[index]?.ErrorCode !== undefined);
44 },
45 });
46 });
47 }),
48 );