use crate::RuntimeError;
use merry_llm::{ModelContent, ModelImage};
use std::sync::Arc;
const PNG_SIGNATURE: &[u8; 8] = b"\x89PNG\r\n\x1a\n";
pub const MAX_USER_IMAGES: usize = 20;
pub const MAX_USER_IMAGE_PNG_BYTES: usize = 10 * 1024 * 1024;
pub const MAX_USER_IMAGE_TOTAL_PNG_BYTES: usize = 20 * 1024 * 1024;
pub const MAX_USER_IMAGE_DIMENSION: u32 = 8_000;
pub const MAX_USER_IMAGE_PIXELS: u64 = 32_000_000;
pub fn user_image_label(index: usize) -> Result<String, RuntimeError> {
if index == 0 {
return Err(invalid_image("user image label index must start at one"));
}
Ok(format!("[Image #{index}]"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserImageInput {
label: String,
png_bytes: Arc<[u8]>,
width: u32,
height: u32,
}
impl UserImageInput {
pub fn png(
label: &str,
png_bytes: impl Into<Arc<[u8]>>,
width: u32,
height: u32,
) -> Result<Self, RuntimeError> {
if label.trim().is_empty() || label.chars().any(char::is_control) {
return Err(invalid_image(
"user image label must be non-blank and contain no control characters",
));
}
let png_bytes = png_bytes.into();
if !png_bytes.starts_with(PNG_SIGNATURE) {
return Err(invalid_image(
"user image bytes must contain normalized PNG data",
));
}
if png_bytes.len() > MAX_USER_IMAGE_PNG_BYTES {
return Err(invalid_image(
"one user image encoded PNG must be at most 10 MiB",
));
}
if width == 0 || height == 0 {
return Err(invalid_image(
"user image dimensions must be greater than zero",
));
}
if width > MAX_USER_IMAGE_DIMENSION || height > MAX_USER_IMAGE_DIMENSION {
return Err(invalid_image(
"user image dimensions must not exceed 8000 pixels per dimension",
));
}
let pixels = u64::from(width)
.checked_mul(u64::from(height))
.ok_or_else(|| invalid_image("user image pixel count overflowed"))?;
if pixels > MAX_USER_IMAGE_PIXELS {
return Err(invalid_image(
"one user image must not exceed 32 million pixels",
));
}
Ok(Self {
label: label.to_owned(),
png_bytes,
width,
height,
})
}
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
#[must_use]
pub fn png_bytes(&self) -> &[u8] {
&self.png_bytes
}
#[must_use]
pub fn shared_png_bytes(&self) -> Arc<[u8]> {
Arc::clone(&self.png_bytes)
}
#[must_use]
pub fn width(&self) -> u32 {
self.width
}
#[must_use]
pub fn height(&self) -> u32 {
self.height
}
fn model_image(&self) -> Result<ModelImage, merry_llm::ModelError> {
ModelImage::png(
self.label(),
self.shared_png_bytes(),
self.width(),
self.height(),
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserMessageInput {
text: String,
images: Vec<UserImageInput>,
}
impl UserMessageInput {
pub fn text_only(text: &str) -> Result<Self, RuntimeError> {
Self::new(text, Vec::new())
}
pub fn new(text: &str, images: Vec<UserImageInput>) -> Result<Self, RuntimeError> {
validate_user_text(text)?;
if images.len() > MAX_USER_IMAGES {
return Err(invalid_image(
"one user message may contain at most 20 images",
));
}
let mut total_png_bytes = 0_usize;
for (offset, image) in images.iter().enumerate() {
let expected_label = user_image_label(offset + 1)?;
if image.label() != expected_label {
return Err(invalid_image(format!(
"user image {} label must be {expected_label}",
offset + 1
)));
}
if !text.contains(image.label()) {
return Err(invalid_image(format!(
"user message text must contain image label {}",
image.label()
)));
}
total_png_bytes = total_png_bytes
.checked_add(image.png_bytes().len())
.ok_or_else(|| invalid_image("user image total PNG byte count overflowed"))?;
}
if total_png_bytes > MAX_USER_IMAGE_TOTAL_PNG_BYTES {
return Err(invalid_image(
"one user message encoded PNG data must total at most 20 MiB",
));
}
Ok(Self {
text: text.to_owned(),
images,
})
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn images(&self) -> &[UserImageInput] {
&self.images
}
pub(crate) fn model_content(&self) -> Result<ModelContent, merry_llm::ModelError> {
if self.images.is_empty() {
return ModelContent::text(self.text());
}
let images = self
.images
.iter()
.map(UserImageInput::model_image)
.collect::<Result<Vec<_>, _>>()?;
ModelContent::user_with_images(self.text(), images)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StepInput {
user_messages: Vec<UserMessageInput>,
user_texts: Vec<String>,
history: StepInputHistory,
}
impl StepInput {
pub fn user_text(text: &str) -> Result<Self, RuntimeError> {
Self::user_texts([text])
}
pub fn user_texts<I, S>(texts: I) -> Result<Self, RuntimeError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let messages = texts
.into_iter()
.map(|text| UserMessageInput::text_only(text.as_ref()))
.collect::<Result<Vec<_>, _>>()?;
Self::from_user_messages(messages)
}
pub fn from_user_messages<I>(messages: I) -> Result<Self, RuntimeError>
where
I: IntoIterator<Item = UserMessageInput>,
{
let user_messages = messages.into_iter().collect::<Vec<_>>();
if user_messages.is_empty() {
return Err(RuntimeError::InvalidStepInput {
reason: "user text burst must contain at least one message",
});
}
let user_texts = user_messages
.iter()
.map(|message| message.text().to_owned())
.collect();
Ok(Self {
user_messages,
user_texts,
history: StepInputHistory::RecordUser,
})
}
#[allow(dead_code)]
pub(crate) fn loop_control_text(text: &str) -> Result<Self, RuntimeError> {
let message = UserMessageInput::text_only(text)?;
Ok(Self {
user_texts: vec![message.text().to_owned()],
user_messages: vec![message],
history: StepInputHistory::ControlOnly,
})
}
pub(crate) fn no_new_user_input() -> Self {
Self {
user_messages: Vec::new(),
user_texts: Vec::new(),
history: StepInputHistory::ControlOnly,
}
}
#[must_use]
pub fn text(&self) -> &str {
self.user_texts
.first()
.map(String::as_str)
.expect("StepInput::text is available only for user text inputs")
}
#[must_use]
pub fn texts(&self) -> &[String] {
&self.user_texts
}
#[must_use]
pub fn user_messages(&self) -> &[UserMessageInput] {
&self.user_messages
}
pub(crate) fn user_messages_for_request(&self) -> &[UserMessageInput] {
&self.user_messages
}
pub(crate) fn user_messages_for_history(&self) -> &[UserMessageInput] {
if self.history == StepInputHistory::RecordUser {
&self.user_messages
} else {
&[]
}
}
pub(crate) fn memory_activation_query(&self) -> Option<String> {
(!self.user_texts.is_empty()).then(|| self.user_texts.join("\n\n"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StepInputHistory {
RecordUser,
ControlOnly,
}
pub(crate) fn validate_user_text(text: &str) -> Result<(), RuntimeError> {
if text.trim().is_empty() {
return Err(RuntimeError::InvalidStepInput {
reason: "user text must not be blank",
});
}
if text
.chars()
.any(|character| character.is_control() && character != '\n' && character != '\t')
{
return Err(RuntimeError::InvalidStepInput {
reason: "user text must not contain control characters other than newline or tab",
});
}
Ok(())
}
fn invalid_image(reason: impl Into<String>) -> RuntimeError {
RuntimeError::InvalidUserImageInput {
reason: reason.into(),
}
}