1use crate::schema::json_schema_for;
2use anyhow::{anyhow, Result};
3use assistant_tool::{ActionLog, Tool};
4use gpui::{App, Entity, Task};
5use language_model::LanguageModelRequestMessage;
6use language_model::LanguageModelToolSchemaFormat;
7use project::Project;
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11use ui::IconName;
12use util::markdown::MarkdownString;
13
14#[derive(Debug, Serialize, Deserialize, JsonSchema)]
15pub struct CreateFileToolInput {
16 /// The path where the file should be created.
17 ///
18 /// <example>
19 /// If the project has the following structure:
20 ///
21 /// - directory1/
22 /// - directory2/
23 ///
24 /// You can create a new file by providing a path of "directory1/new_file.txt"
25 /// </example>
26 pub path: String,
27
28 /// The text contents of the file to create.
29 ///
30 /// <example>
31 /// To create a file with the text "Hello, World!", provide contents of "Hello, World!"
32 /// </example>
33 pub contents: String,
34}
35
36pub struct CreateFileTool;
37
38impl Tool for CreateFileTool {
39 fn name(&self) -> String {
40 "create-file".into()
41 }
42
43 fn needs_confirmation(&self) -> bool {
44 true
45 }
46
47 fn description(&self) -> String {
48 include_str!("./create_file_tool/description.md").into()
49 }
50
51 fn icon(&self) -> IconName {
52 IconName::FileCreate
53 }
54
55 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> serde_json::Value {
56 json_schema_for::<CreateFileToolInput>(format)
57 }
58
59 fn ui_text(&self, input: &serde_json::Value) -> String {
60 match serde_json::from_value::<CreateFileToolInput>(input.clone()) {
61 Ok(input) => {
62 let path = MarkdownString::inline_code(&input.path);
63 format!("Create file {path}")
64 }
65 Err(_) => "Create file".to_string(),
66 }
67 }
68
69 fn run(
70 self: Arc<Self>,
71 input: serde_json::Value,
72 _messages: &[LanguageModelRequestMessage],
73 project: Entity<Project>,
74 action_log: Entity<ActionLog>,
75 cx: &mut App,
76 ) -> Task<Result<String>> {
77 let input = match serde_json::from_value::<CreateFileToolInput>(input) {
78 Ok(input) => input,
79 Err(err) => return Task::ready(Err(anyhow!(err))),
80 };
81 let project_path = match project.read(cx).find_project_path(&input.path, cx) {
82 Some(project_path) => project_path,
83 None => return Task::ready(Err(anyhow!("Path to create was outside the project"))),
84 };
85 let contents: Arc<str> = input.contents.as_str().into();
86 let destination_path: Arc<str> = input.path.as_str().into();
87
88 cx.spawn(async move |cx| {
89 let buffer = project
90 .update(cx, |project, cx| {
91 project.open_buffer(project_path.clone(), cx)
92 })?
93 .await
94 .map_err(|err| anyhow!("Unable to open buffer for {destination_path}: {err}"))?;
95 let edit_id = buffer.update(cx, |buffer, cx| buffer.set_text(contents, cx))?;
96
97 action_log.update(cx, |action_log, cx| {
98 action_log.will_create_buffer(buffer.clone(), edit_id, cx)
99 })?;
100
101 project
102 .update(cx, |project, cx| project.save_buffer(buffer, cx))?
103 .await
104 .map_err(|err| anyhow!("Unable to save buffer for {destination_path}: {err}"))?;
105
106 Ok(format!("Created file {destination_path}"))
107 })
108 }
109}