llmkit 0.1.3

Production-grade LLM client - 100+ providers, 11,000+ models. Pure Rust.
Documentation
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
//! AWS Bedrock provider implementation using the Converse API.
//!
//! This provider supports multiple model families hosted on AWS Bedrock:
//! - **Anthropic Claude**: Claude 4.5, Claude 4, Claude 3.5, Claude 3
//! - **Amazon Nova**: Nova Pro, Nova Lite, Nova Micro, Nova 2 Pro, Nova 2 Lite
//! - **Meta Llama**: Llama 4, Llama 3.3, Llama 3.2, Llama 3.1, Llama 3
//! - **Mistral AI**: Mistral Large, Mistral Small, Mixtral 8x7B
//! - **Cohere**: Command R+, Command R
//! - **AI21 Labs**: Jamba 1.5
//! - **Amazon Titan**: Titan Text Express, Titan Text Lite
//! - **DeepSeek**: DeepSeek-R1, DeepSeek-V3
//! - **Qwen (Alibaba)**: Qwen 2.5 (uses InvokeModel fallback)
//!
//! # Converse API
//!
//! This provider uses the AWS Bedrock Converse API which provides:
//! - Unified request/response format across all supported models
//! - Native support for cross-region inference profiles (e.g., `us.amazon.nova-micro-v1:0`)
//! - Consistent tool use support
//! - Automatic model routing
//!
//! # Configuration
//!
//! Bedrock uses AWS credentials. You can provide:
//! - Default credentials from `~/.aws/credentials`
//! - Environment variables (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`)
//! - IAM role (when running on AWS)
//!
//! # Example
//!
//! ```ignore
//! use llmkit::providers::bedrock::BedrockProvider;
//!
//! // Using default AWS credentials
//! let provider = BedrockProvider::from_env("us-east-1").await?;
//!
//! // With cross-region inference profile
//! let request = CompletionRequest::new(
//!     "us.amazon.nova-micro-v1:0",  // US inference profile
//!     vec![Message::user("Hello!")],
//! );
//! let response = provider.complete(request).await?;
//! ```

use std::collections::HashMap;
use std::pin::Pin;

use async_trait::async_trait;
use aws_config::BehaviorVersion;
use aws_sdk_bedrockruntime::primitives::Blob;
use aws_sdk_bedrockruntime::types::{
    ContentBlock as BedrockContentBlock, ConversationRole, DocumentBlock, DocumentFormat,
    DocumentSource, ImageBlock, ImageFormat, ImageSource, InferenceConfiguration,
    Message as BedrockMessage, SystemContentBlock, Tool, ToolConfiguration, ToolInputSchema,
    ToolResultBlock, ToolResultContentBlock, ToolResultStatus, ToolSpecification, ToolUseBlock,
};
use aws_sdk_bedrockruntime::Client as BedrockClient;
use futures::Stream;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::{Error, Result};
use crate::provider::Provider;
use crate::types::{
    CompletionRequest, CompletionResponse, ContentBlock, ContentDelta, Role, StopReason,
    StreamChunk, StreamEventType, Usage,
};

/// Configuration for Bedrock provider.
#[derive(Debug, Clone)]
pub struct BedrockConfig {
    /// AWS region (e.g., "us-east-1")
    pub region: String,

    /// Request timeout
    pub timeout: std::time::Duration,

    /// Model IDs that should use InvokeModel instead of Converse
    /// (for models without Converse API support)
    pub invoke_model_overrides: HashMap<String, bool>,
}

impl Default for BedrockConfig {
    fn default() -> Self {
        Self {
            region: "us-east-1".to_string(),
            timeout: std::time::Duration::from_secs(120),
            invoke_model_overrides: HashMap::new(),
        }
    }
}

impl BedrockConfig {
    /// Create a new config with the specified region.
    pub fn new(region: impl Into<String>) -> Self {
        Self {
            region: region.into(),
            ..Default::default()
        }
    }

    /// Builder: Set timeout.
    pub fn with_timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Builder: Force a model to use InvokeModel instead of Converse.
    pub fn with_invoke_model_override(mut self, model_id: impl Into<String>) -> Self {
        self.invoke_model_overrides.insert(model_id.into(), true);
        self
    }
}

/// Builder for BedrockProvider.
pub struct BedrockBuilder {
    config: BedrockConfig,
}

impl BedrockBuilder {
    /// Create a new builder with default config.
    pub fn new() -> Self {
        Self {
            config: BedrockConfig::default(),
        }
    }

    /// Set the AWS region.
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.config.region = region.into();
        self
    }

    /// Set timeout.
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.config.timeout = timeout;
        self
    }

    /// Force a model to use InvokeModel instead of Converse.
    pub fn invoke_model_override(mut self, model_id: impl Into<String>) -> Self {
        self.config
            .invoke_model_overrides
            .insert(model_id.into(), true);
        self
    }

    /// Build the provider using default AWS credential chain.
    pub async fn build(self) -> Result<BedrockProvider> {
        let config = aws_config::defaults(BehaviorVersion::latest())
            .region(aws_config::Region::new(self.config.region.clone()))
            .load()
            .await;

        let client = BedrockClient::new(&config);

        Ok(BedrockProvider {
            client,
            config: self.config,
        })
    }
}

