| 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 `ConverseStream` request with the binding-injected `modelId` made |
| 7 | * optional: it defaults to the first bound model id and may be overridden |
| 8 | * per call with any of the bound model ids. |
| 9 | */ |
| 10 | export interface ConverseStreamRequest extends Omit< |
| 11 | bedrock.ConverseStreamRequest, |
| 12 | "modelId" |
| 13 | > { |
| 14 | /** |
| 15 | * The model to run inference on for this call. Must be one of the model |
| 16 | * ids the binding was created with (IAM is scoped to exactly those). |
| 17 | * @default the first bound model id |
| 18 | */ |
| 19 | modelId?: string; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Runtime binding for `bedrock-runtime:ConverseStream` — the streaming |
| 24 | * variant of {@link Converse}. The response arrives as an event `Stream` of |
| 25 | * `ConverseStreamOutput` events (`messageStart`, `contentBlockDelta`, |
| 26 | * `messageStop`, `metadata`, ...) instead of a single message. |
| 27 | * |
| 28 | * The binding grants the function `bedrock:InvokeModelWithResponseStream` |
| 29 | * (the IAM action streaming operations authorize against) scoped to exactly |
| 30 | * the bound models. A model reference may be a foundation-model id, a |
| 31 | * cross-region inference profile id (e.g. `us.amazon.nova-micro-v1:0`), or a |
| 32 | * full Bedrock ARN. |
| 33 | * |
| 34 | * Model access is an account entitlement — enable the model in the Bedrock |
| 35 | * console (Model access) before invoking, otherwise calls fail with |
| 36 | * `AccessDeniedException`. |
| 37 | * |
| 38 | * ### Streaming a Conversation |
| 39 | * **Example:** Aggregate Streamed Text Deltas |
| 40 | * ```typescript |
| 41 | * // init |
| 42 | * const converseStream = yield* Bedrock.ConverseStream("us.amazon.nova-micro-v1:0"); |
| 43 | * |
| 44 | * // runtime |
| 45 | * const result = yield* converseStream({ |
| 46 | * messages: [{ role: "user", content: [{ text: "Say hello." }] }], |
| 47 | * inferenceConfig: { maxTokens: 64 }, |
| 48 | * }); |
| 49 | * const events = yield* Stream.runCollect(result.stream ?? Stream.empty); |
| 50 | * const text = events |
| 51 | * .map((event) => event.contentBlockDelta?.delta.text ?? "") |
| 52 | * .join(""); |
| 53 | * ``` |
| 54 | * |
| 55 | * @binding |
| 56 | */ |
| 57 | export interface ConverseStream extends Binding.Service< |
| 58 | ConverseStream, |
| 59 | "AWS.Bedrock.ConverseStream", |
| 60 | ( |
| 61 | model: string, |
| 62 | ...additionalModels: string[] |
| 63 | ) => Effect.Effect< |
| 64 | ( |
| 65 | request: ConverseStreamRequest, |
| 66 | ) => Effect.Effect< |
| 67 | bedrock.ConverseStreamResponse, |
| 68 | bedrock.ConverseStreamError |
| 69 | > |
| 70 | > |
| 71 | > {} |
| 72 | export const ConverseStream = Binding.Service<ConverseStream>( |
| 73 | "AWS.Bedrock.ConverseStream", |
| 74 | ); |