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