edit.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package tools
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/kujtimiihoxha/termai/internal/config"
  11. "github.com/kujtimiihoxha/termai/internal/git"
  12. "github.com/kujtimiihoxha/termai/internal/lsp"
  13. "github.com/kujtimiihoxha/termai/internal/permission"
  14. )
  15. type EditParams struct {
  16. FilePath string `json:"file_path"`
  17. OldString string `json:"old_string"`
  18. NewString string `json:"new_string"`
  19. }
  20. type EditPermissionsParams struct {
  21. FilePath string `json:"file_path"`
  22. Diff string `json:"diff"`
  23. }
  24. type EditResponseMetadata struct {
  25. Additions int `json:"additions"`
  26. Removals int `json:"removals"`
  27. }
  28. type editTool struct {
  29. lspClients map[string]*lsp.Client
  30. permissions permission.Service
  31. }
  32. const (
  33. EditToolName = "edit"
  34. editDescription = `Edits files by replacing text, creating new files, or deleting content. For moving or renaming files, use the Bash tool with the 'mv' command instead. For larger file edits, use the FileWrite tool to overwrite files.
  35. Before using this tool:
  36. 1. Use the FileRead tool to understand the file's contents and context
  37. 2. Verify the directory path is correct (only applicable when creating new files):
  38. - Use the LS tool to verify the parent directory exists and is the correct location
  39. To make a file edit, provide the following:
  40. 1. file_path: The absolute path to the file to modify (must be absolute, not relative)
  41. 2. old_string: The text to replace (must be unique within the file, and must match the file contents exactly, including all whitespace and indentation)
  42. 3. new_string: The edited text to replace the old_string
  43. Special cases:
  44. - To create a new file: provide file_path and new_string, leave old_string empty
  45. - To delete content: provide file_path and old_string, leave new_string empty
  46. The tool will replace ONE occurrence of old_string with new_string in the specified file.
  47. CRITICAL REQUIREMENTS FOR USING THIS TOOL:
  48. 1. UNIQUENESS: The old_string MUST uniquely identify the specific instance you want to change. This means:
  49. - Include AT LEAST 3-5 lines of context BEFORE the change point
  50. - Include AT LEAST 3-5 lines of context AFTER the change point
  51. - Include all whitespace, indentation, and surrounding code exactly as it appears in the file
  52. 2. SINGLE INSTANCE: This tool can only change ONE instance at a time. If you need to change multiple instances:
  53. - Make separate calls to this tool for each instance
  54. - Each call must uniquely identify its specific instance using extensive context
  55. 3. VERIFICATION: Before using this tool:
  56. - Check how many instances of the target text exist in the file
  57. - If multiple instances exist, gather enough context to uniquely identify each one
  58. - Plan separate tool calls for each instance
  59. WARNING: If you do not follow these requirements:
  60. - The tool will fail if old_string matches multiple locations
  61. - The tool will fail if old_string doesn't match exactly (including whitespace)
  62. - You may change the wrong instance if you don't include enough context
  63. When making edits:
  64. - Ensure the edit results in idiomatic, correct code
  65. - Do not leave the code in a broken state
  66. - Always use absolute file paths (starting with /)
  67. Remember: when making multiple file edits in a row to the same file, you should prefer to send all edits in a single message with multiple calls to this tool, rather than multiple messages with a single call each.`
  68. )
  69. func NewEditTool(lspClients map[string]*lsp.Client, permissions permission.Service) BaseTool {
  70. return &editTool{
  71. lspClients: lspClients,
  72. permissions: permissions,
  73. }
  74. }
  75. func (e *editTool) Info() ToolInfo {
  76. return ToolInfo{
  77. Name: EditToolName,
  78. Description: editDescription,
  79. Parameters: map[string]any{
  80. "file_path": map[string]any{
  81. "type": "string",
  82. "description": "The absolute path to the file to modify",
  83. },
  84. "old_string": map[string]any{
  85. "type": "string",
  86. "description": "The text to replace",
  87. },
  88. "new_string": map[string]any{
  89. "type": "string",
  90. "description": "The text to replace it with",
  91. },
  92. },
  93. Required: []string{"file_path", "old_string", "new_string"},
  94. }
  95. }
  96. func (e *editTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
  97. var params EditParams
  98. if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
  99. return NewTextErrorResponse("invalid parameters"), nil
  100. }
  101. if params.FilePath == "" {
  102. return NewTextErrorResponse("file_path is required"), nil
  103. }
  104. if !filepath.IsAbs(params.FilePath) {
  105. wd := config.WorkingDirectory()
  106. params.FilePath = filepath.Join(wd, params.FilePath)
  107. }
  108. if params.OldString == "" {
  109. result, err := e.createNewFile(ctx, params.FilePath, params.NewString)
  110. if err != nil {
  111. return NewTextErrorResponse(fmt.Sprintf("error creating file: %s", err)), nil
  112. }
  113. return WithResponseMetadata(NewTextResponse(result.text), EditResponseMetadata{
  114. Additions: result.additions,
  115. Removals: result.removals,
  116. }), nil
  117. }
  118. if params.NewString == "" {
  119. result, err := e.deleteContent(ctx, params.FilePath, params.OldString)
  120. if err != nil {
  121. return NewTextErrorResponse(fmt.Sprintf("error deleting content: %s", err)), nil
  122. }
  123. return WithResponseMetadata(NewTextResponse(result.text), EditResponseMetadata{
  124. Additions: result.additions,
  125. Removals: result.removals,
  126. }), nil
  127. }
  128. result, err := e.replaceContent(ctx, params.FilePath, params.OldString, params.NewString)
  129. if err != nil {
  130. return NewTextErrorResponse(fmt.Sprintf("error replacing content: %s", err)), nil
  131. }
  132. waitForLspDiagnostics(ctx, params.FilePath, e.lspClients)
  133. text := fmt.Sprintf("<result>\n%s\n</result>\n", result.text)
  134. text += appendDiagnostics(params.FilePath, e.lspClients)
  135. return WithResponseMetadata(NewTextResponse(text), EditResponseMetadata{
  136. Additions: result.additions,
  137. Removals: result.removals,
  138. }), nil
  139. }
  140. type editResponse struct {
  141. text string
  142. additions int
  143. removals int
  144. }
  145. func (e *editTool) createNewFile(ctx context.Context, filePath, content string) (editResponse, error) {
  146. er := editResponse{}
  147. fileInfo, err := os.Stat(filePath)
  148. if err == nil {
  149. if fileInfo.IsDir() {
  150. return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
  151. }
  152. return er, fmt.Errorf("file already exists: %s. Use the Replace tool to overwrite an existing file", filePath)
  153. } else if !os.IsNotExist(err) {
  154. return er, fmt.Errorf("failed to access file: %w", err)
  155. }
  156. dir := filepath.Dir(filePath)
  157. if err = os.MkdirAll(dir, 0o755); err != nil {
  158. return er, fmt.Errorf("failed to create parent directories: %w", err)
  159. }
  160. sessionID, messageID := getContextValues(ctx)
  161. if sessionID == "" || messageID == "" {
  162. return er, fmt.Errorf("session ID and message ID are required for creating a new file")
  163. }
  164. diff, stats, err := git.GenerateGitDiffWithStats(
  165. removeWorkingDirectoryPrefix(filePath),
  166. "",
  167. content,
  168. )
  169. if err != nil {
  170. return er, fmt.Errorf("failed to get file diff: %w", err)
  171. }
  172. p := e.permissions.Request(
  173. permission.CreatePermissionRequest{
  174. Path: filepath.Dir(filePath),
  175. ToolName: EditToolName,
  176. Action: "create",
  177. Description: fmt.Sprintf("Create file %s", filePath),
  178. Params: EditPermissionsParams{
  179. FilePath: filePath,
  180. Diff: diff,
  181. },
  182. },
  183. )
  184. if !p {
  185. return er, fmt.Errorf("permission denied")
  186. }
  187. err = os.WriteFile(filePath, []byte(content), 0o644)
  188. if err != nil {
  189. return er, fmt.Errorf("failed to write file: %w", err)
  190. }
  191. recordFileWrite(filePath)
  192. recordFileRead(filePath)
  193. er.text = "File created: " + filePath
  194. er.additions = stats.Additions
  195. er.removals = stats.Removals
  196. return er, nil
  197. }
  198. func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string) (editResponse, error) {
  199. er := editResponse{}
  200. fileInfo, err := os.Stat(filePath)
  201. if err != nil {
  202. if os.IsNotExist(err) {
  203. return er, fmt.Errorf("file not found: %s", filePath)
  204. }
  205. return er, fmt.Errorf("failed to access file: %w", err)
  206. }
  207. if fileInfo.IsDir() {
  208. return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
  209. }
  210. if getLastReadTime(filePath).IsZero() {
  211. return er, fmt.Errorf("you must read the file before editing it. Use the View tool first")
  212. }
  213. modTime := fileInfo.ModTime()
  214. lastRead := getLastReadTime(filePath)
  215. if modTime.After(lastRead) {
  216. return er, fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
  217. filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
  218. }
  219. content, err := os.ReadFile(filePath)
  220. if err != nil {
  221. return er, fmt.Errorf("failed to read file: %w", err)
  222. }
  223. oldContent := string(content)
  224. index := strings.Index(oldContent, oldString)
  225. if index == -1 {
  226. return er, fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
  227. }
  228. lastIndex := strings.LastIndex(oldContent, oldString)
  229. if index != lastIndex {
  230. return er, fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
  231. }
  232. newContent := oldContent[:index] + oldContent[index+len(oldString):]
  233. sessionID, messageID := getContextValues(ctx)
  234. if sessionID == "" || messageID == "" {
  235. return er, fmt.Errorf("session ID and message ID are required for creating a new file")
  236. }
  237. diff, stats, err := git.GenerateGitDiffWithStats(
  238. removeWorkingDirectoryPrefix(filePath),
  239. oldContent,
  240. newContent,
  241. )
  242. if err != nil {
  243. return er, fmt.Errorf("failed to get file diff: %w", err)
  244. }
  245. p := e.permissions.Request(
  246. permission.CreatePermissionRequest{
  247. Path: filepath.Dir(filePath),
  248. ToolName: EditToolName,
  249. Action: "delete",
  250. Description: fmt.Sprintf("Delete content from file %s", filePath),
  251. Params: EditPermissionsParams{
  252. FilePath: filePath,
  253. Diff: diff,
  254. },
  255. },
  256. )
  257. if !p {
  258. return er, fmt.Errorf("permission denied")
  259. }
  260. err = os.WriteFile(filePath, []byte(newContent), 0o644)
  261. if err != nil {
  262. return er, fmt.Errorf("failed to write file: %w", err)
  263. }
  264. recordFileWrite(filePath)
  265. recordFileRead(filePath)
  266. er.text = "Content deleted from file: " + filePath
  267. er.additions = stats.Additions
  268. er.removals = stats.Removals
  269. return er, nil
  270. }
  271. func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newString string) (editResponse, error) {
  272. er := editResponse{}
  273. fileInfo, err := os.Stat(filePath)
  274. if err != nil {
  275. if os.IsNotExist(err) {
  276. return er, fmt.Errorf("file not found: %s", filePath)
  277. }
  278. return er, fmt.Errorf("failed to access file: %w", err)
  279. }
  280. if fileInfo.IsDir() {
  281. return er, fmt.Errorf("path is a directory, not a file: %s", filePath)
  282. }
  283. if getLastReadTime(filePath).IsZero() {
  284. return er, fmt.Errorf("you must read the file before editing it. Use the View tool first")
  285. }
  286. modTime := fileInfo.ModTime()
  287. lastRead := getLastReadTime(filePath)
  288. if modTime.After(lastRead) {
  289. return er, fmt.Errorf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
  290. filePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339))
  291. }
  292. content, err := os.ReadFile(filePath)
  293. if err != nil {
  294. return er, fmt.Errorf("failed to read file: %w", err)
  295. }
  296. oldContent := string(content)
  297. index := strings.Index(oldContent, oldString)
  298. if index == -1 {
  299. return er, fmt.Errorf("old_string not found in file. Make sure it matches exactly, including whitespace and line breaks")
  300. }
  301. lastIndex := strings.LastIndex(oldContent, oldString)
  302. if index != lastIndex {
  303. return er, fmt.Errorf("old_string appears multiple times in the file. Please provide more context to ensure a unique match")
  304. }
  305. newContent := oldContent[:index] + newString + oldContent[index+len(oldString):]
  306. sessionID, messageID := getContextValues(ctx)
  307. if sessionID == "" || messageID == "" {
  308. return er, fmt.Errorf("session ID and message ID are required for creating a new file")
  309. }
  310. diff, stats, err := git.GenerateGitDiffWithStats(
  311. removeWorkingDirectoryPrefix(filePath),
  312. oldContent,
  313. newContent,
  314. )
  315. if err != nil {
  316. return er, fmt.Errorf("failed to get file diff: %w", err)
  317. }
  318. p := e.permissions.Request(
  319. permission.CreatePermissionRequest{
  320. Path: filepath.Dir(filePath),
  321. ToolName: EditToolName,
  322. Action: "replace",
  323. Description: fmt.Sprintf("Replace content in file %s", filePath),
  324. Params: EditPermissionsParams{
  325. FilePath: filePath,
  326. Diff: diff,
  327. },
  328. },
  329. )
  330. if !p {
  331. return er, fmt.Errorf("permission denied")
  332. }
  333. err = os.WriteFile(filePath, []byte(newContent), 0o644)
  334. if err != nil {
  335. return er, fmt.Errorf("failed to write file: %w", err)
  336. }
  337. recordFileWrite(filePath)
  338. recordFileRead(filePath)
  339. er.text = "Content replaced in file: " + filePath
  340. er.additions = stats.Additions
  341. er.removals = stats.Removals
  342. return er, nil
  343. }