The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as Output from "alchemy/Output"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as AWSBindings from "../AWSBindings.ts"; |
| 4 | import type { BrowserCustom } from "./BrowserCustom.ts"; |
| 5 | import type { CodeInterpreter } from "./CodeInterpreter.ts"; |
| 6 | import type { Memory } from "./Memory.ts"; |
| 7 | import type { Runtime } from "./Runtime.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Shared scaffolding for Bedrock AgentCore data-plane HTTP bindings. |
| 11 | * |
| 12 | * NOT exported from `index.ts` — every single-operation `{Op}Http.ts` in this |
| 13 | * service is a thin `Layer.effect(Cap, makeAgentCoreHttpBinding({ … }))` over |
| 14 | * the builder below. Everything except the operation, the IAM action list, |
| 15 | * the injected identifier, and the granted ARNs is boilerplate: the runtime |
| 16 | * callable injects the bound resource's identifier (memory id, code |
| 17 | * interpreter id, browser id, or agent runtime ARN) as `requestKey`, and the |
| 18 | * deploy-time half grants `actions` on `arns`. |
| 19 | * |
| 20 | * Genuinely-different bindings (custom request shaping like the |
| 21 | * `sessionTimeout` duration conversion in `StartCodeInterpreterSession` / |
| 22 | * `StartBrowserSession`) stay bespoke. |
| 23 | */ |
| 24 | |
| 25 | /** The AgentCore resources data-plane bindings can scope to. */ |
| 26 | export type AgentCoreBindable = Memory | CodeInterpreter | BrowserCustom | Runtime; |
| 27 | |
| 28 | /** |
| 29 | * Build the impl Effect for a single-operation AgentCore data-plane binding. |
| 30 | * The runtime callable injects the resolved `identifier` as the request's |
| 31 | * `requestKey` field; the deploy-time half grants `actions` on `arns`. |
| 32 | */ |
| 33 | export const makeAgentCoreHttpBinding = < |
| 34 | Res extends AgentCoreBindable, |
| 35 | I extends object, |
| 36 | K extends keyof I & string, |
| 37 | A, |
| 38 | E, |
| 39 | R, |
| 40 | IdReq = never, |
| 41 | >(options: { |
| 42 | /** Fully-qualified binding tag, e.g. `AWS.BedrockAgentCore.GetEvent`. */ |
| 43 | tag: string; |
| 44 | /** The distilled operation; `requestKey` is injected from the resource. */ |
| 45 | operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>; |
| 46 | /** IAM actions granted on `arns`. */ |
| 47 | actions: readonly string[]; |
| 48 | /** The request field the resolved identifier is injected as. */ |
| 49 | requestKey: K; |
| 50 | /** Resolve the injected identifier from the bound resource. */ |
| 51 | identifier: (resource: Res) => Output.Output<string, IdReq>; |
| 52 | /** IAM resource ARNs granted `actions`. */ |
| 53 | arns: (resource: Res) => readonly Output.Output<string>[]; |
| 54 | }) => |
| 55 | Effect.gen(function* () { |
| 56 | const bind = yield* AWSBindings.Bind; |
| 57 | const op = yield* AWSBindings.withCredentials(options.operation); |
| 58 | |
| 59 | return Effect.fn(function* (resource: Res) { |
| 60 | const Identifier = yield* options.identifier(resource); |
| 61 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 62 | yield* bind`${options.tag}(${resource})`({ |
| 63 | policyStatements: [ |
| 64 | { |
| 65 | Effect: "Allow", |
| 66 | Action: [...options.actions], |
| 67 | Resource: [...options.arns(resource)], |
| 68 | }, |
| 69 | ], |
| 70 | }); |
| 71 | } |
| 72 | return Effect.fn(`${options.tag}(${resource.LogicalId})`)(function* (request?: Omit<I, K>) { |
| 73 | return yield* op({ |
| 74 | ...request, |
| 75 | [options.requestKey]: yield* Identifier, |
| 76 | } as I); |
| 77 | }); |
| 78 | }); |
| 79 | }); |