impl Default for BedrockBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// AWS Bedrock provider.
///
/// Uses the Converse API for unified access to all supported model families.
/// Falls back to InvokeModel for models without Converse support (e.g., Qwen).
pub struct BedrockProvider {
    client: BedrockClient,
    config: BedrockConfig,
}

impl BedrockProvider {
    /// Create a builder for the provider.
    pub fn builder() -> BedrockBuilder {
        BedrockBuilder::new()
    }

    /// Create a provider with default credentials and specified region.
    pub async fn from_env(region: impl Into<String>) -> Result<Self> {
        Self::builder().region(region).build().await
    }

    /// Create a provider from environment variable.
    pub async fn from_env_region() -> Result<Self> {
        let region = std::env::var("AWS_REGION")
            .or_else(|_| std::env::var("AWS_DEFAULT_REGION"))
            .unwrap_or_else(|_| "us-east-1".to_string());
        Self::from_env(region).await
    }

    /// Check if a model requires InvokeModel instead of Converse.
    fn requires_invoke_model(&self, model_id: &str) -> bool {
        let id = model_id.to_lowercase();

        // Check explicit overrides first
        if let Some(&use_invoke) = self.config.invoke_model_overrides.get(model_id) {
            return use_invoke;
        }

        // Models without Converse support
        // Note: Bedrock uses "qwen2-5" format (hyphen) while official Qwen uses "qwen2.5" (dot)
        id.contains("qwen2.5")
            || id.contains("qwen2-5")
            || id.contains("qwen2-vl")
            || id.contains("titan-embed")
    }
}

// ============================================================================
// Converse API Message Conversion
// ============================================================================

/// Build Converse API messages from CompletionRequest.
fn build_converse_messages(request: &CompletionRequest) -> Result<Vec<BedrockMessage>> {
    let mut messages = Vec::new();

    for msg in &request.messages {
        // Skip system messages - they go in the system parameter
        if msg.role == Role::System {
            continue;
        }

        let role = match msg.role {
            Role::User => ConversationRole::User,
            Role::Assistant => ConversationRole::Assistant,
            Role::System => continue, // Already handled above
        };

        let mut content_blocks = Vec::new();

        for block in &msg.content {
            match block {
                ContentBlock::Text { text } => {
                    content_blocks.push(BedrockContentBlock::Text(text.clone()));
                }
                ContentBlock::Image { media_type, data } => {
                    // Determine image format from media type
                    let format = match media_type.as_str() {
                        "image/png" => ImageFormat::Png,
                        "image/jpeg" | "image/jpg" => ImageFormat::Jpeg,
                        "image/gif" => ImageFormat::Gif,
                        "image/webp" => ImageFormat::Webp,
                        _ => ImageFormat::Png, // Default to PNG
                    };

                    // Decode base64 to bytes
                    let bytes =
                        base64::Engine::decode(&base64::engine::general_purpose::STANDARD, data)
                            .map_err(|e| {
                                Error::invalid_request(format!("Invalid base64 image: {}", e))
                            })?;

                    let image_block = ImageBlock::builder()
                        .format(format)
                        .source(ImageSource::Bytes(Blob::new(bytes)))
                        .build()
                        .map_err(|e| Error::invalid_request(e.to_string()))?;

                    content_blocks.push(BedrockContentBlock::Image(image_block));
                }
                ContentBlock::ToolUse { id, name, input } => {
                    // Convert serde_json::Value to aws_smithy_types::Document
                    let doc = json_value_to_document(input);

                    let tool_use = ToolUseBlock::builder()
                        .tool_use_id(id)
                        .name(name)
                        .input(doc)
                        .build()
                        .map_err(|e| Error::invalid_request(e.to_string()))?;

                    content_blocks.push(BedrockContentBlock::ToolUse(tool_use));
                }
                ContentBlock::ToolResult {
                    tool_use_id,
                    content,
                    is_error,
                } => {
                    let status = if *is_error {
                        ToolResultStatus::Error
                    } else {
                        ToolResultStatus::Success
                    };

                    let tool_result = ToolResultBlock::builder()
                        .tool_use_id(tool_use_id)
                        .content(ToolResultContentBlock::Text(content.clone()))
                        .status(status)
                        .build()
                        .map_err(|e| Error::invalid_request(e.to_string()))?;

                    content_blocks.push(BedrockContentBlock::ToolResult(tool_result));
                }
                ContentBlock::Document { source, .. } => {
                    // Extract media_type and data from DocumentSource
                    if let crate::types::DocumentSource::Base64 { media_type, data } = source {
                        // Determine document format
                        let format = match media_type.as_str() {
                            "application/pdf" => DocumentFormat::Pdf,
                            "text/plain" => DocumentFormat::Txt,
                            "text/html" => DocumentFormat::Html,
                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => {
                                DocumentFormat::Docx
                            }
                            _ => DocumentFormat::Pdf, // Default
                        };

                        let bytes = base64::Engine::decode(
                            &base64::engine::general_purpose::STANDARD,
                            data,
                        )
                        .map_err(|e| {
                            Error::invalid_request(format!("Invalid base64 document: {}", e))
                        })?;

                        let doc = DocumentBlock::builder()
                            .format(format)
                            .name("document")
                            .source(DocumentSource::Bytes(Blob::new(bytes)))
                            .build()
                            .map_err(|e| Error::invalid_request(e.to_string()))?;

                        content_blocks.push(BedrockContentBlock::Document(doc));
                    }
                    // URL and File sources not directly supported by Bedrock Converse API
                }
                ContentBlock::ImageUrl { .. } => {
                    // Image URLs are not directly supported by Bedrock - would need to fetch and convert
                    // Skip for now - users should pass base64 encoded images
                }
                ContentBlock::Thinking { thinking } => {
                    // Thinking blocks can be passed as text to models that support it
                    content_blocks.push(BedrockContentBlock::Text(thinking.clone()));
                }
                ContentBlock::TextWithCache { text, .. } => {
                    // Bedrock doesn't support cache control - just pass the text
                    content_blocks.push(BedrockContentBlock::Text(text.clone()));
                }
            }
        }

        if !content_blocks.is_empty() {
            let message = BedrockMessage::builder()
                .role(role)
                .set_content(Some(content_blocks))
                .build()
                .map_err(|e| Error::invalid_request(e.to_string()))?;

            messages.push(message);
        }
    }

    Ok(messages)
}

