rig-bedrock 0.46.0

AWS Bedrock model provider for Rig integration.
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
//! The Converse reply decoder. It reads the JSON Bedrock sent through
//! [`Lenient`]: a stream's events one by one, and a whole reply as the
//! events that would have streamed it, so both fold into the same turn.
//!
//! Every Converse content block becomes a block, its Converse JSON assembled
//! as its deltas arrive. Signed or redacted reasoning, cited text, a client
//! call with object input, and every block Rig has no canonical form for
//! keep that JSON as the provider item, set only when the block is
//! complete: at its `contentBlockStop`, or in a whole reply, where the item
//! is the block as Bedrock sent it. A hosted tool's typed `toolUse` and its
//! `toolResult` are opaque items that replay to the same model together,
//! never a call Rig runs.

use std::collections::BTreeMap;

use base64::{Engine, prelude::BASE64_STANDARD};
use rig_core::completion::{FinishReason, Usage};
use rig_core::error::ProviderError;
use rig_core::json_utils::Lenient;
use rig_core::json_utils::remove_preserving_order;
use rig_core::message::{
    DocumentRange, DocumentSourceKind, Image, ImageMediaType, Source, SourceLocation,
};
use rig_core::operation::{Block, CallFragment, Completion, Finish, merge};
use rig_core::wire::{Flow, Out, WireCitation, WireEvent};
use serde_json::{Map, Value, json};

use crate::completion::ConverseFrame;
use crate::types::errors;

/// What an open block is. The writer holds its JSON.
enum Open {
    Text,
    Reasoning,
    Call,
    Hosted,
    Image,
    Opaque,
}

/// One Converse reply's state: a whole reply or a stream of events.
#[derive(Default)]
pub struct StreamState {
    open: BTreeMap<usize, Open>,
    reason: Option<String>,
}

/// The one key of a Converse union and its value.
fn member(value: &Value) -> Option<(&str, &Value)> {
    let (key, value) = value.as_object()?.iter().next()?;
    Some((key.as_str(), value))
}

impl StreamState {
    /// Write one event in delivery order; the reply's end at `metadata`.
    fn event(
        &mut self,
        event: &Value,
        out: &mut Out<'_, Completion>,
    ) -> Result<Option<Finish>, ProviderError> {
        let Some((kind, payload)) = member(event) else {
            return Ok(None);
        };
        let index = payload
            .u64("contentBlockIndex")
            .and_then(|index| usize::try_from(index).ok())
            .unwrap_or(0);
        let part = |key| payload.get(key).unwrap_or(&Value::Null);
        match kind {
            "contentBlockStart" => self.start(out, index, part("start"))?,
            "contentBlockDelta" => self.delta(out, index, part("delta"))?,
            "contentBlockStop" => self.stop(out, index, None)?,
            "messageStop" => self.reason = payload.str("stopReason").map(str::to_owned),
            // The writer closes every block still open as incomplete.
            "metadata" => {
                return Ok(Some(Finish {
                    usage: payload.at("/usage").map(usage).unwrap_or_default(),
                    reason: self.reason.as_deref().map(finish_reason),
                    ..Finish::default()
                }));
            }
            "messageStart" => {}
            kind if kind.ends_with("Exception") => return Err(errors::exception(kind, payload)),
            kind => skip(kind),
        }
        Ok(None)
    }

    /// Converse starts tool-use, tool-result and image blocks, and any
    /// block it adds later.
    fn start(
        &mut self,
        out: &mut Out<'_, Completion>,
        index: usize,
        start: &Value,
    ) -> Result<(), ProviderError> {
        let Some((kind, body)) = member(start) else {
            return Ok(());
        };
        let open = match kind {
            // A call that never names its tool is dropped at its end, since
            // nothing can answer it.
            "toolUse" if body.str("type").is_none() => {
                let fragment = CallFragment {
                    id: body.str("toolUseId"),
                    name: body.str("name"),
                    arguments: None,
                };
                out.fragment(Some(index), fragment)?;
                out.edit(index, |slot| start.clone_into(slot))?;
                Open::Call
            }
            "image" if let Some(media_type) = media_type(body.str("format")) => {
                let image = Image {
                    data: DocumentSourceKind::Base64(String::new()),
                    media_type: Some(media_type),
                    ..Image::default()
                };
                out.open(index, Block::Image(image), Value::Null)?;
                Open::Image
            }
            // An image in a format Rig does not name keeps its JSON and does
            // not replay.
            kind => {
                let replay = kind != "image";
                out.open(index, Block::Opaque { replay }, start.clone())?;
                if kind == "toolUse" {
                    Open::Hosted
                } else {
                    Open::Opaque
                }
            }
        };
        self.open.insert(index, open);
        Ok(())
    }

