(
&self,
msg: &mut SessionMessage,
raw_url: &str,
filename: &str,
mime: &str,
project_root: &str,
)
| 393 | } |
| 394 | |
| 395 | async fn add_file_url_part( |
| 396 | &self, |
| 397 | msg: &mut SessionMessage, |
| 398 | raw_url: &str, |
| 399 | filename: &str, |
| 400 | mime: &str, |
| 401 | project_root: &str, |
| 402 | ) { |
| 403 | let parsed = match url::Url::parse(raw_url) { |
| 404 | Ok(url) => url, |
| 405 | Err(err) => { |
| 406 | msg.add_text(format!("Invalid file URL `{}`: {}", raw_url, err)); |
| 407 | msg.add_file(raw_url.to_string(), filename.to_string(), mime.to_string()); |
| 408 | return; |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | let file_path = match parsed.to_file_path() { |
| 413 | Ok(path) => path, |
| 414 | Err(_) => { |
| 415 | msg.add_text(format!("Invalid file path URL `{}`", raw_url)); |
| 416 | msg.add_file(raw_url.to_string(), filename.to_string(), mime.to_string()); |
| 417 | return; |
| 418 | } |
| 419 | }; |
| 420 | |
| 421 | let metadata = match tokio::fs::metadata(&file_path).await { |
| 422 | Ok(meta) => meta, |
| 423 | Err(err) => { |
| 424 | msg.add_text(format!( |
| 425 | "Read tool failed to read {} with error: {}", |
| 426 | file_path.display(), |
| 427 | err |
| 428 | )); |
| 429 | msg.add_file(raw_url.to_string(), filename.to_string(), mime.to_string()); |
| 430 | return; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | if metadata.is_dir() { |
| 435 | let listing = Self::read_directory_preview(&file_path).await; |
| 436 | msg.add_text(format!( |
| 437 | "Called the Read tool with the following input: {}", |
| 438 | serde_json::json!({ "filePath": file_path.display().to_string() }) |
| 439 | )); |
| 440 | msg.add_text(listing); |
| 441 | msg.add_file( |
| 442 | raw_url.to_string(), |
| 443 | filename.to_string(), |
| 444 | "application/x-directory".to_string(), |
| 445 | ); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | let bytes = match tokio::fs::read(&file_path).await { |
| 450 | Ok(bytes) => bytes, |
| 451 | Err(err) => { |
| 452 | msg.add_text(format!( |
no test coverage detected