read_file_tool.rs

  1use std::sync::Arc;
  2
  3use crate::{code_symbols_tool::file_outline, schema::json_schema_for};
  4use anyhow::{Result, anyhow};
  5use assistant_tool::{ActionLog, Tool, ToolResult};
  6use gpui::{App, Entity, Task};
  7use itertools::Itertools;
  8use language_model::{LanguageModelRequestMessage, LanguageModelToolSchemaFormat};
  9use project::Project;
 10use schemars::JsonSchema;
 11use serde::{Deserialize, Serialize};
 12use ui::IconName;
 13use util::markdown::MarkdownString;
 14
 15/// If the model requests to read a file whose size exceeds this, then
 16/// the tool will return an error along with the model's symbol outline,
 17/// and suggest trying again using line ranges from the outline.
 18const MAX_FILE_SIZE_TO_READ: usize = 16384;
 19
 20#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 21pub struct ReadFileToolInput {
 22    /// The relative path of the file to read.
 23    ///
 24    /// This path should never be absolute, and the first component
 25    /// of the path should always be a root directory in a project.
 26    ///
 27    /// <example>
 28    /// If the project has the following root directories:
 29    ///
 30    /// - directory1
 31    /// - directory2
 32    ///
 33    /// If you want to access `file.txt` in `directory1`, you should use the path `directory1/file.txt`.
 34    /// If you want to access `file.txt` in `directory2`, you should use the path `directory2/file.txt`.
 35    /// </example>
 36    pub path: String,
 37
 38    /// Optional line number to start reading on (1-based index)
 39    #[serde(default)]
 40    pub start_line: Option<usize>,
 41
 42    /// Optional line number to end reading on (1-based index)
 43    #[serde(default)]
 44    pub end_line: Option<usize>,
 45}
 46
 47pub struct ReadFileTool;
 48
 49impl Tool for ReadFileTool {
 50    fn name(&self) -> String {
 51        "read_file".into()
 52    }
 53
 54    fn needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
 55        false
 56    }
 57
 58    fn description(&self) -> String {
 59        include_str!("./read_file_tool/description.md").into()
 60    }
 61
 62    fn icon(&self) -> IconName {
 63        IconName::FileSearch
 64    }
 65
 66    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
 67        json_schema_for::<ReadFileToolInput>(format)
 68    }
 69
 70    fn ui_text(&self, input: &serde_json::Value) -> String {
 71        match serde_json::from_value::<ReadFileToolInput>(input.clone()) {
 72            Ok(input) => {
 73                let path = MarkdownString::inline_code(&input.path);
 74                match (input.start_line, input.end_line) {
 75                    (Some(start), None) => format!("Read file {path} (from line {start})"),
 76                    (Some(start), Some(end)) => format!("Read file {path} (lines {start}-{end})"),
 77                    _ => format!("Read file {path}"),
 78                }
 79            }
 80            Err(_) => "Read file".to_string(),
 81        }
 82    }
 83
 84    fn run(
 85        self: Arc<Self>,
 86        input: serde_json::Value,
 87        _messages: &[LanguageModelRequestMessage],
 88        project: Entity<Project>,
 89        action_log: Entity<ActionLog>,
 90        cx: &mut App,
 91    ) -> ToolResult {
 92        let input = match serde_json::from_value::<ReadFileToolInput>(input) {
 93            Ok(input) => input,
 94            Err(err) => return Task::ready(Err(anyhow!(err))).into(),
 95        };
 96
 97        let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
 98            return Task::ready(Err(anyhow!("Path {} not found in project", &input.path,))).into();
 99        };
100
101        let file_path = input.path.clone();
102        cx.spawn(async move |cx| {
103            let buffer = cx
104                .update(|cx| {
105                    project.update(cx, |project, cx| project.open_buffer(project_path, cx))
106                })?
107                .await?;
108
109            // Check if specific line ranges are provided
110            if input.start_line.is_some() || input.end_line.is_some() {
111                let result = buffer.read_with(cx, |buffer, _cx| {
112                    let text = buffer.text();
113                    let start = input.start_line.unwrap_or(1);
114                    let lines = text.split('\n').skip(start - 1);
115                    if let Some(end) = input.end_line {
116                        let count = end.saturating_sub(start).max(1); // Ensure at least 1 line
117                        Itertools::intersperse(lines.take(count), "\n").collect()
118                    } else {
119                        Itertools::intersperse(lines, "\n").collect()
120                    }
121                })?;
122
123                action_log.update(cx, |log, cx| {
124                    log.buffer_read(buffer, cx);
125                })?;
126
127                Ok(result)
128            } else {
129                // No line ranges specified, so check file size to see if it's too big.
130                let file_size = buffer.read_with(cx, |buffer, _cx| buffer.text().len())?;
131
132                if file_size <= MAX_FILE_SIZE_TO_READ {
133                    // File is small enough, so return its contents.
134                    let result = buffer.read_with(cx, |buffer, _cx| buffer.text())?;
135
136                    action_log.update(cx, |log, cx| {
137                        log.buffer_read(buffer, cx);
138                    })?;
139
140                    Ok(result)
141                } else {
142                    // File is too big, so return an error with the outline
143                    // and a suggestion to read again with line numbers.
144                    let outline = file_outline(project, file_path, action_log, None, 0, cx).await?;
145
146                    Ok(format!("This file was too big to read all at once. Here is an outline of its symbols:\n\n{outline}\n\nUsing the line numbers in this outline, you can call this tool again while specifying the start_line and end_line fields to see the implementations of symbols in the outline."))
147                }
148            }
149        }).into()
150    }
151}