use agent_client_protocol::schema::v1::ContentBlock;
pub fn prompt_text(blocks: &[ContentBlock]) -> String {
blocks
.iter()
.map(render_block)
.collect::<Vec<_>>()
.join("\n")
}
fn render_block(block: &ContentBlock) -> String {
match block {
ContentBlock::Text(text) => text.text.clone(),
ContentBlock::ResourceLink(link) => match &link.description {
Some(description) => format!("@{} ({}): {description}", link.name, link.uri),
None => format!("@{} ({})", link.name, link.uri),
},
ContentBlock::Image(_) => "[image content, not supported by this agent]".to_owned(),
ContentBlock::Audio(_) => "[audio content, not supported by this agent]".to_owned(),
ContentBlock::Resource(_) => "[embedded resource, not supported by this agent]".to_owned(),
_ => "[unrecognized content, not supported by this agent]".to_owned(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use agent_client_protocol::schema::v1::{ImageContent, ResourceLink, TextContent};
#[test]
fn text_blocks_join_with_newlines() {
let blocks = vec![
ContentBlock::Text(TextContent::new("first")),
ContentBlock::Text(TextContent::new("second")),
];
assert_eq!(prompt_text(&blocks), "first\nsecond");
}
#[test]
fn a_resource_link_renders_as_an_at_mention() {
let blocks = vec![ContentBlock::ResourceLink(ResourceLink::new(
"main.rs",
"file:///repo/src/main.rs",
))];
assert_eq!(prompt_text(&blocks), "@main.rs (file:///repo/src/main.rs)");
}
#[test]
fn an_unsupported_block_becomes_a_visible_marker_not_silence() {
let blocks = vec![ContentBlock::Image(ImageContent::new(
String::new(),
"image/png",
))];
assert!(prompt_text(&blocks).contains("not supported"));
}
#[test]
fn an_empty_prompt_is_an_empty_string() {
assert_eq!(prompt_text(&[]), "");
}
}