1package provider
2
3// Amazon Bedrock Converse-Stream client.
4//
5// Endpoint: POST https://bedrock-runtime.{region}.amazonaws.com/model/{id}/converse-stream
6//
7// Auth: two paths supported.
8//
9// - **Bearer token** via AWS_BEARER_TOKEN_BEDROCK. Modern, simple,
10// `bedrock:CallWithBearerToken` IAM permission required.
11// - **SigV4 signing** via AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY
12// (+ optional AWS_SESSION_TOKEN for temporary credentials, e.g. SSO
13// or IRSA-issued). Hand-rolled here so we don't pull in aws-sdk-go-v2.
14//
15// If both are present, bearer wins (simpler / fewer moving parts).
16//
17// Wire format: AWS event-stream binary framing. Each message is:
18//
19// [4-byte total length BE]
20// [4-byte headers length BE]
21// [4-byte CRC32 of the 8-byte prelude]
22// [headers ...] each = [1B name-length][name][1B value-type][2B value-length][value]
23// [payload ...] payload-length = total - headers - 4 (trailer) - 12 (prelude+prelude-crc)
24// [4-byte CRC32 of message bytes minus trailer]
25//
26// We only consume the stream — never produce it — so we don't validate
27// CRCs (the TLS+TCP transport already protects in-flight bytes). We do
28// validate length-bounds to avoid OOB reads on a hostile / corrupted
29// stream.
30//
31// Event types we care about (from `:event-type` header):
32// - messageStart { role: "assistant" }
33// - contentBlockStart { contentBlockIndex, start: { toolUse: { toolUseId, name } } }
34// - contentBlockDelta { contentBlockIndex, delta: { text?, toolUse?: { input } } }
35// - contentBlockStop { contentBlockIndex }
36// - messageStop { stopReason }
37// - metadata { usage: { inputTokens, outputTokens } }
38
39import (
40 "bytes"
41 "context"
42 "crypto/hmac"
43 "crypto/sha256"
44 "encoding/binary"
45 "encoding/hex"
46 "encoding/json"
47 "fmt"
48 "io"
49 "net/http"
50 "net/url"
51 "os"
52 "sort"
53 "strings"
54 "time"
55)
56
57type bedrockClient struct {
58 // Auth mode is determined at construction time. Exactly one of
59 // bearerToken or sigv4 is populated.
60 bearerToken string
61 sigv4 *bedrockSigV4Creds
62
63 region string
64 baseURL string
65 http *http.Client
66}
67
68// bedrockSigV4Creds holds the IAM credentials used to sign each request.
69type bedrockSigV4Creds struct {
70 accessKeyID string
71 secretAccessKey string
72 sessionToken string // optional; STS / SSO / IRSA temp creds
73}
74
75// NewBedrockClient returns a Bedrock client.
76//
77// Auth resolution (first match wins):
78//
79// 1. apiKey == real bearer-ish string (not "<aws>") -> bearer route.
80// 2. AWS_BEARER_TOKEN_BEDROCK env var -> bearer route.
81// 3. AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY (+ optional
82// AWS_SESSION_TOKEN) -> SigV4 route.
83// 4. AWS_PROFILE -> read ~/.aws/credentials, take that profile's keys.
84//
85// region defaults to us-east-1 unless AWS_REGION / AWS_DEFAULT_REGION is
86// set or the baseURL embeds a region.
87func NewBedrockClient(apiKey, baseURL string) Client {
88 region := os.Getenv("AWS_REGION")
89 if region == "" {
90 region = os.Getenv("AWS_DEFAULT_REGION")
91 }
92 if region == "" {
93 region = "us-east-1"
94 }
95 if baseURL == "" {
96 baseURL = "https://bedrock-runtime." + region + ".amazonaws.com"
97 }
98 c := &bedrockClient{
99 region: region,
100 baseURL: strings.TrimRight(baseURL, "/"),
101 http: &http.Client{Timeout: 0},
102 }
103
104 // Bearer route.
105 token := apiKey
106 if token == "" || token == "<aws>" {
107 token = os.Getenv("AWS_BEARER_TOKEN_BEDROCK")
108 }
109 if token != "" && token != "<aws>" {
110 c.bearerToken = token
111 return c
112 }
113
114 // SigV4 route: env vars first.
115 ak := os.Getenv("AWS_ACCESS_KEY_ID")
116 sk := os.Getenv("AWS_SECRET_ACCESS_KEY")
117 st := os.Getenv("AWS_SESSION_TOKEN")
118 if ak != "" && sk != "" {
119 c.sigv4 = &bedrockSigV4Creds{accessKeyID: ak, secretAccessKey: sk, sessionToken: st}
120 return c
121 }
122
123 // SigV4 route: ~/.aws/credentials via AWS_PROFILE.
124 if profile := os.Getenv("AWS_PROFILE"); profile != "" {
125 if creds, err := readAWSCredentialsFile(profile); err == nil {
126 c.sigv4 = creds
127 return c
128 }
129 }
130
131 return &unimplementedClient{
132 name: "amazon-bedrock",
133 hint: "no Bedrock credentials found (set AWS_BEARER_TOKEN_BEDROCK, AWS_ACCESS_KEY_ID+AWS_SECRET_ACCESS_KEY, or AWS_PROFILE)",
134 }
135}
136
137// readAWSCredentialsFile parses ~/.aws/credentials and returns the
138// access-key/secret-key (and optional session-token) for the named
139// profile. The file is an INI-like format with `[profile]` headers.
140func readAWSCredentialsFile(profile string) (*bedrockSigV4Creds, error) {
141 home, err := os.UserHomeDir()
142 if err != nil {
143 return nil, err
144 }
145 b, err := os.ReadFile(home + "/.aws/credentials")
146 if err != nil {
147 return nil, err
148 }
149 var current string
150 creds := map[string]map[string]string{}
151 for _, line := range strings.Split(string(b), "\n") {
152 line = strings.TrimSpace(line)
153 if line == "" || strings.HasPrefix(line, "#") {
154 continue
155 }
156 if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
157 current = strings.TrimSpace(line[1 : len(line)-1])
158 creds[current] = map[string]string{}
159 continue
160 }
161 if current == "" {
162 continue
163 }
164 eq := strings.IndexByte(line, '=')
165 if eq < 0 {
166 continue
167 }
168 k := strings.TrimSpace(line[:eq])
169 v := strings.TrimSpace(line[eq+1:])
170 creds[current][k] = v
171 }
172 p, ok := creds[profile]
173 if !ok {
174 return nil, fmt.Errorf("aws profile %q not found in ~/.aws/credentials", profile)
175 }
176 ak := p["aws_access_key_id"]
177 sk := p["aws_secret_access_key"]
178 if ak == "" || sk == "" {
179 return nil, fmt.Errorf("aws profile %q missing aws_access_key_id or aws_secret_access_key", profile)
180 }
181 return &bedrockSigV4Creds{
182 accessKeyID: ak,
183 secretAccessKey: sk,
184 sessionToken: p["aws_session_token"],
185 }, nil
186}
187
188func (c *bedrockClient) Name() string { return "amazon-bedrock" }
189
190// ---- request building ----
191
192type bedrockMessage struct {
193 Role string `json:"role"`
194 Content []map[string]interface{} `json:"content"`
195}
196
197// bedrockCachePoint is the Converse API cache checkpoint content block.
198// Appending this to a system or message content array tells Bedrock to
199// create a cache boundary at that position in the prompt prefix.
200var bedrockCachePoint = map[string]interface{}{
201 "cachePoint": map[string]interface{}{"type": "default"},
202}
203
204type bedrockToolSpec struct {
205 ToolSpec struct {
206 Name string `json:"name"`
207 Description string `json:"description,omitempty"`
208 InputSchema struct {
209 JSON json.RawMessage `json:"json"`
210 } `json:"inputSchema"`
211 } `json:"toolSpec"`
212}
213
214type bedrockRequest struct {
215 Messages []bedrockMessage `json:"messages"`
216 System []map[string]interface{} `json:"system,omitempty"`
217 InferenceConfig struct {
218 MaxTokens int `json:"maxTokens,omitempty"`
219 Temperature *float32 `json:"temperature,omitempty"`
220 } `json:"inferenceConfig,omitempty"`
221 ToolConfig *struct {
222 Tools []bedrockToolSpec `json:"tools"`
223 } `json:"toolConfig,omitempty"`
224}
225
226func normalizeBedrockToolResults(msgs []Message) []Message {
227 resultByID := map[string]ToolResultBlock{}
228 for _, m := range msgs {
229 for _, c := range m.Content {
230 if tr, ok := c.(ToolResultBlock); ok {
231 if _, exists := resultByID[tr.CallID]; !exists {
232 resultByID[tr.CallID] = tr
233 }
234 }
235 }
236 }
237
238 out := make([]Message, 0, len(msgs))
239 for _, m := range msgs {
240 copy := m
241 copy.Content = nil
242 var toolCalls []ToolCallBlock
243 for _, c := range m.Content {
244 switch v := c.(type) {
245 case ToolResultBlock:
246 // Bedrock requires toolResult blocks immediately after the
247 // assistant toolUse they answer. Reinsert them from the
248 // assistant pass below instead of preserving their original
249 // location, which may be separated by user text in active
250 // sessions.
251 continue
252 case ToolCallBlock:
253 toolCalls = append(toolCalls, v)
254 }
255 copy.Content = append(copy.Content, c)
256 }
257 if len(copy.Content) > 0 {
258 out = append(out, copy)
259 }
260 if m.Role != RoleAssistant || len(toolCalls) == 0 {
261 continue
262 }
263
264 results := make([]Content, 0, len(toolCalls))
265 for _, tc := range toolCalls {
266 if tr, ok := resultByID[tc.ID]; ok {
267 results = append(results, tr)
268 continue
269 }
270 results = append(results, ToolResultBlock{
271 CallID: tc.ID,
272 IsError: true,
273 Content: []Content{TextBlock{Text: "tool call did not complete before the next user message"}},
274 })
275 }
276 out = append(out, Message{Role: RoleTool, Content: results, Time: m.Time})
277 }
278 return out
279}
280
281// bedrockModelSupportsCaching reports whether the resolved model ID
282// supports explicit prompt caching via cachePoint markers on Bedrock.
283// We use PriceCacheWrite > 0 as a proxy: every Bedrock-hosted Claude
284// model with a write price in the catalog supports cachePoint markers.
285// Nova models use automatic caching and don't need explicit markers.
286func bedrockModelSupportsCaching(modelID string) bool {
287 // Strip geo prefix (us./eu./apac./au./global.) before catalog lookup.
288 for _, p := range bedrockGeoPrefixes {
289 if strings.HasPrefix(modelID, p+".") {
290 modelID = modelID[len(p)+1:]
291 break
292 }
293 }
294 if m, err := FindModel("amazon-bedrock", modelID); err == nil {
295 return m.PriceCacheWrite > 0
296 }
297 // Unknown model: enable for Anthropic Claude families — cachePoint is
298 // silently ignored by the API if the model doesn't support it.
299 return strings.HasPrefix(modelID, "anthropic.claude-")
300}
301
302func (c *bedrockClient) buildRequest(req Request) (*bedrockRequest, error) {
303 out := &bedrockRequest{}
304
305 // Resolve the model ID as it will appear on the wire so the caching
306 // check operates on the same ID used for FindModel.
307 resolvedModel := resolveBedrockInferenceProfileID(req.Model, c.region)
308 caching := bedrockModelSupportsCaching(resolvedModel)
309
310 if req.System != "" {
311 sysBlock := map[string]interface{}{"text": req.System}
312 if caching {
313 // Append cachePoint after the system text so the stable system
314 // prompt is cached as the first breakpoint.
315 out.System = []map[string]interface{}{sysBlock, bedrockCachePoint}
316 } else {
317 out.System = []map[string]interface{}{sysBlock}
318 }
319 }
320 out.InferenceConfig.Temperature = req.Temperature
321 out.InferenceConfig.MaxTokens = req.MaxTokens
322 if out.InferenceConfig.MaxTokens == 0 {
323 out.InferenceConfig.MaxTokens = 4096
324 }
325 for _, m := range normalizeBedrockToolResults(req.Messages) {
326 role := string(m.Role)
327 if role == "tool" {
328 role = "user"
329 }
330 bm := bedrockMessage{Role: role}
331 for _, c := range m.Content {
332 switch v := c.(type) {
333 case TextBlock:
334 bm.Content = append(bm.Content, map[string]interface{}{"text": v.Text})
335 case ToolCallBlock:
336 var input map[string]interface{}
337 _ = json.Unmarshal(v.Arguments, &input)
338 if input == nil {
339 input = map[string]interface{}{}
340 }
341 bm.Content = append(bm.Content, map[string]interface{}{
342 "toolUse": map[string]interface{}{
343 "toolUseId": v.ID, "name": v.Name, "input": input,
344 },
345 })
346 case ToolResultBlock:
347 var resultContent []map[string]interface{}
348 for _, inner := range v.Content {
349 if tb, ok := inner.(TextBlock); ok {
350 resultContent = append(resultContent, map[string]interface{}{"text": tb.Text})
351 }
352 }
353 status := "success"
354 if v.IsError {
355 status = "error"
356 }
357 bm.Content = append(bm.Content, map[string]interface{}{
358 "toolResult": map[string]interface{}{
359 "toolUseId": v.CallID,
360 "content": resultContent,
361 "status": status,
362 },
363 })
364 }
365 }
366 if len(bm.Content) == 0 {
367 continue
368 }
369 out.Messages = append(out.Messages, bm)
370 }
371 if len(req.Tools) > 0 {
372 tc := struct {
373 Tools []bedrockToolSpec `json:"tools"`
374 }{}
375 for _, t := range req.Tools {
376 var ts bedrockToolSpec
377 ts.ToolSpec.Name = t.Name
378 ts.ToolSpec.Description = t.Description
379 ts.ToolSpec.InputSchema.JSON = t.Schema
380 tc.Tools = append(tc.Tools, ts)
381 }
382 out.ToolConfig = &tc
383 }
384
385 if caching {
386 // Tag the last user message with a cachePoint. This extends the
387 // cached prefix to cover the full conversation history up to the
388 // current turn, so the next turn reads that history cheaply.
389 bedrockTagLastUserCache(out.Messages)
390 }
391
392 return out, nil
393}
394
395// bedrockTagLastUserCache appends a cachePoint block to the last user
396// message in the Bedrock message list. It is the Bedrock equivalent of
397// Anthropic's cache_control:{type:"ephemeral"} on the last user message.
398func bedrockTagLastUserCache(msgs []bedrockMessage) {
399 for i := len(msgs) - 1; i >= 0; i-- {
400 if msgs[i].Role == "user" {
401 msgs[i].Content = append(msgs[i].Content, bedrockCachePoint)
402 return
403 }
404 }
405}
406
407// resolveBedrockInferenceProfileID maps a bare foundation-model ID to
408// its region-matched cross-region inference-profile ID.
409//
410// Several newer Bedrock models (Anthropic Claude 4.x, DeepSeek, etc.)
411// cannot be invoked with on-demand throughput by their plain
412// foundation-model ID; Bedrock returns HTTP 400 demanding "the ID or
413// ARN of an inference profile that contains this model". The profile
414// ID is the same model ID with a geographic prefix (us/eu/apac/...).
415//
416// We only rewrite IDs that (a) lack an existing geo prefix and (b)
417// belong to a model family that requires a profile. IDs that already
418// carry a prefix (e.g. "eu.anthropic...", "global.anthropic...") or
419// fully-qualified ARNs are returned unchanged, so explicit user
420// choices and custom application inference profiles still work.
421func resolveBedrockInferenceProfileID(modelID, region string) string {
422 if modelID == "" {
423 return modelID
424 }
425 // ARNs are already inference-profile references; leave untouched.
426 if strings.HasPrefix(modelID, "arn:") {
427 return modelID
428 }
429 // Already geo-prefixed (us. / eu. / apac. / ap. / us-gov. / global.)?
430 if bedrockHasGeoPrefix(modelID) {
431 return modelID
432 }
433 if !bedrockRequiresInferenceProfile(modelID) {
434 return modelID
435 }
436 prefix := bedrockGeoPrefixForRegion(region)
437 if prefix == "" {
438 return modelID
439 }
440 return prefix + "." + modelID
441}
442
443// bedrockGeoPrefixes are the cross-region inference-profile geo
444// prefixes Bedrock uses. A model ID that starts with one of these
445// (followed by a dot) is already a profile reference.
446var bedrockGeoPrefixes = []string{"us-gov", "us", "eu", "apac", "ap", "global", "au"}
447
448func bedrockHasGeoPrefix(modelID string) bool {
449 for _, p := range bedrockGeoPrefixes {
450 if strings.HasPrefix(modelID, p+".") {
451 return true
452 }
453 }
454 return false
455}
456
457// bedrockRequiresInferenceProfile reports whether a bare
458// foundation-model ID is one of the families AWS only exposes through
459// a cross-region inference profile for on-demand throughput.
460func bedrockRequiresInferenceProfile(modelID string) bool {
461 switch {
462 case strings.HasPrefix(modelID, "anthropic.claude-"):
463 return true
464 case strings.HasPrefix(modelID, "deepseek."):
465 return true
466 default:
467 return false
468 }
469}
470
471// bedrockGeoPrefixForRegion maps an AWS region to the geo prefix used
472// by its cross-region inference profiles. Returns "" when the region
473// has no known mapping, in which case the model ID is left unchanged.
474func bedrockGeoPrefixForRegion(region string) string {
475 switch {
476 case region == "":
477 return "us"
478 case strings.HasPrefix(region, "us-gov-"):
479 return "us-gov"
480 case strings.HasPrefix(region, "us-"):
481 return "us"
482 case strings.HasPrefix(region, "eu-"):
483 return "eu"
484 case strings.HasPrefix(region, "ap-"):
485 return "apac"
486 case strings.HasPrefix(region, "ca-"):
487 return "us"
488 case strings.HasPrefix(region, "sa-"):
489 return "us"
490 default:
491 return "us"
492 }
493}
494
495func (c *bedrockClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
496 wire, err := c.buildRequest(req)
497 if err != nil {
498 return nil, err
499 }
500 body, err := json.Marshal(wire)
501 if err != nil {
502 return nil, err
503 }
504 modelID := resolveBedrockInferenceProfileID(req.Model, c.region)
505 url := c.baseURL + "/model/" + modelID + "/converse-stream"
506 httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
507 if err != nil {
508 return nil, err
509 }
510 httpReq.Header.Set("content-type", "application/json")
511 httpReq.Header.Set("accept", "application/vnd.amazon.eventstream")
512 if c.bearerToken != "" {
513 httpReq.Header.Set("authorization", "Bearer "+c.bearerToken)
514 } else if c.sigv4 != nil {
515 if err := signSigV4(httpReq, body, "bedrock", c.region, c.sigv4, time.Now().UTC()); err != nil {
516 return nil, fmt.Errorf("bedrock: sigv4 sign: %w", err)
517 }
518 } else {
519 return nil, fmt.Errorf("bedrock: no auth configured")
520 }
521 resp, err := c.http.Do(httpReq)
522 if err != nil {
523 return nil, fmt.Errorf("bedrock: %w", err)
524 }
525 if resp.StatusCode != http.StatusOK {
526 b, _ := io.ReadAll(resp.Body)
527 resp.Body.Close()
528 msg := strings.TrimSpace(string(b))
529 // A 403 on the bearer route is almost always a region mismatch:
530 // short-term Bedrock API keys are scoped to the region of the
531 // console session that minted them, but zot defaults to
532 // us-east-1. Surface the resolved region and the fix so the user
533 // is not left guessing why a freshly-copied key is "invalid".
534 if resp.StatusCode == http.StatusForbidden && c.bearerToken != "" {
535 return nil, fmt.Errorf(
536 "bedrock: http 403 (region=%s): %s\nhint: Bedrock API keys are region-scoped. If your key was created in another region, set AWS_REGION (e.g. AWS_REGION=eu-central-1) or pass --base-url https://bedrock-runtime.<region>.amazonaws.com",
537 c.region, msg)
538 }
539 return nil, fmt.Errorf("bedrock: http %d: %s", resp.StatusCode, msg)
540 }
541 out := make(chan Event, 16)
542 go c.runStream(ctx, resp, req, out)
543 return out, nil
544}
545
546func (c *bedrockClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
547 defer close(out)
548 defer resp.Body.Close()
549
550 out <- EventStart{Provider: "amazon-bedrock", Model: req.Model}
551
552 // State accumulated across deltas.
553 contentBlocks := map[int]*bedrockBlockState{}
554 stop := StopEnd
555 finalMsg := Message{Role: RoleAssistant, Time: time.Now()}
556 var usage Usage
557
558 for {
559 if ctx.Err() != nil {
560 stop = StopAborted
561 break
562 }
563 evt, err := readEventStreamMessage(resp.Body)
564 if err != nil {
565 if err == io.EOF {
566 break
567 }
568 out <- EventDone{Stop: StopError, Err: err, Message: finalMsg}
569 return
570 }
571 eventType := evt.headerString(":event-type")
572 if eventType == "" {
573 eventType = bedrockEventTypeFromPayload(evt.payload)
574 }
575 messageType := evt.headerString(":message-type")
576 if messageType == "exception" {
577 out <- EventDone{Stop: StopError, Err: fmt.Errorf("bedrock exception (%s): %s", evt.headerString(":exception-type"), string(evt.payload)), Message: finalMsg}
578 return
579 }
580 switch eventType {
581 case "messageStart":
582 // nothing to do; role is always "assistant"
583 case "contentBlockStart":
584 var d struct {
585 ContentBlockIndex int `json:"contentBlockIndex"`
586 Start struct {
587 ToolUse *struct {
588 ToolUseID string `json:"toolUseId"`
589 Name string `json:"name"`
590 } `json:"toolUse"`
591 } `json:"start"`
592 }
593 if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockStart", &d); err != nil {
594 continue
595 }
596 st := &bedrockBlockState{}
597 contentBlocks[d.ContentBlockIndex] = st
598 if d.Start.ToolUse != nil {
599 st.isToolUse = true
600 st.toolID = d.Start.ToolUse.ToolUseID
601 st.toolName = d.Start.ToolUse.Name
602 out <- EventToolStart{ID: st.toolID, Name: st.toolName}
603 }
604 case "contentBlockDelta":
605 var d struct {
606 ContentBlockIndex int `json:"contentBlockIndex"`
607 Delta struct {
608 Text string `json:"text"`
609 ToolUse *struct {
610 Input string `json:"input"`
611 } `json:"toolUse"`
612 } `json:"delta"`
613 }
614 if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockDelta", &d); err != nil {
615 continue
616 }
617 st := contentBlocks[d.ContentBlockIndex]
618 if st == nil {
619 st = &bedrockBlockState{}
620 contentBlocks[d.ContentBlockIndex] = st
621 }
622 if d.Delta.Text != "" {
623 st.text.WriteString(d.Delta.Text)
624 out <- EventTextDelta{Delta: d.Delta.Text}
625 }
626 if d.Delta.ToolUse != nil && d.Delta.ToolUse.Input != "" {
627 st.toolArgs.WriteString(d.Delta.ToolUse.Input)
628 out <- EventToolArgs{ID: st.toolID, Delta: d.Delta.ToolUse.Input}
629 }
630 case "contentBlockStop":
631 var d struct {
632 ContentBlockIndex int `json:"contentBlockIndex"`
633 }
634 if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockStop", &d); err != nil {
635 continue
636 }
637 st := contentBlocks[d.ContentBlockIndex]
638 if st == nil {
639 continue
640 }
641 if st.isToolUse {
642 args := st.toolArgs.String()
643 if args == "" {
644 args = "{}"
645 }
646 finalMsg.Content = append(finalMsg.Content, ToolCallBlock{
647 ID: st.toolID, Name: st.toolName, Arguments: json.RawMessage(args),
648 })
649 out <- EventToolEnd{ID: st.toolID}
650 } else if st.text.Len() > 0 {
651 finalMsg.Content = append(finalMsg.Content, TextBlock{Text: st.text.String()})
652 }
653 case "messageStop":
654 var d struct {
655 StopReason string `json:"stopReason"`
656 }
657 _ = unmarshalBedrockEventPayload(evt.payload, "messageStop", &d)
658 switch d.StopReason {
659 case "tool_use":
660 stop = StopToolUse
661 case "end_turn":
662 stop = StopEnd
663 case "max_tokens":
664 stop = StopLength
665 default:
666 stop = StopEnd
667 }
668 case "metadata":
669 var d struct {
670 Usage struct {
671 InputTokens int `json:"inputTokens"`
672 OutputTokens int `json:"outputTokens"`
673 CacheReadInputTokens int `json:"cacheReadInputTokens"`
674 CacheWriteInputTokens int `json:"cacheWriteInputTokens"`
675 } `json:"usage"`
676 }
677 if err := unmarshalBedrockEventPayload(evt.payload, "metadata", &d); err == nil {
678 usage.InputTokens = d.Usage.InputTokens
679 usage.OutputTokens = d.Usage.OutputTokens
680 usage.CacheReadTokens = d.Usage.CacheReadInputTokens
681 usage.CacheWriteTokens = d.Usage.CacheWriteInputTokens
682 if m, err := FindModel("amazon-bedrock", req.Model); err == nil {
683 usage.CostUSD = ComputeCost(m, usage)
684 }
685 out <- EventUsage{Usage: usage}
686 }
687 }
688 }
689 out <- EventDone{Stop: stop, Message: finalMsg}
690}
691
692type bedrockBlockState struct {
693 isToolUse bool
694 toolID string
695 toolName string
696 toolArgs strings.Builder
697 text strings.Builder
698}
699
700func bedrockEventTypeFromPayload(payload []byte) string {
701 var outer map[string]json.RawMessage
702 if err := json.Unmarshal(payload, &outer); err != nil {
703 return ""
704 }
705 for _, name := range []string{"messageStart", "contentBlockStart", "contentBlockDelta", "contentBlockStop", "messageStop", "metadata"} {
706 if _, ok := outer[name]; ok {
707 return name
708 }
709 }
710 return ""
711}
712
713func unmarshalBedrockEventPayload(payload []byte, eventType string, dst any) error {
714 var outer map[string]json.RawMessage
715 if err := json.Unmarshal(payload, &outer); err == nil {
716 if wrapped, ok := outer[eventType]; ok {
717 return json.Unmarshal(wrapped, dst)
718 }
719 }
720 if err := json.Unmarshal(payload, dst); err != nil {
721 return fmt.Errorf("bedrock: parse %s payload: %w", eventType, err)
722 }
723 return nil
724}
725
726// ---- event-stream binary framing parser ----
727
728type eventStreamMessage struct {
729 headers map[string]string
730 payload []byte
731}
732
733func (m *eventStreamMessage) headerString(name string) string { return m.headers[name] }
734
735// readEventStreamMessage reads one event-stream message from r. Returns
736// io.EOF when the stream is finished cleanly.
737func readEventStreamMessage(r io.Reader) (*eventStreamMessage, error) {
738 var prelude [8]byte
739 if _, err := io.ReadFull(r, prelude[:]); err != nil {
740 return nil, err
741 }
742 totalLen := binary.BigEndian.Uint32(prelude[0:4])
743 headersLen := binary.BigEndian.Uint32(prelude[4:8])
744 if totalLen < 16 || headersLen > totalLen-16 {
745 return nil, fmt.Errorf("bedrock: malformed event-stream prelude (total=%d headers=%d)", totalLen, headersLen)
746 }
747 // Skip prelude CRC (4 bytes); we don't validate.
748 var preludeCRC [4]byte
749 if _, err := io.ReadFull(r, preludeCRC[:]); err != nil {
750 return nil, err
751 }
752 headersBuf := make([]byte, headersLen)
753 if _, err := io.ReadFull(r, headersBuf); err != nil {
754 return nil, err
755 }
756 payloadLen := int(totalLen) - 16 - int(headersLen)
757 payload := make([]byte, payloadLen)
758 if _, err := io.ReadFull(r, payload); err != nil {
759 return nil, err
760 }
761 var trailer [4]byte
762 if _, err := io.ReadFull(r, trailer[:]); err != nil {
763 return nil, err
764 }
765 // Parse headers.
766 hdrs := map[string]string{}
767 i := 0
768 for i < len(headersBuf) {
769 if i+1 > len(headersBuf) {
770 break
771 }
772 nameLen := int(headersBuf[i])
773 i++
774 if i+nameLen > len(headersBuf) {
775 return nil, fmt.Errorf("bedrock: header name overflow")
776 }
777 name := string(headersBuf[i : i+nameLen])
778 i += nameLen
779 if i+1 > len(headersBuf) {
780 return nil, fmt.Errorf("bedrock: header value-type missing")
781 }
782 valueType := headersBuf[i]
783 i++
784 // Type 7 = string with 2-byte length. Type 6 = byte-array similar.
785 // Other types (bool/int/long/timestamp/uuid) we treat as opaque
786 // strings since bedrock's event-type headers are always type 7.
787 switch valueType {
788 case 7, 6:
789 if i+2 > len(headersBuf) {
790 return nil, fmt.Errorf("bedrock: header value length missing")
791 }
792 vLen := int(binary.BigEndian.Uint16(headersBuf[i : i+2]))
793 i += 2
794 if i+vLen > len(headersBuf) {
795 return nil, fmt.Errorf("bedrock: header value overflow")
796 }
797 hdrs[name] = string(headersBuf[i : i+vLen])
798 i += vLen
799 case 0: // bool true
800 hdrs[name] = "true"
801 case 1: // bool false
802 hdrs[name] = "false"
803 case 2: // byte
804 if i+1 > len(headersBuf) {
805 return nil, fmt.Errorf("bedrock: header byte missing")
806 }
807 i++
808 case 3: // int16
809 if i+2 > len(headersBuf) {
810 return nil, fmt.Errorf("bedrock: header int16 missing")
811 }
812 i += 2
813 case 4: // int32
814 if i+4 > len(headersBuf) {
815 return nil, fmt.Errorf("bedrock: header int32 missing")
816 }
817 i += 4
818 case 5: // int64
819 if i+8 > len(headersBuf) {
820 return nil, fmt.Errorf("bedrock: header int64 missing")
821 }
822 i += 8
823 case 8: // timestamp (int64)
824 if i+8 > len(headersBuf) {
825 return nil, fmt.Errorf("bedrock: header timestamp missing")
826 }
827 i += 8
828 case 9: // uuid (16 bytes)
829 if i+16 > len(headersBuf) {
830 return nil, fmt.Errorf("bedrock: header uuid missing")
831 }
832 i += 16
833 default:
834 return nil, fmt.Errorf("bedrock: unknown header value-type %d", valueType)
835 }
836 }
837 return &eventStreamMessage{headers: hdrs, payload: payload}, nil
838}
839
840// ---- SigV4 signing ----
841//
842// AWS Signature Version 4. Implemented from scratch so we don't pull in
843// aws-sdk-go-v2 (large transitive dep tree just for one signer). The
844// algorithm is documented at:
845// https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
846//
847// Steps:
848//
849// 1. Build the canonical request: method + path + canonical query +
850// canonical headers + signed headers + payload-hash.
851// 2. Build the string-to-sign: algorithm + amz-date + credential scope +
852// sha256(canonical request).
853// 3. Derive the signing key by chaining HMAC-SHA256:
854// kSecret -> kDate -> kRegion -> kService -> kSigning
855// 4. signature = HMAC-SHA256(kSigning, stringToSign)
856// 5. Authorization header: AWS4-HMAC-SHA256 Credential=..., SignedHeaders=..., Signature=...
857//
858// Payload hash is sha256 of the JSON body. For Bedrock's streaming
859// endpoint this works because the request body is fully buffered before
860// the call — we don't need the streaming-payload variant.
861
862func sigHMAC(key, msg []byte) []byte {
863 m := hmac.New(sha256.New, key)
864 m.Write(msg)
865 return m.Sum(nil)
866}
867
868func sigSHA256Hex(b []byte) string {
869 h := sha256.Sum256(b)
870 return hex.EncodeToString(h[:])
871}
872
873func signSigV4(req *http.Request, payload []byte, service, region string, creds *bedrockSigV4Creds, now time.Time) error {
874 amzDate := now.Format("20060102T150405Z")
875 shortDate := now.Format("20060102")
876 credentialScope := shortDate + "/" + region + "/" + service + "/aws4_request"
877
878 // Required headers.
879 req.Header.Set("host", req.URL.Host)
880 req.Header.Set("x-amz-date", amzDate)
881 if creds.sessionToken != "" {
882 req.Header.Set("x-amz-security-token", creds.sessionToken)
883 }
884 payloadHash := sigSHA256Hex(payload)
885 req.Header.Set("x-amz-content-sha256", payloadHash)
886
887 // Canonical headers: lowercase name, trimmed value, sorted by name,
888 // each on its own line ending with \n.
889 var names []string
890 values := map[string]string{}
891 for k, vs := range req.Header {
892 lower := strings.ToLower(k)
893 // Skip authorization (we're computing it) and any
894 // connection-specific headers.
895 if lower == "authorization" {
896 continue
897 }
898 names = append(names, lower)
899 values[lower] = strings.Join(vs, ",")
900 }
901 // host is special — it's not in req.Header by default; set above.
902 sort.Strings(names)
903 var canonHdrs strings.Builder
904 for _, n := range names {
905 canonHdrs.WriteString(n)
906 canonHdrs.WriteByte(':')
907 canonHdrs.WriteString(strings.TrimSpace(values[n]))
908 canonHdrs.WriteByte('\n')
909 }
910 signedHeaders := strings.Join(names, ";")
911
912 // Canonical query string: keys sorted by name, each key and value
913 // URI-escaped (RFC 3986).
914 q := req.URL.Query()
915 var qKeys []string
916 for k := range q {
917 qKeys = append(qKeys, k)
918 }
919 sort.Strings(qKeys)
920 var canonQuery strings.Builder
921 for i, k := range qKeys {
922 if i > 0 {
923 canonQuery.WriteByte('&')
924 }
925 canonQuery.WriteString(url.QueryEscape(k))
926 canonQuery.WriteByte('=')
927 canonQuery.WriteString(url.QueryEscape(q.Get(k)))
928 }
929
930 // Canonical request.
931 canonReq := req.Method + "\n" +
932 canonicalSigV4Path(req.URL.Path) + "\n" +
933 canonQuery.String() + "\n" +
934 canonHdrs.String() + "\n" +
935 signedHeaders + "\n" +
936 payloadHash
937
938 // String to sign.
939 stringToSign := "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n" + sigSHA256Hex([]byte(canonReq))
940
941 // Signing key.
942 kDate := sigHMAC([]byte("AWS4"+creds.secretAccessKey), []byte(shortDate))
943 kRegion := sigHMAC(kDate, []byte(region))
944 kService := sigHMAC(kRegion, []byte(service))
945 kSigning := sigHMAC(kService, []byte("aws4_request"))
946
947 // Signature.
948 signature := hex.EncodeToString(sigHMAC(kSigning, []byte(stringToSign)))
949
950 // Authorization header.
951 auth := "AWS4-HMAC-SHA256 " +
952 "Credential=" + creds.accessKeyID + "/" + credentialScope + ", " +
953 "SignedHeaders=" + signedHeaders + ", " +
954 "Signature=" + signature
955 req.Header.Set("Authorization", auth)
956 return nil
957}
958
959// canonicalSigV4Path returns the path with each segment URI-encoded
960// according to SigV4 rules (everything except unreserved chars
961// A-Za-z0-9-_.~ and / between segments). Bedrock model IDs contain
962// dots, colons, and slashes; the colons must be percent-encoded.
963func canonicalSigV4Path(p string) string {
964 if p == "" {
965 return "/"
966 }
967 segments := strings.Split(p, "/")
968 for i, s := range segments {
969 segments[i] = sigV4EncodeSegment(s)
970 }
971 return strings.Join(segments, "/")
972}
973
974func sigV4EncodeSegment(s string) string {
975 var b strings.Builder
976 for i := 0; i < len(s); i++ {
977 c := s[i]
978 // Unreserved per RFC 3986.
979 if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
980 c == '-' || c == '_' || c == '.' || c == '~' {
981 b.WriteByte(c)
982 continue
983 }
984 fmt.Fprintf(&b, "%%%02X", c)
985 }
986 return b.String()
987}