@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 100 lines 3.73 KB
Raw
1 import * as Output from "alchemy/Output";
2 import * as Effect from "effect/Effect";
3 import * as AWSBindings from "../AWSBindings.ts";
4 import type { Adapter } from "./Adapter.ts";
5
6 /**
7 * Shared HTTP scaffolding for the Textract runtime bindings.
8 *
9 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
10 * thin `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the two
11 * builders below. Everything except the operation and the IAM action is
12 * boilerplate.
13 *
14 * Textract's document analysis actions (sync `Analyze*`/`Detect*` and the
15 * async `Start*`/`Get*` job APIs) have no resource-level IAM, so the
16 * account-level builder grants on `*`. The adapter management actions
17 * authorize on the adapter / adapter-version resource types, which use
18 * Textract's nonstandard leading-slash ARN path
19 * (`arn:aws:textract:{region}:{account}:/adapters/{adapterId}`), so the
20 * adapter-scoped builder grants on the bound adapter's ARN and its
21 * `/versions/*` children.
22 */
23 export const makeTextractHttpBinding = <I extends object, A, E, R>(options: {
24 /**
25 * Short capability name used in the binding sid and runtime span, e.g.
26 * `"AnalyzeDocument"`.
27 */
28 capability: string;
29 /** IAM actions granted on `Resource: ["*"]`. */
30 iamActions: readonly string[];
31 /** The distilled operation implementing the capability. */
32 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
33 }) =>
34 Effect.gen(function* () {
35 const bind = yield* AWSBindings.Bind;
36 const op = yield* AWSBindings.withCredentials(options.operation);
37
38 return Effect.fn(function* () {
39 if (!globalThis.__ALCHEMY_RUNTIME__) {
40 yield* bind`AWS.Textract.${options.capability}()`({
41 policyStatements: [
42 {
43 Effect: "Allow",
44 Action: [...options.iamActions],
45 Resource: ["*"],
46 },
47 ],
48 });
49 }
50 return Effect.fn(`AWS.Textract.${options.capability}`)(function* (request?: I) {
51 return yield* op((request ?? {}) as I);
52 });
53 });
54 });
55
56 /**
57 * Build the impl Effect for an adapter-scoped Textract operation: the
58 * binding is constructed with an {@link Adapter}, the deploy-time half
59 * grants `iamActions` on the adapter's ARN (and its `/versions/*`
60 * children), and the runtime callable injects the adapter's `AdapterId`
61 * into every request.
62 */
63 export const makeTextractAdapterHttpBinding = <I extends { AdapterId?: string }, A, E, R>(options: {
64 /** Short capability name, e.g. `"GetAdapter"`. */
65 capability: string;
66 /** IAM actions granted on the adapter ARN + `/versions/*`. */
67 iamActions: readonly string[];
68 /** The distilled operation; `AdapterId` is injected from the adapter. */
69 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
70 }) =>
71 Effect.gen(function* () {
72 const bind = yield* AWSBindings.Bind;
73 const op = yield* AWSBindings.withCredentials(options.operation);
74
75 return Effect.fn(function* <Ad extends Adapter>(adapter: Ad) {
76 const AdapterId = yield* adapter.adapterId;
77 if (!globalThis.__ALCHEMY_RUNTIME__) {
78 yield* bind`AWS.Textract.${options.capability}(${adapter})`({
79 policyStatements: [
80 {
81 Effect: "Allow",
82 Action: [...options.iamActions],
83 Resource: [
84 Output.interpolate`${adapter.adapterArn}`,
85 Output.interpolate`${adapter.adapterArn}/versions/*`,
86 ],
87 },
88 ],
89 });
90 }
91 return Effect.fn(`AWS.Textract.${options.capability}(${adapter.LogicalId})`)(function* (
92 request?: Omit<I, "AdapterId">,
93 ) {
94 return yield* op({
95 ...request,
96 AdapterId: yield* AdapterId,
97 } as I);
98 });
99 });
100 });