|
| 1 | +package bedrock |
| 2 | + |
| 3 | +import ( |
| 4 | + stdcontext "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" |
| 12 | + "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 14 | + awsbedrock "github.com/aws/aws-sdk-go-v2/service/bedrock" |
| 15 | + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime" |
| 16 | + "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" |
| 17 | +) |
| 18 | + |
| 19 | +// BedrockClient handles communication with AWS Bedrock API |
| 20 | +type BedrockClient struct { |
| 21 | + client *bedrockruntime.Client |
| 22 | + bedrockClient *awsbedrock.Client |
| 23 | + region string |
| 24 | +} |
| 25 | + |
| 26 | +// anthropicRequest represents the request body for Claude models on AWS Bedrock |
| 27 | +type anthropicRequest struct { |
| 28 | + AnthropicVersion string `json:"anthropic_version"` |
| 29 | + MaxTokens int `json:"max_tokens"` |
| 30 | + Messages []anthropicMessage `json:"messages"` |
| 31 | +} |
| 32 | + |
| 33 | +// anthropicMessage represents a message in the Anthropic Messages API format |
| 34 | +type anthropicMessage struct { |
| 35 | + Role string `json:"role"` |
| 36 | + Content string `json:"content"` |
| 37 | +} |
| 38 | + |
| 39 | +// NewBedrockClient creates a new Bedrock client |
| 40 | +// Args: |
| 41 | +// |
| 42 | +// apiKey: string - AWS credentials in format "ACCESS_KEY_ID:SECRET_ACCESS_KEY" |
| 43 | +// region: string - AWS region (default: "us-east-1") |
| 44 | +// |
| 45 | +// Returns: initialized BedrockClient, error if apiKey is empty or invalid format |
| 46 | +func NewBedrockClient(apiKey, region string) (*BedrockClient, error) { |
| 47 | + if apiKey == "" { |
| 48 | + return nil, fmt.Errorf("Bedrock API key not provided") |
| 49 | + } |
| 50 | + |
| 51 | + // Parse API key in format "ACCESS_KEY_ID:SECRET_ACCESS_KEY" |
| 52 | + parts := splitAPIKey(apiKey) |
| 53 | + if len(parts) != 2 { |
| 54 | + return nil, fmt.Errorf("invalid API key format: expected ACCESS_KEY_ID:SECRET_ACCESS_KEY") |
| 55 | + } |
| 56 | + |
| 57 | + accessKeyID := parts[0] |
| 58 | + secretAccessKey := parts[1] |
| 59 | + |
| 60 | + if accessKeyID == "" || secretAccessKey == "" { |
| 61 | + return nil, fmt.Errorf("invalid API key format: both access key ID and secret access key are required") |
| 62 | + } |
| 63 | + |
| 64 | + if region == "" { |
| 65 | + region = "us-east-1" |
| 66 | + } |
| 67 | + |
| 68 | + // Create AWS config with static credentials |
| 69 | + cfg, err := config.LoadDefaultConfig(stdcontext.TODO(), |
| 70 | + config.WithRegion(region), |
| 71 | + config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider( |
| 72 | + accessKeyID, |
| 73 | + secretAccessKey, |
| 74 | + "", // Session token (not used) |
| 75 | + )), |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to load AWS config: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Create Bedrock Runtime client |
| 82 | + client := bedrockruntime.NewFromConfig(cfg) |
| 83 | + |
| 84 | + // Create Bedrock client for control plane operations (like ListFoundationModels) |
| 85 | + bedrockClient := awsbedrock.NewFromConfig(cfg) |
| 86 | + |
| 87 | + return &BedrockClient{ |
| 88 | + client: client, |
| 89 | + bedrockClient: bedrockClient, |
| 90 | + region: region, |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +// splitAPIKey splits the API key by colon separator |
| 95 | +func splitAPIKey(apiKey string) []string { |
| 96 | + result := []string{} |
| 97 | + current := "" |
| 98 | + |
| 99 | + for _, char := range apiKey { |
| 100 | + if char == ':' { |
| 101 | + result = append(result, current) |
| 102 | + current = "" |
| 103 | + } else { |
| 104 | + current += string(char) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if current != "" || len(result) > 0 { |
| 109 | + result = append(result, current) |
| 110 | + } |
| 111 | + |
| 112 | + return result |
| 113 | +} |
| 114 | + |
| 115 | +// formatAWSError formats AWS SDK errors with HTTP status codes and descriptive messages |
| 116 | +func formatAWSError(err error, operation string) error { |
| 117 | + if err == nil { |
| 118 | + return nil |
| 119 | + } |
| 120 | + |
| 121 | + // Try to extract HTTP response error |
| 122 | + var httpErr *awshttp.ResponseError |
| 123 | + if errors.As(err, &httpErr) { |
| 124 | + statusCode := httpErr.Response.StatusCode |
| 125 | + |
| 126 | + // Format error message based on status code |
| 127 | + var message string |
| 128 | + switch statusCode { |
| 129 | + case http.StatusBadRequest: // 400 |
| 130 | + message = "Bad Request - Invalid request format or parameters" |
| 131 | + case http.StatusForbidden: // 403 |
| 132 | + message = "Invalid credentials or insufficient permissions" |
| 133 | + case http.StatusNotFound: // 404 |
| 134 | + message = "Model may be invalid or unavailable in region" |
| 135 | + case http.StatusTooManyRequests: // 429 |
| 136 | + message = "Rate limit exceeded - please retry after a delay" |
| 137 | + case http.StatusInternalServerError: // 500 |
| 138 | + message = "AWS Bedrock service error" |
| 139 | + default: |
| 140 | + message = fmt.Sprintf("HTTP error: %s", httpErr.Error()) |
| 141 | + } |
| 142 | + |
| 143 | + return fmt.Errorf("Bedrock %s (status %d): %s", operation, statusCode, message) |
| 144 | + } |
| 145 | + |
| 146 | + // If not an HTTP error, return the original error with context |
| 147 | + return fmt.Errorf("Bedrock %s: %w", operation, err) |
| 148 | +} |
| 149 | + |
| 150 | +// buildRequestBody constructs the request body for Claude models on AWS Bedrock |
| 151 | +// Args: |
| 152 | +// |
| 153 | +// prompt: string - user's prompt to the AI |
| 154 | +// |
| 155 | +// Returns: anthropicRequest struct ready to be marshaled to JSON |
| 156 | +func buildRequestBody(prompt string) anthropicRequest { |
| 157 | + return anthropicRequest{ |
| 158 | + AnthropicVersion: "bedrock-2023-05-31", |
| 159 | + MaxTokens: 4096, |
| 160 | + Messages: []anthropicMessage{ |
| 161 | + { |
| 162 | + Role: "user", |
| 163 | + Content: prompt, |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// IsAvailable checks if Bedrock API is available |
| 170 | +// Returns: true if Bedrock is reachable, error if not |
| 171 | +func (bc *BedrockClient) IsAvailable() (bool, error) { |
| 172 | + // The API key validation is already done in NewBedrockClient |
| 173 | + // Here we attempt a lightweight API call to verify connectivity |
| 174 | + |
| 175 | + // Use ListFoundationModels as a lightweight check |
| 176 | + // This verifies both connectivity and credentials |
| 177 | + ctx := stdcontext.TODO() |
| 178 | + _, err := bc.bedrockClient.ListFoundationModels(ctx, &awsbedrock.ListFoundationModelsInput{}) |
| 179 | + |
| 180 | + if err != nil { |
| 181 | + return false, formatAWSError(err, "IsAvailable") |
| 182 | + } |
| 183 | + |
| 184 | + return true, nil |
| 185 | +} |
| 186 | + |
| 187 | +// Generate generates AI response with streaming |
| 188 | +// Args: |
| 189 | +// |
| 190 | +// prompt: string - user's prompt to the AI |
| 191 | +// model: string - model name to use (default: "anthropic.claude-haiku-4-5-v1:0") |
| 192 | +// context: []int - optional context tokens from previous conversation |
| 193 | +// |
| 194 | +// Returns: channel for streaming response chunks, error if request fails |
| 195 | +func (bc *BedrockClient) Generate(prompt string, model string, context []int) (<-chan string, error) { |
| 196 | + // Default model to Claude Haiku 4.5 if empty |
| 197 | + if model == "" { |
| 198 | + model = "anthropic.claude-haiku-4-5-v1:0" |
| 199 | + } |
| 200 | + |
| 201 | + // Construct request body using anthropicRequest struct |
| 202 | + requestBody := buildRequestBody(prompt) |
| 203 | + |
| 204 | + // Marshal request body to JSON |
| 205 | + requestBodyJSON, err := json.Marshal(requestBody) |
| 206 | + if err != nil { |
| 207 | + return nil, fmt.Errorf("failed to marshal request body: %w", err) |
| 208 | + } |
| 209 | + |
| 210 | + // Call InvokeModelWithResponseStream API |
| 211 | + ctx := stdcontext.Background() |
| 212 | + output, err := bc.client.InvokeModelWithResponseStream(ctx, &bedrockruntime.InvokeModelWithResponseStreamInput{ |
| 213 | + ModelId: &model, |
| 214 | + Body: requestBodyJSON, |
| 215 | + ContentType: aws.String("application/json"), |
| 216 | + }) |
| 217 | + if err != nil { |
| 218 | + return nil, formatAWSError(err, "Generate") |
| 219 | + } |
| 220 | + |
| 221 | + // Create buffered channel (capacity 10) for response chunks |
| 222 | + responseChan := make(chan string, 10) |
| 223 | + |
| 224 | + // Launch goroutine to process event stream |
| 225 | + go func() { |
| 226 | + defer close(responseChan) |
| 227 | + defer output.GetStream().Close() |
| 228 | + |
| 229 | + // Track if we encountered an error during processing |
| 230 | + var processingErr error |
| 231 | + |
| 232 | + // Process event stream |
| 233 | + for event := range output.GetStream().Events() { |
| 234 | + switch e := event.(type) { |
| 235 | + case *types.ResponseStreamMemberChunk: |
| 236 | + // Parse the chunk bytes to extract text content |
| 237 | + var response map[string]any |
| 238 | + if err := json.Unmarshal(e.Value.Bytes, &response); err != nil { |
| 239 | + // Event parsing failure - send error and stop processing |
| 240 | + processingErr = fmt.Errorf("error parsing response: %w", err) |
| 241 | + responseChan <- fmt.Sprintf("Error parsing response: %v", err) |
| 242 | + return |
| 243 | + } |
| 244 | + |
| 245 | + // Check for content_block_delta event type |
| 246 | + if eventType, ok := response["type"].(string); ok && eventType == "content_block_delta" { |
| 247 | + // Extract delta object |
| 248 | + if delta, ok := response["delta"].(map[string]any); ok { |
| 249 | + // Extract text from delta |
| 250 | + if text, ok := delta["text"].(string); ok && text != "" { |
| 251 | + responseChan <- text |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + // Check for message_stop event to close channel |
| 257 | + if eventType, ok := response["type"].(string); ok && eventType == "message_stop" { |
| 258 | + return |
| 259 | + } |
| 260 | + |
| 261 | + default: |
| 262 | + // Ignore other event types (including error events which are handled by stream.Err()) |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + // Check for stream errors after processing completes |
| 267 | + // This catches errors that occurred during streaming but weren't event parsing errors |
| 268 | + if processingErr == nil { |
| 269 | + if err := output.GetStream().Err(); err != nil { |
| 270 | + formattedErr := formatAWSError(err, "streaming") |
| 271 | + responseChan <- fmt.Sprintf("Stream error: %v", formattedErr) |
| 272 | + } |
| 273 | + } |
| 274 | + }() |
| 275 | + |
| 276 | + // Return channel immediately |
| 277 | + return responseChan, nil |
| 278 | +} |
| 279 | + |
| 280 | +// ListModels lists available Bedrock models |
| 281 | +// Returns: slice of model names, error if request fails |
| 282 | +func (bc *BedrockClient) ListModels() ([]string, error) { |
| 283 | + // Return hardcoded list of supported Claude models |
| 284 | + return []string{ |
| 285 | + "anthropic.claude-haiku-4-5-v1:0", |
| 286 | + "anthropic.claude-3-5-sonnet-20241022-v2:0", |
| 287 | + "anthropic.claude-3-5-haiku-20241022-v1:0", |
| 288 | + }, nil |
| 289 | +} |
0 commit comments