ironclaw 0.22.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! AWS Bedrock LLM provider using the native Converse API.
//!
//! Uses `aws-sdk-bedrockruntime` to call `client.converse()` directly,
//! bypassing the OpenAI-compatible layer. Supports standard AWS auth methods:
//! IAM credentials, SSO profiles, and instance roles — all handled
//! transparently by the AWS SDK credential chain.

use std::collections::HashMap;
use std::sync::RwLock;

use async_trait::async_trait;
use aws_config::{BehaviorVersion, Region};
use aws_sdk_bedrockruntime::Client;
use aws_sdk_bedrockruntime::operation::converse::ConverseError;
use aws_sdk_bedrockruntime::types::{
    AnyToolChoice, AutoToolChoice, ContentBlock, ConversationRole, InferenceConfiguration, Message,
    StopReason, SystemContentBlock, Tool, ToolChoice, ToolConfiguration, ToolInputSchema,
    ToolResultBlock, ToolResultContentBlock, ToolResultStatus, ToolSpecification, ToolUseBlock,
};
use aws_smithy_types::Document;
use rust_decimal::Decimal;

use crate::llm::config::BedrockConfig;
use crate::llm::error::LlmError;
use crate::llm::provider::{
    CompletionRequest, CompletionResponse, FinishReason, LlmProvider, ModelMetadata, ToolCall,
    ToolCompletionRequest, ToolCompletionResponse, ToolDefinition,
};

/// AWS Bedrock provider using the native Converse API.
pub struct BedrockProvider {
    client: Client,
    /// Base model ID for display purposes (without prefix).
    display_model: String,
    /// Cross-region prefix (e.g. "us.", "global.") or empty.
    cross_region_prefix: String,
    /// Active model ID (with cross-region prefix), switchable at runtime via `set_model()`.
    active_model: RwLock<String>,
}

impl BedrockProvider {
    /// Create a new Bedrock provider from configuration.
    ///
    /// Async because the AWS SDK config loader requires an async context
    /// to resolve credentials from SSO profiles, IMDS, etc.
    pub async fn new(config: &BedrockConfig) -> Result<Self, LlmError> {
        let cross_region_prefix = config
            .cross_region
            .as_ref()
            .map(|prefix| format!("{}.", prefix))
            .unwrap_or_default();

        let model_id = format!("{}{}", cross_region_prefix, config.model);

        let mut builder = aws_config::defaults(BehaviorVersion::latest())
            .region(Region::new(config.region.clone()));
        if let Some(ref profile) = config.profile {
            builder = builder.profile_name(profile);
        }
        let sdk_config = builder.load().await;

        let client = Client::new(&sdk_config);

        Ok(Self {
            client,
            display_model: config.model.clone(),
            cross_region_prefix,
            active_model: RwLock::new(model_id),
        })
    }

    /// Get the currently active model ID (with cross-region prefix).
    fn current_model_id(&self) -> String {
        match self.active_model.read() {
            Ok(guard) => guard.clone(),
            Err(poisoned) => {
                tracing::warn!("active_model lock poisoned while reading; continuing");
                poisoned.into_inner().clone()
            }
        }
    }
}

#[async_trait]
impl LlmProvider for BedrockProvider {
    fn model_name(&self) -> &str {
        &self.display_model
    }

    fn cost_per_token(&self) -> (Decimal, Decimal) {
        // Bedrock billing is on the AWS bill, not trackable per-token here.
        (Decimal::ZERO, Decimal::ZERO)
    }

    async fn complete(&self, request: CompletionRequest) -> Result<CompletionResponse, LlmError> {
        let model_id = self.current_model_id();

        let mut messages = request.messages;
        crate::llm::provider::sanitize_tool_messages(&mut messages);

        let (system_blocks, bedrock_messages) = convert_messages(&messages)?;

        if bedrock_messages.is_empty() {
            return Err(LlmError::RequestFailed {
                provider: "bedrock".to_string(),
                reason: "Bedrock requires at least one user or assistant message".to_string(),
            });
        }

        let mut builder = self
            .client
            .converse()
            .model_id(&model_id)
            .set_system(if system_blocks.is_empty() {
                None
            } else {
                Some(system_blocks)
            })
            .set_messages(Some(bedrock_messages));

        if let Some(config) = build_inference_config(
            request.temperature,
            request.max_tokens,
            request.stop_sequences.as_deref(),
        ) {
            builder = builder.inference_config(config);
        }

        let response = builder.send().await.map_err(|e| map_sdk_error(&e))?;

        let (text, _tool_calls) = extract_content_blocks(response.output())?;
        let (input_tokens, output_tokens) = extract_token_usage(response.usage());

        Ok(CompletionResponse {
            content: text,
            input_tokens,
            output_tokens,
            finish_reason: map_stop_reason(response.stop_reason()),
            cache_creation_input_tokens: 0,
            cache_read_input_tokens: 0,
        })
    }

