read_file_tool.rs

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