1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! AWS Bedrock Converse API inference adapter (epic #2400 Wave 2, #2407).
//!
//! Why: Bedrock speaks the Anthropic-dialect Converse API (IAM-based auth, no
//! API key, private-VPC deployments), not the OpenAI-compatible
//! `/chat/completions` schema the [`super::providers::OpenAiCompatAdapter`]
//! family shares — so it needs its own [`InferenceAdapter`] implementation. This
//! module is that adapter, ported from tcode's WORKING `llm::bedrock` transport
//! (#2407) so the M3 bake-off `bedrock/us.anthropic.claude-sonnet-4-6` path is
//! preserved with no regression: the wire-format conversion (`convert`) and the
//! `cachePoint` prompt-cache translation (`cache`) are byte-for-byte the proven
//! logic, retargeted only from tcode's wire types to
//! [`crate::inference::types`] and from `LlmError` to [`InferenceError`].
//! What: [`BedrockAdapter`] wraps a lazily-constructed
//! `aws_sdk_bedrockruntime::Client` (so a `Configurator` that merely registers
//! the factory never touches AWS credentials — #2245) and implements
//! [`InferenceAdapter`]: `chat` converts the request via [`build_converse_parts`]
//! (which funnels [`convert::build_converse_messages`]/
//! [`convert::build_tool_config`]), calls the unary `Converse` operation, maps
//! SDK errors to [`InferenceError::Provider`], and converts the response back via
//! [`convert::converse_output_to_chat_response`]; `chat_stream` (#4426) sends the
//! SAME converted request to `ConverseStream` instead and maps its binary
//! event-stream framing into the neutral [`ChatStream`] via [`stream`], so a
//! `bedrock/*` turn streams token-by-token rather than falling back to the
//! trait's buffered default; `map_tool_choice` emits Converse's own tool-choice
//! JSON shape. Region resolves `TRUSTY_AWS_REGION` > `AWS_REGION` > `us-east-1`;
//! credentials come from the standard AWS credential chain (env,
//! `~/.aws/credentials`, IMDS, SSO — so `AWS_PROFILE=cto` works with zero code
//! changes). [`register_bedrock_factory`] wires the adapter into a
//! [`Configurator`] under [`ProviderId::Bedrock`].
//! Test: `super::tests::*` (region resolution, message/tool-choice/response
//! conversion, and the `stream_*` suite driving the real streaming path against
//! a scripted `ConverseStream` event sequence — all offline, no real AWS) plus
//! `#[ignore]`-gated live `Converse` and `ConverseStream` calls; the configurator
//! wiring is covered by `crates/trusty-common/tests/inference_bedrock.rs`.
pub
pub
pub
use async_trait;
use BehaviorVersion;
use RegionProviderChain;
use Client as BedrockRuntimeClient;
use ;
use DisplayErrorContext;
use Region;
use ;
use OnceCell;
use crateInferenceAdapter;
use crate;
use crateInferenceError;
use crate;
use crateChatStream;
use crate;
/// Region env var: trusty-specific override (checked before the standard
/// `AWS_REGION`).
const ENV_REGION_TRUSTY: &str = "TRUSTY_AWS_REGION";
/// Region env var: standard AWS fallback.
const ENV_REGION_AWS: &str = "AWS_REGION";
/// Default AWS region when neither env var is set.
const DEFAULT_REGION: &str = "us-east-1";
/// Resolve the AWS region for the Bedrock client.
///
/// Why: operators may specify region via either `TRUSTY_AWS_REGION`
/// (trusty-specific) or `AWS_REGION` (standard); the trusty var takes precedence.
/// What: returns the first non-empty value, in priority order: `explicit`, then
/// `TRUSTY_AWS_REGION`, then `AWS_REGION`, else `"us-east-1"`.
/// Test: `super::tests::region_resolution_*`.
pub
/// AWS Bedrock Converse API inference adapter.
///
/// Why: satisfies the shared [`InferenceAdapter`] contract so the configurator,
/// the agent loop, trusty-review, and tga can drive a `bedrock/*` model through
/// `Box<dyn InferenceAdapter>` identically to any OpenAI-dialect provider — the
/// Converse mechanics stay entirely inside this adapter.
/// What: holds the resolved region, a lazily-built `BedrockRuntimeClient` (a
/// `tokio::sync::OnceCell`, so construction touches no AWS credentials until the
/// first `chat` — a `Configurator` that merely registers the factory never hits
/// the AWS SDK, #2245), and the Bedrock [`ProviderCapabilities`].
/// Test: `super::tests::*`.
/// One [`ChatRequest`] converted into the four pieces both Converse operations
/// take (#4426).
///
/// Why: `Converse` and `ConverseStream` have DIFFERENT fluent-builder types, so
/// the request assembly cannot be shared by passing a builder around — but the
/// conversion itself must be shared, or the streaming and buffered transports
/// silently drift (a `cache_control` marker, a tool-pairing repair, or a
/// sampling knob honoured on one path and not the other is exactly the class of
/// bug that makes "streaming broke tool calls" reports). Producing the pieces
/// once and letting each caller mount them on its own builder keeps a single
/// conversion with two call sites.
/// What: `system` is Converse's system-prompt array (empty when the transcript
/// has no system content); `messages` is the alternation-safe, tool-pairing-
/// repaired conversation; `inference` carries `max_tokens`/`temperature`;
/// `tool_config` is `None` when the request declares no tools (or when the
/// tool-choice mapping says to omit it entirely).
/// Test: covered through both call sites — `super::tests::*` conversion tests
/// for the pieces and `super::tests::stream_*` for the streaming mount.
pub
/// Convert a [`ChatRequest`] into the shared Converse request pieces.
///
/// Why: see [`ConverseParts`] — this is the ONE conversion both
/// [`BedrockAdapter::chat`] and [`BedrockAdapter::chat_stream`] run, so the two
/// transports are guaranteed to send the same thing.
/// What: delegates messages/system to [`convert::build_converse_messages`] and
/// tools to [`convert::build_tool_config`] (only when `request.tools` is set),
/// and builds the [`InferenceConfiguration`] from `max_tokens`/`temperature`
/// via `set_*` so an absent knob omits the field rather than sending a default.
/// Errors propagate from the converters (an unrepresentable message or an
/// invalid tool schema).
/// Test: `super::tests::*` (message/tool conversion) and
/// `super::tests::stream_*`.
pub
/// Strip the `bedrock/` dispatch-routing prefix from a model slug, yielding the
/// bare id the Converse API's `model_id` parameter expects.
///
/// Why: `request.model` carries the FULL dispatch slug (e.g.
/// `bedrock/us.anthropic.claude-sonnet-4-6`) so provider resolution can
/// pattern-match the `bedrock/` prefix — but AWS Bedrock's Converse `model_id`
/// rejects that prefixed form outright (`ValidationException: The provided model
/// identifier is invalid`, confirmed live), while the bare id succeeds.
/// [`BedrockAdapter::chat`] calls this right before `.model_id(...)` so the value
/// sent to AWS is correct regardless of what any caller passes.
/// #4493: this was one of two hand-rolled per-provider copies of that rule
/// (Anthropic-direct had the other) while the OpenAI-dialect providers had none
/// — the reason `openai/gpt-4o-mini` reached `api.openai.com` prefixed. It now
/// delegates to the ONE shared implementation so the three cannot drift.
/// What: forwards to [`ProviderId::wire_model_id`] for [`ProviderId::Bedrock`],
/// which returns `slug` with a leading `"bedrock/"` removed, or `slug` unchanged
/// when there is no such prefix (defensive passthrough — a caller that already
/// hands over a bare id must not be mangled).
/// Test: `super::tests::bedrock_model_id_strips_prefix`,
/// `super::tests::bedrock_model_id_passthrough_without_prefix`.
pub
/// Build a Bedrock adapter for a resolved provider.
///
/// Why: the single construction path the factory funnels through. Bedrock
/// resolves with NO key (the AWS credential chain, not a [`KeyStore`] secret), so
/// unlike the keyed OpenAI-dialect factories this ignores `resolved.key()`
/// entirely.
/// What: builds a [`BedrockAdapter`] against the ambient region
/// (`TRUSTY_AWS_REGION`/`AWS_REGION`/default). Infallible — the AWS client is
/// constructed lazily on first `chat`.
/// Test: `crates/trusty-common/tests/inference_bedrock.rs`.
///
/// [`KeyStore`]: crate::credentials::KeyStore
/// Production factory: build a Bedrock adapter for a resolved provider.
///
/// Why: this is what [`register_bedrock_factory`] registers into a
/// [`Configurator`] so `build("bedrock/<slug>", &store)` yields a live Bedrock
/// adapter.
/// What: delegates to [`build`].
/// Test: `crates/trusty-common/tests/inference_bedrock.rs`.
/// Register the Bedrock adapter factory into `cfg` under [`ProviderId::Bedrock`].
///
/// Why: Bedrock is deliberately NOT part of
/// [`super::providers::register_default_factories`] (that entry point is the
/// OpenAI-dialect family). A consumer that wants Bedrock opts in with this one
/// explicit call, keeping the Anthropic-dialect wiring additive and separate
/// from the OpenAI-dialect registration seam (#2407/#2408).
/// What: registers [`factory`] under [`ProviderId::Bedrock`]; a later
/// registration for Bedrock replaces it.
/// Test: `crates/trusty-common/tests/inference_bedrock.rs::bedrock_factory_registers_and_builds`.