Skip to main content

Module convert

Module convert 

Source
Expand description

Convert the crate’s generic Message format to the Bedrock Converse API’s JSON schema, and back-convert tool definitions.

Bedrock Converse requires:

  • A separate top-level system array for system prompts.
  • Strict alternation of user / assistant messages.
  • Assistant tool-use blocks with toolUse objects (input as an object).
  • Tool results appear in the next user message as toolResult blocks.

§Examples

use codetether_agent::provider::bedrock::{convert_messages, convert_tools};
use codetether_agent::provider::{ContentPart, Message, Role, ToolDefinition};
use serde_json::json;

let msgs = vec![
    Message {
        role: Role::System,
        content: vec![ContentPart::Text { text: "You are helpful.".into() }],
    },
    Message {
        role: Role::User,
        content: vec![ContentPart::Text { text: "hi".into() }],
    },
];
let (system, api_msgs) = convert_messages(&msgs);
assert_eq!(system.len(), 1);
assert_eq!(api_msgs.len(), 1);
assert_eq!(api_msgs[0]["role"], "user");

let tools = vec![ToolDefinition {
    name: "echo".into(),
    description: "Echo text".into(),
    parameters: json!({"type":"object"}),
}];
let converted = convert_tools(&tools);
assert_eq!(converted[0]["toolSpec"]["name"], "echo");

Functions§

convert_messages
Convert generic Messages to Bedrock Converse API format.
convert_tools
Convert crate-internal ToolDefinitions into Bedrock toolConfig.tools entries.