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
| 1 | import type { GcpOpContext } from "@distilled.cloud/gcp/composer_v1"; |
| 2 | import type { Output } from "alchemy/Output"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as GCPBindings from "../GCPBindings.ts"; |
| 5 | import { type BindingIam, grantFor } from "../HttpBinding.ts"; |
| 6 | import type { Environment } from "./Environment.ts"; |
| 7 | import type { EnvironmentsUserWorkloadsConfigMap } from "./EnvironmentsUserWorkloadsConfigMap.ts"; |
| 8 | import type { EnvironmentsUserWorkloadsSecret } from "./EnvironmentsUserWorkloadsSecret.ts"; |
| 9 | |
| 10 | type GcpHttpOp<I, A, E> = Effect.Effect<(input: I) => Effect.Effect<A, E>, never, GcpOpContext> & |
| 11 | ((input: I) => Effect.Effect<A, E, GcpOpContext>); |
| 12 | |
| 13 | type NamedResource = { |
| 14 | readonly name: Output<string, never>; |
| 15 | readonly LogicalId: string; |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * Shared HTTP scaffolding for Cloud Composer bindings whose request |
| 20 | * identifies the resource by a single name path parameter. |
| 21 | * Distilled ops are OperationMethods: yield them once at Layer |
| 22 | * construction (after providing Credentials + HttpClient) so the inner |
| 23 | * runtime Effect is `Effect<A, E>` and does not leak `GcpOpContext`. |
| 24 | * NOT exported from index.ts. |
| 25 | */ |
| 26 | export const makeNamedHttpBinding = <Resource extends NamedResource, I, A, E>(options: { |
| 27 | tag: string; |
| 28 | iam: BindingIam; |
| 29 | nameKey?: string; |
| 30 | operation: GcpHttpOp<I, A, E>; |
| 31 | }) => |
| 32 | Effect.gen(function* () { |
| 33 | const bind = yield* GCPBindings.Bind; |
| 34 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 35 | const nameKey = options.nameKey ?? "name"; |
| 36 | return Effect.fn(function* (resource: Resource) { |
| 37 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 38 | yield* bind`${options.tag}(${resource})`({ |
| 39 | iam: [grantFor(options.iam, resource.name)], |
| 40 | }); |
| 41 | } |
| 42 | const name = yield* resource.name; |
| 43 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (request?: object) { |
| 44 | const resourceName = yield* name; |
| 45 | return yield* run({ |
| 46 | ...request, |
| 47 | [nameKey]: resourceName, |
| 48 | } as I); |
| 49 | }); |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | /** |
| 54 | * Shared HTTP scaffolding for Cloud Composer environment bindings. |
| 55 | * NOT exported from index.ts. |
| 56 | */ |
| 57 | export const makeEnvironmentHttpBinding = <I, A, E>(options: { |
| 58 | tag: string; |
| 59 | iam: BindingIam; |
| 60 | nameKey: string; |
| 61 | operation: GcpHttpOp<I, A, E>; |
| 62 | }) => |
| 63 | makeNamedHttpBinding<Environment, I, A, E>({ |
| 64 | tag: options.tag, |
| 65 | iam: options.iam, |
| 66 | nameKey: options.nameKey, |
| 67 | operation: options.operation, |
| 68 | }); |
| 69 | |
| 70 | export const makeUserWorkloadsConfigMapHttpBinding = <I, A, E>(options: { |
| 71 | tag: string; |
| 72 | iam: BindingIam; |
| 73 | operation: GcpHttpOp<I, A, E>; |
| 74 | }) => |
| 75 | makeNamedHttpBinding<EnvironmentsUserWorkloadsConfigMap, I, A, E>({ |
| 76 | tag: options.tag, |
| 77 | iam: options.iam, |
| 78 | nameKey: "name", |
| 79 | operation: options.operation, |
| 80 | }); |
| 81 | |
| 82 | export const makeUserWorkloadsSecretHttpBinding = <I, A, E>(options: { |
| 83 | tag: string; |
| 84 | iam: BindingIam; |
| 85 | operation: GcpHttpOp<I, A, E>; |
| 86 | }) => |
| 87 | makeNamedHttpBinding<EnvironmentsUserWorkloadsSecret, I, A, E>({ |
| 88 | tag: options.tag, |
| 89 | iam: options.iam, |
| 90 | nameKey: "name", |
| 91 | operation: options.operation, |
| 92 | }); |