    /// One delta: its text, reasoning or arguments go out as they arrive,
    /// and it merges into the block's JSON.
    fn delta(
        &mut self,
        out: &mut Out<'_, Completion>,
        index: usize,
        delta: &Value,
    ) -> Result<(), ProviderError> {
        let Some((kind, body)) = member(delta) else {
            return Ok(());
        };
        let redacted = body.get("redactedContent");
        if !self.open.contains_key(&index) {
            let (block, open) = match kind {
                "text" | "citation" => (Block::Text, Open::Text),
                "reasoningContent" => {
                    let redacted = redacted.is_some();
                    (Block::Reasoning { redacted }, Open::Reasoning)
                }
                "toolUse" => {
                    self.start(out, index, &json!({ "toolUse": {} }))?;
                    return self.delta(out, index, delta);
                }
                kind => {
                    skip(kind);
                    return Ok(());
                }
            };
            out.open(index, block, Value::Null)?;
            self.open.insert(index, open);
        }
        // Redacted bytes are kept as their chunks and encoded once whole:
        // base64 of each chunk would not concatenate.
        let open = self.open.get(&index);
        if let (Some(text), Some(Open::Text | Open::Reasoning)) =
            (body.as_str().or_else(|| body.str("text")), open)
        {
            out.push(index, text)?;
        }
        let (pointer, merged) = match (kind, open) {
            ("text", Some(Open::Text)) => ("", json!({ "text": body })),
            ("citation", Some(Open::Text)) => {
                if let Some(cited) = citation_of(body) {
                    out.cite(index, cited);
                }
                ("", json!({ "citations": [body] }))
            }
            ("reasoningContent", Some(Open::Reasoning)) => match redacted {
                Some(chunk) => ("", json!({ "redactedContent": [chunk] })),
                None => ("", body.clone()),
            },
            ("toolUse", Some(open @ (Open::Call | Open::Hosted))) => {
                if matches!(open, Open::Call) {
                    let arguments = body.str("input");
                    let fragment = CallFragment {
                        id: None,
                        name: None,
                        arguments,
                    };
                    out.fragment(Some(index), fragment)?;
                }
                ("/toolUse", body.clone())
            }
            ("toolResult", Some(Open::Opaque)) => ("/toolResult", json!({ "content": body })),
            ("image", Some(Open::Image)) => {
                if let Some(data) = body.at("/source/bytes").and_then(Value::as_str) {
                    out.push(index, data)?;
                }
                return Ok(());
            }
            (kind, _) => {
                skip(kind);
                return Ok(());
            }
        };
        out.edit(index, |item| {
            if let (Some(at), Value::Object(merged)) = (item.pointer_mut(pointer), &merged) {
                merge(at, merged);
            }
        })
    }

