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 on (1-based index)
32 #[serde(default)]
33 pub start_line: Option<usize>,
34
35 /// Optional line number to end reading on (1-based index)
36 #[serde(default)]
37 pub end_line: 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.end_line.is_some() {
87 let start = input.start_line.unwrap_or(1);
88 let lines = text.split('\n').skip(start - 1);
89 if let Some(end) = input.end_line {
90 let count = end.saturating_sub(start);
91 Itertools::intersperse(lines.take(count), "\n").collect()
92 } else {
93 Itertools::intersperse(lines, "\n").collect()
94 }
95 } else {
96 text
97 };
98
99 Ok(string)
100 } else {
101 Err(anyhow!("File does not exist"))
102 }
103 })??;
104
105 action_log.update(&mut cx, |log, cx| {
106 log.buffer_read(buffer, cx);
107 })?;
108
109 anyhow::Ok(result)
110 })
111 }
112}