The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as AWSBindings from "../AWSBindings.ts"; |
| 3 | import { AWSEnvironment } from "../Environment.ts"; |
| 4 | |
| 5 | /** |
| 6 | * Shared scaffolding for AWS SageMaker Runtime HTTP bindings. |
| 7 | * |
| 8 | * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a |
| 9 | * thin `Layer.effect(Cap, makeEndpointInvocationHttpBinding({ … }))` over the |
| 10 | * builder below. Everything except the operation and the IAM action list is |
| 11 | * boilerplate: SageMaker Runtime is a pure data-plane API whose three |
| 12 | * invocation operations all take one or more endpoint names, inject |
| 13 | * `EndpointName` into the request, and authorize against the endpoint ARNs. |
| 14 | */ |
| 15 | |
| 16 | const currentEnv = AWSEnvironment.current as unknown as Effect.Effect<{ |
| 17 | accountId: string; |
| 18 | region: string; |
| 19 | }>; |
| 20 | |
| 21 | /** |
| 22 | * Build the impl Effect for an endpoint-invocation operation |
| 23 | * (`InvokeEndpoint`, `InvokeEndpointAsync`, |
| 24 | * `InvokeEndpointWithResponseStream`). The binding accepts one or more |
| 25 | * endpoint names, grants `actions` on exactly those endpoint ARNs, and the |
| 26 | * runtime callable defaults `EndpointName` to the first bound endpoint (it |
| 27 | * may be overridden per call with any of the bound names). |
| 28 | */ |
| 29 | export const makeEndpointInvocationHttpBinding = < |
| 30 | I extends { EndpointName: string }, |
| 31 | A, |
| 32 | E, |
| 33 | R, |
| 34 | >(options: { |
| 35 | /** Fully-qualified binding tag, e.g. `AWS.SageMakerRuntime.InvokeEndpoint`. */ |
| 36 | tag: string; |
| 37 | /** The distilled operation; `EndpointName` defaults to the first bound endpoint. */ |
| 38 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 39 | /** IAM actions granted on the bound endpoint ARNs. */ |
| 40 | actions: readonly string[]; |
| 41 | }) => |
| 42 | Effect.gen(function* () { |
| 43 | const bind = yield* AWSBindings.Bind; |
| 44 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 45 | |
| 46 | return Effect.fn(function* (endpoint: string, ...additionalEndpoints: string[]) { |
| 47 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 48 | const { accountId, region } = yield* currentEnv; |
| 49 | // Sort so the binding identity (SID + ARN list) is deterministic |
| 50 | // regardless of argument order. |
| 51 | const sorted = [...new Set([endpoint, ...additionalEndpoints])].sort(); |
| 52 | yield* bind`${options.tag}(${sorted.join(",")})`({ |
| 53 | policyStatements: [ |
| 54 | { |
| 55 | Effect: "Allow", |
| 56 | Action: [...options.actions], |
| 57 | Resource: sorted.map( |
| 58 | (name) => `arn:aws:sagemaker:${region}:${accountId}:endpoint/${name}`, |
| 59 | ), |
| 60 | }, |
| 61 | ], |
| 62 | }); |
| 63 | } |
| 64 | return Effect.fn(`${options.tag}(${endpoint})`)(function* ( |
| 65 | request: Omit<I, "EndpointName"> & { EndpointName?: string }, |
| 66 | ) { |
| 67 | return yield* op({ |
| 68 | ...request, |
| 69 | EndpointName: request.EndpointName ?? endpoint, |
| 70 | } as unknown as I); |
| 71 | }); |
| 72 | }); |
| 73 | }); |