use rho_protocol as wire;
use rho_sdk::{
model::{ContentBlock, ImageContent},
UserInput,
};
use serde::{Deserialize, Serialize};
use crate::run_artifacts::AttachmentEvent;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub(crate) enum PromptBlock {
Text { text: String },
Image { mime_type: String, data: String },
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct CliPrompt {
blocks: Vec<PromptBlock>,
}
impl CliPrompt {
pub(crate) fn text(text: impl Into<String>) -> Self {
Self {
blocks: vec![PromptBlock::Text { text: text.into() }],
}
}
pub(crate) fn from_blocks(blocks: Vec<PromptBlock>) -> Self {
Self { blocks }
}
pub(crate) fn from_user_input(input: &UserInput) -> Result<Self, &'static str> {
let blocks = input
.blocks()
.iter()
.map(|block| match block {
ContentBlock::Text(text) => Ok(PromptBlock::Text { text: text.clone() }),
ContentBlock::Image(ImageContent { mime_type, data }) => Ok(PromptBlock::Image {
mime_type: mime_type.clone(),
data: data.clone(),
}),
ContentBlock::ToolCall(_) => Err("a prompt cannot carry a tool call"),
})
.collect::<Result<Vec<_>, _>>()?;
Ok(Self { blocks })
}
pub(crate) fn blocks(&self) -> &[PromptBlock] {
&self.blocks
}
pub(crate) fn into_blocks(self) -> Vec<PromptBlock> {
self.blocks
}
pub(crate) fn has_images(&self) -> bool {
self.blocks
.iter()
.any(|block| matches!(block, PromptBlock::Image { .. }))
}
pub(crate) fn with_preamble(&self, preamble: &str) -> Self {
let mut blocks = self.blocks.clone();
match blocks.first_mut() {
Some(PromptBlock::Text { text }) => {
*text = format!("{preamble}\n\n{text}");
}
Some(PromptBlock::Image { .. }) | None => {
blocks.insert(
0,
PromptBlock::Text {
text: preamble.to_owned(),
},
);
}
}
Self { blocks }
}
pub(crate) fn text_content(&self) -> String {
text_of(&self.blocks)
}
pub(crate) fn to_wire_blocks(&self) -> Vec<wire::ContentBlock> {
let non_empty: Vec<wire::ContentBlock> = self
.blocks
.iter()
.filter_map(|block| match block {
PromptBlock::Text { text } if !text.is_empty() => {
Some(wire::ContentBlock::Text { text: text.clone() })
}
PromptBlock::Text { .. } => None,
PromptBlock::Image { mime_type, data } => Some(wire::ContentBlock::Image {
mime_type: mime_type.clone(),
data: data.clone(),
}),
})
.collect();
if non_empty.is_empty() {
vec![wire::ContentBlock::Text {
text: String::new(),
}]
} else {
non_empty
}
}
pub(crate) fn to_attachment_event(&self) -> AttachmentEvent {
match self.blocks.as_slice() {
[PromptBlock::Text { text }] => AttachmentEvent::Prompt(text.clone()),
_ => AttachmentEvent::PromptContent(self.to_wire_blocks()),
}
}
}
pub(crate) fn text_of(blocks: &[PromptBlock]) -> String {
blocks
.iter()
.filter_map(|block| match block {
PromptBlock::Text { text } => Some(text.as_str()),
PromptBlock::Image { .. } => None,
})
.collect::<Vec<_>>()
.join("\n\n")
}
impl From<String> for CliPrompt {
fn from(text: String) -> Self {
Self::text(text)
}
}
impl From<&str> for CliPrompt {
fn from(text: &str) -> Self {
Self::text(text)
}
}
#[cfg(test)]
#[path = "prompt_tests.rs"]
mod tests;