(input: &mut String, files: &[PathBuf])
| 990 | } |
| 991 | |
| 992 | fn append_cli_file_attachments(input: &mut String, files: &[PathBuf]) -> anyhow::Result<()> { |
| 993 | for file_path in files { |
| 994 | let resolved = if file_path.is_absolute() { |
| 995 | file_path.clone() |
| 996 | } else { |
| 997 | std::env::current_dir()?.join(file_path) |
| 998 | }; |
| 999 | let metadata = fs::metadata(&resolved).map_err(|e| { |
| 1000 | anyhow::anyhow!( |
| 1001 | "Failed to read attachment metadata {}: {}", |
| 1002 | resolved.display(), |
| 1003 | e |
| 1004 | ) |
| 1005 | })?; |
| 1006 | let display = resolved |
| 1007 | .strip_prefix(std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))) |
| 1008 | .unwrap_or(&resolved) |
| 1009 | .display() |
| 1010 | .to_string(); |
| 1011 | |
| 1012 | if metadata.is_dir() { |
| 1013 | let tree = Ripgrep::tree(&resolved, Some(150)).unwrap_or_else(|_| { |
| 1014 | format!("(directory listing unavailable for {})", resolved.display()) |
| 1015 | }); |
| 1016 | input.push_str("\n\n[Attachment: directory "); |
| 1017 | input.push_str(&display); |
| 1018 | input.push_str("]\n"); |
| 1019 | input.push_str(&tree); |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1023 | let bytes = fs::read(&resolved).map_err(|e| { |
| 1024 | anyhow::anyhow!("Failed to read attachment {}: {}", resolved.display(), e) |
| 1025 | })?; |
| 1026 | let mut text = String::from_utf8_lossy(&bytes).to_string(); |
| 1027 | const MAX_ATTACHMENT_BYTES: usize = 120_000; |
| 1028 | if text.len() > MAX_ATTACHMENT_BYTES { |
| 1029 | text.truncate(MAX_ATTACHMENT_BYTES); |
| 1030 | text.push_str("\n\n[truncated]"); |
| 1031 | } |
| 1032 | input.push_str("\n\n[Attachment: file "); |
| 1033 | input.push_str(&display); |
| 1034 | input.push_str("]\n```text\n"); |
| 1035 | input.push_str(&text); |
| 1036 | if !text.ends_with('\n') { |
| 1037 | input.push('\n'); |
| 1038 | } |
| 1039 | input.push_str("```"); |
| 1040 | } |
| 1041 | Ok(()) |
| 1042 | } |
| 1043 | |
| 1044 | fn collect_run_input(message: Vec<String>) -> anyhow::Result<String> { |
| 1045 | let mut input = message.join(" "); |
no test coverage detected