@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 78 lines 2.95 KB
Raw
1 import type * as DynamoDB from "@distilled.cloud/aws/dynamodb";
2 import { makeBatchedSink } from "alchemy/Util/BatchedSink";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import { BatchWriteItem } from "./BatchWriteItem.ts";
6 import { GrantTables } from "./BindingHttp.ts";
7 import type { Table } from "./Table.ts";
8 import { TableSink, type TableSinkEntry } from "./TableSink.ts";
9
10 const encoder = new TextEncoder();
11
12 /**
13 * Map the `UnprocessedItems` echoed by `BatchWriteItem` back onto the
14 * original batch entries, preserving input order. The response is
15 * deserialized (identity is lost), so entries are matched by canonical JSON
16 * as a multiset — DynamoDB rejects batches containing duplicate keys, so a
17 * multiset over serialized entries is unambiguous in practice. If any echoed
18 * entry fails to match (e.g. key-order drift in the echo), fall back to the
19 * echoed entries verbatim so nothing is ever silently dropped from the retry.
20 */
21 const selectUnprocessed = (
22 unprocessed: { [key: string]: DynamoDB.WriteRequest[] | undefined } | undefined,
23 batch: readonly TableSinkEntry[],
24 ): readonly TableSinkEntry[] => {
25 // The sink writes a single table, so at most one key is present.
26 const echoed = Object.values(unprocessed ?? {}).flatMap((requests) => requests ?? []);
27 if (echoed.length === 0) {
28 return [];
29 }
30 const remaining = new Map<string, number>();
31 for (const request of echoed) {
32 const key = JSON.stringify(request);
33 remaining.set(key, (remaining.get(key) ?? 0) + 1);
34 }
35 const matched = batch.filter((entry) => {
36 const key = JSON.stringify(entry);
37 const count = remaining.get(key) ?? 0;
38 if (count === 0) {
39 return false;
40 }
41 remaining.set(key, count - 1);
42 return true;
43 });
44 return matched.length === echoed.length ? matched : echoed;
45 };
46
47 export const TableSinkHttp = Layer.effect(
48 TableSink,
49 Effect.gen(function* () {
50 const grantTables = yield* GrantTables;
51 const batchWriteItem = yield* BatchWriteItem;
52
53 return Effect.fn(function* (table: Table) {
54 // Requests are signed by the provided `BatchWriteItem` implementation.
55 yield* grantTables(`AWS.DynamoDB.TableSink(${table.LogicalId})`, () => [
56 {
57 Effect: "Allow",
58 Action: ["dynamodb:BatchWriteItem"],
59 Resource: [table.tableArn],
60 },
61 ]);
62 const write = yield* batchWriteItem(table);
63 return makeBatchedSink<
64 TableSinkEntry,
65 DynamoDB.BatchWriteItemOutput,
66 DynamoDB.BatchWriteItemError
67 >({
68 maxRecords: 25,
69 maxBytes: 16_777_216,
70 sizeOf: (request) => encoder.encode(JSON.stringify(request)).length,
71 send: (batch) => write({ RequestItems: { [table.LogicalId]: [...batch] } }),
72 // UnprocessedItems are transient (throttling, internal errors) —
73 // re-submit them on the bounded schedule.
74 unprocessed: (out, batch) => selectUnprocessed(out.UnprocessedItems, batch),
75 });
76 });
77 }),
78 );