v0.2.14 63e33d9

amazon_bedrock.go RawHistoryFolder

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
197type bedrockToolSpec struct {
198	ToolSpec struct {
199		Name        string `json:"name"`
200		Description string `json:"description,omitempty"`
201		InputSchema struct {
202			JSON json.RawMessage `json:"json"`
203		} `json:"inputSchema"`
204	} `json:"toolSpec"`
205}
206
207type bedrockRequest struct {
208	Messages        []bedrockMessage    `json:"messages"`
209	System          []map[string]string `json:"system,omitempty"`
210	InferenceConfig struct {
211		MaxTokens   int      `json:"maxTokens,omitempty"`
212		Temperature *float32 `json:"temperature,omitempty"`
213	} `json:"inferenceConfig,omitempty"`
214	ToolConfig *struct {
215		Tools []bedrockToolSpec `json:"tools"`
216	} `json:"toolConfig,omitempty"`
217}
218
219func normalizeBedrockToolResults(msgs []Message) []Message {
220	resultByID := map[string]ToolResultBlock{}
221	for _, m := range msgs {
222		for _, c := range m.Content {
223			if tr, ok := c.(ToolResultBlock); ok {
224				if _, exists := resultByID[tr.CallID]; !exists {
225					resultByID[tr.CallID] = tr
226				}
227			}
228		}
229	}
230
231	out := make([]Message, 0, len(msgs))
232	for _, m := range msgs {
233		copy := m
234		copy.Content = nil
235		var toolCalls []ToolCallBlock
236		for _, c := range m.Content {
237			switch v := c.(type) {
238			case ToolResultBlock:
239				// Bedrock requires toolResult blocks immediately after the
240				// assistant toolUse they answer. Reinsert them from the
241				// assistant pass below instead of preserving their original
242				// location, which may be separated by user text in active
243				// sessions.
244				continue
245			case ToolCallBlock:
246				toolCalls = append(toolCalls, v)
247			}
248			copy.Content = append(copy.Content, c)
249		}
250		if len(copy.Content) > 0 {
251			out = append(out, copy)
252		}
253		if m.Role != RoleAssistant || len(toolCalls) == 0 {
254			continue
255		}
256
257		results := make([]Content, 0, len(toolCalls))
258		for _, tc := range toolCalls {
259			if tr, ok := resultByID[tc.ID]; ok {
260				results = append(results, tr)
261				continue
262			}
263			results = append(results, ToolResultBlock{
264				CallID:  tc.ID,
265				IsError: true,
266				Content: []Content{TextBlock{Text: "tool call did not complete before the next user message"}},
267			})
268		}
269		out = append(out, Message{Role: RoleTool, Content: results, Time: m.Time})
270	}
271	return out
272}
273
274func (c *bedrockClient) buildRequest(req Request) (*bedrockRequest, error) {
275	out := &bedrockRequest{}
276	if req.System != "" {
277		out.System = []map[string]string{{"text": req.System}}
278	}
279	out.InferenceConfig.Temperature = req.Temperature
280	out.InferenceConfig.MaxTokens = req.MaxTokens
281	if out.InferenceConfig.MaxTokens == 0 {
282		out.InferenceConfig.MaxTokens = 4096
283	}
284	for _, m := range normalizeBedrockToolResults(req.Messages) {
285		role := string(m.Role)
286		if role == "tool" {
287			role = "user"
288		}
289		bm := bedrockMessage{Role: role}
290		for _, c := range m.Content {
291			switch v := c.(type) {
292			case TextBlock:
293				bm.Content = append(bm.Content, map[string]interface{}{"text": v.Text})
294			case ToolCallBlock:
295				var input map[string]interface{}
296				_ = json.Unmarshal(v.Arguments, &input)
297				if input == nil {
298					input = map[string]interface{}{}
299				}
300				bm.Content = append(bm.Content, map[string]interface{}{
301					"toolUse": map[string]interface{}{
302						"toolUseId": v.ID, "name": v.Name, "input": input,
303					},
304				})
305			case ToolResultBlock:
306				var resultContent []map[string]interface{}
307				for _, inner := range v.Content {
308					if tb, ok := inner.(TextBlock); ok {
309						resultContent = append(resultContent, map[string]interface{}{"text": tb.Text})
310					}
311				}
312				status := "success"
313				if v.IsError {
314					status = "error"
315				}
316				bm.Content = append(bm.Content, map[string]interface{}{
317					"toolResult": map[string]interface{}{
318						"toolUseId": v.CallID,
319						"content":   resultContent,
320						"status":    status,
321					},
322				})
323			}
324		}
325		if len(bm.Content) == 0 {
326			continue
327		}
328		out.Messages = append(out.Messages, bm)
329	}
330	if len(req.Tools) > 0 {
331		tc := struct {
332			Tools []bedrockToolSpec `json:"tools"`
333		}{}
334		for _, t := range req.Tools {
335			var ts bedrockToolSpec
336			ts.ToolSpec.Name = t.Name
337			ts.ToolSpec.Description = t.Description
338			ts.ToolSpec.InputSchema.JSON = t.Schema
339			tc.Tools = append(tc.Tools, ts)
340		}
341		out.ToolConfig = &tc
342	}
343	return out, nil
344}
345
346// resolveBedrockInferenceProfileID maps a bare foundation-model ID to
347// its region-matched cross-region inference-profile ID.
348//
349// Several newer Bedrock models (Anthropic Claude 4.x, DeepSeek, etc.)
350// cannot be invoked with on-demand throughput by their plain
351// foundation-model ID; Bedrock returns HTTP 400 demanding "the ID or
352// ARN of an inference profile that contains this model". The profile
353// ID is the same model ID with a geographic prefix (us/eu/apac/...).
354//
355// We only rewrite IDs that (a) lack an existing geo prefix and (b)
356// belong to a model family that requires a profile. IDs that already
357// carry a prefix (e.g. "eu.anthropic...", "global.anthropic...") or
358// fully-qualified ARNs are returned unchanged, so explicit user
359// choices and custom application inference profiles still work.
360func resolveBedrockInferenceProfileID(modelID, region string) string {
361	if modelID == "" {
362		return modelID
363	}
364	// ARNs are already inference-profile references; leave untouched.
365	if strings.HasPrefix(modelID, "arn:") {
366		return modelID
367	}
368	// Already geo-prefixed (us. / eu. / apac. / ap. / us-gov. / global.)?
369	if bedrockHasGeoPrefix(modelID) {
370		return modelID
371	}
372	if !bedrockRequiresInferenceProfile(modelID) {
373		return modelID
374	}
375	prefix := bedrockGeoPrefixForRegion(region)
376	if prefix == "" {
377		return modelID
378	}
379	return prefix + "." + modelID
380}
381
382// bedrockGeoPrefixes are the cross-region inference-profile geo
383// prefixes Bedrock uses. A model ID that starts with one of these
384// (followed by a dot) is already a profile reference.
385var bedrockGeoPrefixes = []string{"us-gov", "us", "eu", "apac", "ap", "global", "au"}
386
387func bedrockHasGeoPrefix(modelID string) bool {
388	for _, p := range bedrockGeoPrefixes {
389		if strings.HasPrefix(modelID, p+".") {
390			return true
391		}
392	}
393	return false
394}
395
396// bedrockRequiresInferenceProfile reports whether a bare
397// foundation-model ID is one of the families AWS only exposes through
398// a cross-region inference profile for on-demand throughput.
399func bedrockRequiresInferenceProfile(modelID string) bool {
400	switch {
401	case strings.HasPrefix(modelID, "anthropic.claude-"):
402		return true
403	case strings.HasPrefix(modelID, "deepseek."):
404		return true
405	default:
406		return false
407	}
408}
409
410// bedrockGeoPrefixForRegion maps an AWS region to the geo prefix used
411// by its cross-region inference profiles. Returns "" when the region
412// has no known mapping, in which case the model ID is left unchanged.
413func bedrockGeoPrefixForRegion(region string) string {
414	switch {
415	case region == "":
416		return "us"
417	case strings.HasPrefix(region, "us-gov-"):
418		return "us-gov"
419	case strings.HasPrefix(region, "us-"):
420		return "us"
421	case strings.HasPrefix(region, "eu-"):
422		return "eu"
423	case strings.HasPrefix(region, "ap-"):
424		return "apac"
425	case strings.HasPrefix(region, "ca-"):
426		return "us"
427	case strings.HasPrefix(region, "sa-"):
428		return "us"
429	default:
430		return "us"
431	}
432}
433
434func (c *bedrockClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
435	wire, err := c.buildRequest(req)
436	if err != nil {
437		return nil, err
438	}
439	body, err := json.Marshal(wire)
440	if err != nil {
441		return nil, err
442	}
443	modelID := resolveBedrockInferenceProfileID(req.Model, c.region)
444	url := c.baseURL + "/model/" + modelID + "/converse-stream"
445	httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
446	if err != nil {
447		return nil, err
448	}
449	httpReq.Header.Set("content-type", "application/json")
450	httpReq.Header.Set("accept", "application/vnd.amazon.eventstream")
451	if c.bearerToken != "" {
452		httpReq.Header.Set("authorization", "Bearer "+c.bearerToken)
453	} else if c.sigv4 != nil {
454		if err := signSigV4(httpReq, body, "bedrock", c.region, c.sigv4, time.Now().UTC()); err != nil {
455			return nil, fmt.Errorf("bedrock: sigv4 sign: %w", err)
456		}
457	} else {
458		return nil, fmt.Errorf("bedrock: no auth configured")
459	}
460	resp, err := c.http.Do(httpReq)
461	if err != nil {
462		return nil, fmt.Errorf("bedrock: %w", err)
463	}
464	if resp.StatusCode != http.StatusOK {
465		b, _ := io.ReadAll(resp.Body)
466		resp.Body.Close()
467		msg := strings.TrimSpace(string(b))
468		// A 403 on the bearer route is almost always a region mismatch:
469		// short-term Bedrock API keys are scoped to the region of the
470		// console session that minted them, but zot defaults to
471		// us-east-1. Surface the resolved region and the fix so the user
472		// is not left guessing why a freshly-copied key is "invalid".
473		if resp.StatusCode == http.StatusForbidden && c.bearerToken != "" {
474			return nil, fmt.Errorf(
475				"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",
476				c.region, msg)
477		}
478		return nil, fmt.Errorf("bedrock: http %d: %s", resp.StatusCode, msg)
479	}
480	out := make(chan Event, 16)
481	go c.runStream(ctx, resp, req, out)
482	return out, nil
483}
484
485func (c *bedrockClient) runStream(ctx context.Context, resp *http.Response, req Request, out chan<- Event) {
486	defer close(out)
487	defer resp.Body.Close()
488
489	out <- EventStart{Provider: "amazon-bedrock", Model: req.Model}
490
491	// State accumulated across deltas.
492	contentBlocks := map[int]*bedrockBlockState{}
493	stop := StopEnd
494	finalMsg := Message{Role: RoleAssistant, Time: time.Now()}
495	var usage Usage
496
497	for {
498		if ctx.Err() != nil {
499			stop = StopAborted
500			break
501		}
502		evt, err := readEventStreamMessage(resp.Body)
503		if err != nil {
504			if err == io.EOF {
505				break
506			}
507			out <- EventDone{Stop: StopError, Err: err, Message: finalMsg}
508			return
509		}
510		eventType := evt.headerString(":event-type")
511		if eventType == "" {
512			eventType = bedrockEventTypeFromPayload(evt.payload)
513		}
514		messageType := evt.headerString(":message-type")
515		if messageType == "exception" {
516			out <- EventDone{Stop: StopError, Err: fmt.Errorf("bedrock exception (%s): %s", evt.headerString(":exception-type"), string(evt.payload)), Message: finalMsg}
517			return
518		}
519		switch eventType {
520		case "messageStart":
521			// nothing to do; role is always "assistant"
522		case "contentBlockStart":
523			var d struct {
524				ContentBlockIndex int `json:"contentBlockIndex"`
525				Start             struct {
526					ToolUse *struct {
527						ToolUseID string `json:"toolUseId"`
528						Name      string `json:"name"`
529					} `json:"toolUse"`
530				} `json:"start"`
531			}
532			if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockStart", &d); err != nil {
533				continue
534			}
535			st := &bedrockBlockState{}
536			contentBlocks[d.ContentBlockIndex] = st
537			if d.Start.ToolUse != nil {
538				st.isToolUse = true
539				st.toolID = d.Start.ToolUse.ToolUseID
540				st.toolName = d.Start.ToolUse.Name
541				out <- EventToolStart{ID: st.toolID, Name: st.toolName}
542			}
543		case "contentBlockDelta":
544			var d struct {
545				ContentBlockIndex int `json:"contentBlockIndex"`
546				Delta             struct {
547					Text    string `json:"text"`
548					ToolUse *struct {
549						Input string `json:"input"`
550					} `json:"toolUse"`
551				} `json:"delta"`
552			}
553			if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockDelta", &d); err != nil {
554				continue
555			}
556			st := contentBlocks[d.ContentBlockIndex]
557			if st == nil {
558				st = &bedrockBlockState{}
559				contentBlocks[d.ContentBlockIndex] = st
560			}
561			if d.Delta.Text != "" {
562				st.text.WriteString(d.Delta.Text)
563				out <- EventTextDelta{Delta: d.Delta.Text}
564			}
565			if d.Delta.ToolUse != nil && d.Delta.ToolUse.Input != "" {
566				st.toolArgs.WriteString(d.Delta.ToolUse.Input)
567				out <- EventToolArgs{ID: st.toolID, Delta: d.Delta.ToolUse.Input}
568			}
569		case "contentBlockStop":
570			var d struct {
571				ContentBlockIndex int `json:"contentBlockIndex"`
572			}
573			if err := unmarshalBedrockEventPayload(evt.payload, "contentBlockStop", &d); err != nil {
574				continue
575			}
576			st := contentBlocks[d.ContentBlockIndex]
577			if st == nil {
578				continue
579			}
580			if st.isToolUse {
581				args := st.toolArgs.String()
582				if args == "" {
583					args = "{}"
584				}
585				finalMsg.Content = append(finalMsg.Content, ToolCallBlock{
586					ID: st.toolID, Name: st.toolName, Arguments: json.RawMessage(args),
587				})
588				out <- EventToolEnd{ID: st.toolID}
589			} else if st.text.Len() > 0 {
590				finalMsg.Content = append(finalMsg.Content, TextBlock{Text: st.text.String()})
591			}
592		case "messageStop":
593			var d struct {
594				StopReason string `json:"stopReason"`
595			}
596			_ = unmarshalBedrockEventPayload(evt.payload, "messageStop", &d)
597			switch d.StopReason {
598			case "tool_use":
599				stop = StopToolUse
600			case "end_turn":
601				stop = StopEnd
602			case "max_tokens":
603				stop = StopLength
604			default:
605				stop = StopEnd
606			}
607		case "metadata":
608			var d struct {
609				Usage struct {
610					InputTokens           int `json:"inputTokens"`
611					OutputTokens          int `json:"outputTokens"`
612					CacheReadInputTokens  int `json:"cacheReadInputTokens"`
613					CacheWriteInputTokens int `json:"cacheWriteInputTokens"`
614				} `json:"usage"`
615			}
616			if err := unmarshalBedrockEventPayload(evt.payload, "metadata", &d); err == nil {
617				usage.InputTokens = d.Usage.InputTokens
618				usage.OutputTokens = d.Usage.OutputTokens
619				usage.CacheReadTokens = d.Usage.CacheReadInputTokens
620				usage.CacheWriteTokens = d.Usage.CacheWriteInputTokens
621				if m, err := FindModel("amazon-bedrock", req.Model); err == nil {
622					usage.CostUSD = ComputeCost(m, usage)
623				}
624				out <- EventUsage{Usage: usage}
625			}
626		}
627	}
628	out <- EventDone{Stop: stop, Message: finalMsg}
629}
630
631type bedrockBlockState struct {
632	isToolUse bool
633	toolID    string
634	toolName  string
635	toolArgs  strings.Builder
636	text      strings.Builder
637}
638
639func bedrockEventTypeFromPayload(payload []byte) string {
640	var outer map[string]json.RawMessage
641	if err := json.Unmarshal(payload, &outer); err != nil {
642		return ""
643	}
644	for _, name := range []string{"messageStart", "contentBlockStart", "contentBlockDelta", "contentBlockStop", "messageStop", "metadata"} {
645		if _, ok := outer[name]; ok {
646			return name
647		}
648	}
649	return ""
650}
651
652func unmarshalBedrockEventPayload(payload []byte, eventType string, dst any) error {
653	var outer map[string]json.RawMessage
654	if err := json.Unmarshal(payload, &outer); err == nil {
655		if wrapped, ok := outer[eventType]; ok {
656			return json.Unmarshal(wrapped, dst)
657		}
658	}
659	if err := json.Unmarshal(payload, dst); err != nil {
660		return fmt.Errorf("bedrock: parse %s payload: %w", eventType, err)
661	}
662	return nil
663}
664
665// ---- event-stream binary framing parser ----
666
667type eventStreamMessage struct {
668	headers map[string]string
669	payload []byte
670}
671
672func (m *eventStreamMessage) headerString(name string) string { return m.headers[name] }
673
674// readEventStreamMessage reads one event-stream message from r. Returns
675// io.EOF when the stream is finished cleanly.
676func readEventStreamMessage(r io.Reader) (*eventStreamMessage, error) {
677	var prelude [8]byte
678	if _, err := io.ReadFull(r, prelude[:]); err != nil {
679		return nil, err
680	}
681	totalLen := binary.BigEndian.Uint32(prelude[0:4])
682	headersLen := binary.BigEndian.Uint32(prelude[4:8])
683	if totalLen < 16 || headersLen > totalLen-16 {
684		return nil, fmt.Errorf("bedrock: malformed event-stream prelude (total=%d headers=%d)", totalLen, headersLen)
685	}
686	// Skip prelude CRC (4 bytes); we don't validate.
687	var preludeCRC [4]byte
688	if _, err := io.ReadFull(r, preludeCRC[:]); err != nil {
689		return nil, err
690	}
691	headersBuf := make([]byte, headersLen)
692	if _, err := io.ReadFull(r, headersBuf); err != nil {
693		return nil, err
694	}
695	payloadLen := int(totalLen) - 16 - int(headersLen)
696	payload := make([]byte, payloadLen)
697	if _, err := io.ReadFull(r, payload); err != nil {
698		return nil, err
699	}
700	var trailer [4]byte
701	if _, err := io.ReadFull(r, trailer[:]); err != nil {
702		return nil, err
703	}
704	// Parse headers.
705	hdrs := map[string]string{}
706	i := 0
707	for i < len(headersBuf) {
708		if i+1 > len(headersBuf) {
709			break
710		}
711		nameLen := int(headersBuf[i])
712		i++
713		if i+nameLen > len(headersBuf) {
714			return nil, fmt.Errorf("bedrock: header name overflow")
715		}
716		name := string(headersBuf[i : i+nameLen])
717		i += nameLen
718		if i+1 > len(headersBuf) {
719			return nil, fmt.Errorf("bedrock: header value-type missing")
720		}
721		valueType := headersBuf[i]
722		i++
723		// Type 7 = string with 2-byte length. Type 6 = byte-array similar.
724		// Other types (bool/int/long/timestamp/uuid) we treat as opaque
725		// strings since bedrock's event-type headers are always type 7.
726		switch valueType {
727		case 7, 6:
728			if i+2 > len(headersBuf) {
729				return nil, fmt.Errorf("bedrock: header value length missing")
730			}
731			vLen := int(binary.BigEndian.Uint16(headersBuf[i : i+2]))
732			i += 2
733			if i+vLen > len(headersBuf) {
734				return nil, fmt.Errorf("bedrock: header value overflow")
735			}
736			hdrs[name] = string(headersBuf[i : i+vLen])
737			i += vLen
738		case 0: // bool true
739			hdrs[name] = "true"
740		case 1: // bool false
741			hdrs[name] = "false"
742		case 2: // byte
743			if i+1 > len(headersBuf) {
744				return nil, fmt.Errorf("bedrock: header byte missing")
745			}
746			i++
747		case 3: // int16
748			if i+2 > len(headersBuf) {
749				return nil, fmt.Errorf("bedrock: header int16 missing")
750			}
751			i += 2
752		case 4: // int32
753			if i+4 > len(headersBuf) {
754				return nil, fmt.Errorf("bedrock: header int32 missing")
755			}
756			i += 4
757		case 5: // int64
758			if i+8 > len(headersBuf) {
759				return nil, fmt.Errorf("bedrock: header int64 missing")
760			}
761			i += 8
762		case 8: // timestamp (int64)
763			if i+8 > len(headersBuf) {
764				return nil, fmt.Errorf("bedrock: header timestamp missing")
765			}
766			i += 8
767		case 9: // uuid (16 bytes)
768			if i+16 > len(headersBuf) {
769				return nil, fmt.Errorf("bedrock: header uuid missing")
770			}
771			i += 16
772		default:
773			return nil, fmt.Errorf("bedrock: unknown header value-type %d", valueType)
774		}
775	}
776	return &eventStreamMessage{headers: hdrs, payload: payload}, nil
777}
778
779// ---- SigV4 signing ----
780//
781// AWS Signature Version 4. Implemented from scratch so we don't pull in
782// aws-sdk-go-v2 (large transitive dep tree just for one signer). The
783// algorithm is documented at:
784//   https://docs.aws.amazon.com/general/latest/gr/sigv4-signed-request-examples.html
785//
786// Steps:
787//
788//   1. Build the canonical request: method + path + canonical query +
789//      canonical headers + signed headers + payload-hash.
790//   2. Build the string-to-sign: algorithm + amz-date + credential scope +
791//      sha256(canonical request).
792//   3. Derive the signing key by chaining HMAC-SHA256:
793//      kSecret -> kDate -> kRegion -> kService -> kSigning
794//   4. signature = HMAC-SHA256(kSigning, stringToSign)
795//   5. Authorization header: AWS4-HMAC-SHA256 Credential=..., SignedHeaders=..., Signature=...
796//
797// Payload hash is sha256 of the JSON body. For Bedrock's streaming
798// endpoint this works because the request body is fully buffered before
799// the call — we don't need the streaming-payload variant.
800
801func sigHMAC(key, msg []byte) []byte {
802	m := hmac.New(sha256.New, key)
803	m.Write(msg)
804	return m.Sum(nil)
805}
806
807func sigSHA256Hex(b []byte) string {
808	h := sha256.Sum256(b)
809	return hex.EncodeToString(h[:])
810}
811
812func signSigV4(req *http.Request, payload []byte, service, region string, creds *bedrockSigV4Creds, now time.Time) error {
813	amzDate := now.Format("20060102T150405Z")
814	shortDate := now.Format("20060102")
815	credentialScope := shortDate + "/" + region + "/" + service + "/aws4_request"
816
817	// Required headers.
818	req.Header.Set("host", req.URL.Host)
819	req.Header.Set("x-amz-date", amzDate)
820	if creds.sessionToken != "" {
821		req.Header.Set("x-amz-security-token", creds.sessionToken)
822	}
823	payloadHash := sigSHA256Hex(payload)
824	req.Header.Set("x-amz-content-sha256", payloadHash)
825
826	// Canonical headers: lowercase name, trimmed value, sorted by name,
827	// each on its own line ending with \n.
828	var names []string
829	values := map[string]string{}
830	for k, vs := range req.Header {
831		lower := strings.ToLower(k)
832		// Skip authorization (we're computing it) and any
833		// connection-specific headers.
834		if lower == "authorization" {
835			continue
836		}
837		names = append(names, lower)
838		values[lower] = strings.Join(vs, ",")
839	}
840	// host is special — it's not in req.Header by default; set above.
841	sort.Strings(names)
842	var canonHdrs strings.Builder
843	for _, n := range names {
844		canonHdrs.WriteString(n)
845		canonHdrs.WriteByte(':')
846		canonHdrs.WriteString(strings.TrimSpace(values[n]))
847		canonHdrs.WriteByte('\n')
848	}
849	signedHeaders := strings.Join(names, ";")
850
851	// Canonical query string: keys sorted by name, each key and value
852	// URI-escaped (RFC 3986).
853	q := req.URL.Query()
854	var qKeys []string
855	for k := range q {
856		qKeys = append(qKeys, k)
857	}
858	sort.Strings(qKeys)
859	var canonQuery strings.Builder
860	for i, k := range qKeys {
861		if i > 0 {
862			canonQuery.WriteByte('&')
863		}
864		canonQuery.WriteString(url.QueryEscape(k))
865		canonQuery.WriteByte('=')
866		canonQuery.WriteString(url.QueryEscape(q.Get(k)))
867	}
868
869	// Canonical request.
870	canonReq := req.Method + "\n" +
871		canonicalSigV4Path(req.URL.Path) + "\n" +
872		canonQuery.String() + "\n" +
873		canonHdrs.String() + "\n" +
874		signedHeaders + "\n" +
875		payloadHash
876
877	// String to sign.
878	stringToSign := "AWS4-HMAC-SHA256\n" + amzDate + "\n" + credentialScope + "\n" + sigSHA256Hex([]byte(canonReq))
879
880	// Signing key.
881	kDate := sigHMAC([]byte("AWS4"+creds.secretAccessKey), []byte(shortDate))
882	kRegion := sigHMAC(kDate, []byte(region))
883	kService := sigHMAC(kRegion, []byte(service))
884	kSigning := sigHMAC(kService, []byte("aws4_request"))
885
886	// Signature.
887	signature := hex.EncodeToString(sigHMAC(kSigning, []byte(stringToSign)))
888
889	// Authorization header.
890	auth := "AWS4-HMAC-SHA256 " +
891		"Credential=" + creds.accessKeyID + "/" + credentialScope + ", " +
892		"SignedHeaders=" + signedHeaders + ", " +
893		"Signature=" + signature
894	req.Header.Set("Authorization", auth)
895	return nil
896}
897
898// canonicalSigV4Path returns the path with each segment URI-encoded
899// according to SigV4 rules (everything except unreserved chars
900// A-Za-z0-9-_.~ and / between segments). Bedrock model IDs contain
901// dots, colons, and slashes; the colons must be percent-encoded.
902func canonicalSigV4Path(p string) string {
903	if p == "" {
904		return "/"
905	}
906	segments := strings.Split(p, "/")
907	for i, s := range segments {
908		segments[i] = sigV4EncodeSegment(s)
909	}
910	return strings.Join(segments, "/")
911}
912
913func sigV4EncodeSegment(s string) string {
914	var b strings.Builder
915	for i := 0; i < len(s); i++ {
916		c := s[i]
917		// Unreserved per RFC 3986.
918		if (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
919			c == '-' || c == '_' || c == '.' || c == '~' {
920			b.WriteByte(c)
921			continue
922		}
923		fmt.Fprintf(&b, "%%%02X", c)
924	}
925	return b.String()
926}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close