use std::fmt;
use std::io;
use std::path::{Path, PathBuf};
use base64::Engine;
use rig_core::completion::Message;
use rig_core::conversation::{Conversation, EntryRef, FILE_CLOSE, FILE_OPEN, attached_file};
use rig_core::message::{ImageMediaType, UserContent};
use crate::workspace::{Stat, Workspace, home_path};
use crate::{MAX_LINES, show_lines};
pub(crate) const MOST_BYTES: u64 = 5 * 1024 * 1024 / 4 * 3;
const MOST_TEXT_BYTES: usize = 32 * 1024;
const EXTENSIONS: [&str; 5] = ["png", "jpg", "jpeg", "gif", "webp"];
pub fn is_image_path(path: &Path) -> bool {
path.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| {
EXTENSIONS
.iter()
.any(|known| extension.eq_ignore_ascii_case(known))
})
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Attached {
pub files: Vec<(String, UserContent)>,
pub notes: Vec<String>,
pub missing: Vec<String>,
}
pub fn attachments(text: &str, base: &Path, workspace: &dyn Workspace) -> Attached {
let mut attached = Attached::default();
let mut seen: Vec<PathBuf> = Vec::new();
for token in text.split_whitespace() {
let Some(reference) = token.strip_prefix('@') else {
continue;
};
let trimmed = reference.trim_end_matches([',', '.', ';', ':', '!', '?', ')', '"', '\'']);
if Path::new(trimmed).starts_with("~") && workspace.host().is_some() {
attached.notes.push(format!(
"{trimmed} is not attached: home paths are this machine's."
));
continue;
}
let found = [reference, trimmed].into_iter().find_map(|named| {
let label = home_path(Path::new(named)).unwrap_or_else(|| PathBuf::from(named));
let path = base.join(&label);
matches!(workspace.stat(&path), Ok(Stat::File { .. }))
.then(|| (label.display().to_string(), path))
});
let Some((label, path)) = found else {
if is_path_reference(trimmed) && !attached.missing.iter().any(|seen| seen == trimmed) {
attached.missing.push(trimmed.to_owned());
}
continue;
};
if seen.contains(&path) {
continue;
}
let read = if is_image_path(&path) {
read_image(workspace, &path)
} else {
read_file(workspace, &path, &label)
};
match read {
Ok(content) => attached.files.push((label, content)),
Err(why) => attached
.notes
.push(format!("{label} is not attached: {why}.")),
}
seen.push(path);
}
attached
}
pub fn is_path_reference(reference: &str) -> bool {
reference.contains('/')
|| reference.rsplit_once('.').is_some_and(|(name, extension)| {
!name.is_empty()
&& (1..=4).contains(&extension.len())
&& extension.chars().all(|c| c.is_ascii_alphanumeric())
})
}
pub fn read_image(workspace: &dyn Workspace, path: &Path) -> Result<UserContent, String> {
let image = image(workspace, path).map_err(|failure| failure.to_string())?;
Ok(UserContent::image_base64(
image.base64(),
Some(image.media_type),
None,
))
}
pub(crate) struct Image {
pub bytes: Vec<u8>,
pub media_type: ImageMediaType,
}
impl Image {
pub fn base64(&self) -> String {
base64::engine::general_purpose::STANDARD.encode(&self.bytes)
}
}
#[derive(Debug)]
pub(crate) enum NoImage {
Read(io::Error),
Other,
TooLarge(u64),
}
impl fmt::Display for NoImage {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Read(failure) => write!(formatter, "{failure}"),
Self::Other => write!(formatter, "it is not a PNG, JPEG, GIF or WebP image"),
Self::TooLarge(size) => write!(
formatter,
"it is an image of {} KB, more than the {} KB a provider takes",
size.div_ceil(1024),
MOST_BYTES / 1024
),
}
}
}
pub(crate) fn image(workspace: &dyn Workspace, path: &Path) -> Result<Image, NoImage> {
let contents = workspace.read(path, MOST_BYTES).map_err(NoImage::Read)?;
let media_type = ImageMediaType::sniff(&contents.bytes).ok_or(NoImage::Other)?;
if contents.size > MOST_BYTES {
return Err(NoImage::TooLarge(contents.size));
}
Ok(Image {
bytes: contents.bytes,
media_type,
})
}
fn read_file(workspace: &dyn Workspace, path: &Path, label: &str) -> Result<UserContent, String> {
let text = workspace
.read_text(path)
.map_err(|failure| failure.to_string())?;
if text.contains('\0') {
return Err("it is not a text file".to_owned());
}
let lines = show_lines(&text, 1, MAX_LINES, MOST_TEXT_BYTES);
Ok(UserContent::text(format!(
"{FILE_OPEN}{label}\">\n{lines}{FILE_CLOSE}"
)))
}
pub fn typed(conversation: &Conversation, at: usize) -> Option<String> {
typed_in(conversation.entries().nth(at)?)
}
pub fn typed_prompts(conversation: &Conversation) -> impl Iterator<Item = String> + '_ {
conversation.entries().filter_map(typed_in)
}
fn typed_in(entry: EntryRef<'_>) -> Option<String> {
let Message::User { content } = entry.message else {
return None;
};
if !entry.is_users_own() {
return None;
}
let typed: Vec<&str> = content
.iter()
.filter_map(|content| match content {
UserContent::Text(text) if attached_file(&text.text).is_none() => {
Some(text.text.trim())
}
_ => None,
})
.filter(|text| !text.is_empty())
.collect();
(!typed.is_empty()).then(|| typed.join("\n\n"))
}
pub fn first_typed(conversation: &Conversation) -> Option<String> {
(0..conversation.messages().len()).find_map(|at| typed(conversation, at))
}
#[cfg(test)]
mod tests;