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

main
ts 83 lines 3.11 KB
Raw
1 import type * as TSW from "@distilled.cloud/aws/timestream-write";
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 { RecordsSink, type RecordsSinkProps, type RecordsSinkRecord } from "./RecordsSink.ts";
8 import type { Table } from "./Table.ts";
9 import { WriteRecords } from "./WriteRecords.ts";
10
11 /**
12 * Normalized `WriteRecords` outcome: which positions of the submitted batch
13 * Timestream permanently rejected. A fully-ingested call has no rejections.
14 */
15 interface WriteOutcome {
16 readonly rejectedIndices: ReadonlySet<number>;
17 }
18
19 const noRejections: WriteOutcome = { rejectedIndices: new Set() };
20
21 type WriteSendError = Exclude<
22 TSW.WriteRecordsError | TSW.DescribeEndpointsError,
23 TSW.RejectedRecordsException
24 >;
25
26 export const RecordsSinkHttp = Layer.effect(
27 RecordsSink,
28 Effect.gen(function* () {
29 const bind = yield* AWSBindings.Bind;
30 const writeRecords = yield* WriteRecords;
31
32 return Effect.fn(function* (table: Table, props?: RecordsSinkProps) {
33 if (!globalThis.__ALCHEMY_RUNTIME__) {
34 yield* bind`AWS.Timestream.RecordsSink(${table})`({
35 policyStatements: [
36 {
37 Effect: "Allow",
38 Action: ["timestream:WriteRecords"],
39 Resource: [Output.interpolate`${table.tableArn}`],
40 },
41 // Timestream requires endpoint discovery; the ingest endpoint
42 // is resolved at runtime via DescribeEndpoints, which is not
43 // scoped to a resource.
44 {
45 Effect: "Allow",
46 Action: ["timestream:DescribeEndpoints"],
47 Resource: ["*"],
48 },
49 ],
50 });
51 }
52 const write = yield* writeRecords(table);
53 const commonAttributes = props?.commonAttributes;
54 return makeBatchedSink<RecordsSinkRecord, WriteOutcome, WriteSendError>({
55 maxRecords: 100,
56 send: (batch) =>
57 write({
58 Records: [...batch],
59 ...(commonAttributes === undefined ? {} : { CommonAttributes: commonAttributes }),
60 }).pipe(
61 Effect.map(() => noRejections),
62 // Timestream ingests the valid subset and reports invalid records
63 // positionally via RejectedRecordsException. Rejections are
64 // permanent (schema conflicts, out-of-retention timestamps,
65 // version conflicts) — drop them, never retry.
66 Effect.catchTag("RejectedRecordsException", (error) =>
67 Effect.succeed<WriteOutcome>({
68 rejectedIndices: new Set(
69 (error.RejectedRecords ?? []).flatMap((rejected: TSW.RejectedRecord) =>
70 rejected.RecordIndex === undefined ? [] : [rejected.RecordIndex],
71 ),
72 ),
73 }),
74 ),
75 ),
76 rejected: (out, batch) =>
77 out.rejectedIndices.size === 0
78 ? []
79 : batch.filter((_, index) => out.rejectedIndices.has(index)),
80 });
81 });
82 }),
83 );