/// Build system content from request.
fn build_system_content(request: &CompletionRequest) -> Option<Vec<SystemContentBlock>> {
    // Check for explicit system field first
    if let Some(ref system) = request.system {
        return Some(vec![SystemContentBlock::Text(system.clone())]);
    }

    // Also check for system messages in the messages array
    let system_text: String = request
        .messages
        .iter()
        .filter(|m| m.role == Role::System)
        .map(|m| m.text_content())
        .collect::<Vec<_>>()
        .join("\n\n");

    if system_text.is_empty() {
        None
    } else {
        Some(vec![SystemContentBlock::Text(system_text)])
    }
}

/// Build inference configuration from request.
fn build_inference_config(request: &CompletionRequest) -> Option<InferenceConfiguration> {
    let mut builder = InferenceConfiguration::builder();
    let mut has_config = false;

    if let Some(max_tokens) = request.max_tokens {
        builder = builder.max_tokens(max_tokens as i32);
        has_config = true;
    }

    if let Some(temperature) = request.temperature {
        builder = builder.temperature(temperature);
        has_config = true;
    }

    if let Some(top_p) = request.top_p {
        builder = builder.top_p(top_p);
        has_config = true;
    }

    if let Some(ref stop_sequences) = request.stop_sequences {
        builder = builder.set_stop_sequences(Some(stop_sequences.clone()));
        has_config = true;
    }

    if has_config {
        Some(builder.build())
    } else {
        None
    }
}

/// Build tool configuration from request.
fn build_tool_config(request: &CompletionRequest) -> Option<ToolConfiguration> {
    let tools = request.tools.as_ref()?;

    if tools.is_empty() {
        return None;
    }

    let tool_specs: Vec<Tool> = tools
        .iter()
        .filter_map(|t| {
            let input_schema = ToolInputSchema::Json(json_value_to_document(&t.input_schema));

            let spec = ToolSpecification::builder()
                .name(&t.name)
                .description(&t.description)
                .input_schema(input_schema)
                .build()
                .ok()?;

            Some(Tool::ToolSpec(spec))
        })
        .collect();

    if tool_specs.is_empty() {
        return None;
    }

    ToolConfiguration::builder()
        .set_tools(Some(tool_specs))
        .build()
        .ok()
}

// ============================================================================
// Helper Functions for Document Conversion
// ============================================================================

/// Convert serde_json::Value to aws_smithy_types::Document.
fn json_value_to_document(value: &Value) -> aws_smithy_types::Document {
    match value {
        Value::Null => aws_smithy_types::Document::Null,
        Value::Bool(b) => aws_smithy_types::Document::Bool(*b),
        Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                aws_smithy_types::Document::Number(aws_smithy_types::Number::PosInt(i as u64))
            } else if let Some(f) = n.as_f64() {
                aws_smithy_types::Document::Number(aws_smithy_types::Number::Float(f))
            } else {
                aws_smithy_types::Document::Null
            }
        }
        Value::String(s) => aws_smithy_types::Document::String(s.clone()),
        Value::Array(arr) => {
            aws_smithy_types::Document::Array(arr.iter().map(json_value_to_document).collect())
        }
        Value::Object(obj) => aws_smithy_types::Document::Object(
            obj.iter()
                .map(|(k, v)| (k.clone(), json_value_to_document(v)))
                .collect(),
        ),
    }
}

