read_file_tool.rs

  1use std::path::Path;
  2use std::sync::Arc;
  3
  4use anyhow::{anyhow, Result};
  5use assistant_tool::{ActionLog, Tool};
  6use gpui::{App, Entity, Task};
  7use itertools::Itertools;
  8use language_model::LanguageModelRequestMessage;
  9use project::Project;
 10use schemars::JsonSchema;
 11use serde::{Deserialize, Serialize};
 12
 13#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 14pub struct ReadFileToolInput {
 15    /// The relative path of the file to read.
 16    ///
 17    /// This path should never be absolute, and the first component
 18    /// of the path should always be a root directory in a project.
 19    ///
 20    /// <example>
 21    /// If the project has the following root directories:
 22    ///
 23    /// - directory1
 24    /// - directory2
 25    ///
 26    /// If you wanna access `file.txt` in `directory1`, you should use the path `directory1/file.txt`.
 27    /// If you wanna access `file.txt` in `directory2`, you should use the path `directory2/file.txt`.
 28    /// </example>
 29    pub path: Arc<Path>,
 30
 31    /// Optional line number to start reading from (0-based index)
 32    #[serde(default)]
 33    pub start_line: Option<usize>,
 34
 35    /// Optional number of lines to read
 36    #[serde(default)]
 37    pub line_count: Option<usize>,
 38}
 39
 40pub struct ReadFileTool;
 41
 42impl Tool for ReadFileTool {
 43    fn name(&self) -> String {
 44        "read-file".into()
 45    }
 46
 47    fn description(&self) -> String {
 48        include_str!("./read_file_tool/description.md").into()
 49    }
 50
 51    fn input_schema(&self) -> serde_json::Value {
 52        let schema = schemars::schema_for!(ReadFileToolInput);
 53        serde_json::to_value(&schema).unwrap()
 54    }
 55
 56    fn run(
 57        self: Arc<Self>,
 58        input: serde_json::Value,
 59        _messages: &[LanguageModelRequestMessage],
 60        project: Entity<Project>,
 61        action_log: Entity<ActionLog>,
 62        cx: &mut App,
 63    ) -> Task<Result<String>> {
 64        let input = match serde_json::from_value::<ReadFileToolInput>(input) {
 65            Ok(input) => input,
 66            Err(err) => return Task::ready(Err(anyhow!(err))),
 67        };
 68
 69        let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
 70            return Task::ready(Err(anyhow!("Path not found in project")));
 71        };
 72
 73        cx.spawn(|mut cx| async move {
 74            let buffer = cx
 75                .update(|cx| {
 76                    project.update(cx, |project, cx| project.open_buffer(project_path, cx))
 77                })?
 78                .await?;
 79
 80            let result = buffer.read_with(&cx, |buffer, _cx| {
 81                if buffer
 82                    .file()
 83                    .map_or(false, |file| file.disk_state().exists())
 84                {
 85                    let text = buffer.text();
 86                    let string = if input.start_line.is_some() || input.line_count.is_some() {
 87                        let lines = text.split('\n').skip(input.start_line.unwrap_or(0));
 88                        if let Some(line_count) = input.line_count {
 89                            Itertools::intersperse(lines.take(line_count), "\n").collect()
 90                        } else {
 91                            Itertools::intersperse(lines, "\n").collect()
 92                        }
 93                    } else {
 94                        text
 95                    };
 96
 97                    Ok(string)
 98                } else {
 99                    Err(anyhow!("File does not exist"))
100                }
101            })??;
102
103            action_log.update(&mut cx, |log, cx| {
104                log.buffer_read(buffer, cx);
105            })?;
106
107            anyhow::Ok(result)
108        })
109    }
110}