pub struct Blocks { /* private fields */ }Expand description
Fluent builder for a list of Anthropic content blocks.
All chained appenders return &mut Self; finalize with
Blocks::build (consumes the builder) or Blocks::build_message.
Implementations§
Source§impl Blocks
impl Blocks
Sourcepub fn text_with_cache(
&mut self,
s: impl Into<String>,
cache_control: CacheControl,
) -> &mut Self
pub fn text_with_cache( &mut self, s: impl Into<String>, cache_control: CacheControl, ) -> &mut Self
Append a text block carrying a cache_control marker.
Sourcepub fn image_b64(
&mut self,
data: &[u8],
media_type: impl Into<String>,
) -> Result<&mut Self, BlockError>
pub fn image_b64( &mut self, data: &[u8], media_type: impl Into<String>, ) -> Result<&mut Self, BlockError>
Append a base64-encoded inline image block.
data is the raw image bytes; the builder base64-encodes them
internally so callers never have to think about the encoding.
Returns BlockError::UnsupportedImageMediaType if media_type
is not in VALID_IMAGE_MEDIA_TYPES.
Sourcepub fn image_b64_with_cache(
&mut self,
data: &[u8],
media_type: impl Into<String>,
cache_control: CacheControl,
) -> Result<&mut Self, BlockError>
pub fn image_b64_with_cache( &mut self, data: &[u8], media_type: impl Into<String>, cache_control: CacheControl, ) -> Result<&mut Self, BlockError>
Same as Blocks::image_b64 but also tags the block with a
cache_control marker.
Sourcepub fn image_url(&mut self, url: impl Into<String>) -> &mut Self
pub fn image_url(&mut self, url: impl Into<String>) -> &mut Self
Append an image block that references a remote URL.
Sourcepub fn tool_use(
&mut self,
id: impl Into<String>,
name: impl Into<String>,
input: Value,
) -> &mut Self
pub fn tool_use( &mut self, id: impl Into<String>, name: impl Into<String>, input: Value, ) -> &mut Self
Append a tool_use block. input is taken as a serde_json::Value
so callers may use the serde_json::json! macro or pass any
type implementing Into<Value>.
Sourcepub fn tool_result(
&mut self,
tool_use_id: impl Into<String>,
content: Value,
is_error: bool,
) -> &mut Self
pub fn tool_result( &mut self, tool_use_id: impl Into<String>, content: Value, is_error: bool, ) -> &mut Self
Append a tool_result block.
Sourcepub fn document_b64(
&mut self,
data: &[u8],
media_type: impl Into<String>,
) -> Result<&mut Self, BlockError>
pub fn document_b64( &mut self, data: &[u8], media_type: impl Into<String>, ) -> Result<&mut Self, BlockError>
Append a base64-encoded document block. Defaults to PDF when
no media type is supplied (see Blocks::document_pdf_b64).
Returns BlockError::UnsupportedDocumentMediaType if
media_type is not in VALID_DOCUMENT_MEDIA_TYPES.
Sourcepub fn document_pdf_b64(&mut self, data: &[u8]) -> &mut Self
pub fn document_pdf_b64(&mut self, data: &[u8]) -> &mut Self
Convenience wrapper for the common application/pdf case.
Sourcepub fn extend<I>(&mut self, blocks: I) -> &mut Selfwhere
I: IntoIterator<Item = ContentBlock>,
pub fn extend<I>(&mut self, blocks: I) -> &mut Selfwhere
I: IntoIterator<Item = ContentBlock>,
Splice an existing iterable of content blocks into the builder.
Sourcepub fn build(&mut self) -> Vec<ContentBlock>
pub fn build(&mut self) -> Vec<ContentBlock>
Drain the builder and return the accumulated block list.
Takes &mut self (rather than self) so it composes naturally
with the &mut Self-returning fluent chain (e.g.
Blocks::new().text("hi").build()). The builder is left empty
after the call; calling .build() twice yields [blocks, []].
Sourcepub fn build_message(&mut self, role: impl Into<String>) -> Message
pub fn build_message(&mut self, role: impl Into<String>) -> Message
Drain the builder and wrap the accumulated blocks in a
{role, content} envelope.
Sourcepub fn tool_result_block(
tool_use_id: impl Into<String>,
content: Value,
is_error: bool,
) -> ContentBlock
pub fn tool_result_block( tool_use_id: impl Into<String>, content: Value, is_error: bool, ) -> ContentBlock
Build a single tool_result block without going through the
builder. Mirrors the Python Blocks.tool_result classmethod.