    async fn complete_with_tools(
        &self,
        request: ToolCompletionRequest,
    ) -> Result<ToolCompletionResponse, LlmError> {
        let model_id = self.current_model_id();

        let mut messages = request.messages;
        crate::llm::provider::sanitize_tool_messages(&mut messages);

        let (system_blocks, bedrock_messages) = convert_messages(&messages)?;

        if bedrock_messages.is_empty() {
            return Err(LlmError::RequestFailed {
                provider: "bedrock".to_string(),
                reason: "Bedrock requires at least one user or assistant message".to_string(),
            });
        }

        let tool_config = build_tool_config(&request.tools, request.tool_choice.as_deref())?;

        let mut builder = self
            .client
            .converse()
            .model_id(&model_id)
            .set_system(if system_blocks.is_empty() {
                None
            } else {
                Some(system_blocks)
            })
            .set_messages(Some(bedrock_messages));

        if let Some(tc) = tool_config {
            builder = builder.tool_config(tc);
        }

        if let Some(config) = build_inference_config(
            request.temperature,
            request.max_tokens,
            request.stop_sequences.as_deref(),
        ) {
            builder = builder.inference_config(config);
        }

        let response = builder.send().await.map_err(|e| map_sdk_error(&e))?;

        let (text, tool_calls) = extract_content_blocks(response.output())?;
        let (input_tokens, output_tokens) = extract_token_usage(response.usage());

        Ok(ToolCompletionResponse {
            content: if text.is_empty() { None } else { Some(text) },
            tool_calls,
            input_tokens,
            output_tokens,
            finish_reason: map_stop_reason(response.stop_reason()),
            cache_creation_input_tokens: 0,
            cache_read_input_tokens: 0,
        })
    }

    async fn model_metadata(&self) -> Result<ModelMetadata, LlmError> {
        Ok(ModelMetadata {
            id: self.current_model_id(),
            context_length: None,
        })
    }

    fn active_model_name(&self) -> String {
        self.current_model_id()
    }

    fn effective_model_name(&self, _requested_model: Option<&str>) -> String {
        // Bedrock doesn't support per-request model overrides in Converse API;
        // the model is part of the request builder, not the message body.
        self.active_model_name()
    }

