Allow read-file tool to read a subset of a file (#26966)
Release Notes: - N/A
This commit is contained in:
parent
4b775505f5
commit
122e73f152
3 changed files with 24 additions and 1 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -715,6 +715,7 @@ dependencies = [
|
||||||
"feature_flags",
|
"feature_flags",
|
||||||
"futures 0.3.31",
|
"futures 0.3.31",
|
||||||
"gpui",
|
"gpui",
|
||||||
|
"itertools 0.14.0",
|
||||||
"language",
|
"language",
|
||||||
"language_model",
|
"language_model",
|
||||||
"pretty_assertions",
|
"pretty_assertions",
|
||||||
|
|
|
@ -18,6 +18,7 @@ chrono.workspace = true
|
||||||
collections.workspace = true
|
collections.workspace = true
|
||||||
feature_flags.workspace = true
|
feature_flags.workspace = true
|
||||||
futures.workspace = true
|
futures.workspace = true
|
||||||
|
itertools.workspace = true
|
||||||
gpui.workspace = true
|
gpui.workspace = true
|
||||||
language.workspace = true
|
language.workspace = true
|
||||||
language_model.workspace = true
|
language_model.workspace = true
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::sync::Arc;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use assistant_tool::{ActionLog, Tool};
|
use assistant_tool::{ActionLog, Tool};
|
||||||
use gpui::{App, Entity, Task};
|
use gpui::{App, Entity, Task};
|
||||||
|
use itertools::Itertools;
|
||||||
use language_model::LanguageModelRequestMessage;
|
use language_model::LanguageModelRequestMessage;
|
||||||
use project::Project;
|
use project::Project;
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
|
@ -26,6 +27,14 @@ pub struct ReadFileToolInput {
|
||||||
/// If you wanna access `file.txt` in `directory2`, you should use the path `directory2/file.txt`.
|
/// If you wanna access `file.txt` in `directory2`, you should use the path `directory2/file.txt`.
|
||||||
/// </example>
|
/// </example>
|
||||||
pub path: Arc<Path>,
|
pub path: Arc<Path>,
|
||||||
|
|
||||||
|
/// Optional line number to start reading from (0-based index)
|
||||||
|
#[serde(default)]
|
||||||
|
pub start_line: Option<usize>,
|
||||||
|
|
||||||
|
/// Optional number of lines to read
|
||||||
|
#[serde(default)]
|
||||||
|
pub line_count: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ReadFileTool;
|
pub struct ReadFileTool;
|
||||||
|
@ -73,7 +82,19 @@ impl Tool for ReadFileTool {
|
||||||
.file()
|
.file()
|
||||||
.map_or(false, |file| file.disk_state().exists())
|
.map_or(false, |file| file.disk_state().exists())
|
||||||
{
|
{
|
||||||
Ok(buffer.text())
|
let text = buffer.text();
|
||||||
|
let string = if input.start_line.is_some() || input.line_count.is_some() {
|
||||||
|
let lines = text.split('\n').skip(input.start_line.unwrap_or(0));
|
||||||
|
if let Some(line_count) = input.line_count {
|
||||||
|
Itertools::intersperse(lines.take(line_count), "\n").collect()
|
||||||
|
} else {
|
||||||
|
Itertools::intersperse(lines, "\n").collect()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
text
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(string)
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!("File does not exist"))
|
Err(anyhow!("File does not exist"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue