Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions refact-agent/engine/src/files_correction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn _shortify_paths_from_indexed(paths: &Vec<String>, indexed_paths: Arc<HashSet<
/// In Windows, tries to fix the path, permissive about paths like \\?\C:\path, incorrect amount of \ and more.
///
/// Temporarily remove verbatim, to resolve ., .., symlinks if possible, it will be added again later.
fn preprocess_path_for_normalization(p: String) -> String {
pub fn preprocess_path_for_normalization(p: String) -> String {
use itertools::Itertools;

let p = p.replace(r"/", r"\");
Expand Down Expand Up @@ -380,7 +380,7 @@ fn preprocess_path_for_normalization(p: String) -> String {

#[cfg(not(windows))]
/// In Unix, do nothing
fn preprocess_path_for_normalization(p: String) -> String {
pub fn preprocess_path_for_normalization(p: String) -> String {
p
}

Expand Down Expand Up @@ -447,8 +447,12 @@ fn absolute(path: &Path) -> Result<PathBuf, String> {
pub fn canonical_path<T: Into<String>>(p: T) -> PathBuf {
let p: String = p.into();
let path= PathBuf::from(preprocess_path_for_normalization(p));
canonicalize_normalized_path(path)
}

path.canonicalize().unwrap_or_else(|_| absolute(&path).unwrap_or(path))
/// If you did not call preprocess_path_for_normalization() before, use crate::files_correction::canonical_path() instead
pub fn canonicalize_normalized_path(p: PathBuf) -> PathBuf {
p.canonicalize().unwrap_or_else(|_| absolute(&p).unwrap_or(p))
}

pub fn serialize_path<S: serde::Serializer>(path: &PathBuf, serializer: S) -> Result<S::Ok, S::Error> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex as AMutex;
use crate::files_correction::canonical_path;
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
use crate::global_context::GlobalContext;
use tokio::sync::RwLock as ARwLock;

Expand All @@ -25,13 +25,14 @@ pub struct ToolCreateTextDoc;
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolCreateTextDocArgs, String> {
let path = match args.get("path") {
Some(Value::String(s)) => {
let path = canonical_path(&s.trim().to_string());
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
if !path.is_absolute() {
return Err(format!(
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
path
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
s.trim()
));
}
let path = canonicalize_normalized_path(path);
if path.exists() {
return Err(format!(
"Error: Cannot create file at '{:?}' because it already exists. Please choose a different path or use update_textdoc/replace_textdoc to modify existing files.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::at_commands::at_commands::AtCommandsContext;
use crate::call_validation::{ChatContent, ChatMessage, ContextEnum, DiffChunk};
use crate::files_correction::canonical_path;
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
use crate::global_context::GlobalContext;
use crate::integrations::integr_abstract::IntegrationConfirmation;
use crate::tools::file_edit::auxiliary::{
Expand All @@ -25,13 +25,14 @@ pub struct ToolReplaceTextDoc;
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolReplaceTextDocArgs, String> {
let path = match args.get("path") {
Some(Value::String(s)) => {
let path = canonical_path(&s.trim().to_string());
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
if !path.is_absolute() {
return Err(format!(
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
path
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
s.trim()
));
}
let path = canonicalize_normalized_path(path);
if !path.exists() {
return Err(format!(
"Error: The file '{:?}' does not exist. Please check if the path is correct and the file exists.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex as AMutex;
use crate::files_correction::canonical_path;
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
use tokio::sync::RwLock as ARwLock;
use crate::global_context::GlobalContext;

Expand All @@ -25,13 +25,14 @@ pub struct ToolUpdateTextDoc;
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolUpdateTextDocArgs, String> {
let path = match args.get("path") {
Some(Value::String(s)) => {
let path = canonical_path(&s.trim().to_string());
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
if !path.is_absolute() {
return Err(format!(
"Error: The provided path '{:?}' is not absolute. Please provide a full path starting from the root directory.",
path
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
s.trim()
));
}
let path = canonicalize_normalized_path(path);
if !path.exists() {
return Err(format!(
"Error: The file '{:?}' does not exist. Please check if the path is correct and the file exists.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::PathBuf;
use std::sync::Arc;
use regex::Regex;
use tokio::sync::Mutex as AMutex;
use crate::files_correction::canonical_path;
use crate::files_correction::{canonicalize_normalized_path, preprocess_path_for_normalization};
use tokio::sync::RwLock as ARwLock;
use crate::global_context::GlobalContext;

Expand All @@ -26,13 +26,14 @@ pub struct ToolUpdateTextDocRegex;
fn parse_args(args: &HashMap<String, Value>) -> Result<ToolUpdateTextDocRegexArgs, String> {
let path = match args.get("path") {
Some(Value::String(s)) => {
let path = canonical_path(&s.trim().to_string());
let path = PathBuf::from(preprocess_path_for_normalization(s.trim().to_string()));
if !path.is_absolute() {
return Err(format!(
"argument 'path' should be an absolute path: {:?}",
path
"Error: The provided path '{}' is not absolute. Please provide a full path starting from the root directory.",
s.trim()
));
}
let path = canonicalize_normalized_path(path);
if !path.exists() {
return Err(format!("argument 'path' doesn't exists: {:?}", path));
}
Expand Down
Loading