@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 112 lines 4.42 KB
Raw
1 import * as s3vectors from "@distilled.cloud/aws/s3vectors";
2 import * as Effect from "effect/Effect";
3 /**
4 * Shared scaffolding for the S3 Vectors data-plane HTTP bindings.
5 *
6 * NOT exported from `index.ts` — the per-level `Vectors{Read,Write,}Http.ts`
7 * layers are thin compositions over this builder. Everything except the
8 * distilled operations and the IAM action set is boilerplate:
9 *
10 * - the deploy-time half registers `Allow(host, AWS.S3Vectors.{name}(index))`
11 * with the requested actions on exactly the bound index's ARN;
12 * - the runtime client methods inject the resolved `indexArn` into each
13 * distilled request.
14 */
15 import * as AWSBindings from "../AWSBindings.ts";
16 import type { Index } from "./VectorIndex.ts";
17 import type {
18 GetVectorsRequest,
19 ListVectorsRequest,
20 QueryVectorsRequest,
21 ReadVectorsClient,
22 } from "./VectorsRead.ts";
23 import type {
24 DeleteVectorsRequest,
25 PutVectorsRequest,
26 WriteVectorsClient,
27 } from "./VectorsWrite.ts";
28
29 /** IAM actions granted by the read-level binding. */
30 export const readVectorsActions = [
31 "s3vectors:QueryVectors",
32 "s3vectors:GetVectors",
33 "s3vectors:ListVectors",
34 ] as const;
35
36 /** IAM actions granted by the write-level binding. */
37 export const writeVectorsActions = ["s3vectors:PutVectors", "s3vectors:DeleteVectors"] as const;
38
39 /**
40 * Build the shared body of an S3 Vectors data-plane `*Http` binding layer:
41 * resolve the distilled operations once at layer construction (via
42 * `makeClient`), register the least-privilege IAM statement on the host
43 * Function at deploy time, and hand the per-index `indexArn` resolver to the
44 * capability-specific client builder.
45 */
46 export const makeVectorsHttpBinding = <Client, R>(options: {
47 /** Capability name in the binding SID + trace spans, e.g. `"VectorsRead"`. */
48 name: string;
49 /** IAM actions granted on the bound index's ARN. */
50 actions: readonly string[];
51 /** Layer-scoped builder of the per-index typed runtime client. */
52 makeClient: Effect.Effect<(indexArn: Effect.Effect<string>, label: string) => Client, never, R>;
53 }) =>
54 Effect.gen(function* () {
55 const bind = yield* AWSBindings.Bind;
56 const makeClient = yield* options.makeClient;
57
58 return Effect.fn(function* <I extends Index>(index: I) {
59 const IndexArn = yield* index.indexArn;
60 if (!globalThis.__ALCHEMY_RUNTIME__) {
61 yield* bind`AWS.S3Vectors.${options.name}(${index})`({
62 policyStatements: [
63 {
64 Effect: "Allow",
65 Action: [...options.actions],
66 Resource: [index.indexArn],
67 },
68 ],
69 });
70 }
71 return makeClient(IndexArn, `AWS.S3Vectors.${options.name}(${index.LogicalId})`);
72 });
73 });
74
75 /** Layer-scoped builder of the read-level {@link ReadVectorsClient}. */
76 export const makeReadVectorsClient = Effect.gen(function* () {
77 const queryVectors = yield* AWSBindings.withCredentials(s3vectors.queryVectors);
78 const getVectors = yield* AWSBindings.withCredentials(s3vectors.getVectors);
79 const listVectors = yield* AWSBindings.withCredentials(s3vectors.listVectors);
80
81 return (IndexArn: Effect.Effect<string>, label: string): ReadVectorsClient => ({
82 query: Effect.fn(`${label}.query`)(function* (request: QueryVectorsRequest) {
83 const indexArn = yield* IndexArn;
84 return yield* queryVectors({ ...request, indexArn });
85 }),
86 get: Effect.fn(`${label}.get`)(function* (request: GetVectorsRequest) {
87 const indexArn = yield* IndexArn;
88 return yield* getVectors({ ...request, indexArn });
89 }),
90 list: Effect.fn(`${label}.list`)(function* (request: ListVectorsRequest) {
91 const indexArn = yield* IndexArn;
92 return yield* listVectors({ ...request, indexArn });
93 }),
94 });
95 });
96
97 /** Layer-scoped builder of the write-level {@link WriteVectorsClient}. */
98 export const makeWriteVectorsClient = Effect.gen(function* () {
99 const putVectors = yield* AWSBindings.withCredentials(s3vectors.putVectors);
100 const deleteVectors = yield* AWSBindings.withCredentials(s3vectors.deleteVectors);
101
102 return (IndexArn: Effect.Effect<string>, label: string): WriteVectorsClient => ({
103 put: Effect.fn(`${label}.put`)(function* (request: PutVectorsRequest) {
104 const indexArn = yield* IndexArn;
105 return yield* putVectors({ ...request, indexArn });
106 }),
107 delete: Effect.fn(`${label}.delete`)(function* (request: DeleteVectorsRequest) {
108 const indexArn = yield* IndexArn;
109 return yield* deleteVectors({ ...request, indexArn });
110 }),
111 });
112 });