| 1 | import type * as agentcore from "@distilled.cloud/aws/bedrock-agentcore"; |
| 2 | import type * as Duration from "effect/Duration"; |
| 3 | import type * as Effect from "effect/Effect"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import type { CodeInterpreter } from "./CodeInterpreter.ts"; |
| 6 | |
| 7 | export interface StartCodeInterpreterSessionRequest extends Omit< |
| 8 | agentcore.StartCodeInterpreterSessionRequest, |
| 9 | "codeInterpreterIdentifier" | "sessionTimeoutSeconds" |
| 10 | > { |
| 11 | /** |
| 12 | * How long the session may sit idle before AgentCore terminates it, e.g. |
| 13 | * `"5 minutes"` or `Duration.minutes(5)` (a bare number is milliseconds). |
| 14 | */ |
| 15 | sessionTimeout?: Duration.Input; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Starts an isolated code-execution session on a code interpreter. |
| 20 | * |
| 21 | * Bind a {@link CodeInterpreter} inside a function runtime to open sandboxed |
| 22 | * sessions; pair with `InvokeCodeInterpreter` to run code in the session and |
| 23 | * `StopCodeInterpreterSession` to end it. Provide |
| 24 | * `AgentCore.StartCodeInterpreterSessionHttp` on the Function effect to |
| 25 | * implement the binding. |
| 26 | * |
| 27 | * ### Running Code |
| 28 | * **Example:** Start a Session, Execute Code, Stop |
| 29 | * ```typescript |
| 30 | * // init |
| 31 | * const startSession = yield* AgentCore.StartCodeInterpreterSession(interpreter); |
| 32 | * const invoke = yield* AgentCore.InvokeCodeInterpreter(interpreter); |
| 33 | * const stopSession = yield* AgentCore.StopCodeInterpreterSession(interpreter); |
| 34 | * |
| 35 | * return { |
| 36 | * fetch: Effect.gen(function* () { |
| 37 | * // runtime |
| 38 | * const session = yield* startSession({ sessionTimeout: "5 minutes" }); |
| 39 | * const result = yield* invoke({ |
| 40 | * sessionId: session.sessionId, |
| 41 | * name: "executeCode", |
| 42 | * arguments: { language: "python", code: "print(21 * 2)" }, |
| 43 | * }); |
| 44 | * const chunks = yield* Stream.runCollect(result.stream); |
| 45 | * yield* stopSession({ sessionId: session.sessionId }); |
| 46 | * return HttpServerResponse.json({ chunks: Array.from(chunks) }); |
| 47 | * }), |
| 48 | * }; |
| 49 | * ``` |
| 50 | * |
| 51 | * @binding |
| 52 | */ |
| 53 | export interface StartCodeInterpreterSession extends Binding.Service< |
| 54 | StartCodeInterpreterSession, |
| 55 | "AWS.BedrockAgentCore.StartCodeInterpreterSession", |
| 56 | <R extends CodeInterpreter>( |
| 57 | codeInterpreter: R, |
| 58 | ) => Effect.Effect< |
| 59 | ( |
| 60 | request: StartCodeInterpreterSessionRequest, |
| 61 | ) => Effect.Effect< |
| 62 | agentcore.StartCodeInterpreterSessionResponse, |
| 63 | agentcore.StartCodeInterpreterSessionError |
| 64 | > |
| 65 | > |
| 66 | > {} |
| 67 | export const StartCodeInterpreterSession = |
| 68 | Binding.Service<StartCodeInterpreterSession>( |
| 69 | "AWS.BedrockAgentCore.StartCodeInterpreterSession", |
| 70 | ); |