edit.go 15 KB

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