| 1 | import * as agentcore from "@distilled.cloud/aws/bedrock-agentcore"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import { toWireSeconds } from "../../Util/Duration.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import { |
| 8 | StartCodeInterpreterSession, |
| 9 | type StartCodeInterpreterSessionRequest, |
| 10 | } from "./StartCodeInterpreterSession.ts"; |
| 11 | import type { CodeInterpreter } from "./CodeInterpreter.ts"; |
| 12 | |
| 13 | // Bespoke (not the shared scaffold): converts the ergonomic `sessionTimeout` |
| 14 | // Duration.Input to the wire `sessionTimeoutSeconds` field. |
| 15 | export const StartCodeInterpreterSessionHttp = Layer.effect( |
| 16 | StartCodeInterpreterSession, |
| 17 | Effect.gen(function* () { |
| 18 | const startCodeInterpreterSession = |
| 19 | yield* agentcore.startCodeInterpreterSession; |
| 20 | |
| 21 | return Effect.fn(function* <R extends CodeInterpreter>(codeInterpreter: R) { |
| 22 | const Identifier = yield* codeInterpreter.codeInterpreterId; |
| 23 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 24 | const host = yield* Binding.Host; |
| 25 | if (isBindingHost(host)) { |
| 26 | yield* host.bind`Allow(${host}, AWS.BedrockAgentCore.StartCodeInterpreterSession(${codeInterpreter}))`( |
| 27 | { |
| 28 | policyStatements: [ |
| 29 | { |
| 30 | Effect: "Allow", |
| 31 | Action: ["bedrock-agentcore:StartCodeInterpreterSession"], |
| 32 | Resource: [codeInterpreter.codeInterpreterArn], |
| 33 | }, |
| 34 | ], |
| 35 | }, |
| 36 | ); |
| 37 | } |
| 38 | } |
| 39 | return Effect.fn( |
| 40 | `AWS.BedrockAgentCore.StartCodeInterpreterSession(${codeInterpreter.LogicalId})`, |
| 41 | )(function* ({ |
| 42 | sessionTimeout, |
| 43 | ...request |
| 44 | }: StartCodeInterpreterSessionRequest) { |
| 45 | return yield* startCodeInterpreterSession({ |
| 46 | ...request, |
| 47 | sessionTimeoutSeconds: toWireSeconds(sessionTimeout), |
| 48 | codeInterpreterIdentifier: yield* Identifier, |
| 49 | }); |
| 50 | }); |
| 51 | }); |
| 52 | }), |
| 53 | ); |