@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, 06:00 UTC

main
ts 82 lines 3.11 KB
Raw
1 import { Endpoint } from "@distilled.cloud/aws";
2 import * as TSQ from "@distilled.cloud/aws/timestream-query";
3 import * as TSW from "@distilled.cloud/aws/timestream-write";
4 import * as Effect from "effect/Effect";
5
6 /**
7 * Amazon Timestream uses the **endpoint discovery** pattern: control-plane and
8 * data-plane requests must be routed to a cell-specific endpoint returned by
9 * `DescribeEndpoints`, not the static regional endpoint (which answers every
10 * operation other than `DescribeEndpoints` with `UnknownOperationException`).
11 *
12 * These helpers call `DescribeEndpoints` once, cache the discovered address for
13 * its advertised `CachePeriodInMinutes`, and wrap a distilled Timestream effect
14 * so it targets the discovered endpoint via the `Endpoint` service override.
15 */
16
17 type Kind = "write" | "query";
18
19 interface CachedEndpoint {
20 readonly url: string;
21 readonly expiresAt: number;
22 }
23
24 // Module-level cache keyed by discovery kind. `DescribeEndpoints` is a cheap
25 // call but the API asks clients to respect the returned cache period; caching
26 // also avoids an extra round-trip on every write/query.
27 const cache = new Map<Kind, CachedEndpoint>();
28
29 /**
30 * Discover (and cache) the cell-specific endpoint for `kind`, using the given
31 * `DescribeEndpoints` effect. Parameterized over the discovery effect so each
32 * caller carries only its own service module's error union — and so binding
33 * layers can pass an operation captured via yield-first (`yield* op`) whose
34 * calls are requirement-free.
35 */
36 export const discover = <E, R>(
37 kind: Kind,
38 describe: Effect.Effect<{ Endpoints: { Address: string; CachePeriodInMinutes: number }[] }, E, R>,
39 ): Effect.Effect<string, E, R> =>
40 Effect.gen(function* () {
41 const now = Date.now();
42 const cached = cache.get(kind);
43 if (cached !== undefined && cached.expiresAt > now) {
44 return cached.url;
45 }
46 const response = yield* describe;
47 const endpoint = response.Endpoints[0];
48 const url = `https://${endpoint.Address}`;
49 cache.set(kind, {
50 url,
51 expiresAt: now + (endpoint.CachePeriodInMinutes ?? 5) * 60_000,
52 });
53 return url;
54 });
55
56 /**
57 * Wrap a distilled Timestream effect so it targets the endpoint produced by
58 * `discovered` (see {@link discover}) via the `Endpoint` service override.
59 */
60 export const withEndpoint =
61 <EDisc, RDisc>(discovered: Effect.Effect<string, EDisc, RDisc>) =>
62 <A, E, R>(
63 effect: Effect.Effect<A, E, R>,
64 ): Effect.Effect<A, E | EDisc, Exclude<R, Endpoint.Endpoint> | RDisc> =>
65 discovered.pipe(
66 Effect.flatMap((url) =>
67 effect.pipe(Effect.provideService(Endpoint.Endpoint, Effect.succeed(url))),
68 ),
69 );
70
71 /**
72 * Route a distilled `timestream-write` effect through the discovered ingest
73 * endpoint. Adds `DescribeEndpoints`'s error union (including the synthetic
74 * `TimestreamNotOnboarded` tag) to the wrapped effect's errors.
75 */
76 export const withWriteEndpoint = withEndpoint(discover("write", TSW.describeEndpoints({})));
77
78 /**
79 * Route a distilled `timestream-query` effect through the discovered query
80 * endpoint.
81 */
82 export const withQueryEndpoint = withEndpoint(discover("query", TSQ.describeEndpoints({})));