1use serde_json::json;
2use std::io::{BufRead, BufReader};
3use std::path::Path;
4
5use lash_core::{ToolCall, ToolDefinition, ToolResult, ToolRetryPolicy, ToolScheduling};
6
7use lash_tool_support::{
8 StaticToolExecute, StaticToolProvider, object_schema, parse_optional_usize_arg, require_str,
9 run_blocking_value,
10};
11
12#[derive(Default)]
14pub struct ReadFile;
15
16pub fn read_file_provider() -> StaticToolProvider<ReadFile> {
18 StaticToolProvider::new(vec![read_file_tool_definition()], ReadFile)
19}
20
21const DEFAULT_LIMIT: usize = 2000;
22const MAX_LINE_LEN: usize = 2000;
23const MAX_OUTPUT_BYTES: usize = 50 * 1024;
24const MAX_OUTPUT_BYTES_LABEL: &str = "50 KB";
25
26struct ImageAttachmentData {
27 data: Vec<u8>,
28 media_type: lash_core::MediaType,
29 width: Option<u32>,
30 height: Option<u32>,
31 label: String,
32}
33
34enum ReadFileBlockingResult {
35 Tool(ToolResult),
36 Image(ImageAttachmentData),
37}
38
39impl ReadFileBlockingResult {
40 fn tool(result: ToolResult) -> Self {
41 Self::Tool(result)
42 }
43
44 fn into_tool_result(self, context: &lash_core::ToolContext<'_>) -> ToolResult {
45 match self {
46 Self::Tool(result) => result,
47 Self::Image(image) => store_image_attachment(context, image),
48 }
49 }
50}
51
52#[async_trait::async_trait]
53impl StaticToolExecute for ReadFile {
54 async fn execute(&self, call: ToolCall<'_>) -> ToolResult {
55 let args = call.args;
56 let path_str = match require_str(args, "path") {
57 Ok(s) => s.to_string(),
58 Err(e) => return e,
59 };
60
61 let offset = args
62 .get("offset")
63 .and_then(|v| v.as_u64())
64 .map(|v| v as usize)
65 .unwrap_or(1)
66 .max(1);
67
68 let limit = match parse_limit(args) {
69 Ok(limit) => limit,
70 Err(e) => return e,
71 };
72
73 match run_blocking_value(move || execute_read_file_sync(&path_str, offset, limit)).await {
74 Ok(result) => result.into_tool_result(call.context),
75 Err(err) => ToolResult::err_fmt(format_args!("{err}")),
76 }
77 }
78}
79
80fn read_file_tool_definition() -> ToolDefinition {
81 ToolDefinition::raw(
82 "tool:read_file",
83 "read_file",
84 "Read a file. Text returns lines prefixed as `LINE: text`, PDFs return extracted text, and images return visual content. Default: 2000 lines. Use `ls` for directories.",
85 object_schema(
86 serde_json::json!({
87 "path": { "type": "string" },
88 "offset": {
89 "type": "integer",
90 "minimum": 1,
91 "description": "Line offset to start reading from (1-based)"
92 },
93 "limit": {
94 "type": "integer",
95 "minimum": 1,
96 "default": DEFAULT_LIMIT,
97 "description": "Maximum lines to read (default: 2000)."
98 }
99 }),
100 &["path"],
101 ),
102 serde_json::json!({ "type": "string" }),
103 )
104 .with_examples(vec![
105 r#"await files.read({ path: "Cargo.toml" })?"#.into(),
106 r#"await files.read({ path: "src/main.rs", offset: 1, limit: 120 })?"#.into(),
107 ])
108 .with_agent_surface(lash_tool_support::agent_surface(
109 ["files"],
110 "read",
111 &["cat", "view_file"],
112 ))
113 .with_scheduling(ToolScheduling::Parallel)
114 .with_retry_policy(ToolRetryPolicy::safe(2, 25, 100))
115}
116
117fn parse_limit(args: &serde_json::Value) -> Result<usize, ToolResult> {
118 Ok(
119 parse_optional_usize_arg(args, "limit", Some(DEFAULT_LIMIT), false, 1)?
120 .unwrap_or(DEFAULT_LIMIT),
121 )
122}
123
124fn execute_read_file_sync(path_str: &str, offset: usize, limit: usize) -> ReadFileBlockingResult {
125 let path = Path::new(path_str);
126 if !path.exists() {
127 return ReadFileBlockingResult::tool(ToolResult::err_fmt(format_args!(
128 "Path does not exist: {path_str}. Use `ls` or `glob` to locate the correct path."
129 )));
130 }
131
132 if path.is_dir() {
134 let mut output = list_directory(path, offset, limit).into_output();
135 if output.is_success()
136 && let lash_core::ToolCallOutcome::Success(lash_core::ToolValue::String(s)) =
137 &mut output.outcome
138 {
139 s.insert_str(0, "(Hint: use `ls` for directory listings.)\n");
140 }
141 return ReadFileBlockingResult::tool(ToolResult::from_output(output));
142 }
143
144 if let Some(mime) = image_mime(path) {
146 return read_image(path, path_str, mime);
147 }
148
149 if path
151 .extension()
152 .and_then(|e| e.to_str())
153 .map(|e| e.eq_ignore_ascii_case("pdf"))
154 .unwrap_or(false)
155 {
156 return ReadFileBlockingResult::tool(read_pdf(path, path_str, offset, limit));
157 }
158
159 if is_likely_binary(path) {
161 return ReadFileBlockingResult::tool(ToolResult::err_fmt(format_args!(
162 "Binary file detected: {path_str}. Use image-aware reads for images, or `shell.exec` for binary inspection."
163 )));
164 }
165
166 let file = match std::fs::File::open(path) {
167 Ok(file) => file,
168 Err(e) => {
169 return ReadFileBlockingResult::tool(ToolResult::err_fmt(format_args!(
170 "Failed to open file: {e}"
171 )));
172 }
173 };
174 let reader = BufReader::new(file);
175 let slice = match collect_window(
176 reader.lines(),
177 offset,
178 limit,
179 |line_no, line| format!("{line_no}: {line}"),
180 "file",
181 ) {
182 Ok(slice) => slice,
183 Err(err) => return ReadFileBlockingResult::tool(err),
184 };
185
186 ReadFileBlockingResult::tool(ToolResult::ok(json!(render_window(
187 &slice,
188 WindowKind::Lines
189 ))))
190}
191
192fn list_directory(path: &Path, offset: usize, limit: usize) -> ToolResult {
193 match std::fs::read_dir(path) {
194 Ok(entries) => {
195 let mut items: Vec<String> = Vec::new();
196 for entry in entries.flatten() {
197 let name = entry.file_name().to_string_lossy().to_string();
198 let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
199 if is_dir {
200 items.push(format!("{}/", name));
201 } else {
202 items.push(name);
203 }
204 }
205 items.sort();
206 let slice = match collect_window(
207 items.into_iter().map(Ok::<String, std::io::Error>),
208 offset,
209 limit,
210 |_index, entry| entry.to_string(),
211 "directory",
212 ) {
213 Ok(slice) => slice,
214 Err(err) => return err,
215 };
216 ToolResult::ok(json!(render_window(&slice, WindowKind::Entries)))
217 }
218 Err(e) => ToolResult::err_fmt(format_args!("Failed to read directory: {e}")),
219 }
220}
221
222fn is_likely_binary(path: &Path) -> bool {
224 use std::io::Read;
225 let mut file = match std::fs::File::open(path) {
226 Ok(f) => f,
227 Err(_) => return false,
228 };
229 let mut buf = [0u8; 8192];
230 let n = match file.read(&mut buf) {
231 Ok(n) => n,
232 Err(_) => return false,
233 };
234 buf[..n].contains(&0)
235}
236
237fn image_mime(path: &Path) -> Option<&'static str> {
239 let ext = path.extension()?.to_str()?.to_ascii_lowercase();
240 match ext.as_str() {
241 "png" => Some("image/png"),
242 "jpg" | "jpeg" => Some("image/jpeg"),
243 "gif" => Some("image/gif"),
244 "webp" => Some("image/webp"),
245 "bmp" => Some("image/bmp"),
246 _ => None,
247 }
248}
249
250fn read_image(path: &Path, path_str: &str, mime: &str) -> ReadFileBlockingResult {
253 let data = match std::fs::read(path) {
254 Ok(d) => d,
255 Err(e) => {
256 return ReadFileBlockingResult::tool(ToolResult::err_fmt(format_args!(
257 "Failed to read image: {e}"
258 )));
259 }
260 };
261
262 let size_kb = data.len() / 1024;
263 let dims = image_dimensions(&data, mime);
264 let label = match dims {
265 Some((w, h)) => format!("{} ({}KB {}x{})", path_str, size_kb, w, h),
266 None => format!("{} ({}KB)", path_str, size_kb),
267 };
268
269 let Some(media_type) = lash_core::MediaType::from_mime(mime) else {
270 return ReadFileBlockingResult::tool(ToolResult::err_fmt(format_args!(
271 "Unsupported image MIME type: {mime}"
272 )));
273 };
274 ReadFileBlockingResult::Image(ImageAttachmentData {
275 data,
276 media_type,
277 width: dims.map(|(width, _)| width),
278 height: dims.map(|(_, height)| height),
279 label,
280 })
281}
282
283fn store_image_attachment(
284 context: &lash_core::ToolContext<'_>,
285 image: ImageAttachmentData,
286) -> ToolResult {
287 let reference = match context.attachments().put(
288 image.data,
289 lash_core::AttachmentCreateMeta::new(
290 image.media_type,
291 image.width,
292 image.height,
293 Some(image.label),
294 ),
295 ) {
296 Ok(reference) => reference,
297 Err(err) => {
298 return ToolResult::err_fmt(format_args!("Failed to store image attachment: {err}"));
299 }
300 };
301 ToolResult::from_output(lash_core::ToolCallOutput::success(
302 lash_core::ToolValue::Attachment(reference),
303 ))
304}
305
306fn read_pdf(path: &Path, path_str: &str, offset: usize, limit: usize) -> ToolResult {
308 let pdf_bytes = match std::fs::read(path) {
309 Ok(b) => b,
310 Err(e) => return ToolResult::err_fmt(format_args!("Failed to read PDF: {e}")),
311 };
312
313 let file_size_kb = pdf_bytes.len() / 1024;
314
315 let text = match pdf_extract::extract_text_from_mem(&pdf_bytes) {
316 Ok(t) => t,
317 Err(e) => {
318 return ToolResult::err_fmt(format_args!(
319 "Failed to extract text from PDF {path_str}: {e}"
320 ));
321 }
322 };
323
324 let slice = match collect_window(
325 text.lines()
326 .map(|line| Ok::<String, std::io::Error>(line.to_string())),
327 offset,
328 limit,
329 |line_no, line| format!("{line_no}: {line}"),
330 "PDF",
331 ) {
332 Ok(slice) => slice,
333 Err(err) => return err,
334 };
335
336 let mut formatted = render_window(&slice, WindowKind::Lines);
337
338 let header = format!(
339 "[PDF: {} ({}KB, {} lines extracted)]\n",
340 path_str, file_size_kb, slice.total_items
341 );
342 formatted.insert_str(0, &header);
343
344 ToolResult::ok(json!(formatted))
345}
346
347fn image_dimensions(data: &[u8], mime: &str) -> Option<(u32, u32)> {
349 match mime {
350 "image/png" => png_dimensions(data),
351 "image/jpeg" => jpeg_dimensions(data),
352 "image/gif" => gif_dimensions(data),
353 _ => None,
354 }
355}
356
357fn png_dimensions(data: &[u8]) -> Option<(u32, u32)> {
359 if data.len() < 24 {
360 return None;
361 }
362 if &data[..8] != b"\x89PNG\r\n\x1a\n" {
364 return None;
365 }
366 let w = u32::from_be_bytes([data[16], data[17], data[18], data[19]]);
367 let h = u32::from_be_bytes([data[20], data[21], data[22], data[23]]);
368 Some((w, h))
369}
370
371fn jpeg_dimensions(data: &[u8]) -> Option<(u32, u32)> {
373 let mut i = 0;
374 while i + 1 < data.len() {
375 if data[i] != 0xFF {
376 i += 1;
377 continue;
378 }
379 let marker = data[i + 1];
380 if marker == 0xC0 || marker == 0xC2 {
382 if i + 9 >= data.len() {
383 return None;
384 }
385 let h = u16::from_be_bytes([data[i + 5], data[i + 6]]) as u32;
386 let w = u16::from_be_bytes([data[i + 7], data[i + 8]]) as u32;
387 return Some((w, h));
388 }
389 if marker == 0xD8 || marker == 0xD9 || marker == 0x01 || (0xD0..=0xD7).contains(&marker) {
391 i += 2;
392 } else if i + 3 < data.len() {
393 let len = u16::from_be_bytes([data[i + 2], data[i + 3]]) as usize;
394 i += 2 + len;
395 } else {
396 break;
397 }
398 }
399 None
400}
401
402fn gif_dimensions(data: &[u8]) -> Option<(u32, u32)> {
404 if data.len() < 10 {
405 return None;
406 }
407 if &data[..3] != b"GIF" {
409 return None;
410 }
411 let w = u16::from_le_bytes([data[6], data[7]]) as u32;
412 let h = u16::from_le_bytes([data[8], data[9]]) as u32;
413 Some((w, h))
414}
415
416struct WindowSlice {
417 rendered: Vec<String>,
418 total_items: usize,
419 shown_start: Option<usize>,
420 shown_end: Option<usize>,
421 has_more_items: bool,
422 truncated_by_bytes: bool,
423}
424
425enum WindowKind {
426 Lines,
427 Entries,
428}
429
430fn collect_window<I, E, F>(
431 items: I,
432 offset: usize,
433 limit: usize,
434 mut format_item: F,
435 item_label: &str,
436) -> Result<WindowSlice, ToolResult>
437where
438 I: IntoIterator<Item = Result<String, E>>,
439 E: std::fmt::Display,
440 F: FnMut(usize, &str) -> String,
441{
442 let mut total_items = 0usize;
443 let mut bytes = 0usize;
444 let mut rendered = Vec::new();
445 let mut has_more_items = false;
446 let mut truncated_by_bytes = false;
447
448 for item in items {
449 let item = item.map_err(|err| {
450 ToolResult::err_fmt(format_args!("Failed to read {item_label}: {err}"))
451 })?;
452 total_items += 1;
453 if total_items < offset {
454 continue;
455 }
456 if rendered.len() >= limit {
457 has_more_items = true;
458 continue;
459 }
460
461 let item = truncate_line(&item);
462 let rendered_item = format_item(total_items, &item);
463 let size = rendered_item.len() + usize::from(!rendered.is_empty());
464 if bytes + size > MAX_OUTPUT_BYTES {
465 truncated_by_bytes = true;
466 has_more_items = true;
467 break;
468 }
469 bytes += size;
470 rendered.push(rendered_item);
471 }
472
473 if total_items < offset && !(total_items == 0 && offset == 1) {
474 return Err(ToolResult::err_fmt(format_args!(
475 "Offset {offset} is out of range for this {item_label} ({total_items} items)"
476 )));
477 }
478
479 let shown_start = (!rendered.is_empty()).then_some(offset);
480 let shown_end = shown_start.map(|start| start + rendered.len().saturating_sub(1));
481
482 Ok(WindowSlice {
483 rendered,
484 total_items,
485 shown_start,
486 shown_end,
487 has_more_items,
488 truncated_by_bytes,
489 })
490}
491
492fn render_window(slice: &WindowSlice, kind: WindowKind) -> String {
493 let mut output = slice.rendered.join("\n");
494 let Some(shown_start) = slice.shown_start else {
495 return output;
496 };
497 let Some(shown_end) = slice.shown_end else {
498 return output;
499 };
500
501 let next_offset = shown_end + 1;
502 match kind {
503 WindowKind::Lines => {
504 if slice.truncated_by_bytes {
505 output.push_str(&format!(
506 "\n[output capped at {}. Showing lines {}-{}. Use offset={} to continue.]",
507 MAX_OUTPUT_BYTES_LABEL, shown_start, shown_end, next_offset
508 ));
509 } else if slice.has_more_items {
510 output.push_str(&format!(
511 "\n[results truncated: showing lines {}-{} of {}. Use offset={} to continue.]",
512 shown_start, shown_end, slice.total_items, next_offset
513 ));
514 }
515 }
516 WindowKind::Entries => {
517 if slice.truncated_by_bytes {
518 output.push_str(&format!(
519 "\n[output capped at {}. Showing entries {}-{}. Use offset={} to continue.]",
520 MAX_OUTPUT_BYTES_LABEL, shown_start, shown_end, next_offset
521 ));
522 } else if slice.has_more_items {
523 output.push_str(&format!(
524 "\n[results truncated: showing entries {}-{} of {}. Use offset={} to continue.]",
525 shown_start, shown_end, slice.total_items, next_offset
526 ));
527 }
528 }
529 }
530 output
531}
532
533fn truncate_line(line: &str) -> String {
534 if line.len() > MAX_LINE_LEN {
535 let end = floor_char_boundary(line, MAX_LINE_LEN);
538 format!("{}...", &line[..end])
539 } else {
540 line.to_string()
541 }
542}
543
544fn floor_char_boundary(text: &str, index: usize) -> usize {
545 if index >= text.len() {
546 return text.len();
547 }
548 let mut idx = index;
549 while idx > 0 && !text.is_char_boundary(idx) {
550 idx -= 1;
551 }
552 idx
553}
554
555#[cfg(test)]
556mod tests {
557 use super::*;
558 use std::sync::Arc;
559
560 use lash_core::AttachmentStore;
561 use serde_json::json;
562 use tempfile::TempDir;
563
564 #[tokio::test]
565 async fn test_read_file() {
566 let dir = TempDir::new().unwrap();
567 let path = dir.path().join("test.txt");
568 std::fs::write(&path, "line1\nline2\nline3").unwrap();
569 let result = lash_core::testing::run_tool(
570 &read_file_provider(),
571 "read_file",
572 &json!({"path": path.to_str().unwrap()}),
573 )
574 .await;
575 assert!(result.is_success());
576 let value = result.value_for_projection();
577 let text = value.as_str().unwrap();
578 assert!(text.contains("1: line1"));
579 assert!(text.contains("2: line2"));
580 assert!(text.contains("3: line3"));
581 assert!(!text.contains('|'));
582 }
583
584 #[tokio::test]
585 async fn test_read_with_offset_and_limit() {
586 let dir = TempDir::new().unwrap();
587 let path = dir.path().join("test.txt");
588 std::fs::write(&path, "line1\nline2\nline3\nline4\nline5").unwrap();
589 let result = lash_core::testing::run_tool(
590 &read_file_provider(),
591 "read_file",
592 &json!({"path": path.to_str().unwrap(), "offset": 2, "limit": 2}),
593 )
594 .await;
595 assert!(result.is_success());
596 let value = result.value_for_projection();
597 let text = value.as_str().unwrap();
598 assert!(text.contains("2: line2"));
599 assert!(text.contains("3: line3"));
600 assert!(!text.contains("1: line1"));
601 assert!(!text.contains("4: line4"));
602 assert!(text.contains("results truncated"));
603 assert!(text.contains("offset=4"));
604 }
605
606 #[tokio::test]
607 async fn test_read_caps_large_output_by_bytes() {
608 let dir = TempDir::new().unwrap();
609 let path = dir.path().join("test.txt");
610 let content = (0..200)
611 .map(|idx| format!("{idx}: {}", "x".repeat(400)))
612 .collect::<Vec<_>>()
613 .join("\n");
614 std::fs::write(&path, content).unwrap();
615 let result = lash_core::testing::run_tool(
616 &read_file_provider(),
617 "read_file",
618 &json!({"path": path.to_str().unwrap(), "limit": 200}),
619 )
620 .await;
621 assert!(result.is_success());
622 let value = result.value_for_projection();
623 let text = value.as_str().unwrap();
624 assert!(text.contains("output capped at 50 KB"));
625 assert!(text.contains("Use offset="));
626 }
627
628 #[tokio::test]
629 async fn test_read_nonexistent() {
630 let result = lash_core::testing::run_tool(
631 &read_file_provider(),
632 "read_file",
633 &json!({"path": "/nonexistent/path/to/file.txt"}),
634 )
635 .await;
636 assert!(!result.is_success());
637 }
638
639 #[test]
642 fn test_png_dimensions_valid() {
643 let mut data = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
645 data.extend_from_slice(&[0, 0, 0, 13]);
647 data.extend_from_slice(b"IHDR");
649 data.extend_from_slice(&640u32.to_be_bytes());
651 data.extend_from_slice(&480u32.to_be_bytes());
653 let (w, h) = png_dimensions(&data).unwrap();
654 assert_eq!((w, h), (640, 480));
655 }
656
657 #[test]
658 fn test_png_dimensions_truncated() {
659 assert!(png_dimensions(&[0x89, b'P', b'N', b'G']).is_none());
660 }
661
662 #[test]
663 fn test_png_dimensions_wrong_sig() {
664 let data = vec![0; 24];
665 assert!(png_dimensions(&data).is_none());
666 }
667
668 #[test]
671 fn test_jpeg_dimensions_valid() {
672 let mut data = vec![0xFF, 0xD8]; data.extend_from_slice(&[0xFF, 0xC0]);
676 data.extend_from_slice(&[0x00, 0x11]);
678 data.push(8);
680 data.extend_from_slice(&480u16.to_be_bytes());
682 data.extend_from_slice(&640u16.to_be_bytes());
684 data.push(0);
686 let (w, h) = jpeg_dimensions(&data).unwrap();
687 assert_eq!((w, h), (640, 480));
688 }
689
690 #[test]
691 fn test_jpeg_dimensions_truncated() {
692 assert!(jpeg_dimensions(&[0xFF, 0xD8, 0xFF, 0xC0]).is_none());
693 }
694
695 #[test]
698 fn test_gif87a_dimensions() {
699 let mut data = b"GIF87a".to_vec();
700 data.extend_from_slice(&320u16.to_le_bytes());
702 data.extend_from_slice(&200u16.to_le_bytes());
704 let (w, h) = gif_dimensions(&data).unwrap();
705 assert_eq!((w, h), (320, 200));
706 }
707
708 #[test]
709 fn test_gif89a_dimensions() {
710 let mut data = b"GIF89a".to_vec();
711 data.extend_from_slice(&100u16.to_le_bytes());
712 data.extend_from_slice(&50u16.to_le_bytes());
713 let (w, h) = gif_dimensions(&data).unwrap();
714 assert_eq!((w, h), (100, 50));
715 }
716
717 #[test]
718 fn test_gif_bad_signature() {
719 let data = b"NOT_GIF___".to_vec();
720 assert!(gif_dimensions(&data).is_none());
721 }
722
723 #[test]
726 fn test_image_mime() {
727 assert_eq!(image_mime(Path::new("photo.png")), Some("image/png"));
728 assert_eq!(image_mime(Path::new("photo.jpg")), Some("image/jpeg"));
729 assert_eq!(image_mime(Path::new("photo.jpeg")), Some("image/jpeg"));
730 assert_eq!(image_mime(Path::new("anim.gif")), Some("image/gif"));
731 assert_eq!(image_mime(Path::new("photo.webp")), Some("image/webp"));
732 assert_eq!(image_mime(Path::new("photo.bmp")), Some("image/bmp"));
733 assert_eq!(image_mime(Path::new("file.txt")), None);
734 assert_eq!(image_mime(Path::new("noext")), None);
735 }
736
737 #[tokio::test]
738 async fn test_read_image_returns_attachment_value() {
739 let dir = TempDir::new().unwrap();
740 let path = dir.path().join("tiny.png");
741 let mut data = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
742 data.extend_from_slice(&[0, 0, 0, 13]);
743 data.extend_from_slice(b"IHDR");
744 data.extend_from_slice(&1u32.to_be_bytes());
745 data.extend_from_slice(&1u32.to_be_bytes());
746 std::fs::write(&path, &data).unwrap();
747
748 let store = Arc::new(lash_core::InMemoryAttachmentStore::new());
749 let host = Arc::new(lash_core::testing::MockSessionManager::default());
750 let context = lash_core::ToolContext::__for_testing(
751 "test-session".into(),
752 host.clone(),
753 host.clone(),
754 host,
755 Arc::new(lash_core::UnavailableProcessService),
756 store.clone(),
757 lash_core::DirectCompletionClient::from_fn(|_, _| {
758 Err(lash_core::PluginError::Session(
759 "direct completions are unavailable in read_file tests".to_string(),
760 ))
761 }),
762 None,
763 );
764 let result = ReadFile
765 .execute(lash_core::ToolCall {
766 name: "read_file",
767 args: &json!({"path": path.to_str().unwrap()}),
768 context: &context,
769 progress: None,
770 })
771 .await;
772
773 let lash_core::ToolCallOutcome::Success(lash_core::ToolValue::Attachment(reference)) =
774 result.into_output().outcome
775 else {
776 panic!("expected attachment result");
777 };
778 assert_eq!(reference.byte_len, data.len() as u64);
779 assert_eq!(reference.width, Some(1));
780 assert_eq!(reference.height, Some(1));
781 assert_eq!(store.get(&reference.id).unwrap().bytes, data);
782 }
783
784 #[test]
785 fn truncate_line_does_not_split_multibyte_char() {
786 let mut line = "a".repeat(MAX_LINE_LEN - 1);
790 line.push('€');
791 line.push_str(&"b".repeat(50));
792 assert!(line.len() > MAX_LINE_LEN);
793
794 let truncated = truncate_line(&line);
795
796 assert!(truncated.ends_with("..."));
798 let body = truncated.strip_suffix("...").unwrap();
799 assert_eq!(body, "a".repeat(MAX_LINE_LEN - 1));
801 assert!(body.is_char_boundary(body.len()));
802 }
803}