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 needs_confirmation(&self) -> bool {
48 false
49 }
50
51 fn description(&self) -> String {
52 include_str!("./read_file_tool/description.md").into()
53 }
54
55 fn input_schema(&self) -> serde_json::Value {
56 let schema = schemars::schema_for!(ReadFileToolInput);
57 serde_json::to_value(&schema).unwrap()
58 }
59
60 fn ui_text(&self, input: &serde_json::Value) -> String {
61 match serde_json::from_value::<ReadFileToolInput>(input.clone()) {
62 Ok(input) => format!("Read file `{}`", input.path.display()),
63 Err(_) => "Read file".to_string(),
64 }
65 }
66
67 fn run(
68 self: Arc<Self>,
69 input: serde_json::Value,
70 _messages: &[LanguageModelRequestMessage],
71 project: Entity<Project>,
72 action_log: Entity<ActionLog>,
73 cx: &mut App,
74 ) -> Task<Result<String>> {
75 let input = match serde_json::from_value::<ReadFileToolInput>(input) {
76 Ok(input) => input,
77 Err(err) => return Task::ready(Err(anyhow!(err))),
78 };
79
80 let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
81 return Task::ready(Err(anyhow!(
82 "Path {} not found in project",
83 &input.path.display()
84 )));
85 };
86
87 cx.spawn(async move |cx| {
88 let buffer = cx
89 .update(|cx| {
90 project.update(cx, |project, cx| project.open_buffer(project_path, cx))
91 })?
92 .await?;
93
94 let result = buffer.read_with(cx, |buffer, _cx| {
95 let text = buffer.text();
96 if input.start_line.is_some() || input.end_line.is_some() {
97 let start = input.start_line.unwrap_or(1);
98 let lines = text.split('\n').skip(start - 1);
99 if let Some(end) = input.end_line {
100 let count = end.saturating_sub(start);
101 Itertools::intersperse(lines.take(count), "\n").collect()
102 } else {
103 Itertools::intersperse(lines, "\n").collect()
104 }
105 } else {
106 text
107 }
108 })?;
109
110 action_log.update(cx, |log, cx| {
111 log.buffer_read(buffer, cx);
112 })?;
113
114 anyhow::Ok(result)
115 })
116 }
117}