Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14d930f

Browse files
chore: avoid sending file content back to LLM while creating file (tailcallhq#950)
Co-authored-by: Tushar Mathur <[email protected]>
1 parent 619ba9d commit 14d930f

7 files changed

Lines changed: 25 additions & 17 deletions

crates/forge_app/src/execution_result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ impl ExecutionResult {
5454
let diff =
5555
console::strip_ansi_codes(&DiffFormat::format(&before, &input.content))
5656
.to_string();
57-
Element::new("file_diff").cdata(diff)
57+
Element::new("file_overwritten").append(Element::new("file_diff").cdata(diff))
5858
} else {
59-
Element::new("file_content").cdata(&input.content)
59+
Element::new("file_created")
6060
};
6161

6262
elm = elm

crates/forge_app/src/snapshots/forge_app__execution_result__tests__fs_create_basic.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
source: crates/forge_app/src/execution_result.rs
33
expression: to_value(actual)
44
---
5-
<file_content
5+
<file_created
66
path="/home/user/new_file.txt"
77
total_lines="1"
8-
><![CDATA[Hello, world!]]>
9-
</file_content>
8+
>
9+
</file_created>

crates/forge_app/src/snapshots/forge_app__execution_result__tests__fs_create_overwrite.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
source: crates/forge_app/src/execution_result.rs
33
expression: to_value(actual)
44
---
5-
<file_diff
5+
<file_overwritten
66
path="/home/user/existing_file.txt"
77
total_lines="1"
8-
><![CDATA[1 |-Old content
8+
>
9+
<file_diff><![CDATA[1 |-Old content
910
1 |+New content for the file
10-
]]>
11-
</file_diff>
11+
]]></file_diff>
12+
</file_overwritten>

crates/forge_app/src/snapshots/forge_app__execution_result__tests__fs_create_with_warning.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
source: crates/forge_app/src/execution_result.rs
33
expression: to_value(actual)
44
---
5-
<file_content
5+
<file_created
66
path="/home/user/file_with_warning.txt"
77
total_lines="1"
8-
><![CDATA[Content with warning]]>
8+
>
99
<warning>File created in non-standard location</warning>
10-
</file_content>
10+
</file_created>

crates/forge_fs/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pub enum Error {
1111
#[error("Start position {start} is beyond the file size of {total} characters")]
1212
StartBeyondFileSize { start: u64, total: u64 },
1313

14+
#[error("Start position {start} and end position {end} must be 1-based (inclusive)")]
15+
IndexStartingWithZero { start: u64, end: u64 },
16+
1417
#[error("Start position {start} is greater than end position {end}")]
1518
StartGreaterThanEnd { start: u64, end: u64 },
1619

crates/forge_fs/src/read_range.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ impl crate::ForgeFS {
2626
let path_ref = path.as_ref();
2727

2828
// Basic validation
29-
if start_line == 0 || end_line == 0 {
30-
return Err(Error::StartBeyondFileSize { start: start_line, total: 0 }.into());
31-
}
3229
if start_line > end_line {
3330
return Err(Error::StartGreaterThanEnd { start: start_line, end: end_line }.into());
3431
}
@@ -38,6 +35,10 @@ impl crate::ForgeFS {
3835
.await
3936
.with_context(|| format!("Failed to open file {}", path_ref.display()))?;
4037

38+
if start_line == 0 || end_line == 0 {
39+
return Err(Error::IndexStartingWithZero { start: start_line, end: end_line }.into());
40+
}
41+
4142
let (is_text, file_type) = Self::is_binary(&mut file).await?;
4243
if !is_text {
4344
return Err(Error::BinaryFileNotSupported(file_type).into());
@@ -47,7 +48,10 @@ impl crate::ForgeFS {
4748
let content = tokio::fs::read_to_string(path_ref)
4849
.await
4950
.with_context(|| format!("Failed to read file content from {}", path_ref.display()))?;
50-
51+
if start_line < 2 && content.is_empty() {
52+
// If the file is empty, return empty content
53+
return Ok((String::new(), FileInfo::new(start_line, end_line, 0)));
54+
}
5155
// Split into lines
5256
let lines: Vec<&str> = content.lines().collect();
5357
let total_lines = lines.len() as u64;

crates/forge_services/src/tools_v2/fs_create.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<F: Infrastructure> FsCreateService for ForgeFsCreate<F> {
5757
}
5858

5959
// record the file content before they're modified
60-
let old_content = if file_exists {
60+
let old_content = if file_exists && overwrite {
6161
Some(self.0.file_read_service().read_utf8(path).await?)
6262
} else {
6363
None

0 commit comments

Comments
 (0)