use std::{
collections::HashMap,
io::Cursor,
path::{Path, PathBuf},
};
use askama::Template;
use async_fs as fs;
use image::{GenericImageView, ImageFormat, ImageReader, imageops::FilterType};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize, Serializer, ser::SerializeMap};
use tracing::debug;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum OutputFormat {
#[default]
Text,
Image,
Video,
Binary,
Auto,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum MediaResolution {
#[default]
Auto,
Low,
Medium,
High,
Native,
}
impl MediaResolution {
const fn max_dimension(self) -> Option<u32> {
match self {
Self::Auto | Self::Native => None,
Self::Low => Some(512),
Self::Medium => Some(1024),
Self::High => Some(2048),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Content {
Text {
text: String,
truncated: bool,
},
Image {
data: String,
media_type: String,
},
}
#[derive(Debug, Clone)]
pub enum OutputEntry {
Empty,
Inline {
content: Content,
},
Loaded {
content: Content,
raw: Vec<u8>,
format: OutputFormat,
},
Stored {
url: String,
path: String,
content: Option<Content>,
},
}
impl Serialize for OutputEntry {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::Empty => {
let map = serializer.serialize_map(Some(0))?;
map.end()
}
Self::Inline { content } | Self::Loaded { content, .. } => {
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("content", content)?;
map.end()
}
Self::Stored { url, path, content } => {
let count = if content.is_some() { 3 } else { 2 };
let mut map = serializer.serialize_map(Some(count))?;
map.serialize_entry("url", url)?;
map.serialize_entry("path", path)?;
if let Some(c) = content {
map.serialize_entry("content", c)?;
}
map.end()
}
}
}
}
impl<'de> Deserialize<'de> for OutputEntry {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Helper {
url: Option<String>,
path: Option<String>,
content: Option<Content>,
}
let h = Helper::deserialize(deserializer)?;
Ok(match (h.url, h.path, h.content) {
(Some(url), path, content) => Self::Stored {
url,
path: path.unwrap_or_default(),
content,
},
(None, _, Some(content)) => Self::Inline { content },
(None, _, None) => Self::Empty,
})
}
}
impl std::fmt::Display for OutputEntry {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => Ok(()),
Self::Inline { content } | Self::Loaded { content, .. } => match content {
Content::Text { text, truncated } => {
write!(f, "{text}")?;
if *truncated {
write!(
f,
"\n[truncated: full output is available in the stored file]"
)?;
}
Ok(())
}
Content::Image { media_type, .. } => {
write!(f, "[Image: {media_type}]")
}
},
Self::Stored { url, path, content } => {
if let Some(content) = content {
match content {
Content::Text { text, truncated } => {
write!(f, "{text}")?;
if *truncated {
write!(
f,
"\n[full content at {url}; absolute path: {path}; output was truncated in-chat]"
)?;
}
}
Content::Image { media_type, .. } => {
write!(f, "[Image: {media_type}] at {url} ({path})")?;
}
}
} else {
write!(f, "[content at {url} ({path})]")?;
}
Ok(())
}
}
}
}
impl OutputEntry {
#[must_use]
pub fn stored_path(&self, base_dir: &Path) -> Option<PathBuf> {
match self {
Self::Stored { url, path, .. } => {
if !path.is_empty() {
return Some(PathBuf::from(path));
}
let filename = url.strip_prefix("outputs/").unwrap_or(url);
Some(base_dir.join(filename))
}
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct OutputRef {
pub entry: OutputEntry,
pub format: OutputFormat,
pub size: usize,
}
pub const INLINE_OUTPUT_LIMIT: usize = 16_000;
#[derive(Template)]
#[template(path = "stored_output_preview.txt", escape = "none")]
struct StoredOutputPreviewTemplate<'a> {
preview: &'a str,
reason: &'a str,
line_count: usize,
byte_count: usize,
url: &'a str,
}
fn stored_text_content(
text: &str,
reason: &str,
line_count: usize,
byte_count: usize,
url: &str,
max_lines: usize,
byte_budget: usize,
) -> Content {
let preview = head_preview(text, max_lines, byte_budget);
let rendered = StoredOutputPreviewTemplate {
preview: preview.trim_end_matches('\n'),
reason,
line_count,
byte_count,
url,
}
.render()
.expect("stored output preview template must render");
Content::Text {
text: rendered,
truncated: true,
}
}
pub fn head_preview(text: &str, max_lines: usize, byte_budget: usize) -> &str {
let mut end = 0;
for (index, line) in text.split_inclusive('\n').enumerate() {
if index == max_lines || end + line.len() > byte_budget {
break;
}
end += line.len();
}
if end == 0 {
let mut cut = byte_budget.min(text.len());
while cut > 0 && !text.is_char_boundary(cut) {
cut -= 1;
}
end = cut;
}
text.get(..end)
.expect("prefix end is aligned to a line or char boundary")
}
#[derive(Debug, Clone)]
pub struct PendingUrl {
pub url: String,
pub raw: Vec<u8>,
pub format: OutputFormat,
}
#[derive(Debug)]
pub struct OutputStore {
dir: PathBuf,
entries: HashMap<String, OutputRef>,
next_id: u64,
}
impl OutputStore {
pub async fn new(dir: impl Into<PathBuf>) -> std::io::Result<Self> {
let dir = dir.into();
fs::create_dir_all(&dir).await?;
Ok(Self {
dir,
entries: HashMap::new(),
next_id: 0,
})
}
#[must_use]
pub fn dir(&self) -> &Path {
&self.dir
}
pub async fn save(
&mut self,
data: &[u8],
format: OutputFormat,
resolution: MediaResolution,
) -> std::io::Result<OutputEntry> {
let entry = Self::save_to_dir(&self.dir, data, format, resolution).await?;
let id = format!("output_{}", self.next_id);
self.next_id += 1;
let format = if format == OutputFormat::Auto {
detect_format(data)
} else {
format
};
let output_ref = OutputRef {
entry: entry.clone(),
format,
size: data.len(),
};
self.entries.insert(id, output_ref);
Ok(entry)
}
pub async fn save_to_dir(
dir: &Path,
data: &[u8],
format: OutputFormat,
resolution: MediaResolution,
) -> std::io::Result<OutputEntry> {
Self::save_to_dir_with_limit(dir, data, format, resolution, Some(INLINE_OUTPUT_LIMIT)).await
}
pub async fn save_to_dir_with_limit(
dir: &Path,
data: &[u8],
format: OutputFormat,
resolution: MediaResolution,
limit: Option<usize>,
) -> std::io::Result<OutputEntry> {
if data.is_empty() {
return Ok(OutputEntry::Empty);
}
let format = if format == OutputFormat::Auto {
detect_format(data)
} else {
format
};
let exceeds_limit = limit.is_some_and(|max| data.len() > max);
if exceeds_limit && !matches!(format, OutputFormat::Image) {
return save_large_output(dir, data, format).await;
}
match format {
OutputFormat::Text | OutputFormat::Auto => save_text_output(dir, data, format).await,
OutputFormat::Image => {
let prepared = prepare_image_for_context(data, resolution)?;
let preview_content = Content::Image {
data: base64_encode(&prepared.bytes),
media_type: prepared.media_type,
};
let preview_fits_inline = limit.is_none_or(|max| prepared.bytes.len() <= max);
let source_fits_inline = limit.is_none_or(|max| data.len() <= max);
if source_fits_inline && preview_fits_inline {
Ok(OutputEntry::Inline {
content: preview_content,
})
} else {
let (url, path) = create_file(dir, data, format).await?;
Ok(OutputEntry::Stored {
path: path.display().to_string(),
url,
content: preview_fits_inline.then_some(preview_content),
})
}
}
OutputFormat::Video | OutputFormat::Binary => {
save_large_output(dir, data, format).await
}
}
}
pub async fn offload(&mut self, entry: &OutputEntry) -> std::io::Result<OutputEntry> {
match entry {
OutputEntry::Loaded {
content,
raw,
format,
} => {
let (url, path) = create_file(&self.dir, raw, *format).await?;
Ok(OutputEntry::Stored {
path: path.display().to_string(),
url,
content: Some(content.clone()),
})
}
other => Ok(other.clone()),
}
}
#[must_use]
pub fn get(&self, id: &str) -> Option<&OutputRef> {
self.entries.get(id)
}
#[must_use]
pub fn allocate_url(&self, entry: &OutputEntry) -> Option<PendingUrl> {
match entry {
OutputEntry::Loaded { raw, format, .. } => {
let ext = extension_for_data(raw, *format);
let name = generate_word_filename();
let url = format!("outputs/{name}.{ext}");
Some(PendingUrl {
url,
raw: raw.clone(),
format: *format,
})
}
_ => None,
}
}
#[must_use]
pub fn allocate_text_url(&self) -> String {
let name = generate_word_filename();
format!("outputs/{name}.txt")
}
pub async fn write_text(&self, url: &str, content: &str) -> std::io::Result<PathBuf> {
let filename = url.strip_prefix("outputs/").unwrap_or(url);
let filepath = self.dir.join(filename);
fs::write(&filepath, content.as_bytes()).await?;
debug!(url = %url, size = content.len(), "wrote text output");
Ok(filepath)
}
pub async fn write_pending(&self, pending: &PendingUrl) -> std::io::Result<PathBuf> {
let filename = pending.url.strip_prefix("outputs/").unwrap_or(&pending.url);
let filepath = self.dir.join(filename);
fs::write(&filepath, &pending.raw).await?;
debug!(url = %pending.url, size = pending.raw.len(), "wrote pending output");
Ok(filepath)
}
pub async fn read(&self, url: &str) -> std::io::Result<Vec<u8>> {
let filename = url.strip_prefix("outputs/").unwrap_or(url);
let filepath = self.dir.join(filename);
fs::read(&filepath).await
}
pub async fn cleanup(&mut self) -> std::io::Result<()> {
for (_, output_ref) in self.entries.drain() {
if let OutputEntry::Stored { url, path, .. } = output_ref.entry {
let filename = url.strip_prefix("outputs/").unwrap_or(&url);
let filepath = if path.is_empty() {
self.dir.join(filename)
} else {
PathBuf::from(path)
};
if let Err(e) = fs::remove_file(&filepath).await {
tracing::warn!(path = %filepath.display(), error = %e, "failed to remove output file");
}
}
}
Ok(())
}
}
pub async fn save_raw_to_file(dir: &Path, data: &[u8]) -> std::io::Result<String> {
let (url, _) = create_file(dir, data, OutputFormat::Text).await?;
debug!(url = %url, size = data.len(), "saved raw output");
Ok(url)
}
async fn save_text_output(
dir: &Path,
data: &[u8],
format: OutputFormat,
) -> std::io::Result<OutputEntry> {
if data.len() <= INLINE_OUTPUT_LIMIT {
let content = Content::Text {
text: String::from_utf8_lossy(data).into_owned(),
truncated: false,
};
Ok(OutputEntry::Inline { content })
} else {
save_large_output(dir, data, format).await
}
}
pub async fn save_text_with_line_limit(
dir: &Path,
data: &[u8],
format: OutputFormat,
resolution: MediaResolution,
max_lines: usize,
byte_limit: Option<usize>,
) -> std::io::Result<OutputEntry> {
if data.is_empty() {
return Ok(OutputEntry::Empty);
}
let format = if format == OutputFormat::Auto {
detect_format(data)
} else {
format
};
if !matches!(format, OutputFormat::Text | OutputFormat::Auto) {
return OutputStore::save_to_dir_with_limit(dir, data, format, resolution, byte_limit)
.await;
}
let text = String::from_utf8_lossy(data);
let line_count = text.lines().count();
let exceeds_bytes = byte_limit.is_some_and(|max| data.len() > max);
let exceeds_lines = line_count > max_lines;
if !exceeds_bytes && !exceeds_lines {
let content = Content::Text {
text: text.into_owned(),
truncated: false,
};
return Ok(OutputEntry::Inline { content });
}
let (url, path) = create_file(dir, data, format).await?;
let byte_budget = byte_limit.unwrap_or(INLINE_OUTPUT_LIMIT);
let reason = if exceeds_lines {
format!("output exceeded max_lines limit ({line_count} lines > {max_lines} max)")
} else {
format!("output exceeded the {byte_budget}-byte inline limit")
};
let content = stored_text_content(
&text,
&reason,
line_count,
data.len(),
&url,
max_lines,
byte_budget,
);
Ok(OutputEntry::Stored {
path: path.display().to_string(),
url,
content: Some(content),
})
}
async fn save_large_output(
dir: &Path,
data: &[u8],
format: OutputFormat,
) -> std::io::Result<OutputEntry> {
let (url, path) = create_file(dir, data, format).await?;
let content = match format {
OutputFormat::Text | OutputFormat::Auto => {
let text = String::from_utf8_lossy(data);
let line_count = text.lines().count();
let reason = format!("output exceeded the {INLINE_OUTPUT_LIMIT}-byte inline limit");
Some(stored_text_content(
&text,
&reason,
line_count,
data.len(),
&url,
usize::MAX,
INLINE_OUTPUT_LIMIT,
))
}
_ => None, };
Ok(OutputEntry::Stored {
path: path.display().to_string(),
url,
content,
})
}
#[derive(Debug)]
struct PreparedImage {
bytes: Vec<u8>,
media_type: String,
}
fn prepare_image_for_context(
data: &[u8],
resolution: MediaResolution,
) -> std::io::Result<PreparedImage> {
let media_type = detect_image_media_type(data);
let Some(max_dimension) = resolution.max_dimension() else {
return Ok(PreparedImage {
bytes: data.to_vec(),
media_type,
});
};
let image = ImageReader::new(Cursor::new(data))
.with_guessed_format()
.map_err(io_error)?
.decode()
.map_err(io_error)?;
let (width, height) = image.dimensions();
if width <= max_dimension && height <= max_dimension {
return Ok(PreparedImage {
bytes: data.to_vec(),
media_type,
});
}
let format = image_format_from_media_type(media_type.as_str()).ok_or_else(|| {
std::io::Error::other(format!(
"unsupported image media type for terminal resizing: {media_type}"
))
})?;
let resized = image.resize(max_dimension, max_dimension, FilterType::Lanczos3);
let mut cursor = Cursor::new(Vec::new());
resized.write_to(&mut cursor, format).map_err(io_error)?;
Ok(PreparedImage {
bytes: cursor.into_inner(),
media_type,
})
}
async fn create_file(
dir: &Path,
data: &[u8],
format: OutputFormat,
) -> std::io::Result<(String, PathBuf)> {
let ext = extension_for_data(data, format);
let name = generate_word_filename();
let filename = format!("{name}.{ext}");
let url = format!("outputs/{filename}");
let filepath = dir.join(&filename);
fs::write(&filepath, data).await?;
debug!(url = %url, size = data.len(), format = ?format, "saved output");
Ok((url, filepath))
}
fn io_error(error: impl std::fmt::Display) -> std::io::Error {
std::io::Error::other(error.to_string())
}
pub fn generate_word_filename() -> String {
crate::naming::random_word_slug(4)
}
fn detect_format(data: &[u8]) -> OutputFormat {
if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
return OutputFormat::Image;
}
if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
return OutputFormat::Image;
}
if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
return OutputFormat::Image;
}
if data.starts_with(b"RIFF") && data.len() > 12 && &data[8..12] == b"WEBP" {
return OutputFormat::Image;
}
if data.len() > 12 && &data[4..8] == b"ftyp" {
return OutputFormat::Video;
}
if let Ok(text) = std::str::from_utf8(data) {
let is_text = text
.chars()
.all(|c| c == '\n' || c == '\r' || c == '\t' || (c >= ' ' && c != '\x7f'));
if is_text {
return OutputFormat::Text;
}
}
OutputFormat::Binary
}
const fn format_extension(format: OutputFormat) -> &'static str {
match format {
OutputFormat::Text | OutputFormat::Auto => "txt",
OutputFormat::Image | OutputFormat::Binary => "bin",
OutputFormat::Video => "mp4",
}
}
fn image_extension(data: &[u8]) -> Option<&'static str> {
if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
Some("png")
} else if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
Some("jpg")
} else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
Some("gif")
} else if data.starts_with(b"RIFF") && data.len() > 12 && &data[8..12] == b"WEBP" {
Some("webp")
} else {
None
}
}
fn extension_for_data(data: &[u8], format: OutputFormat) -> &'static str {
if matches!(format, OutputFormat::Image) {
return image_extension(data).unwrap_or_else(|| format_extension(format));
}
format_extension(format)
}
fn detect_image_media_type(data: &[u8]) -> String {
if data.starts_with(&[0x89, 0x50, 0x4E, 0x47]) {
"image/png".to_string()
} else if data.starts_with(&[0xFF, 0xD8, 0xFF]) {
"image/jpeg".to_string()
} else if data.starts_with(b"GIF87a") || data.starts_with(b"GIF89a") {
"image/gif".to_string()
} else if data.starts_with(b"RIFF") && data.len() > 12 && &data[8..12] == b"WEBP" {
"image/webp".to_string()
} else {
"application/octet-stream".to_string()
}
}
fn image_format_from_media_type(media_type: &str) -> Option<ImageFormat> {
match media_type {
"image/png" => Some(ImageFormat::Png),
"image/jpeg" => Some(ImageFormat::Jpeg),
"image/gif" => Some(ImageFormat::Gif),
"image/webp" => Some(ImageFormat::WebP),
_ => None,
}
}
fn base64_encode(data: &[u8]) -> String {
use base64::{Engine, engine::general_purpose::STANDARD};
STANDARD.encode(data)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
use base64::Engine;
use image::{DynamicImage, ImageBuffer, Rgba};
#[test]
fn test_detect_format() {
assert_eq!(
detect_format(&[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]),
OutputFormat::Image
);
assert_eq!(detect_format(b"Hello, world!"), OutputFormat::Text);
assert_eq!(
detect_format(&[0x00, 0x01, 0x02, 0x03]),
OutputFormat::Binary
);
}
#[test]
fn test_output_entry_inline_serialize() {
let entry = OutputEntry::Inline {
content: Content::Text {
text: "done".to_string(),
truncated: false,
},
};
let json = serde_json::to_string(&entry).unwrap();
assert!(json.contains("\"content\""));
assert!(!json.contains("\"url\""));
}
#[test]
fn test_output_entry_stored_serialize() {
let entry = OutputEntry::Stored {
url: "outputs/test.txt".to_string(),
path: "/tmp/test.txt".to_string(),
content: Some(Content::Text {
text: "preview".to_string(),
truncated: true,
}),
};
let json = serde_json::to_string(&entry).unwrap();
assert!(json.contains("\"url\""));
assert!(json.contains("\"content\""));
}
#[test]
fn test_output_entry_empty_serialize() {
let entry = OutputEntry::Empty;
let json = serde_json::to_string(&entry).unwrap();
assert_eq!(json, "{}");
}
#[test]
fn test_generate_word_filename() {
let name = generate_word_filename();
assert_eq!(name.split('-').count(), 4, "Should have 4 words: {name}");
}
#[tokio::test]
async fn save_image_low_resolution_downscales_inline_content() {
let dir = tempfile::tempdir().unwrap();
let jpeg = encode_test_image(4000, 3000, ImageFormat::Jpeg);
let entry = OutputStore::save_to_dir_with_limit(
dir.path(),
&jpeg,
OutputFormat::Image,
MediaResolution::Low,
None,
)
.await
.unwrap();
let OutputEntry::Inline {
content: Content::Image { data, media_type },
} = entry
else {
panic!("expected inline image preview");
};
assert_eq!(media_type, "image/jpeg");
let preview_bytes = base64::engine::general_purpose::STANDARD
.decode(data)
.unwrap();
let preview = ImageReader::new(Cursor::new(preview_bytes))
.with_guessed_format()
.unwrap()
.decode()
.unwrap();
assert!(preview.width() <= 512);
assert!(preview.height() <= 512);
}
#[tokio::test]
async fn save_large_image_keeps_stored_file_and_preview_when_resolution_fits_limit() {
const LIMIT: usize = 100_000;
let dir = tempfile::tempdir().unwrap();
let jpeg = encode_test_image(4000, 3000, ImageFormat::Jpeg);
assert!(jpeg.len() > LIMIT);
let entry = OutputStore::save_to_dir_with_limit(
dir.path(),
&jpeg,
OutputFormat::Image,
MediaResolution::Low,
Some(LIMIT),
)
.await
.unwrap();
let OutputEntry::Stored {
url,
path,
content: Some(Content::Image { data, media_type }),
} = entry
else {
panic!("expected stored image preview");
};
assert_eq!(media_type, "image/jpeg");
assert!(
std::path::Path::new(&path)
.extension()
.is_some_and(|extension| extension.eq_ignore_ascii_case("jpg")
|| extension.eq_ignore_ascii_case("jpeg"))
);
assert!(dir.path().join(url.trim_start_matches("outputs/")).exists());
let preview_bytes = base64::engine::general_purpose::STANDARD
.decode(data)
.unwrap();
let preview = ImageReader::new(Cursor::new(preview_bytes))
.with_guessed_format()
.unwrap()
.decode()
.unwrap();
assert!(preview.width() <= 512);
assert!(preview.height() <= 512);
}
fn encode_test_image(width: u32, height: u32, format: ImageFormat) -> Vec<u8> {
let image = ImageBuffer::from_fn(width, height, |x, y| {
let r = u8::try_from((x * 255) / width.max(1)).expect("red channel fits in u8");
let g = u8::try_from((y * 255) / height.max(1)).expect("green channel fits in u8");
let b = u8::try_from(((x + y) * 255) / (width + height).max(1))
.expect("blue channel fits in u8");
Rgba([r, g, b, 255])
});
let dynamic = DynamicImage::ImageRgba8(image);
let mut cursor = Cursor::new(Vec::new());
dynamic.write_to(&mut cursor, format).unwrap();
cursor.into_inner()
}
#[test]
fn head_preview_respects_line_budget() {
let text = "one\ntwo\nthree\n";
assert_eq!(head_preview(text, 2, usize::MAX), "one\ntwo\n");
assert_eq!(head_preview(text, 10, usize::MAX), text);
}
#[test]
fn head_preview_cuts_at_line_boundary_within_byte_budget() {
let text = "aaaa\nbbbb\ncccc\n";
assert_eq!(head_preview(text, usize::MAX, 12), "aaaa\nbbbb\n");
}
#[test]
fn head_preview_never_splits_multibyte_chars() {
let text = "界".repeat(400);
let preview = head_preview(&text, usize::MAX, 500);
assert!(!preview.is_empty());
assert!(preview.len() <= 500);
assert!(preview.chars().all(|c| c == '界'));
}
#[tokio::test]
async fn stored_text_output_includes_head_preview_and_guidance() {
let dir = tempfile::tempdir().unwrap();
let mut data = String::new();
for i in 0..2000 {
data.extend([format!("line number {i} with some padding text\n")]);
}
assert!(data.len() > INLINE_OUTPUT_LIMIT);
let entry = save_text_with_line_limit(
dir.path(),
data.as_bytes(),
OutputFormat::Text,
MediaResolution::Auto,
10_000,
Some(INLINE_OUTPUT_LIMIT),
)
.await
.unwrap();
let OutputEntry::Stored {
content: Some(Content::Text { text, truncated }),
url,
..
} = entry
else {
panic!("expected stored entry with text content");
};
assert!(truncated);
assert!(
text.starts_with("line number 0 "),
"preview must lead with real content: {text}"
);
assert!(
text.contains(&url),
"guidance must reference the saved file"
);
assert!(text.contains("Use head/tail/grep/sed"));
}
#[tokio::test]
async fn stored_text_output_preview_honors_max_lines() {
let dir = tempfile::tempdir().unwrap();
let data = "alpha\nbeta\ngamma\ndelta\n".repeat(2);
let entry = save_text_with_line_limit(
dir.path(),
data.as_bytes(),
OutputFormat::Text,
MediaResolution::Auto,
3,
Some(INLINE_OUTPUT_LIMIT),
)
.await
.unwrap();
let OutputEntry::Stored {
content: Some(Content::Text { text, .. }),
..
} = entry
else {
panic!("expected stored entry with text content");
};
assert!(
text.starts_with("alpha\nbeta\ngamma\n[Preview only:"),
"preview must stop at max_lines: {text}"
);
}
}