    /// The block at `index` stopped: it becomes whole, with `whole` as its
    /// item when it came in a whole reply.
    fn stop(
        &mut self,
        out: &mut Out<'_, Completion>,
        index: usize,
        whole: Option<&Value>,
    ) -> Result<(), ProviderError> {
        let Some(open) = self.open.remove(&index) else {
            return Ok(());
        };
        let call = matches!(open, Open::Call | Open::Hosted);
        out.edit(index, |item| {
            let built = match open {
                Open::Text => (!item.arr("citations").is_empty()).then(|| {
                    let content = [json!({ "text": item.get("text") })];
                    json!({ "citationsContent": { "content": content, "citations": item.get("citations") } })
                }),
                // Unsigned reasoning keeps no item: it is rebuilt from its
                // text, as text for a family that rejects unsigned reasoning.
                Open::Reasoning => match (item.get("redactedContent"), item.str("signature")) {
                    (Some(chunks), _) => {
                        let bytes: Vec<u8> = chunks
                            .as_array()
                            .into_iter()
                            .flatten()
                            .filter_map(|chunk| BASE64_STANDARD.decode(chunk.as_str()?).ok())
                            .flatten()
                            .collect();
                        let redacted = BASE64_STANDARD.encode(bytes);
                        Some(json!({ "reasoningContent": { "redactedContent": redacted } }))
                    }
                    (None, Some(signature)) => {
                        let text = item.str("text").unwrap_or_default();
                        let reasoning = json!({ "text": text, "signature": signature });
                        Some(json!({ "reasoningContent": { "reasoningText": reasoning } }))
                    }
                    (None, None) => None,
                },
                // Converse takes a JSON object as a tool's input; a call's
                // input is the one its arguments were read from.
                Open::Call | Open::Hosted => {
                    let input = item
                        .at("/toolUse/input")
                        .and_then(Value::as_str)
                        .filter(|input| !input.trim().is_empty())
                        .unwrap_or("{}");
                    let input = serde_json::from_str::<Value>(input)
                        .ok()
                        .filter(Value::is_object)
                        .or_else(|| matches!(open, Open::Hosted).then(|| json!({})));
                    input.and_then(|input| {
                        let mut call = item.get("toolUse")?.clone();
                        merge(&mut call, &Map::from_iter([("input".to_owned(), input)]));
                        Some(json!({ "toolUse": call }))
                    })
                }
                Open::Image => None,
                Open::Opaque => Some(item.clone()),
            };
            *item = match (built, whole) {
                (Some(_), Some(whole)) if !call => whole.clone(),
                (built, _) => built.unwrap_or_default(),
            };
        })?;
        out.finish(index)
    }

    /// A whole reply, written as the events that would have streamed it.
    fn whole(
        &mut self,
        document: &Value,
        mut out: Out<'_, Completion>,
    ) -> Result<Flow, ProviderError> {
        let content = document
            .at("/output/message")
            .map_or(&[][..], |message| message.arr("content"));
        for (index, block) in content.iter().enumerate() {
            let Some((kind, body)) = member(block) else {
                continue;
            };
            // A started block's streamed field arrives as its delta.
            let (started, deltas) = match kind {
                "text" => (None, vec![json!({ "text": body })]),
                "citationsContent" => {
                    let text = body
                        .arr("content")
                        .iter()
                        .map(|part| json!({ "text": part.get("text") }));
                    let cited = body
                        .arr("citations")
                        .iter()
                        .map(|citation| json!({ "citation": citation }));
                    (None, text.chain(cited).collect())
                }
                "reasoningContent" => {
                    let reasoning = body.get("reasoningText").unwrap_or(body);
                    (None, vec![json!({ "reasoningContent": reasoning })])
                }
                "toolUse" => {
                    let input = body.get("input").map(Value::to_string);
                    (
                        Some("input"),
                        vec![json!({ "toolUse": { "input": input } })],
                    )
                }
                "toolResult" => (
                    Some("content"),
                    vec![json!({ "toolResult": body.get("content") })],
                ),
                "image" => (
                    Some("source"),
                    vec![json!({ "image": { "source": body.get("source") } })],
                ),
                _ => (Some(""), Vec::new()),
            };
            if let Some(field) = started {
                let mut opened = body.clone();
                if let Value::Object(fields) = &mut opened {
                    remove_preserving_order(fields, field);
                }
                self.start(&mut out, index, &json!({ kind: opened }))?;
            }
            for delta in &deltas {
                self.delta(&mut out, index, delta)?;
            }
            self.stop(&mut out, index, Some(block))?;
        }
        self.reason = document.str("stopReason").map(str::to_owned);
        let end = json!({ "metadata": { "usage": document.get("usage") } });
        Ok(match self.event(&end, &mut out)? {
            Some(end) => out.end(end),
            None => Flow::More,
        })
    }
}