/// Convert aws_smithy_types::Document to serde_json::Value.
fn document_to_json_value(doc: &aws_smithy_types::Document) -> Value {
    match doc {
        aws_smithy_types::Document::Null => Value::Null,
        aws_smithy_types::Document::Bool(b) => Value::Bool(*b),
        aws_smithy_types::Document::Number(n) => match n {
            aws_smithy_types::Number::PosInt(i) => Value::Number((*i).into()),
            aws_smithy_types::Number::NegInt(i) => Value::Number((*i).into()),
            aws_smithy_types::Number::Float(f) => {
                serde_json::Number::from_f64(*f).map_or(Value::Null, Value::Number)
            }
        },
        aws_smithy_types::Document::String(s) => Value::String(s.clone()),
        aws_smithy_types::Document::Array(arr) => {
            Value::Array(arr.iter().map(document_to_json_value).collect())
        }
        aws_smithy_types::Document::Object(obj) => Value::Object(
            obj.iter()
                .map(|(k, v)| (k.clone(), document_to_json_value(v)))
                .collect(),
        ),
    }
}

// ============================================================================
// Converse API Response Parsing
// ============================================================================

/// Parse Converse API response to CompletionResponse.
fn parse_converse_response(
    response: aws_sdk_bedrockruntime::operation::converse::ConverseOutput,
    model: &str,
) -> Result<CompletionResponse> {
    let output = response
        .output
        .ok_or_else(|| Error::server(500, "No output in Bedrock response"))?;

    let message = match output {
        aws_sdk_bedrockruntime::types::ConverseOutput::Message(msg) => msg,
        _ => return Err(Error::server(500, "Unexpected output type from Bedrock")),
    };

    let mut content = Vec::new();

    for block in message.content {
        match block {
            BedrockContentBlock::Text(text) => {
                content.push(ContentBlock::Text { text });
            }
            BedrockContentBlock::ToolUse(tool_use) => {
                // Convert Document to serde_json::Value using our helper
                let input = document_to_json_value(&tool_use.input);

                content.push(ContentBlock::ToolUse {
                    id: tool_use.tool_use_id,
                    name: tool_use.name,
                    input,
                });
            }
            _ => {
                // Skip other content types
            }
        }
    }

    let stop_reason = match response.stop_reason {
        aws_sdk_bedrockruntime::types::StopReason::EndTurn => StopReason::EndTurn,
        aws_sdk_bedrockruntime::types::StopReason::ToolUse => StopReason::ToolUse,
        aws_sdk_bedrockruntime::types::StopReason::MaxTokens => StopReason::MaxTokens,
        aws_sdk_bedrockruntime::types::StopReason::StopSequence => StopReason::StopSequence,
        aws_sdk_bedrockruntime::types::StopReason::ContentFiltered => StopReason::ContentFilter,
        _ => StopReason::EndTurn,
    };

    let usage = response
        .usage
        .map(|u| Usage {
            input_tokens: u.input_tokens as u32,
            output_tokens: u.output_tokens as u32,
            cache_creation_input_tokens: 0,
            cache_read_input_tokens: 0,
        })
        .unwrap_or_default();

    Ok(CompletionResponse {
        id: format!("bedrock-{}", uuid::Uuid::new_v4()),
        model: model.to_string(),
        content,
        stop_reason,
        usage,
    })
}

// ============================================================================
// Converse Stream Parsing
// ============================================================================

