The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as bedrock from "@distilled.cloud/aws/bedrock-runtime"; |
| 2 | import * as Binding from "alchemy/Binding"; |
| 3 | import { |
| 4 | AiError, |
| 5 | LanguageModel as AiLanguageModel, |
| 6 | IdGenerator, |
| 7 | Prompt, |
| 8 | Response, |
| 9 | Tool, |
| 10 | } from "effect/ai"; |
| 11 | import * as Context from "effect/Context"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Stream from "effect/Stream"; |
| 15 | import type { ConverseRequest } from "./Converse.ts"; |
| 16 | import type { ConverseStreamRequest } from "./ConverseStream.ts"; |
| 17 | |
| 18 | /** |
| 19 | * Inference parameters applied to every request made through the |
| 20 | * {@link LanguageModel}. They translate to the Converse API's |
| 21 | * `inferenceConfig` (plus `additionalModelRequestFields` for |
| 22 | * model-specific extensions like `top_k` or reasoning budgets). |
| 23 | */ |
| 24 | export interface LanguageModelParameters { |
| 25 | /** |
| 26 | * Maximum number of tokens the model may generate in the response. |
| 27 | */ |
| 28 | maxTokens?: number; |
| 29 | /** |
| 30 | * Sampling temperature. Lower values make the output more deterministic. |
| 31 | */ |
| 32 | temperature?: number; |
| 33 | /** |
| 34 | * Nucleus-sampling probability mass. |
| 35 | */ |
| 36 | topP?: number; |
| 37 | /** |
| 38 | * Sequences that stop generation when the model emits them. |
| 39 | */ |
| 40 | stopSequences?: string[]; |
| 41 | /** |
| 42 | * Model-specific request fields passed through verbatim as the Converse |
| 43 | * API's `additionalModelRequestFields` — e.g. `{ top_k: 50 }` for |
| 44 | * Anthropic models or `{ inferenceConfig: { topK: 5 } }` for Amazon Nova. |
| 45 | */ |
| 46 | additionalModelRequestFields?: unknown; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Options for constructing a Bedrock-backed Effect AI `LanguageModel`. |
| 51 | */ |
| 52 | export interface LanguageModelOptions { |
| 53 | /** |
| 54 | * Default inference parameters. Every field can be overridden per call |
| 55 | * with {@link withModelParameters}. |
| 56 | */ |
| 57 | parameters?: LanguageModelParameters; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Per-call overrides applied with {@link withModelParameters}: any |
| 62 | * {@link LanguageModelParameters} field plus the model to run the call on. |
| 63 | */ |
| 64 | export interface LanguageModelCallParameters extends LanguageModelParameters { |
| 65 | /** |
| 66 | * The model to run inference on for this call. Must be one of the model |
| 67 | * ids the {@link LanguageModel} binding was created with (IAM is scoped to |
| 68 | * exactly those). |
| 69 | * @default the first bound model id |
| 70 | */ |
| 71 | modelId?: string; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Fiber-scoped {@link LanguageModelCallParameters} consulted on every |
| 76 | * generateText / streamText call made through a Bedrock-backed |
| 77 | * `LanguageModel`. Set it for a region of your program with |
| 78 | * {@link withModelParameters}. |
| 79 | */ |
| 80 | export const CurrentModelParameters = Context.Reference<LanguageModelCallParameters>( |
| 81 | "AWS.Bedrock.CurrentModelParameters", |
| 82 | { |
| 83 | defaultValue: () => ({}), |
| 84 | }, |
| 85 | ); |
| 86 | |
| 87 | /** |
| 88 | * Scope per-call inference parameters (and optionally the target model) |
| 89 | * onto an Effect or Stream that talks to a Bedrock-backed `LanguageModel`. |
| 90 | * Defined fields override the binding's construction-time `parameters`; |
| 91 | * everything else falls through to those defaults. |
| 92 | * |
| 93 | * ```typescript |
| 94 | * const response = yield* LanguageModel.generateText({ prompt }).pipe( |
| 95 | * Bedrock.withModelParameters({ temperature: 0, maxTokens: 64 }), |
| 96 | * ); |
| 97 | * ``` |
| 98 | */ |
| 99 | export const withModelParameters = |
| 100 | (parameters: LanguageModelCallParameters) => |
| 101 | <S extends Effect.Effect<any, any, any> | Stream.Stream<any, any, any>>(self: S): S => |
| 102 | (Effect.isEffect(self) |
| 103 | ? Effect.provideService(CurrentModelParameters, parameters)(self) |
| 104 | : Stream.provideService( |
| 105 | CurrentModelParameters, |
| 106 | parameters, |
| 107 | )(self as Stream.Stream<any, any, any>)) as S; |
| 108 | |
| 109 | /** |
| 110 | * Runtime binding that turns an Amazon Bedrock model into an |
| 111 | * `effect/ai` {@link AiLanguageModel.LanguageModel} `Layer`, so any |
| 112 | * Effect AI program (`LanguageModel.generateText`, `streamText`, `Chat`, |
| 113 | * toolkits, ...) runs against Bedrock without code changes. |
| 114 | * |
| 115 | * Calls are translated to the Bedrock Converse API — Bedrock's unified |
| 116 | * messages API that works across all conversational foundation models |
| 117 | * (Amazon Nova, Anthropic Claude, Meta Llama, Mistral, ...) — so one binding |
| 118 | * covers every model. Bind one model or a list of models: the function is |
| 119 | * granted `bedrock:InvokeModel` and `bedrock:InvokeModelWithResponseStream` |
| 120 | * scoped to exactly those models, the first is the default, and runtime code |
| 121 | * picks between them (and tunes inference parameters) per call with |
| 122 | * {@link withModelParameters}. A model reference may be a foundation-model |
| 123 | * id, a cross-region inference profile id (e.g. `us.amazon.nova-micro-v1:0`), |
| 124 | * or a full Bedrock ARN. |
| 125 | * |
| 126 | * Model access is an account entitlement — enable the model in the Bedrock |
| 127 | * console (Model access) before invoking, otherwise calls fail with |
| 128 | * `AccessDeniedException`. Many newer models are only invocable through a |
| 129 | * cross-region inference profile id, not their bare foundation-model id. |
| 130 | * |
| 131 | * ### Effect AI on Bedrock |
| 132 | * **Example:** Generate Text |
| 133 | * ```typescript |
| 134 | * import { LanguageModel } from "effect/ai"; |
| 135 | * |
| 136 | * // init: bind the model and get a LanguageModel Layer |
| 137 | * const model = yield* Bedrock.LanguageModel("us.amazon.nova-micro-v1:0", { |
| 138 | * parameters: { maxTokens: 1024, temperature: 0.7 }, |
| 139 | * }); |
| 140 | * |
| 141 | * // runtime: any Effect AI program works against Bedrock |
| 142 | * const response = yield* LanguageModel.generateText({ |
| 143 | * prompt: "Say hello.", |
| 144 | * }).pipe(Effect.provide(model)); |
| 145 | * ``` |
| 146 | * |
| 147 | * **Example:** Stream Text |
| 148 | * ```typescript |
| 149 | * const parts = LanguageModel.streamText({ prompt }).pipe( |
| 150 | * Stream.provide(model), |
| 151 | * ); |
| 152 | * // parts is a Stream of text-start / text-delta / ... / finish parts |
| 153 | * ``` |
| 154 | * |
| 155 | * ### Runtime Configuration |
| 156 | * **Example:** Override Parameters Per Call |
| 157 | * The binding's `parameters` are only defaults — scope overrides onto any |
| 158 | * call with `withModelParameters`. |
| 159 | * ```typescript |
| 160 | * const response = yield* LanguageModel.generateText({ prompt }).pipe( |
| 161 | * Bedrock.withModelParameters({ temperature: 0, maxTokens: 64 }), |
| 162 | * ); |
| 163 | * ``` |
| 164 | * |
| 165 | * **Example:** Bind Multiple Models and Pick Per Call |
| 166 | * IAM access is fixed at deploy time (scoped to the bound list); which of |
| 167 | * those models serves a given request is a runtime decision. |
| 168 | * ```typescript |
| 169 | * // init: one Layer, IAM for both models, Nova Micro is the default |
| 170 | * const model = yield* Bedrock.LanguageModel([ |
| 171 | * "us.amazon.nova-micro-v1:0", |
| 172 | * "us.anthropic.claude-sonnet-4-20250514-v1:0", |
| 173 | * ]); |
| 174 | * |
| 175 | * // runtime: route this call to Claude |
| 176 | * const response = yield* LanguageModel.generateText({ prompt }).pipe( |
| 177 | * Bedrock.withModelParameters({ |
| 178 | * modelId: "us.anthropic.claude-sonnet-4-20250514-v1:0", |
| 179 | * }), |
| 180 | * ); |
| 181 | * ``` |
| 182 | * |
| 183 | * ### Tool Calling |
| 184 | * **Example:** Call Tools with a Toolkit |
| 185 | * ```typescript |
| 186 | * import { Tool, Toolkit } from "effect/ai"; |
| 187 | * import * as Schema from "effect/Schema"; |
| 188 | * |
| 189 | * const GetWeather = Tool.make("get_weather", { |
| 190 | * description: "Get the current weather for a city.", |
| 191 | * parameters: Schema.Struct({ city: Schema.String }), |
| 192 | * success: Schema.Struct({ temperatureF: Schema.Number }), |
| 193 | * }); |
| 194 | * const WeatherToolkit = Toolkit.make(GetWeather); |
| 195 | * |
| 196 | * const response = yield* LanguageModel.generateText({ |
| 197 | * prompt: "What's the weather in Seattle?", |
| 198 | * toolkit: WeatherToolkit, |
| 199 | * }).pipe( |
| 200 | * Effect.provide(WeatherToolkit.toLayer({ |
| 201 | * get_weather: ({ city }) => Effect.succeed({ temperatureF: 72 }), |
| 202 | * })), |
| 203 | * Effect.provide(model), |
| 204 | * ); |
| 205 | * ``` |
| 206 | * |
| 207 | * @binding |
| 208 | */ |
| 209 | export interface LanguageModel extends Binding.Service< |
| 210 | LanguageModel, |
| 211 | "AWS.Bedrock.LanguageModel", |
| 212 | ( |
| 213 | model: string | readonly [string, ...string[]], |
| 214 | options?: LanguageModelOptions, |
| 215 | ) => Effect.Effect<Layer.Layer<AiLanguageModel.LanguageModel>> |
| 216 | > {} |
| 217 | export const LanguageModel = Binding.Service<LanguageModel>("AWS.Bedrock.LanguageModel"); |
| 218 | |
| 219 | /** |
| 220 | * The already-bound Converse callables the adapter drives. Produced by |
| 221 | * `yield* Bedrock.Converse(model)` / `yield* Bedrock.ConverseStream(model)` |
| 222 | * (or any function of the same shape). |
| 223 | */ |
| 224 | export interface MakeLanguageModelOptions { |
| 225 | /** Non-streaming Converse callable (modelId already bound). */ |
| 226 | readonly converse: ( |
| 227 | request: ConverseRequest, |
| 228 | ) => Effect.Effect<bedrock.ConverseResponse, bedrock.ConverseError>; |
| 229 | /** Streaming Converse callable (modelId already bound). */ |
| 230 | readonly converseStream: ( |
| 231 | request: ConverseStreamRequest, |
| 232 | ) => Effect.Effect<bedrock.ConverseStreamResponse, bedrock.ConverseStreamError>; |
| 233 | /** Default inference parameters for every call. */ |
| 234 | readonly parameters?: LanguageModelParameters; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Provide an {@link AiLanguageModel.LanguageModel} layer backed by the |
| 239 | * supplied Bedrock Converse callables. |
| 240 | */ |
| 241 | export const makeLanguageModelLayer = ( |
| 242 | options: MakeLanguageModelOptions, |
| 243 | ): Layer.Layer<AiLanguageModel.LanguageModel> => |
| 244 | Layer.effect(AiLanguageModel.LanguageModel, makeLanguageModel(options)); |
| 245 | |
| 246 | /** |
| 247 | * Build an {@link AiLanguageModel.LanguageModel} that proxies generateText / |
| 248 | * streamText through the Bedrock Converse API. |
| 249 | */ |
| 250 | export const makeLanguageModel = ({ |
| 251 | converse, |
| 252 | converseStream, |
| 253 | parameters, |
| 254 | }: MakeLanguageModelOptions): Effect.Effect<AiLanguageModel.LanguageModel> => |
| 255 | AiLanguageModel.make({ |
| 256 | generateText: (options) => |
| 257 | Effect.gen(function* () { |
| 258 | // Read the fiber-scoped per-call overrides (withModelParameters) |
| 259 | // and merge them over the binding's construction-time defaults. |
| 260 | const call = yield* CurrentModelParameters; |
| 261 | const request = toConverseRequest({ |
| 262 | options, |
| 263 | parameters: mergeParameters(parameters, call), |
| 264 | modelId: call.modelId, |
| 265 | }); |
| 266 | const response = yield* converse(request).pipe( |
| 267 | Effect.mapError((cause) => toAiError(cause, "generateText")), |
| 268 | ); |
| 269 | return toResponseParts(response); |
| 270 | }), |
| 271 | streamText: (options) => |
| 272 | Stream.unwrap( |
| 273 | Effect.gen(function* () { |
| 274 | const idGen = yield* IdGenerator.IdGenerator; |
| 275 | const call = yield* CurrentModelParameters; |
| 276 | const request = toConverseRequest({ |
| 277 | options, |
| 278 | parameters: mergeParameters(parameters, call), |
| 279 | modelId: call.modelId, |
| 280 | }); |
| 281 | const response = yield* converseStream(request).pipe( |
| 282 | Effect.mapError((cause) => toAiError(cause, "streamText")), |
| 283 | ); |
| 284 | return parseStream(response, idGen); |
| 285 | }), |
| 286 | ), |
| 287 | }); |
| 288 | |
| 289 | // --------------------------------------------------------------------------- |
| 290 | // Prompt → Converse messages |
| 291 | // |
| 292 | // The Converse API separates system prompts from the message list, requires |
| 293 | // strict user/assistant alternation, and carries tool results inside *user* |
| 294 | // messages — so conversion merges consecutive same-role messages after |
| 295 | // mapping. |
| 296 | // --------------------------------------------------------------------------- |
| 297 | |
| 298 | const IMAGE_FORMATS: Record<string, bedrock.ImageFormat> = { |
| 299 | "image/png": "png", |
| 300 | "image/jpeg": "jpeg", |
| 301 | "image/gif": "gif", |
| 302 | "image/webp": "webp", |
| 303 | }; |
| 304 | |
| 305 | const base64ToUint8Array = (data: string): Uint8Array => { |
| 306 | const binary = atob(data); |
| 307 | const bytes = new Uint8Array(binary.length); |
| 308 | for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i); |
| 309 | return bytes; |
| 310 | }; |
| 311 | |
| 312 | const fileToImageBlock = ( |
| 313 | data: string | Uint8Array | URL, |
| 314 | mediaType: string, |
| 315 | ): bedrock.ContentBlock | undefined => { |
| 316 | const format = IMAGE_FORMATS[mediaType]; |
| 317 | // Converse only accepts inline bytes (or S3 locations) — remote URLs and |
| 318 | // non-image documents are not representable, so they are dropped. |
| 319 | if (format === undefined || data instanceof URL) return undefined; |
| 320 | const bytes = |
| 321 | data instanceof Uint8Array |
| 322 | ? data |
| 323 | : base64ToUint8Array(data.startsWith("data:") ? data.slice(data.indexOf(",") + 1) : data); |
| 324 | return { image: { format, source: { bytes } } }; |
| 325 | }; |
| 326 | |
| 327 | const toolCallInput = (params: unknown): unknown => { |
| 328 | if (typeof params !== "string") return params ?? {}; |
| 329 | try { |
| 330 | return JSON.parse(params); |
| 331 | } catch { |
| 332 | return {}; |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | interface ConverseMessages { |
| 337 | readonly system: bedrock.SystemContentBlock[]; |
| 338 | readonly messages: bedrock.Message[]; |
| 339 | } |
| 340 | |
| 341 | const toUserContent = (parts: Prompt.UserMessage["content"]): bedrock.ContentBlock[] => |
| 342 | parts.flatMap((p): bedrock.ContentBlock[] => { |
| 343 | if (p.type === "text") return p.text.length > 0 ? [{ text: p.text }] : []; |
| 344 | if (p.type === "file") { |
| 345 | const block = fileToImageBlock(p.data, p.mediaType); |
| 346 | return block === undefined ? [] : [block]; |
| 347 | } |
| 348 | return []; |
| 349 | }); |
| 350 | |
| 351 | const toAssistantContent = (parts: Prompt.AssistantMessage["content"]): bedrock.ContentBlock[] => |
| 352 | parts.flatMap((p): bedrock.ContentBlock[] => { |
| 353 | // Reasoning parts are not replayed: Bedrock requires the original |
| 354 | // cryptographic signature alongside replayed reasoning, which the |
| 355 | // framework does not round-trip. Models tolerate its absence. |
| 356 | if (p.type === "text") return p.text.length > 0 ? [{ text: p.text }] : []; |
| 357 | if (p.type === "tool-call") { |
| 358 | return [ |
| 359 | { |
| 360 | toolUse: { |
| 361 | toolUseId: p.id, |
| 362 | name: p.name, |
| 363 | input: toolCallInput(p.params), |
| 364 | }, |
| 365 | }, |
| 366 | ]; |
| 367 | } |
| 368 | return []; |
| 369 | }); |
| 370 | |
| 371 | const toToolResultContent = (parts: Prompt.ToolMessage["content"]): bedrock.ContentBlock[] => |
| 372 | parts.flatMap((p): bedrock.ContentBlock[] => |
| 373 | p.type === "tool-result" |
| 374 | ? [ |
| 375 | { |
| 376 | toolResult: { |
| 377 | toolUseId: p.id, |
| 378 | content: |
| 379 | typeof p.result === "string" ? [{ text: p.result }] : [{ json: p.result ?? {} }], |
| 380 | ...(p.isFailure ? { status: "error" as const } : {}), |
| 381 | }, |
| 382 | }, |
| 383 | ] |
| 384 | : [], |
| 385 | ); |
| 386 | |
| 387 | const convertPrompt = (prompt: Prompt.Prompt): ConverseMessages => { |
| 388 | const system: bedrock.SystemContentBlock[] = []; |
| 389 | const messages: bedrock.Message[] = []; |
| 390 | |
| 391 | const append = (role: bedrock.ConversationRole, content: bedrock.ContentBlock[]) => { |
| 392 | if (content.length === 0) return; |
| 393 | const last = messages[messages.length - 1]; |
| 394 | // Converse requires user/assistant alternation; merge consecutive |
| 395 | // same-role messages (tool results become user messages, so a tool |
| 396 | // message followed by a user message must fuse). |
| 397 | if (last !== undefined && last.role === role) { |
| 398 | last.content.push(...content); |
| 399 | } else { |
| 400 | messages.push({ role, content }); |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | for (const message of prompt.content) { |
| 405 | switch (message.role) { |
| 406 | case "system": |
| 407 | if (message.content.length > 0) system.push({ text: message.content }); |
| 408 | break; |
| 409 | case "user": |
| 410 | append("user", toUserContent(message.content)); |
| 411 | break; |
| 412 | case "assistant": |
| 413 | append("assistant", toAssistantContent(message.content)); |
| 414 | break; |
| 415 | case "tool": |
| 416 | append("user", toToolResultContent(message.content)); |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return { system, messages }; |
| 422 | }; |
| 423 | |
| 424 | // --------------------------------------------------------------------------- |
| 425 | // Tools / toolChoice |
| 426 | // --------------------------------------------------------------------------- |
| 427 | |
| 428 | const toToolSpec = (tool: Tool.Any): bedrock.Tool => ({ |
| 429 | toolSpec: { |
| 430 | name: tool.name, |
| 431 | description: Tool.getDescription(tool), |
| 432 | inputSchema: { json: Tool.getJsonSchema(tool) }, |
| 433 | }, |
| 434 | }); |
| 435 | |
| 436 | const hasToolBlocks = (messages: ReadonlyArray<bedrock.Message>): boolean => |
| 437 | messages.some((m) => |
| 438 | m.content.some((block) => block.toolUse !== undefined || block.toolResult !== undefined), |
| 439 | ); |
| 440 | |
| 441 | const toToolConfig = ( |
| 442 | tools: ReadonlyArray<Tool.Any>, |
| 443 | toolChoice: AiLanguageModel.ProviderOptions["toolChoice"], |
| 444 | messages: ReadonlyArray<bedrock.Message>, |
| 445 | ): bedrock.ToolConfiguration | undefined => { |
| 446 | if (tools.length === 0) return undefined; |
| 447 | const mapped = tools.map(toToolSpec); |
| 448 | |
| 449 | if (toolChoice === "none") { |
| 450 | // Converse has no "none" mode. Omit toolConfig entirely — unless the |
| 451 | // conversation already contains toolUse/toolResult blocks, which the API |
| 452 | // rejects without a toolConfig; then send the tools with auto choice. |
| 453 | return hasToolBlocks(messages) ? { tools: mapped, toolChoice: { auto: {} } } : undefined; |
| 454 | } |
| 455 | if (toolChoice === "required") { |
| 456 | return { tools: mapped, toolChoice: { any: {} } }; |
| 457 | } |
| 458 | if (typeof toolChoice === "object" && "tool" in toolChoice) { |
| 459 | return { |
| 460 | tools: mapped, |
| 461 | toolChoice: { tool: { name: toolChoice.tool } }, |
| 462 | }; |
| 463 | } |
| 464 | if (typeof toolChoice === "object" && "oneOf" in toolChoice) { |
| 465 | const allowed = new Set(toolChoice.oneOf); |
| 466 | return { |
| 467 | tools: mapped.filter((t) => t.toolSpec !== undefined && allowed.has(t.toolSpec.name)), |
| 468 | toolChoice: toolChoice.mode === "required" ? { any: {} } : { auto: {} }, |
| 469 | }; |
| 470 | } |
| 471 | return { tools: mapped, toolChoice: { auto: {} } }; |
| 472 | }; |
| 473 | |
| 474 | // --------------------------------------------------------------------------- |
| 475 | // Request assembly |
| 476 | // --------------------------------------------------------------------------- |
| 477 | |
| 478 | /** |
| 479 | * Merge per-call overrides over construction-time defaults, field by field — |
| 480 | * an undefined override field never clobbers a configured default. |
| 481 | */ |
| 482 | const mergeParameters = ( |
| 483 | defaults: LanguageModelParameters | undefined, |
| 484 | overrides: LanguageModelCallParameters, |
| 485 | ): LanguageModelParameters => ({ |
| 486 | maxTokens: overrides.maxTokens ?? defaults?.maxTokens, |
| 487 | temperature: overrides.temperature ?? defaults?.temperature, |
| 488 | topP: overrides.topP ?? defaults?.topP, |
| 489 | stopSequences: overrides.stopSequences ?? defaults?.stopSequences, |
| 490 | additionalModelRequestFields: |
| 491 | overrides.additionalModelRequestFields ?? defaults?.additionalModelRequestFields, |
| 492 | }); |
| 493 | |
| 494 | const toConverseRequest = ({ |
| 495 | options, |
| 496 | parameters, |
| 497 | modelId, |
| 498 | }: { |
| 499 | readonly options: AiLanguageModel.ProviderOptions; |
| 500 | readonly parameters: LanguageModelParameters | undefined; |
| 501 | readonly modelId?: string | undefined; |
| 502 | }): ConverseRequest & ConverseStreamRequest => { |
| 503 | const { system, messages } = convertPrompt(options.prompt); |
| 504 | const toolConfig = toToolConfig(options.tools, options.toolChoice, messages); |
| 505 | const inferenceConfig: bedrock.InferenceConfiguration = { |
| 506 | ...(parameters?.maxTokens !== undefined ? { maxTokens: parameters.maxTokens } : {}), |
| 507 | ...(parameters?.temperature !== undefined ? { temperature: parameters.temperature } : {}), |
| 508 | ...(parameters?.topP !== undefined ? { topP: parameters.topP } : {}), |
| 509 | ...(parameters?.stopSequences !== undefined ? { stopSequences: parameters.stopSequences } : {}), |
| 510 | }; |
| 511 | return { |
| 512 | messages, |
| 513 | ...(modelId !== undefined ? { modelId } : {}), |
| 514 | ...(system.length > 0 ? { system } : {}), |
| 515 | ...(Object.keys(inferenceConfig).length > 0 ? { inferenceConfig } : {}), |
| 516 | ...(toolConfig !== undefined ? { toolConfig } : {}), |
| 517 | ...(parameters?.additionalModelRequestFields !== undefined |
| 518 | ? { |
| 519 | additionalModelRequestFields: parameters.additionalModelRequestFields, |
| 520 | } |
| 521 | : {}), |
| 522 | }; |
| 523 | }; |
| 524 | |
| 525 | // --------------------------------------------------------------------------- |
| 526 | // Finish reason / usage mapping |
| 527 | // --------------------------------------------------------------------------- |
| 528 | |
| 529 | const mapStopReason = (raw: bedrock.StopReason | undefined): Response.FinishReason => { |
| 530 | switch (raw) { |
| 531 | case "end_turn": |
| 532 | case "stop_sequence": |
| 533 | return "stop"; |
| 534 | case "tool_use": |
| 535 | return "tool-calls"; |
| 536 | case "max_tokens": |
| 537 | case "model_context_window_exceeded": |
| 538 | return "length"; |
| 539 | case "guardrail_intervened": |
| 540 | case "content_filtered": |
| 541 | return "content-filter"; |
| 542 | case "malformed_model_output": |
| 543 | case "malformed_tool_use": |
| 544 | return "error"; |
| 545 | case undefined: |
| 546 | return "unknown"; |
| 547 | default: |
| 548 | return "other"; |
| 549 | } |
| 550 | }; |
| 551 | |
| 552 | const mapUsage = (usage: bedrock.TokenUsage | undefined): Response.Usage => { |
| 553 | // Bedrock's `inputTokens` excludes cache reads/writes, which are reported |
| 554 | // separately — so `total` is the sum of all three. |
| 555 | const input = usage?.inputTokens ?? 0; |
| 556 | const cacheRead = usage?.cacheReadInputTokens ?? 0; |
| 557 | const cacheWrite = usage?.cacheWriteInputTokens ?? 0; |
| 558 | return Response.Usage.make({ |
| 559 | inputTokens: { |
| 560 | uncached: input, |
| 561 | total: input + cacheRead + cacheWrite, |
| 562 | cacheRead, |
| 563 | cacheWrite, |
| 564 | }, |
| 565 | outputTokens: { |
| 566 | total: usage?.outputTokens ?? 0, |
| 567 | text: 0, |
| 568 | reasoning: 0, |
| 569 | }, |
| 570 | }); |
| 571 | }; |
| 572 | |
| 573 | // --------------------------------------------------------------------------- |
| 574 | // generateText: ConverseResponse → Response.PartEncoded[] |
| 575 | // --------------------------------------------------------------------------- |
| 576 | |
| 577 | const toResponseParts = (response: bedrock.ConverseResponse): Array<Response.PartEncoded> => { |
| 578 | const content = response.output.message?.content ?? []; |
| 579 | const parts = content.flatMap((block): Response.PartEncoded[] => { |
| 580 | if (block.text !== undefined && block.text.length > 0) { |
| 581 | return [{ type: "text", text: block.text }]; |
| 582 | } |
| 583 | if (block.reasoningContent?.reasoningText !== undefined) { |
| 584 | const text = block.reasoningContent.reasoningText.text; |
| 585 | return text.length > 0 ? [{ type: "reasoning", text }] : []; |
| 586 | } |
| 587 | if (block.toolUse !== undefined) { |
| 588 | return [ |
| 589 | { |
| 590 | type: "tool-call", |
| 591 | id: block.toolUse.toolUseId, |
| 592 | name: block.toolUse.name, |
| 593 | params: block.toolUse.input ?? {}, |
| 594 | }, |
| 595 | ]; |
| 596 | } |
| 597 | return []; |
| 598 | }); |
| 599 | return [ |
| 600 | ...parts, |
| 601 | { |
| 602 | type: "finish", |
| 603 | reason: mapStopReason(response.stopReason), |
| 604 | usage: mapUsage(response.usage), |
| 605 | response: undefined, |
| 606 | }, |
| 607 | ]; |
| 608 | }; |
| 609 | |
| 610 | // --------------------------------------------------------------------------- |
| 611 | // streamText: ConverseStreamOutput events → Stream<Response.StreamPartEncoded> |
| 612 | // |
| 613 | // Bedrock's stream is fully structured: content blocks are opened by index |
| 614 | // (`contentBlockStart` for tool use; text/reasoning open implicitly on the |
| 615 | // first delta), advanced by `contentBlockDelta`, and closed by |
| 616 | // `contentBlockStop`. `messageStop` carries the stop reason and `metadata` |
| 617 | // carries usage. State threads through `Stream.mapAccumEffect`; the |
| 618 | // per-event `parts` buffer is mutable but scoped to one event. |
| 619 | // --------------------------------------------------------------------------- |
| 620 | |
| 621 | interface OpenBlock { |
| 622 | readonly kind: "text" | "reasoning" | "tool"; |
| 623 | readonly id: string; |
| 624 | } |
| 625 | |
| 626 | interface StreamState { |
| 627 | readonly blocks: ReadonlyMap<number, OpenBlock>; |
| 628 | readonly usage: bedrock.TokenUsage | undefined; |
| 629 | readonly stopReason: bedrock.StopReason | undefined; |
| 630 | } |
| 631 | |
| 632 | const initialStreamState: StreamState = { |
| 633 | blocks: new Map(), |
| 634 | usage: undefined, |
| 635 | stopReason: undefined, |
| 636 | }; |
| 637 | |
| 638 | type StreamParts = Array<Response.StreamPartEncoded>; |
| 639 | |
| 640 | // Text/reasoning blocks open implicitly on their first delta; tool blocks |
| 641 | // are opened by handleBlockStart (Bedrock names them via contentBlockStart). |
| 642 | const openBlock = ( |
| 643 | state: StreamState, |
| 644 | index: number, |
| 645 | block: OpenBlock & { readonly kind: "text" | "reasoning" }, |
| 646 | parts: StreamParts, |
| 647 | ): StreamState => { |
| 648 | parts.push( |
| 649 | block.kind === "text" |
| 650 | ? { type: "text-start", id: block.id } |
| 651 | : { type: "reasoning-start", id: block.id }, |
| 652 | ); |
| 653 | const blocks = new Map(state.blocks); |
| 654 | blocks.set(index, block); |
| 655 | return { ...state, blocks }; |
| 656 | }; |
| 657 | |
| 658 | const closeBlock = (state: StreamState, index: number, parts: StreamParts): StreamState => { |
| 659 | const block = state.blocks.get(index); |
| 660 | if (block === undefined) return state; |
| 661 | parts.push( |
| 662 | block.kind === "text" |
| 663 | ? { type: "text-end", id: block.id } |
| 664 | : block.kind === "reasoning" |
| 665 | ? { type: "reasoning-end", id: block.id } |
| 666 | : { type: "tool-params-end", id: block.id }, |
| 667 | ); |
| 668 | const blocks = new Map(state.blocks); |
| 669 | blocks.delete(index); |
| 670 | return { ...state, blocks }; |
| 671 | }; |
| 672 | |
| 673 | const handleBlockStart = ( |
| 674 | state: StreamState, |
| 675 | event: bedrock.ContentBlockStartEvent, |
| 676 | parts: StreamParts, |
| 677 | ): StreamState => { |
| 678 | const toolUse = event.start.toolUse; |
| 679 | if (toolUse === undefined) return state; |
| 680 | parts.push({ |
| 681 | type: "tool-params-start", |
| 682 | id: toolUse.toolUseId, |
| 683 | name: toolUse.name, |
| 684 | }); |
| 685 | const blocks = new Map(state.blocks); |
| 686 | blocks.set(event.contentBlockIndex, { kind: "tool", id: toolUse.toolUseId }); |
| 687 | return { ...state, blocks }; |
| 688 | }; |
| 689 | |
| 690 | const handleBlockDelta = ( |
| 691 | state: StreamState, |
| 692 | event: bedrock.ContentBlockDeltaEvent, |
| 693 | parts: StreamParts, |
| 694 | idGen: IdGenerator.Service, |
| 695 | ): Effect.Effect<StreamState> => |
| 696 | Effect.gen(function* () { |
| 697 | const index = event.contentBlockIndex; |
| 698 | const delta = event.delta; |
| 699 | let s = state; |
| 700 | |
| 701 | if (delta.text !== undefined) { |
| 702 | let block = s.blocks.get(index); |
| 703 | if (block === undefined) { |
| 704 | const opened = { |
| 705 | kind: "text", |
| 706 | id: yield* idGen.generateId(), |
| 707 | } as const; |
| 708 | s = openBlock(s, index, opened, parts); |
| 709 | block = opened; |
| 710 | } |
| 711 | if (delta.text.length > 0) { |
| 712 | parts.push({ type: "text-delta", id: block.id, delta: delta.text }); |
| 713 | } |
| 714 | return s; |
| 715 | } |
| 716 | |
| 717 | if (delta.reasoningContent !== undefined) { |
| 718 | const text = delta.reasoningContent.text; |
| 719 | // Signature / redacted-content deltas carry no displayable text. |
| 720 | if (text === undefined) return s; |
| 721 | let block = s.blocks.get(index); |
| 722 | if (block === undefined) { |
| 723 | const opened = { |
| 724 | kind: "reasoning", |
| 725 | id: yield* idGen.generateId(), |
| 726 | } as const; |
| 727 | s = openBlock(s, index, opened, parts); |
| 728 | block = opened; |
| 729 | } |
| 730 | if (text.length > 0) { |
| 731 | parts.push({ type: "reasoning-delta", id: block.id, delta: text }); |
| 732 | } |
| 733 | return s; |
| 734 | } |
| 735 | |
| 736 | if (delta.toolUse !== undefined) { |
| 737 | const block = s.blocks.get(index); |
| 738 | if (block !== undefined && delta.toolUse.input.length > 0) { |
| 739 | parts.push({ |
| 740 | type: "tool-params-delta", |
| 741 | id: block.id, |
| 742 | delta: delta.toolUse.input, |
| 743 | }); |
| 744 | } |
| 745 | return s; |
| 746 | } |
| 747 | |
| 748 | return s; |
| 749 | }); |
| 750 | |
| 751 | const streamExceptionOf = (event: bedrock.ConverseStreamOutput): unknown => |
| 752 | event.internalServerException ?? |
| 753 | event.modelStreamErrorException ?? |
| 754 | event.validationException ?? |
| 755 | event.throttlingException ?? |
| 756 | event.serviceUnavailableException; |
| 757 | |
| 758 | const handleStreamEvent = ( |
| 759 | state: StreamState, |
| 760 | event: bedrock.ConverseStreamOutput, |
| 761 | idGen: IdGenerator.Service, |
| 762 | ): Effect.Effect< |
| 763 | readonly [StreamState, ReadonlyArray<Response.StreamPartEncoded>], |
| 764 | AiError.AiError |
| 765 | > => |
| 766 | Effect.gen(function* () { |
| 767 | const exception = streamExceptionOf(event); |
| 768 | if (exception !== undefined) { |
| 769 | return yield* toAiError(exception, "streamText"); |
| 770 | } |
| 771 | const parts: StreamParts = []; |
| 772 | let s = state; |
| 773 | if (event.contentBlockStart !== undefined) { |
| 774 | s = handleBlockStart(s, event.contentBlockStart, parts); |
| 775 | } else if (event.contentBlockDelta !== undefined) { |
| 776 | s = yield* handleBlockDelta(s, event.contentBlockDelta, parts, idGen); |
| 777 | } else if (event.contentBlockStop !== undefined) { |
| 778 | s = closeBlock(s, event.contentBlockStop.contentBlockIndex, parts); |
| 779 | } else if (event.messageStop !== undefined) { |
| 780 | s = { ...s, stopReason: event.messageStop.stopReason }; |
| 781 | } else if (event.metadata !== undefined) { |
| 782 | s = { ...s, usage: event.metadata.usage }; |
| 783 | } |
| 784 | return [s, parts] as const; |
| 785 | }); |
| 786 | |
| 787 | const finalizeStream = (state: StreamState): ReadonlyArray<Response.StreamPartEncoded> => { |
| 788 | const parts: StreamParts = []; |
| 789 | let s = state; |
| 790 | for (const index of [...s.blocks.keys()].sort((a, b) => a - b)) { |
| 791 | s = closeBlock(s, index, parts); |
| 792 | } |
| 793 | parts.push({ |
| 794 | type: "finish", |
| 795 | reason: mapStopReason(s.stopReason), |
| 796 | usage: mapUsage(s.usage), |
| 797 | response: undefined, |
| 798 | }); |
| 799 | return parts; |
| 800 | }; |
| 801 | |
| 802 | const parseStream = ( |
| 803 | response: bedrock.ConverseStreamResponse, |
| 804 | idGen: IdGenerator.Service, |
| 805 | ): Stream.Stream<Response.StreamPartEncoded, AiError.AiError> => { |
| 806 | const events = response.stream; |
| 807 | if (events === undefined) { |
| 808 | return Stream.fromIterable(finalizeStream(initialStreamState)); |
| 809 | } |
| 810 | return events.pipe( |
| 811 | Stream.mapError((cause) => toAiError(cause, "streamText")), |
| 812 | Stream.mapAccumEffect( |
| 813 | () => initialStreamState, |
| 814 | (state, event) => handleStreamEvent(state, event, idGen), |
| 815 | { onHalt: (state) => finalizeStream(state) }, |
| 816 | ), |
| 817 | ); |
| 818 | }; |
| 819 | |
| 820 | // --------------------------------------------------------------------------- |
| 821 | // Error mapping |
| 822 | // --------------------------------------------------------------------------- |
| 823 | |
| 824 | const toAiError = (cause: unknown, method: "generateText" | "streamText"): AiError.AiError => { |
| 825 | const tagged = cause as { _tag?: string; message?: string } | undefined; |
| 826 | const description = [ |
| 827 | tagged?._tag ?? (cause instanceof Error ? cause.name : undefined), |
| 828 | tagged?.message ?? "Bedrock Converse request failed", |
| 829 | ] |
| 830 | .filter((s): s is string => s !== undefined && s.length > 0) |
| 831 | .join(": "); |
| 832 | return AiError.AiError.make({ |
| 833 | module: "AWS.Bedrock.LanguageModel", |
| 834 | method, |
| 835 | reason: AiError.UnknownError.make({ description }), |
| 836 | }); |
| 837 | }; |