    fn set_model(&self, model: &str) -> Result<(), LlmError> {
        let new_id = format!("{}{}", self.cross_region_prefix, model);
        match self.active_model.write() {
            Ok(mut guard) => {
                *guard = new_id;
            }
            Err(poisoned) => {
                tracing::warn!("active_model lock poisoned while writing; continuing");
                *poisoned.into_inner() = new_id;
            }
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Inference configuration
// ---------------------------------------------------------------------------

/// Build an `InferenceConfiguration` from optional temperature and max_tokens.
/// Returns `None` if neither is set.
fn build_inference_config(
    temperature: Option<f32>,
    max_tokens: Option<u32>,
    stop_sequences: Option<&[String]>,
) -> Option<InferenceConfiguration> {
    let mut builder = InferenceConfiguration::builder();
    let mut needs_config = false;

    if let Some(temp) = temperature {
        builder = builder.temperature(temp);
        needs_config = true;
    }
    if let Some(tokens) = max_tokens {
        builder = builder.max_tokens(i32::try_from(tokens).unwrap_or(i32::MAX));
        needs_config = true;
    }
    if let Some(seqs) = stop_sequences
        && !seqs.is_empty()
    {
        builder = builder.set_stop_sequences(Some(seqs.to_vec()));
        needs_config = true;
    }

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

// ---------------------------------------------------------------------------
// Message conversion
// ---------------------------------------------------------------------------

/// Convert IronClaw `ChatMessage` list into Bedrock system blocks + messages.
///
/// Key differences from OpenAI/Anthropic protocol:
/// 1. System messages are extracted and passed separately.
/// 2. Tool results (role=Tool) become `ContentBlock::ToolResult` inside User messages.
/// 3. Consecutive tool results are merged into a single User message.
/// 4. Bedrock requires strict user/assistant alternation.
fn convert_messages(
    messages: &[crate::llm::provider::ChatMessage],
) -> Result<(Vec<SystemContentBlock>, Vec<Message>), LlmError> {
    use crate::llm::provider::Role;

    let mut system_blocks = Vec::new();
    let mut bedrock_messages: Vec<Message> = Vec::new();
    let mut pending_tool_results: Vec<ContentBlock> = Vec::new();

    for msg in messages {
        match msg.role {
            Role::System => {
                if !msg.content.is_empty() {
                    system_blocks.push(SystemContentBlock::Text(msg.content.clone()));
                }
            }
            Role::User => {
                // Flush any pending tool results as a User message first
                flush_tool_results(&mut pending_tool_results, &mut bedrock_messages)?;

                let content = vec![ContentBlock::Text(msg.content.clone())];
                push_message(&mut bedrock_messages, ConversationRole::User, content)?;
            }
            Role::Assistant => {
                // Flush any pending tool results before an assistant message
                flush_tool_results(&mut pending_tool_results, &mut bedrock_messages)?;

                let mut content = Vec::new();

                // Add text content if non-empty
                if !msg.content.is_empty() {
                    content.push(ContentBlock::Text(msg.content.clone()));
                }

                // Add tool use blocks if present
                if let Some(ref tool_calls) = msg.tool_calls {
                    for tc in tool_calls {
                        let input_doc = json_to_document(&tc.arguments);
                        let tool_use = ToolUseBlock::builder()
                            .tool_use_id(&tc.id)
                            .name(&tc.name)
                            .input(input_doc)
                            .build()
                            .map_err(|e| LlmError::RequestFailed {
                                provider: "bedrock".to_string(),
                                reason: format!("Failed to build ToolUseBlock: {}", e),
                            })?;
                        content.push(ContentBlock::ToolUse(tool_use));
                    }
                }

                if !content.is_empty() {
                    push_message(&mut bedrock_messages, ConversationRole::Assistant, content)?;
                }
            }
            Role::Tool => {
                // Accumulate tool results — they'll be flushed as a User message
                let tool_call_id = msg.tool_call_id.as_deref().unwrap_or("unknown");

                let status =
                    if let Ok(json) = serde_json::from_str::<serde_json::Value>(&msg.content) {
                        if json
                            .get("is_error")
                            .and_then(|v| v.as_bool())
                            .unwrap_or(false)
                        {
                            Some(ToolResultStatus::Error)
                        } else {
                            Some(ToolResultStatus::Success)
                        }
                    } else {
                        Some(ToolResultStatus::Success)
                    };

                let tool_result = ToolResultBlock::builder()
                    .tool_use_id(tool_call_id)
                    .content(ToolResultContentBlock::Text(msg.content.clone()))
                    .set_status(status)
                    .build()
                    .map_err(|e| LlmError::RequestFailed {
                        provider: "bedrock".to_string(),
                        reason: format!("Failed to build ToolResultBlock: {}", e),
                    })?;

                pending_tool_results.push(ContentBlock::ToolResult(tool_result));
            }
        }
    }

    // Flush any remaining tool results
    flush_tool_results(&mut pending_tool_results, &mut bedrock_messages)?;

    Ok((system_blocks, bedrock_messages))
}

/// Flush accumulated tool result blocks as a single User message.
fn flush_tool_results(
    pending: &mut Vec<ContentBlock>,
    messages: &mut Vec<Message>,
) -> Result<(), LlmError> {
    if pending.is_empty() {
        return Ok(());
    }

    let content: Vec<ContentBlock> = std::mem::take(pending);
    push_message(messages, ConversationRole::User, content)?;

    Ok(())
}

/// Push a message, enforcing Bedrock's alternation requirement.
///
/// If the last message has the same role, merge the content blocks into it
/// rather than creating a consecutive same-role message.
fn push_message(
    messages: &mut Vec<Message>,
    role: ConversationRole,
    content: Vec<ContentBlock>,
) -> Result<(), LlmError> {
    if content.is_empty() {
        return Ok(());
    }

    // Check if we need to merge with the previous message of the same role
    if let Some(last) = messages.last()
        && *last.role() == role
    {
        // Remove the last message, merge content, and re-push
        let prev = messages.pop().ok_or_else(|| LlmError::RequestFailed {
            provider: "bedrock".to_string(),
            reason: "Unexpected empty message list during merge".to_string(),
        })?;
        let mut merged = prev.content().to_vec();
        merged.extend(content);
        let msg = Message::builder()
            .role(role)
            .set_content(Some(merged))
            .build()
            .map_err(|e| LlmError::RequestFailed {
                provider: "bedrock".to_string(),
                reason: format!("Failed to build merged Message: {}", e),
            })?;
        messages.push(msg);
        return Ok(());
    }

    let msg = Message::builder()
        .role(role)
        .set_content(Some(content))
        .build()
        .map_err(|e| LlmError::RequestFailed {
            provider: "bedrock".to_string(),
            reason: format!("Failed to build Message: {}", e),
        })?;
    messages.push(msg);

    Ok(())
}

// ---------------------------------------------------------------------------
// Tool configuration
// ---------------------------------------------------------------------------

/// Build Bedrock `ToolConfiguration` from IronClaw tool definitions.
fn build_tool_config(
    tools: &[ToolDefinition],
    tool_choice: Option<&str>,
) -> Result<Option<ToolConfiguration>, LlmError> {
    if tools.is_empty() {
        return Ok(None);
    }

    let bedrock_tools: Vec<Tool> = tools
        .iter()
        .map(|td| {
            let input_schema = ToolInputSchema::Json(json_to_document(&td.parameters));
            let spec = ToolSpecification::builder()
                .name(&td.name)
                .description(&td.description)
                .input_schema(input_schema)
                .build()
                .map_err(|e| LlmError::RequestFailed {
                    provider: "bedrock".to_string(),
                    reason: format!("Failed to build ToolSpecification: {}", e),
                })?;
            Ok(Tool::ToolSpec(spec))
        })
        .collect::<Result<Vec<_>, LlmError>>()?;

    let choice = match tool_choice {
        Some("none") => {
            // If tool_choice is "none", don't send tool config at all
            return Ok(None);
        }
        Some("required") => Some(ToolChoice::Any(AnyToolChoice::builder().build())),
        // "auto" or anything else
        _ => Some(ToolChoice::Auto(AutoToolChoice::builder().build())),
    };

    let mut builder = ToolConfiguration::builder().set_tools(Some(bedrock_tools));
    if let Some(c) = choice {
        builder = builder.tool_choice(c);
    }

    let config = builder.build().map_err(|e| LlmError::RequestFailed {
        provider: "bedrock".to_string(),
        reason: format!("Failed to build ToolConfiguration: {}", e),
    })?;

    Ok(Some(config))
}

// ---------------------------------------------------------------------------
// Response extraction
// ---------------------------------------------------------------------------

/// Extract text content and tool calls from the Converse response output.
fn extract_content_blocks(
    output: Option<&aws_sdk_bedrockruntime::types::ConverseOutput>,
) -> Result<(String, Vec<ToolCall>), LlmError> {
    let output = output.ok_or_else(|| LlmError::RequestFailed {
        provider: "bedrock".to_string(),
        reason: "Converse response has no output".to_string(),
    })?;

    let message = output.as_message().map_err(|_| LlmError::RequestFailed {
        provider: "bedrock".to_string(),
        reason: "Converse output is not a message".to_string(),
    })?;

    let mut text_parts = Vec::new();
    let mut tool_calls = Vec::new();

    for block in message.content() {
        match block {
            ContentBlock::Text(t) => {
                text_parts.push(t.clone());
            }
            ContentBlock::ToolUse(tu) => {
                tool_calls.push(ToolCall {
                    id: tu.tool_use_id().to_string(),
                    name: tu.name().to_string(),
                    arguments: document_to_json(tu.input()),
                    reasoning: None,
                });
            }
            // Ignore reasoning, citations, images, etc.
            _ => {}
        }
    }

    Ok((text_parts.join(""), tool_calls))
}

/// Extract token usage from the response, converting i32 → u32 safely.
fn extract_token_usage(usage: Option<&aws_sdk_bedrockruntime::types::TokenUsage>) -> (u32, u32) {
    match usage {
        Some(u) => (
            u32::try_from(u.input_tokens()).unwrap_or(0),
            u32::try_from(u.output_tokens()).unwrap_or(0),
        ),
        None => (0, 0),
    }
}

/// Map Bedrock `StopReason` to IronClaw `FinishReason`.
fn map_stop_reason(reason: &StopReason) -> FinishReason {
    match reason {
        StopReason::EndTurn | StopReason::StopSequence => FinishReason::Stop,
        StopReason::ToolUse => FinishReason::ToolUse,
        StopReason::MaxTokens | StopReason::ModelContextWindowExceeded => FinishReason::Length,
        StopReason::ContentFiltered | StopReason::GuardrailIntervened => {
            FinishReason::ContentFilter
        }
        _ => FinishReason::Unknown,
    }
}

// ---------------------------------------------------------------------------
// Error mapping
// ---------------------------------------------------------------------------

/// Map AWS SDK errors to `LlmError`.
fn map_sdk_error<R: std::fmt::Debug>(
    error: &aws_sdk_bedrockruntime::error::SdkError<ConverseError, R>,
) -> LlmError {
    use aws_sdk_bedrockruntime::error::SdkError;

    match error {
        SdkError::ServiceError(service_err) => {
            let msg = match service_err.err() {
                ConverseError::ModelTimeoutException(e) => {
                    format!("Model timeout: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::ModelNotReadyException(e) => {
                    format!("Model not ready: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::ThrottlingException(e) => {
                    format!("Throttled: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::ValidationException(e) => {
                    format!("Validation error: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::AccessDeniedException(e) => {
                    format!("Access denied: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::ResourceNotFoundException(e) => {
                    format!("Resource not found: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::ModelErrorException(e) => {
                    format!("Model error: {}", e.message().unwrap_or("unknown"))
                }
                ConverseError::InternalServerException(e) => {
                    format!(
                        "Internal server error: {}",
                        e.message().unwrap_or("unknown")
                    )
                }
                ConverseError::ServiceUnavailableException(e) => {
                    format!("Service unavailable: {}", e.message().unwrap_or("unknown"))
                }
                _ => format!("Bedrock service error: {}", service_err.err()),
            };
            LlmError::RequestFailed {
                provider: "bedrock".to_string(),
                reason: msg,
            }
        }
        SdkError::TimeoutError(_) => LlmError::RequestFailed {
            provider: "bedrock".to_string(),
            reason: "Request timed out".to_string(),
        },
        SdkError::DispatchFailure(e) => LlmError::RequestFailed {
            provider: "bedrock".to_string(),
            reason: format!("Connection error: {:?}", e),
        },
        _ => LlmError::RequestFailed {
            provider: "bedrock".to_string(),
            reason: format!("AWS SDK error: {}", error),
        },
    }
}

// ---------------------------------------------------------------------------
// Document ↔ serde_json::Value conversion
// ---------------------------------------------------------------------------

/// Convert `serde_json::Value` to `aws_smithy_types::Document`.
pub(crate) fn json_to_document(value: &serde_json::Value) -> Document {
    match value {
        serde_json::Value::Null => Document::Null,
        serde_json::Value::Bool(b) => Document::Bool(*b),
        serde_json::Value::Number(n) => {
            if let Some(u) = n.as_u64() {
                Document::Number(aws_smithy_types::Number::PosInt(u))
            } else if let Some(i) = n.as_i64() {
                Document::Number(aws_smithy_types::Number::NegInt(i))
            } else if let Some(f) = n.as_f64() {
                Document::Number(aws_smithy_types::Number::Float(f))
            } else {
                Document::Null
            }
        }
        serde_json::Value::String(s) => Document::String(s.clone()),
        serde_json::Value::Array(arr) => {
            Document::Array(arr.iter().map(json_to_document).collect())
        }
        serde_json::Value::Object(obj) => {
            let map: HashMap<String, Document> = obj
                .iter()
                .map(|(k, v)| (k.clone(), json_to_document(v)))
                .collect();
            Document::Object(map)
        }
    }
}

/// Convert `aws_smithy_types::Document` to `serde_json::Value`.
pub(crate) fn document_to_json(doc: &Document) -> serde_json::Value {
    match doc {
        Document::Null => serde_json::Value::Null,
        Document::Bool(b) => serde_json::Value::Bool(*b),
        Document::Number(n) => match n {
            aws_smithy_types::Number::PosInt(u) => {
                serde_json::Value::Number(serde_json::Number::from(*u))
            }
            aws_smithy_types::Number::NegInt(i) => {
                serde_json::Value::Number(serde_json::Number::from(*i))
            }
            aws_smithy_types::Number::Float(f) => serde_json::Number::from_f64(*f)
                .map(serde_json::Value::Number)
                .unwrap_or(serde_json::Value::Null),
        },
        Document::String(s) => serde_json::Value::String(s.clone()),
        Document::Array(arr) => {
            serde_json::Value::Array(arr.iter().map(document_to_json).collect())
        }
        Document::Object(obj) => {
            let map: serde_json::Map<String, serde_json::Value> = obj
                .iter()
                .map(|(k, v)| (k.clone(), document_to_json(v)))
                .collect();
            serde_json::Value::Object(map)
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::provider::{ChatMessage, Role};

    #[test]
    fn test_json_to_document_round_trip() {
        let json = serde_json::json!({
            "name": "test",
            "count": 42,
            "negative": -7,
            "ratio": 3.125,
            "active": true,
            "nothing": null,
            "tags": ["a", "b"],
            "nested": {"x": 1}
        });

        let doc = json_to_document(&json);
        let back = document_to_json(&doc);

        assert_eq!(json, back);
    }

    #[test]
    fn test_json_to_document_empty_object() {
        let json = serde_json::json!({});
        let doc = json_to_document(&json);
        let back = document_to_json(&doc);
        assert_eq!(json, back);
    }

    #[test]
    fn test_convert_messages_system_extraction() {
        let messages = vec![
            ChatMessage::system("You are helpful."),
            ChatMessage::system("Be concise."),
            ChatMessage::user("Hello"),
        ];

        let (system, msgs) = convert_messages(&messages).unwrap();

        assert_eq!(system.len(), 2);
        assert_eq!(msgs.len(), 1);
        assert_eq!(*msgs[0].role(), ConversationRole::User);
    }

    #[test]
    fn test_convert_messages_basic_conversation() {
        let messages = vec![
            ChatMessage::user("Hi"),
            ChatMessage::assistant("Hello!"),
            ChatMessage::user("How are you?"),
        ];

        let (system, msgs) = convert_messages(&messages).unwrap();

        assert!(system.is_empty());
        assert_eq!(msgs.len(), 3);
        assert_eq!(*msgs[0].role(), ConversationRole::User);
        assert_eq!(*msgs[1].role(), ConversationRole::Assistant);
        assert_eq!(*msgs[2].role(), ConversationRole::User);
    }

    #[test]
    fn test_convert_messages_tool_results_merge_into_user() {
        let tc = crate::llm::provider::ToolCall {
            id: "call_1".to_string(),
            name: "echo".to_string(),
            arguments: serde_json::json!({"text": "hi"}),
            reasoning: None,
        };
        let tc2 = crate::llm::provider::ToolCall {
            id: "call_2".to_string(),
            name: "time".to_string(),
            arguments: serde_json::json!({}),
            reasoning: None,
        };

        let messages = vec![
            ChatMessage::user("Do things"),
            ChatMessage::assistant_with_tool_calls(None, vec![tc, tc2]),
            ChatMessage::tool_result("call_1", "echo", "hi back"),
            ChatMessage::tool_result("call_2", "time", "12:00"),
        ];

        let (_, msgs) = convert_messages(&messages).unwrap();

        // user, assistant (with tool_use), user (with merged tool_results)
        assert_eq!(msgs.len(), 3);
        assert_eq!(*msgs[2].role(), ConversationRole::User);
        // The merged user message should have 2 content blocks (both ToolResult)
        assert_eq!(msgs[2].content().len(), 2);
        assert!(msgs[2].content()[0].is_tool_result());
        assert!(msgs[2].content()[1].is_tool_result());
    }

    #[test]
    fn test_convert_messages_consecutive_users_merge() {
        let messages = vec![ChatMessage::user("First"), ChatMessage::user("Second")];

        let (_, msgs) = convert_messages(&messages).unwrap();

        // Should merge into a single User message with 2 text blocks
        assert_eq!(msgs.len(), 1);
        assert_eq!(*msgs[0].role(), ConversationRole::User);
        assert_eq!(msgs[0].content().len(), 2);
    }

    #[test]
    fn test_convert_messages_assistant_with_tool_calls() {
        let tc = crate::llm::provider::ToolCall {
            id: "call_1".to_string(),
            name: "search".to_string(),
            arguments: serde_json::json!({"query": "test"}),
            reasoning: None,
        };

        let messages = vec![
            ChatMessage::user("Search for test"),
            ChatMessage::assistant_with_tool_calls(Some("Let me search.".to_string()), vec![tc]),
        ];

        let (_, msgs) = convert_messages(&messages).unwrap();

        assert_eq!(msgs.len(), 2);
        assert_eq!(*msgs[1].role(), ConversationRole::Assistant);
        // Should have text + tool_use
        assert_eq!(msgs[1].content().len(), 2);
        assert!(msgs[1].content()[0].is_text());
        assert!(msgs[1].content()[1].is_tool_use());
    }

    #[test]
    fn test_convert_messages_empty_assistant_content_with_tool_calls() {
        let tc = crate::llm::provider::ToolCall {
            id: "call_1".to_string(),
            name: "echo".to_string(),
            arguments: serde_json::json!({}),
            reasoning: None,
        };

        let messages = vec![
            ChatMessage::user("Go"),
            ChatMessage::assistant_with_tool_calls(None, vec![tc]),
        ];

        let (_, msgs) = convert_messages(&messages).unwrap();

        assert_eq!(msgs.len(), 2);
        // Empty text should not add a Text block
        let assistant_content = msgs[1].content();
        assert_eq!(assistant_content.len(), 1);
        assert!(assistant_content[0].is_tool_use());
    }

    #[test]
    fn test_build_tool_config_empty_tools() {
        let result = build_tool_config(&[], None).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_build_tool_config_none_choice() {
        let result = build_tool_config(&[], Some("none")).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_build_tool_config_with_tools() {
        let tools = vec![ToolDefinition {
            name: "echo".to_string(),
            description: "Echoes input".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "text": {"type": "string"}
                }
            }),
        }];

        let result = build_tool_config(&tools, Some("auto")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_map_stop_reason() {
        assert_eq!(map_stop_reason(&StopReason::EndTurn), FinishReason::Stop);
        assert_eq!(
            map_stop_reason(&StopReason::StopSequence),
            FinishReason::Stop
        );
        assert_eq!(map_stop_reason(&StopReason::ToolUse), FinishReason::ToolUse);
        assert_eq!(
            map_stop_reason(&StopReason::MaxTokens),
            FinishReason::Length
        );
        assert_eq!(
            map_stop_reason(&StopReason::ContentFiltered),
            FinishReason::ContentFilter
        );
    }

    #[test]
    fn test_model_id_with_cross_region() {
        // Simulate what the constructor does
        let prefix = "us.";
        let model = "anthropic.claude-opus-4-6-v1";
        let model_id = format!("{}{}", prefix, model);
        assert_eq!(model_id, "us.anthropic.claude-opus-4-6-v1");
    }

    #[test]
    fn test_model_id_without_cross_region() {
        let prefix = "";
        let model = "anthropic.claude-opus-4-6-v1";
        let model_id = format!("{}{}", prefix, model);
        assert_eq!(model_id, "anthropic.claude-opus-4-6-v1");
    }

    #[test]
    fn test_convert_messages_tool_result_after_regular_user() {
        // Edge case: tool result appears after a user message (from sanitize_tool_messages rewrite)
        // This shouldn't happen normally but we should handle it gracefully
        let messages = vec![
            ChatMessage::user("Hello"),
            ChatMessage {
                role: Role::Tool,
                content: "result".to_string(),
                tool_call_id: Some("call_1".to_string()),
                name: Some("echo".to_string()),
                tool_calls: None,
                content_parts: Vec::new(),
            },
        ];

        let (_, msgs) = convert_messages(&messages).unwrap();

        // User + tool result (as user) = should merge into one User message
        assert_eq!(msgs.len(), 1);
        assert_eq!(*msgs[0].role(), ConversationRole::User);
    }

    #[test]
    fn test_extract_token_usage_present() {
        let usage = aws_sdk_bedrockruntime::types::TokenUsage::builder()
            .input_tokens(150)
            .output_tokens(42)
            .total_tokens(192)
            .build()
            .unwrap();
        let (input, output) = extract_token_usage(Some(&usage));
        assert_eq!(input, 150);
        assert_eq!(output, 42);
    }

    #[test]
    fn test_extract_token_usage_none() {
        let (input, output) = extract_token_usage(None);
        assert_eq!(input, 0);
        assert_eq!(output, 0);
    }

    #[test]
    fn test_extract_token_usage_negative_clamps_to_zero() {
        // Bedrock uses i32; negative values should not panic
        let usage = aws_sdk_bedrockruntime::types::TokenUsage::builder()
            .input_tokens(-1)
            .output_tokens(-5)
            .total_tokens(0)
            .build()
            .unwrap();
        let (input, output) = extract_token_usage(Some(&usage));
        assert_eq!(input, 0);
        assert_eq!(output, 0);
    }

    #[test]
    fn test_json_to_document_nested_arrays() {
        let json = serde_json::json!([[1, 2], [3, 4]]);
        let doc = json_to_document(&json);
        let back = document_to_json(&doc);
        assert_eq!(json, back);
    }

    #[test]
    fn test_json_to_document_large_numbers() {
        let json = serde_json::json!({
            "big_pos": u64::MAX,
            "big_neg": i64::MIN,
        });
        let doc = json_to_document(&json);
        let back = document_to_json(&doc);
        assert_eq!(json, back);
    }

    #[test]
    fn test_full_tool_round_trip_conversation() {
        // Simulate a complete tool-use conversation:
        // system → user → assistant(tool_calls) → tool_results → user follow-up
        let tc1 = crate::llm::provider::ToolCall {
            id: "call_abc".to_string(),
            name: "get_weather".to_string(),
            arguments: serde_json::json!({"city": "NYC"}),
            reasoning: None,
        };
        let tc2 = crate::llm::provider::ToolCall {
            id: "call_def".to_string(),
            name: "get_time".to_string(),
            arguments: serde_json::json!({"tz": "EST"}),
            reasoning: None,
        };

        let messages = vec![
            ChatMessage::system("You are a helpful assistant."),
            ChatMessage::user("What's the weather and time in NYC?"),
            ChatMessage::assistant_with_tool_calls(
                Some("Let me check both.".to_string()),
                vec![tc1, tc2],
            ),
            ChatMessage::tool_result("call_abc", "get_weather", "72°F and sunny"),
            ChatMessage::tool_result("call_def", "get_time", "3:45 PM EST"),
            ChatMessage::user("Thanks! What about tomorrow?"),
        ];

        let (system, msgs) = convert_messages(&messages).unwrap();

        // 1 system block
        assert_eq!(system.len(), 1);

        // Messages: user, assistant(text+2 tool_use), user(2 tool_results + follow-up text merged)
        // The follow-up user message "Thanks!" merges into the tool_results User message
        // because Bedrock requires strict user/assistant alternation.
        assert_eq!(msgs.len(), 3);

        // msg[0]: user "What's the weather..."
        assert_eq!(*msgs[0].role(), ConversationRole::User);
        assert_eq!(msgs[0].content().len(), 1);
        assert!(msgs[0].content()[0].is_text());

        // msg[1]: assistant with text + 2 tool_use blocks
        assert_eq!(*msgs[1].role(), ConversationRole::Assistant);
        assert_eq!(msgs[1].content().len(), 3); // text + 2 tool_use
        assert!(msgs[1].content()[0].is_text());
        assert!(msgs[1].content()[1].is_tool_use());
        assert!(msgs[1].content()[2].is_tool_use());

        // Verify tool_use IDs and arguments survived conversion
        let tu1 = msgs[1].content()[1].as_tool_use().unwrap();
        assert_eq!(tu1.tool_use_id(), "call_abc");
        assert_eq!(tu1.name(), "get_weather");
        let args1 = document_to_json(tu1.input());
        assert_eq!(args1, serde_json::json!({"city": "NYC"}));

        let tu2 = msgs[1].content()[2].as_tool_use().unwrap();
        assert_eq!(tu2.tool_use_id(), "call_def");
        assert_eq!(tu2.name(), "get_time");

        // msg[2]: user with 2 tool_result blocks + merged follow-up text
        // Tool results are User-role, and "Thanks!" is also User-role, so they merge.
        assert_eq!(*msgs[2].role(), ConversationRole::User);
        assert_eq!(msgs[2].content().len(), 3); // 2 tool_results + 1 text
        assert!(msgs[2].content()[0].is_tool_result());
        assert!(msgs[2].content()[1].is_tool_result());
        assert!(msgs[2].content()[2].is_text());

        // Verify tool_result IDs and content
        let tr1 = msgs[2].content()[0].as_tool_result().unwrap();
        assert_eq!(tr1.tool_use_id(), "call_abc");
        assert_eq!(tr1.content().len(), 1);

        let tr2 = msgs[2].content()[1].as_tool_result().unwrap();
        assert_eq!(tr2.tool_use_id(), "call_def");
    }

    #[test]
    fn test_convert_messages_empty_input() {
        let (system, msgs) = convert_messages(&[]).unwrap();
        assert!(system.is_empty());
        assert!(msgs.is_empty());
    }

    #[test]
    fn test_convert_messages_system_only() {
        let messages = vec![ChatMessage::system("You are helpful.")];
        let (system, msgs) = convert_messages(&messages).unwrap();
        assert_eq!(system.len(), 1);
        assert!(msgs.is_empty());
    }

    #[test]
    fn test_build_tool_config_required_choice() {
        let tools = vec![ToolDefinition {
            name: "echo".to_string(),
            description: "Echoes".to_string(),
            parameters: serde_json::json!({"type": "object"}),
        }];

        let result = build_tool_config(&tools, Some("required")).unwrap();
        assert!(result.is_some());
    }

    #[test]
    fn test_map_stop_reason_all_variants() {
        assert_eq!(
            map_stop_reason(&StopReason::GuardrailIntervened),
            FinishReason::ContentFilter
        );
        assert_eq!(
            map_stop_reason(&StopReason::ModelContextWindowExceeded),
            FinishReason::Length
        );
    }

    #[test]
    fn test_build_inference_config_none_none() {
        assert!(build_inference_config(None, None, None).is_none());
    }

    #[test]
    fn test_build_inference_config_temperature_only() {
        let config = build_inference_config(Some(0.7), None, None);
        assert!(config.is_some());
    }

    #[test]
    fn test_build_inference_config_max_tokens_only() {
        let config = build_inference_config(None, Some(1024), None);
        assert!(config.is_some());
    }

    #[test]
    fn test_build_inference_config_both() {
        let config = build_inference_config(Some(0.5), Some(2048), None);
        assert!(config.is_some());
    }

    #[test]
    fn test_build_inference_config_max_tokens_overflow() {
        // u32::MAX exceeds i32::MAX, should clamp to i32::MAX not wrap
        let config = build_inference_config(None, Some(u32::MAX), None).unwrap();
        // Just verify it builds without panic — the clamped value is inside the opaque struct
        let _ = config;
    }

    #[test]
    fn test_build_inference_config_stop_sequences() {
        let seqs = vec!["STOP".to_string(), "END".to_string()];
        let config = build_inference_config(None, None, Some(&seqs));
        assert!(config.is_some());
    }

    #[test]
    fn test_build_inference_config_empty_stop_sequences_ignored() {
        let seqs: Vec<String> = vec![];
        let config = build_inference_config(None, None, Some(&seqs));
        assert!(config.is_none());
    }

    #[test]
    fn test_empty_messages_returns_error() {
        let messages = vec![ChatMessage::system("System only, no user messages")];
        let (_, bedrock_msgs) = convert_messages(&messages).unwrap();
        assert!(bedrock_msgs.is_empty());
    }
}