use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
#[schemars(rename = "mcp.tool.ContentBlock")]
pub enum ContentBlock {
#[serde(rename = "text")]
#[schemars(title = "Text")]
Text(super::TextContent),
#[serde(rename = "image")]
#[schemars(title = "Image")]
Image(super::ImageContent),
#[serde(rename = "audio")]
#[schemars(title = "Audio")]
Audio(super::AudioContent),
#[serde(rename = "resource_link")]
#[schemars(title = "ResourceLink")]
ResourceLink(super::ResourceLink),
#[serde(rename = "resource")]
#[schemars(title = "EmbeddedResource")]
EmbeddedResource(super::EmbeddedResource),
}
impl From<crate::agent::completions::message::RichContentPart>
for ContentBlock
{
fn from(
part: crate::agent::completions::message::RichContentPart,
) -> Self {
use crate::agent::completions::message::RichContentPart;
match part {
RichContentPart::Text { text } => {
ContentBlock::Text(super::TextContent {
text,
annotations: None,
_meta: None,
})
}
RichContentPart::ImageUrl { image_url } => {
let fallback_url = image_url.url.clone();
match super::ImageContent::try_from(image_url) {
Ok(ic) => ContentBlock::Image(ic),
Err(_) => ContentBlock::Text(super::TextContent {
text: fallback_url,
annotations: None,
_meta: None,
}),
}
}
RichContentPart::InputAudio { input_audio } => {
ContentBlock::Audio(input_audio.into())
}
other @ (RichContentPart::InputVideo { .. }
| RichContentPart::VideoUrl { .. }
| RichContentPart::File { .. }) => {
ContentBlock::Text(super::TextContent {
text: serde_json::to_string(&other).unwrap_or_default(),
annotations: None,
_meta: None,
})
}
}
}
}
impl From<crate::agent::completions::message::RichContent>
for Vec<ContentBlock>
{
fn from(
content: crate::agent::completions::message::RichContent,
) -> Self {
use crate::agent::completions::message::RichContent;
match content {
RichContent::Text(text) => {
vec![ContentBlock::Text(super::TextContent {
text,
annotations: None,
_meta: None,
})]
}
RichContent::Parts(parts) => {
parts.into_iter().map(Into::into).collect()
}
}
}
}