The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { GcpOpContext } from "@distilled.cloud/gcp/contentwarehouse_v1"; |
| 2 | import type { Output } from "alchemy/Output"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as GCPBindings from "../GCPBindings.ts"; |
| 5 | import { grantFor, type BindingIam } from "../HttpBinding.ts"; |
| 6 | import type { Document } from "./Document.ts"; |
| 7 | import type { DocumentSchema } from "./DocumentSchema.ts"; |
| 8 | import type { RuleSet } from "./RuleSet.ts"; |
| 9 | import type { SynonymSet } from "./SynonymSet.ts"; |
| 10 | |
| 11 | /** |
| 12 | * Distilled ops are OperationMethods: yield them once at Layer construction |
| 13 | * (after providing Credentials + HttpClient) so the inner runtime Effect is |
| 14 | * `Effect<A, E>` and does not leak `GcpOpContext`. |
| 15 | */ |
| 16 | type GcpHttpOp<I, A, E> = Effect.Effect<(input: I) => Effect.Effect<A, E>, never, GcpOpContext> & |
| 17 | ((input: I) => Effect.Effect<A, E, GcpOpContext>); |
| 18 | |
| 19 | const makeNamedHttpBinding = < |
| 20 | Resource extends { name: Output<string, never>; LogicalId: string }, |
| 21 | I extends { name: string }, |
| 22 | A, |
| 23 | E, |
| 24 | >(options: { |
| 25 | tag: string; |
| 26 | iam: BindingIam; |
| 27 | operation: GcpHttpOp<I, A, E>; |
| 28 | }) => |
| 29 | Effect.gen(function* () { |
| 30 | const bind = yield* GCPBindings.Bind; |
| 31 | const run = yield* GCPBindings.withCredentials(options.operation); |
| 32 | return Effect.fn(function* (resource: Resource) { |
| 33 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 34 | yield* bind`${options.tag}(${resource})`({ |
| 35 | iam: [grantFor(options.iam, resource.name)], |
| 36 | }); |
| 37 | } |
| 38 | const name = yield* resource.name; |
| 39 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* ( |
| 40 | request?: Omit<I, "name">, |
| 41 | ) { |
| 42 | const resourceName = yield* name; |
| 43 | return yield* run({ |
| 44 | ...request, |
| 45 | name: resourceName, |
| 46 | } as I); |
| 47 | }); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | /** |
| 52 | * Shared HTTP scaffolding for Content Warehouse bindings. |
| 53 | * NOT exported from index.ts. |
| 54 | */ |
| 55 | export const makeDocumentSchemaHttpBinding = <I extends { name: string }, A, E>(options: { |
| 56 | tag: string; |
| 57 | iam: BindingIam; |
| 58 | operation: GcpHttpOp<I, A, E>; |
| 59 | }) => makeNamedHttpBinding<DocumentSchema, I, A, E>(options); |
| 60 | |
| 61 | export const makeDocumentHttpBinding = <I extends { name: string }, A, E>(options: { |
| 62 | tag: string; |
| 63 | iam: BindingIam; |
| 64 | operation: GcpHttpOp<I, A, E>; |
| 65 | }) => makeNamedHttpBinding<Document, I, A, E>(options); |
| 66 | |
| 67 | export const makeRuleSetHttpBinding = <I extends { name: string }, A, E>(options: { |
| 68 | tag: string; |
| 69 | iam: BindingIam; |
| 70 | operation: GcpHttpOp<I, A, E>; |
| 71 | }) => makeNamedHttpBinding<RuleSet, I, A, E>(options); |
| 72 | |
| 73 | export const makeSynonymSetHttpBinding = <I extends { name: string }, A, E>(options: { |
| 74 | tag: string; |
| 75 | iam: BindingIam; |
| 76 | operation: GcpHttpOp<I, A, E>; |
| 77 | }) => makeNamedHttpBinding<SynonymSet, I, A, E>(options); |