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
//! AWS Bedrock Converse API transport for trusty-code (#1021 phase 1).
//!
//! Why: trusty-code's agent loop only ever speaks `ChatRequest`/`ChatResponse`
//! through `LlmClientTrait`; running on AWS Bedrock (IAM-based auth, no
//! OpenRouter API key, private VPC) needs a transport that fulfils that same
//! contract by calling the Bedrock Converse API instead of OpenRouter's HTTP
//! endpoint. This module is that transport — ported from the WORKING
//! reference implementation in `trusty-review::llm::bedrock` (region
//! resolution, default AWS credential chain, Converse call shape), adapted to
//! tcode's multi-turn, many-tool conversational shape rather than
//! trusty-review's single-shot structured-output-forcing use case.
//! What: [`BedrockChatClient`] wraps an `aws_sdk_bedrockruntime::Client`.
//! `chat` converts the request via [`convert::build_converse_messages`] and
//! [`convert::build_tool_config`], calls `Converse` (non-streaming), maps SDK
//! errors to [`LlmError::Bedrock`], and converts the response back via
//! [`convert::converse_output_to_chat_response`]. Region resolves
//! `TRUSTY_AWS_REGION` > `AWS_REGION` > `us-east-1`; credentials come from the
//! standard AWS credential chain (env vars, `~/.aws/credentials`, IMDS, SSO —
//! so `AWS_PROFILE=cto` works with zero code changes). (#2260) When
//! `req`'s messages/tools carry `build_request`'s cache markers,
//! [`convert::build_converse_messages`]/[`convert::build_tool_config`] also
//! emit Bedrock-native `cachePoint` breakpoints via `cache` — see that
//! module's doc for the wire-shape translation and minimum-size guard.
//! Test: `bedrock::tests::*` (region resolution, message/tool-choice/response
//! conversion — all unit-level, no real AWS calls) plus an `#[ignore]`-gated
//! live Converse call.
use BehaviorVersion;
use Client as BedrockRuntimeClient;
use ;
/// 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, mirroring `trusty-review::llm::bedrock::resolve_bedrock_region`.
/// What: Returns the first non-empty value, in priority order: `explicit`,
/// then `TRUSTY_AWS_REGION`, then `AWS_REGION`, else `"us-east-1"`.
/// Test: `bedrock::tests::region_resolution_*`.
pub
/// AWS Bedrock Converse API transport.
///
/// Why: Satisfies the same `chat(&ChatRequest) -> Result<ChatResponse, ..>`
/// contract every other LLM transport in this crate exposes, so
/// `DispatchingLlmClient` (`super::dispatch`) can route `bedrock/*` model
/// slugs here without `AgentLoop` or any other caller knowing the backend
/// differs.
/// What: Holds a pre-built `BedrockRuntimeClient` and the resolved region.
/// `chat` builds a Converse request from `ChatRequest` (messages, tools,
/// tool_choice), sends it, and maps the response back.
/// Test: `bedrock::tests::*`.
/// Strip the `bedrock/` dispatch-routing prefix from a model slug, yielding
/// the bare id the Converse API's `model_id` parameter expects.
///
/// Why: `req.model` carries the FULL dispatch slug (e.g.
/// `bedrock/us.anthropic.claude-sonnet-4-6`) so `provider::provider_for` and
/// `DispatchingLlmClient::routes_to_bedrock` can pattern-match the `bedrock/`
/// prefix to pick this transport — but AWS Bedrock's Converse `model_id`
/// rejects that prefixed form outright: confirmed live against the `cto`
/// account/`us-east-1`, `aws bedrock-runtime converse --model-id
/// "bedrock/us.anthropic.claude-sonnet-4-6"` returns `ValidationException:
/// The provided model identifier is invalid`, while the bare id
/// (`us.anthropic.claude-sonnet-4-6`) succeeds. This is now the single
/// authoritative strip point — `chat` calls it right before `.model_id(...)`
/// so the provider is correct regardless of what any caller passes.
/// What: Returns `slug` with a leading `"bedrock/"` removed via
/// `str::strip_prefix`, or `slug` unchanged when there is no such prefix
/// (defensive passthrough — a caller that already hands over a bare id must
/// not be mangled or panic).
/// Test: `bedrock::tests::bedrock_model_id_strips_prefix`,
/// `bedrock::tests::bedrock_model_id_passthrough_without_prefix`.