| 1 | import type * as bedrock from "@distilled.cloud/aws/bedrock-runtime"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | |
| 5 | /** |
| 6 | * The `InvokeModelWithResponseStream` request with the binding-injected |
| 7 | * `modelId` made optional: it defaults to the first bound model id and may |
| 8 | * be overridden per call with any of the bound model ids. |
| 9 | * |
| 10 | * `body` is the raw, model-specific request payload — there is no |
| 11 | * auto-marshalling. The response `body` is an event `Stream` whose `chunk` |
| 12 | * events carry raw model-specific bytes. |
| 13 | */ |
| 14 | export interface InvokeModelWithResponseStreamRequest extends Omit< |
| 15 | bedrock.InvokeModelWithResponseStreamRequest, |
| 16 | "modelId" |
| 17 | > { |
| 18 | /** |
| 19 | * The model to run inference on for this call. Must be one of the model |
| 20 | * ids the binding was created with (IAM is scoped to exactly those). |
| 21 | * @default the first bound model id |
| 22 | */ |
| 23 | modelId?: string; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Runtime binding for `bedrock-runtime:InvokeModelWithResponseStream` — the |
| 28 | * streaming variant of {@link InvokeModel}. The response arrives as an event |
| 29 | * `Stream` of `chunk` events carrying model-specific payload bytes. |
| 30 | * |
| 31 | * The binding grants the function `bedrock:InvokeModelWithResponseStream` |
| 32 | * (the IAM action streaming operations authorize against) scoped to exactly |
| 33 | * the bound models. A model reference may be a foundation-model id, a |
| 34 | * cross-region inference profile id (e.g. `us.amazon.nova-micro-v1:0`), or a |
| 35 | * full Bedrock ARN. |
| 36 | * |
| 37 | * Prefer {@link ConverseStream} for conversational models — it is |
| 38 | * model-agnostic and its events are typed. |
| 39 | * |
| 40 | * Model access is an account entitlement — enable the model in the Bedrock |
| 41 | * console (Model access) before invoking, otherwise calls fail with |
| 42 | * `AccessDeniedException`. |
| 43 | * |
| 44 | * ### Streaming a Model Response |
| 45 | * **Example:** Aggregate Streamed Chunks |
| 46 | * ```typescript |
| 47 | * // init |
| 48 | * const invokeModelStream = yield* Bedrock.InvokeModelWithResponseStream( |
| 49 | * "us.amazon.nova-micro-v1:0", |
| 50 | * ); |
| 51 | * |
| 52 | * // runtime — body is the raw Nova messages-v1 payload |
| 53 | * const result = yield* invokeModelStream({ |
| 54 | * contentType: "application/json", |
| 55 | * body: JSON.stringify({ |
| 56 | * messages: [{ role: "user", content: [{ text: "Say hello." }] }], |
| 57 | * inferenceConfig: { maxTokens: 64 }, |
| 58 | * }), |
| 59 | * }); |
| 60 | * const events = yield* Stream.runCollect(result.body); |
| 61 | * // each chunk's bytes is a model-specific JSON event |
| 62 | * ``` |
| 63 | * |
| 64 | * @binding |
| 65 | */ |
| 66 | export interface InvokeModelWithResponseStream extends Binding.Service< |
| 67 | InvokeModelWithResponseStream, |
| 68 | "AWS.Bedrock.InvokeModelWithResponseStream", |
| 69 | ( |
| 70 | model: string, |
| 71 | ...additionalModels: string[] |
| 72 | ) => Effect.Effect< |
| 73 | ( |
| 74 | request: InvokeModelWithResponseStreamRequest, |
| 75 | ) => Effect.Effect< |
| 76 | bedrock.InvokeModelWithResponseStreamResponse, |
| 77 | bedrock.InvokeModelWithResponseStreamError |
| 78 | > |
| 79 | > |
| 80 | > {} |
| 81 | export const InvokeModelWithResponseStream = |
| 82 | Binding.Service<InvokeModelWithResponseStream>( |
| 83 | "AWS.Bedrock.InvokeModelWithResponseStream", |
| 84 | ); |