@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 105 lines 3.88 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type { Input } from "alchemy/Input";
3 import type * as Output from "alchemy/Output";
4 import type { ResourceLike } from "alchemy/Resource";
5 /**
6 * Shared HTTP-binding scaffolding for App Runner capabilities.
7 *
8 * INTERNAL — deliberately NOT exported from `index.ts`. Each `{Op}Http.ts`
9 * is a thin call to {@link makeAppRunnerHttpBinding}; everything except the
10 * operation, the identifier resolvers, and the IAM grant is boilerplate:
11 * resolve the bound resources' identifiers (which also registers them on the
12 * host's environment), register the IAM policy statement on the binding host
13 * (skipped inside the deployed function), and return the traced runtime
14 * client that merges the identifiers into each wire request.
15 */
16 import * as Effect from "effect/Effect";
17 import * as Layer from "effect/Layer";
18 import * as AWSBindings from "../AWSBindings.ts";
19 import { AWSEnvironment } from "../Environment.ts";
20
21 /** Region/account available to an IAM grant factory. */
22 export interface AppRunnerIamContext {
23 region: string;
24 accountId: string;
25 }
26
27 export interface AppRunnerHttpBindingSpec {
28 /**
29 * Wire-request fields resolved from the bound resources' attributes,
30 * keyed by the operation's request field name (e.g. `ServiceArn`).
31 */
32 identifiers: Record<string, Output.Output<string, never>>;
33 /** The IAM actions + resource ARNs the host needs for this capability. */
34 iam: (ctx: AppRunnerIamContext) => {
35 actions: string[];
36 resources: Input<string>[];
37 };
38 }
39
40 /**
41 * Build the `Layer.effect` for an App Runner HTTP binding from its three
42 * distinguishing parts: the distilled operation, the identifier resolvers,
43 * and the IAM grant.
44 */
45 export const makeAppRunnerHttpBinding = <
46 Self,
47 Identifier extends string,
48 Shape extends (...args: any[]) => Effect.Effect<any, any, any>,
49 WireIn extends object,
50 Out,
51 Err,
52 OpReq,
53 >(
54 service: Binding.Service<Self, Identifier, Shape>,
55 options: {
56 /** The distilled App Runner operation backing the capability. */
57 operation: Effect.Effect<(input: WireIn) => Effect.Effect<Out, Err>, never, OpReq>;
58 /** Derive identifiers + IAM grant from the bound resources. */
59 spec: (...args: Parameters<Shape>) => AppRunnerHttpBindingSpec;
60 },
61 ): Layer.Layer<Self, never, OpReq> =>
62 Layer.effect(
63 service,
64 Effect.gen(function* () {
65 const bind = yield* AWSBindings.Bind;
66 const operation = yield* AWSBindings.withCredentials(options.operation);
67
68 return Effect.fn(function* (...args: Parameters<Shape>) {
69 const { identifiers, iam } = options.spec(...args);
70 const label = (args as ResourceLike[]).map((arg) => arg.LogicalId).join(", ");
71
72 // Outputs yield DEFERRED effects — resolving them here registers the
73 // attributes on the host environment; re-yield per invocation below.
74 const resolved: [string, Effect.Effect<string>][] = [];
75 for (const [key, output] of Object.entries(identifiers)) {
76 resolved.push([key, yield* output]);
77 }
78
79 if (!globalThis.__ALCHEMY_RUNTIME__) {
80 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
81 accountId: string;
82 region: string;
83 }>;
84 const { actions, resources } = iam({ accountId, region });
85 yield* bind`${service.key}(${label})`({
86 policyStatements: [
87 {
88 Effect: "Allow",
89 Action: actions,
90 Resource: resources,
91 },
92 ],
93 });
94 }
95
96 return Effect.fn(`${service.key}(${label})`)(function* (request?: object) {
97 const ids: Record<string, string> = {};
98 for (const [key, effect] of resolved) {
99 ids[key] = yield* effect;
100 }
101 return yield* operation({ ...request, ...ids } as WireIn);
102 });
103 }) as unknown as Shape;
104 }),
105 );