/// Parse Converse stream to StreamChunk stream.
fn parse_converse_stream(
    output: aws_sdk_bedrockruntime::operation::converse_stream::ConverseStreamOutput,
) -> impl Stream<Item = Result<StreamChunk>> {
    use async_stream::stream;

    stream! {
        let mut event_receiver = output.stream;
        let mut sent_start = false;

        loop {
            match event_receiver.recv().await {
                Ok(Some(event)) => {
                    use aws_sdk_bedrockruntime::types::ConverseStreamOutput as CSO;

                    match event {
                        CSO::MessageStart(_) => {
                            if !sent_start {
                                yield Ok(StreamChunk {
                                    event_type: StreamEventType::MessageStart,
                                    index: None,
                                    delta: None,
                                    stop_reason: None,
                                    usage: None,
                                });
                                sent_start = true;
                            }
                        }
                        CSO::ContentBlockStart(start) => {
                            yield Ok(StreamChunk {
                                event_type: StreamEventType::ContentBlockStart,
                                index: Some(start.content_block_index as usize),
                                delta: None,
                                stop_reason: None,
                                usage: None,
                            });
                        }
                        CSO::ContentBlockDelta(delta) => {
                            if let Some(d) = delta.delta {
                                use aws_sdk_bedrockruntime::types::ContentBlockDelta as CBD;

                                match d {
                                    CBD::Text(text) => {
                                        yield Ok(StreamChunk {
                                            event_type: StreamEventType::ContentBlockDelta,
                                            index: Some(delta.content_block_index as usize),
                                            delta: Some(ContentDelta::Text { text }),
                                            stop_reason: None,
                                            usage: None,
                                        });
                                    }
                                    CBD::ToolUse(tool_use) => {
                                        yield Ok(StreamChunk {
                                            event_type: StreamEventType::ContentBlockDelta,
                                            index: Some(delta.content_block_index as usize),
                                            delta: Some(ContentDelta::ToolUse {
                                                id: None,
                                                name: None,
                                                input_json_delta: Some(tool_use.input),
                                            }),
                                            stop_reason: None,
                                            usage: None,
                                        });
                                    }
                                    _ => {}
                                }
                            }
                        }
                        CSO::ContentBlockStop(_) => {
                            yield Ok(StreamChunk {
                                event_type: StreamEventType::ContentBlockStop,
                                index: None,
                                delta: None,
                                stop_reason: None,
                                usage: None,
                            });
                        }
                        CSO::MessageStop(stop) => {
                            let stop_reason = match stop.stop_reason {
                                aws_sdk_bedrockruntime::types::StopReason::EndTurn => {
                                    Some(StopReason::EndTurn)
                                }
                                aws_sdk_bedrockruntime::types::StopReason::ToolUse => {
                                    Some(StopReason::ToolUse)
                                }
                                aws_sdk_bedrockruntime::types::StopReason::MaxTokens => {
                                    Some(StopReason::MaxTokens)
                                }
                                aws_sdk_bedrockruntime::types::StopReason::StopSequence => {
                                    Some(StopReason::StopSequence)
                                }
                                aws_sdk_bedrockruntime::types::StopReason::ContentFiltered => {
                                    Some(StopReason::ContentFilter)
                                }
                                _ => Some(StopReason::EndTurn),
                            };

                            yield Ok(StreamChunk {
                                event_type: StreamEventType::MessageStop,
                                index: None,
                                delta: None,
                                stop_reason,
                                usage: None,
                            });
                        }
                        CSO::Metadata(meta) => {
                            if let Some(usage) = meta.usage {
                                yield Ok(StreamChunk {
                                    event_type: StreamEventType::MessageDelta,
                                    index: None,
                                    delta: None,
                                    stop_reason: None,
                                    usage: Some(Usage {
                                        input_tokens: usage.input_tokens as u32,
                                        output_tokens: usage.output_tokens as u32,
                                        cache_creation_input_tokens: 0,
                                        cache_read_input_tokens: 0,
                                    }),
                                });
                            }
                        }
                        _ => {
                            // Skip other event types
                        }
                    }
                }
                Ok(None) => {
                    // Stream ended
                    break;
                }
                Err(e) => {
                    yield Err(Error::server(500, format!("Stream error: {}", e)));
                    break;
                }
            }
        }
    }
}

// ============================================================================
// Provider Implementation
// ============================================================================