/// One Converse citation as a whole-block [`WireCitation`]: Converse cites
/// each `citationsContent` block as a whole. A location kind this crate does
/// not know, or a search result without its `source`, is `None` and stays
/// only in the block's item. Ranges are as Anthropic's, which Converse
/// relays: end exclusive, pages from 1, characters and chunks from 0.
fn citation_of(citation: &Value) -> Option<WireCitation> {
    let (kind, at) = member(citation.get("location")?)?;
    let number = |key: &str| at.u64(key).and_then(|n| u32::try_from(n).ok());
    let range = || Some(number("start")?..number("end")?);
    let document = |within: Option<DocumentRange>| SourceLocation::Document {
        index: number("documentIndex"),
        id: None,
        within,
    };
    let location = match kind {
        "documentChar" => document(
            at.u64("start")
                .zip(at.u64("end"))
                .map(|(start, end)| DocumentRange::Chars(start..end)),
        ),
        "documentPage" => document(range().map(DocumentRange::Pages)),
        "documentChunk" => document(range().map(DocumentRange::Blocks)),
        "searchResultLocation" => SourceLocation::SearchResult {
            index: number("searchResultIndex")?,
            source: citation.str("source")?.to_owned(),
            blocks: range(),
        },
        "web" => SourceLocation::Url {
            url: at.str("url")?.to_owned(),
        },
        _ => return None,
    };
    let mut source = Source::new(location);
    if let Some(title) = citation.str("title") {
        source = source.title(title);
    }
    let quoted: Vec<&str> = citation
        .arr("sourceContent")
        .iter()
        .filter_map(|part| part.str("text"))
        .collect();
    if !quoted.is_empty() {
        source = source.cited_text(quoted.concat());
    }
    Some(WireCitation::new(None, vec![source]))
}

/// The image type a Converse image `format` names.
fn media_type(format: Option<&str>) -> Option<ImageMediaType> {
    match format? {
        "gif" => Some(ImageMediaType::GIF),
        "jpeg" => Some(ImageMediaType::JPEG),
        "png" => Some(ImageMediaType::PNG),
        "webp" => Some(ImageMediaType::WEBP),
        _ => None,
    }
}

fn skip(item: &str) {
    tracing::warn!(item, "a Converse item this crate does not model");
}

/// Every stop reason Converse documents. An end of turn or a stop sequence
/// is `Stop`, the token limit and an exceeded context window are `Length`,
/// guardrail and content-filter interventions are `ContentFilter`, and any
/// other reason keeps its wire spelling in `Other`, which fails the turn.
fn finish_reason(reason: &str) -> FinishReason {
    match reason {
        "end_turn" | "stop_sequence" => FinishReason::Stop,
        "max_tokens" | "model_context_window_exceeded" => FinishReason::Length,
        "tool_use" => FinishReason::ToolCalls,
        "content_filtered" | "guardrail_intervened" => FinishReason::ContentFilter,
        other => FinishReason::Other(other.to_owned()),
    }
}

/// Rig's input is Bedrock's `inputTokens` plus its cache reads and writes,
/// and its total input plus output, which is `totalTokens`.
fn usage(usage: &Value) -> Usage {
    let cache_read = usage.u64("cacheReadInputTokens");
    let cache_write = usage.u64("cacheWriteInputTokens");
    let input = [usage.u64("inputTokens"), cache_read, cache_write]
        .into_iter()
        .flatten()
        .fold(0, u64::saturating_add);
    let output = usage.u64("outputTokens").unwrap_or(0);
    Usage::new()
        .input_tokens(input)
        .output_tokens(output)
        .total_tokens(input.saturating_add(output))
        .cached_input_tokens(cache_read)
        .cache_creation_input_tokens(cache_write)
}

impl<'id> rig_core::wire::Decoder<'id, Completion, ConverseFrame> for StreamState {
    type Event = ConverseFrame;

    fn classify(&self, frame: ConverseFrame) -> WireEvent<Self::Event> {
        WireEvent::Known(frame)
    }

    /// EOF without Bedrock's `metadata` event is truncation.
    fn decode(
        &mut self,
        frame: ConverseFrame,
        mut out: Out<'id, Completion>,
    ) -> Result<Flow, ProviderError> {
        let event = match frame {
            ConverseFrame::Whole(document) => return self.whole(&document, out),
            ConverseFrame::Event(event) => event,
        };
        let Some(end) = self.event(&event, &mut out)? else {
            return Ok(Flow::More);
        };
        Ok(out.end(end))
    }
}

pub(crate) mod document;

#[cfg(test)]
pub(crate) mod tests;