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