#[async_trait]
impl Provider for BedrockProvider {
    fn name(&self) -> &str {
        "bedrock"
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse> {
        // Check if model requires InvokeModel fallback
        if self.requires_invoke_model(&request.model) {
            return self.complete_with_invoke_model(request).await;
        }

        // Build Converse API request
        let messages = build_converse_messages(&request)?;
        let system = build_system_content(&request);
        let inference_config = build_inference_config(&request);
        let tool_config = build_tool_config(&request);

        let mut converse_request = self
            .client
            .converse()
            .model_id(&request.model)
            .set_messages(Some(messages));

        if let Some(sys) = system {
            converse_request = converse_request.set_system(Some(sys));
        }

        if let Some(config) = inference_config {
            converse_request = converse_request.inference_config(config);
        }

        if let Some(tools) = tool_config {
            converse_request = converse_request.tool_config(tools);
        }

        let response = converse_request
            .send()
            .await
            .map_err(|e| Error::server(500, format!("Bedrock Converse error: {}", e)))?;

        parse_converse_response(response, &request.model)
    }

    async fn complete_stream(
        &self,
        request: CompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        // Check if model requires InvokeModel fallback
        if self.requires_invoke_model(&request.model) {
            return self.complete_stream_with_invoke_model(request).await;
        }

        // Build Converse Stream API request
        let messages = build_converse_messages(&request)?;
        let system = build_system_content(&request);
        let inference_config = build_inference_config(&request);
        let tool_config = build_tool_config(&request);

        let mut stream_request = self
            .client
            .converse_stream()
            .model_id(&request.model)
            .set_messages(Some(messages));

        if let Some(sys) = system {
            stream_request = stream_request.set_system(Some(sys));
        }

        if let Some(config) = inference_config {
            stream_request = stream_request.inference_config(config);
        }

        if let Some(tools) = tool_config {
            stream_request = stream_request.tool_config(tools);
        }

        let output = stream_request
            .send()
            .await
            .map_err(|e| Error::server(500, format!("Bedrock ConverseStream error: {}", e)))?;

        Ok(Box::pin(parse_converse_stream(output)))
    }

    fn supports_tools(&self) -> bool {
        true
    }

    fn supports_vision(&self) -> bool {
        true
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn supported_models(&self) -> Option<&[&str]> {
        Some(&[
            // Anthropic Claude 4.5
            "anthropic.claude-opus-4-5-20251101-v1:0",
            "anthropic.claude-sonnet-4-5-20250929-v1:0",
            "anthropic.claude-haiku-4-5-20251015-v1:0",
            // Anthropic Claude 4
            "anthropic.claude-opus-4-20250514-v1:0",
            "anthropic.claude-sonnet-4-20250514-v1:0",
            // Anthropic Claude 3.5
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            "anthropic.claude-3-5-haiku-20241022-v1:0",
            // Anthropic Claude 3 (legacy)
            "anthropic.claude-3-opus-20240229-v1:0",
            "anthropic.claude-3-sonnet-20240229-v1:0",
            "anthropic.claude-3-haiku-20240307-v1:0",
            // Amazon Nova 2 (latest)
            "amazon.nova-pro-2-v1:0",
            "amazon.nova-lite-2-v1:0",
            // Amazon Nova 1
            "amazon.nova-pro-v1:0",
            "amazon.nova-lite-v1:0",
            "amazon.nova-micro-v1:0",
            // Cross-region inference profiles (examples)
            "us.amazon.nova-micro-v1:0",
            "eu.amazon.nova-micro-v1:0",
            "apac.amazon.nova-micro-v1:0",
            "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
            "eu.anthropic.claude-3-5-sonnet-20241022-v2:0",
            // Meta Llama 4
            "meta.llama4-maverick-17b-instruct-v1:0",
            "meta.llama4-scout-17b-instruct-v1:0",
            // Meta Llama 3.3
            "meta.llama3-3-70b-instruct-v1:0",
            // Meta Llama 3.2
            "meta.llama3-2-90b-instruct-v1:0",
            "meta.llama3-2-11b-instruct-v1:0",
            "meta.llama3-2-3b-instruct-v1:0",
            "meta.llama3-2-1b-instruct-v1:0",
            // Meta Llama 3.1
            "meta.llama3-1-405b-instruct-v1:0",
            "meta.llama3-1-70b-instruct-v1:0",
            "meta.llama3-1-8b-instruct-v1:0",
            // Mistral
            "mistral.mistral-large-2411-v1:0",
            "mistral.mistral-small-2409-v1:0",
            "mistral.mixtral-8x7b-instruct-v0:1",
            // Cohere
            "cohere.command-r-plus-v1:0",
            "cohere.command-r-v1:0",
            // AI21
            "ai21.jamba-1-5-large-v1:0",
            "ai21.jamba-1-5-mini-v1:0",
            // Amazon Titan
            "amazon.titan-text-express-v1",
            "amazon.titan-text-lite-v1",
            // DeepSeek
            "deepseek.deepseek-r1-v1:0",
            "deepseek.deepseek-v3-v1:0",
            // Qwen (uses InvokeModel fallback)
            "qwen.qwen2-5-72b-instruct-v1:0",
            "qwen.qwen2-5-32b-instruct-v1:0",
            "qwen.qwen2-5-14b-instruct-v1:0",
            "qwen.qwen2-5-7b-instruct-v1:0",
        ])
    }

    fn default_model(&self) -> Option<&str> {
        Some("anthropic.claude-sonnet-4-5-20250929-v1:0")
    }
}

// ============================================================================
// InvokeModel Fallback (for Qwen and other models without Converse support)
// ============================================================================

impl BedrockProvider {
    /// Complete using InvokeModel API (fallback for unsupported models).
    async fn complete_with_invoke_model(
        &self,
        request: CompletionRequest,
    ) -> Result<CompletionResponse> {
        let body = build_qwen_request(&request)?;

        let result = self
            .client
            .invoke_model()
            .model_id(&request.model)
            .content_type("application/json")
            .accept("application/json")
            .body(Blob::new(body))
            .send()
            .await
            .map_err(|e| Error::server(500, format!("Bedrock InvokeModel error: {}", e)))?;

        let response_body = result.body.into_inner();
        parse_qwen_response(&response_body, &request.model)
    }

    /// Stream using InvokeModel API (fallback for unsupported models).
    async fn complete_stream_with_invoke_model(
        &self,
        request: CompletionRequest,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        let body = build_qwen_request(&request)?;

        let result = self
            .client
            .invoke_model_with_response_stream()
            .model_id(&request.model)
            .content_type("application/json")
            .accept("application/json")
            .body(Blob::new(body))
            .send()
            .await
            .map_err(|e| Error::server(500, format!("Bedrock InvokeModel stream error: {}", e)))?;

        Ok(Box::pin(parse_qwen_stream(result)))
    }
}

// ============================================================================
// Qwen Adapter (InvokeModel fallback)
// ============================================================================

/// Build Qwen request body.
fn build_qwen_request(request: &CompletionRequest) -> Result<Vec<u8>> {
    let mut messages: Vec<QwenMessage> = Vec::new();

    // Add system message
    if let Some(ref system) = request.system {
        messages.push(QwenMessage {
            role: "system".to_string(),
            content: system.clone(),
        });
    }

    for msg in &request.messages {
        let role = match msg.role {
            Role::User => "user",
            Role::Assistant => "assistant",
            Role::System => "system",
        };
        messages.push(QwenMessage {
            role: role.to_string(),
            content: msg.text_content(),
        });
    }

    let qwen_request = QwenRequest {
        messages,
        max_tokens: request.max_tokens.unwrap_or(4096),
        temperature: request.temperature,
        top_p: request.top_p,
        stop: request.stop_sequences.clone(),
    };

    serde_json::to_vec(&qwen_request).map_err(|e| Error::invalid_request(e.to_string()))
}

/// Parse Qwen response.
fn parse_qwen_response(body: &[u8], model: &str) -> Result<CompletionResponse> {
    let response: QwenResponse = serde_json::from_slice(body)
        .map_err(|e| Error::server(500, format!("Failed to parse Qwen response: {}", e)))?;

    let choice = response.choices.into_iter().next().unwrap_or_default();

    let stop_reason = match choice.finish_reason.as_deref() {
        Some("stop") => StopReason::EndTurn,
        Some("length") => StopReason::MaxTokens,
        _ => StopReason::EndTurn,
    };

    Ok(CompletionResponse {
        id: response
            .id
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
        model: model.to_string(),
        content: vec![ContentBlock::Text {
            text: choice.message.content,
        }],
        stop_reason,
        usage: Usage {
            input_tokens: response.usage.prompt_tokens,
            output_tokens: response.usage.completion_tokens,
            cache_creation_input_tokens: 0,
            cache_read_input_tokens: 0,
        },
    })
}

/// Parse Qwen stream.
fn parse_qwen_stream(
    output: aws_sdk_bedrockruntime::operation::invoke_model_with_response_stream::InvokeModelWithResponseStreamOutput,
) -> impl Stream<Item = Result<StreamChunk>> {
    use async_stream::stream;
    use aws_sdk_bedrockruntime::types::ResponseStream;

    stream! {
        let mut event_receiver = output.body;
        let mut sent_start = false;

        loop {
            match event_receiver.recv().await {
                Ok(Some(event)) => {
                    if let ResponseStream::Chunk(chunk) = event {
                        if let Some(bytes) = chunk.bytes {
                            let bytes = bytes.into_inner();

                            if !sent_start {
                                yield Ok(StreamChunk {
                                    event_type: StreamEventType::MessageStart,
                                    index: None,
                                    delta: None,
                                    stop_reason: None,
                                    usage: None,
                                });
                                sent_start = true;
                            }

                            if let Ok(parsed) = serde_json::from_slice::<QwenStreamEvent>(&bytes) {
                                if let Some(choices) = parsed.choices {
                                    for choice in choices {
                                        if let Some(delta) = choice.delta {
                                            if let Some(content) = delta.content {
                                                yield Ok(StreamChunk {
                                                    event_type: StreamEventType::ContentBlockDelta,
                                                    index: Some(0),
                                                    delta: Some(ContentDelta::Text { text: content }),
                                                    stop_reason: None,
                                                    usage: None,
                                                });
                                            }
                                        }
                                        if let Some(finish_reason) = choice.finish_reason {
                                            yield Ok(StreamChunk {
                                                event_type: StreamEventType::MessageDelta,
                                                index: None,
                                                delta: None,
                                                stop_reason: Some(match finish_reason.as_str() {
                                                    "stop" => StopReason::EndTurn,
                                                    "length" => StopReason::MaxTokens,
                                                    _ => StopReason::EndTurn,
                                                }),
                                                usage: None,
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Ok(None) => {
                    break;
                }
                Err(e) => {
                    yield Err(Error::server(500, format!("Stream error: {}", e)));
                    break;
                }
            }
        }
    }
}

// ============================================================================
// Qwen Types (for InvokeModel fallback)
// ============================================================================

#[derive(Debug, Serialize)]
struct QwenRequest {
    messages: Vec<QwenMessage>,
    max_tokens: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    stop: Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
struct QwenMessage {
    role: String,
    content: String,
}

#[derive(Debug, Deserialize)]
struct QwenResponse {
    id: Option<String>,
    choices: Vec<QwenChoice>,
    usage: QwenUsage,
}

#[derive(Debug, Default, Deserialize)]
struct QwenChoice {
    message: QwenResponseMessage,
    finish_reason: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct QwenResponseMessage {
    content: String,
}

#[derive(Debug, Default, Deserialize)]
struct QwenUsage {
    prompt_tokens: u32,
    completion_tokens: u32,
}

#[derive(Debug, Deserialize)]
struct QwenStreamEvent {
    choices: Option<Vec<QwenStreamChoice>>,
}

#[derive(Debug, Deserialize)]
struct QwenStreamChoice {
    delta: Option<QwenStreamDelta>,
    finish_reason: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
struct QwenStreamDelta {
    content: Option<String>,
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Message;

    #[test]
    fn test_requires_invoke_model() {
        let config = BedrockConfig::default();

        // Helper function to check require invoke model logic
        fn check_requires_invoke(model_id: &str, config: &BedrockConfig) -> bool {
            let id = model_id.to_lowercase();

            // Check explicit overrides first
            if let Some(&use_invoke) = config.invoke_model_overrides.get(model_id) {
                return use_invoke;
            }

            // Models without Converse support
            // Note: Bedrock uses "qwen2-5" format (hyphen) while official Qwen uses "qwen2.5" (dot)
            id.contains("qwen2.5")
                || id.contains("qwen2-5")
                || id.contains("qwen2-vl")
                || id.contains("titan-embed")
        }

        // Qwen models should require invoke_model
        assert!(check_requires_invoke(
            "qwen.qwen2-5-72b-instruct-v1:0",
            &config
        ));
        assert!(check_requires_invoke(
            "qwen.qwen2.5-14b-instruct-v1:0",
            &config
        ));

        // Other models should use Converse
        assert!(!check_requires_invoke(
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            &config
        ));
        assert!(!check_requires_invoke("amazon.nova-micro-v1:0", &config));
        assert!(!check_requires_invoke("us.amazon.nova-micro-v1:0", &config)); // Inference profile
        assert!(!check_requires_invoke(
            "meta.llama3-70b-instruct-v1:0",
            &config
        ));
        assert!(!check_requires_invoke(
            "mistral.mistral-large-2407-v1:0",
            &config
        ));
        assert!(!check_requires_invoke(
            "cohere.command-r-plus-v1:0",
            &config
        ));
        assert!(!check_requires_invoke("ai21.jamba-1-5-large-v1:0", &config));
        assert!(!check_requires_invoke("deepseek.deepseek-r1-v1:0", &config));
    }

    #[test]
    fn test_inference_profile_support() {
        let config = BedrockConfig::default();

        // Helper function to check require invoke model logic
        fn check_requires_invoke(model_id: &str, config: &BedrockConfig) -> bool {
            let id = model_id.to_lowercase();

            if let Some(&use_invoke) = config.invoke_model_overrides.get(model_id) {
                return use_invoke;
            }

            id.contains("qwen2.5")
                || id.contains("qwen2-5")
                || id.contains("qwen2-vl")
                || id.contains("titan-embed")
        }

        // All inference profiles should use Converse (not invoke_model)
        assert!(!check_requires_invoke("us.amazon.nova-micro-v1:0", &config));
        assert!(!check_requires_invoke("eu.amazon.nova-micro-v1:0", &config));
        assert!(!check_requires_invoke(
            "apac.amazon.nova-micro-v1:0",
            &config
        ));
        assert!(!check_requires_invoke(
            "global.anthropic.claude-opus-4-5-20251101-v1:0",
            &config
        ));
        assert!(!check_requires_invoke(
            "us.anthropic.claude-3-5-sonnet-20241022-v2:0",
            &config
        ));
    }

    #[test]
    fn test_config_builder() {
        let config = BedrockConfig::new("us-west-2")
            .with_timeout(std::time::Duration::from_secs(60))
            .with_invoke_model_override("custom-model");

        assert_eq!(config.region, "us-west-2");
        assert_eq!(config.timeout, std::time::Duration::from_secs(60));
        assert_eq!(
            config.invoke_model_overrides.get("custom-model"),
            Some(&true)
        );
    }

    #[test]
    fn test_build_system_content() {
        // Test with explicit system field
        let request = CompletionRequest::new(
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            vec![Message::user("Hello!")],
        )
        .with_system("You are helpful");

        let system = build_system_content(&request);
        assert!(system.is_some());

        // Test without system
        let request = CompletionRequest::new(
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            vec![Message::user("Hello!")],
        );

        let system = build_system_content(&request);
        assert!(system.is_none());
    }

    #[test]
    fn test_build_inference_config() {
        let request = CompletionRequest::new(
            "anthropic.claude-3-5-sonnet-20241022-v2:0",
            vec![Message::user("Hello!")],
        )
        .with_max_tokens(1024)
        .with_temperature(0.7);

        let config = build_inference_config(&request);
        assert!(config.is_some());
    }

    #[test]
    fn test_qwen_request_conversion() {
        let request = CompletionRequest::new(
            "qwen.qwen2-5-72b-instruct-v1:0",
            vec![Message::user("Hello!")],
        )
        .with_system("You are helpful");

        let body = build_qwen_request(&request).unwrap();
        let parsed: serde_json::Value = serde_json::from_slice(&body).unwrap();

        assert!(parsed["messages"].is_array());
        // Should have system + user = 2 messages
        assert_eq!(parsed["messages"].as_array().unwrap().len(), 2);
    }
}