summaryrefslogtreecommitdiffhomepage
path: root/internal/llm/tools/patch.go
blob: 12060d72a363b16fffc8af2c43fa14cf496597dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package tools

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/kujtimiihoxha/opencode/internal/config"
	"github.com/kujtimiihoxha/opencode/internal/diff"
	"github.com/kujtimiihoxha/opencode/internal/history"
	"github.com/kujtimiihoxha/opencode/internal/lsp"
	"github.com/kujtimiihoxha/opencode/internal/permission"
)

type PatchParams struct {
	FilePath string `json:"file_path"`
	Patch    string `json:"patch"`
}

type PatchPermissionsParams struct {
	FilePath string `json:"file_path"`
	Diff     string `json:"diff"`
}

type PatchResponseMetadata struct {
	Diff      string `json:"diff"`
	Additions int    `json:"additions"`
	Removals  int    `json:"removals"`
}

type patchTool struct {
	lspClients  map[string]*lsp.Client
	permissions permission.Service
	files       history.Service
}

const (
	// TODO: test if this works as expected
	PatchToolName    = "patch"
	patchDescription = `Applies a patch to a file. This tool is similar to the edit tool but accepts a unified diff patch instead of old/new strings.

Before using this tool:

1. Use the FileRead tool to understand the file's contents and context

2. Verify the directory path is correct:
   - Use the LS tool to verify the parent directory exists and is the correct location

To apply a patch, provide the following:
1. file_path: The absolute path to the file to modify (must be absolute, not relative)
2. patch: A unified diff patch to apply to the file

The tool will apply the patch to the specified file. The patch must be in unified diff format.

CRITICAL REQUIREMENTS FOR USING THIS TOOL:

1. PATCH FORMAT: The patch must be in unified diff format, which includes:
   - File headers (--- a/file_path, +++ b/file_path)
   - Hunk headers (@@ -start,count +start,count @@)
   - Added lines (prefixed with +)
   - Removed lines (prefixed with -)

2. CONTEXT: The patch must include sufficient context around the changes to ensure it applies correctly.

3. VERIFICATION: Before using this tool:
   - Ensure the patch applies cleanly to the current state of the file
   - Check that the file exists and you have read it first

WARNING: If you do not follow these requirements:
   - The tool will fail if the patch doesn't apply cleanly
   - You may change the wrong parts of the file if the context is insufficient

When applying patches:
   - Ensure the patch results in idiomatic, correct code
   - Do not leave the code in a broken state
   - Always use absolute file paths (starting with /)

Remember: patches are a powerful way to make multiple related changes at once, but they require careful preparation.`
)

func NewPatchTool(lspClients map[string]*lsp.Client, permissions permission.Service, files history.Service) BaseTool {
	return &patchTool{
		lspClients:  lspClients,
		permissions: permissions,
		files:       files,
	}
}

func (p *patchTool) Info() ToolInfo {
	return ToolInfo{
		Name:        PatchToolName,
		Description: patchDescription,
		Parameters: map[string]any{
			"file_path": map[string]any{
				"type":        "string",
				"description": "The absolute path to the file to modify",
			},
			"patch": map[string]any{
				"type":        "string",
				"description": "The unified diff patch to apply",
			},
		},
		Required: []string{"file_path", "patch"},
	}
}

func (p *patchTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error) {
	var params PatchParams
	if err := json.Unmarshal([]byte(call.Input), &params); err != nil {
		return NewTextErrorResponse("invalid parameters"), nil
	}

	if params.FilePath == "" {
		return NewTextErrorResponse("file_path is required"), nil
	}

	if params.Patch == "" {
		return NewTextErrorResponse("patch is required"), nil
	}

	if !filepath.IsAbs(params.FilePath) {
		wd := config.WorkingDirectory()
		params.FilePath = filepath.Join(wd, params.FilePath)
	}

	// Check if file exists
	fileInfo, err := os.Stat(params.FilePath)
	if err != nil {
		if os.IsNotExist(err) {
			return NewTextErrorResponse(fmt.Sprintf("file not found: %s", params.FilePath)), nil
		}
		return ToolResponse{}, fmt.Errorf("failed to access file: %w", err)
	}

	if fileInfo.IsDir() {
		return NewTextErrorResponse(fmt.Sprintf("path is a directory, not a file: %s", params.FilePath)), nil
	}

	if getLastReadTime(params.FilePath).IsZero() {
		return NewTextErrorResponse("you must read the file before patching it. Use the View tool first"), nil
	}

	modTime := fileInfo.ModTime()
	lastRead := getLastReadTime(params.FilePath)
	if modTime.After(lastRead) {
		return NewTextErrorResponse(
			fmt.Sprintf("file %s has been modified since it was last read (mod time: %s, last read: %s)",
				params.FilePath, modTime.Format(time.RFC3339), lastRead.Format(time.RFC3339),
			)), nil
	}

	// Read the current file content
	content, err := os.ReadFile(params.FilePath)
	if err != nil {
		return ToolResponse{}, fmt.Errorf("failed to read file: %w", err)
	}

	oldContent := string(content)

	// Parse and apply the patch
	diffResult, err := diff.ParseUnifiedDiff(params.Patch)
	if err != nil {
		return NewTextErrorResponse(fmt.Sprintf("failed to parse patch: %v", err)), nil
	}

	// Apply the patch to get the new content
	newContent, err := applyPatch(oldContent, diffResult)
	if err != nil {
		return NewTextErrorResponse(fmt.Sprintf("failed to apply patch: %v", err)), nil
	}

	if oldContent == newContent {
		return NewTextErrorResponse("patch did not result in any changes to the file"), nil
	}

	sessionID, messageID := GetContextValues(ctx)
	if sessionID == "" || messageID == "" {
		return ToolResponse{}, fmt.Errorf("session ID and message ID are required for patching a file")
	}

	// Generate a diff for permission request and metadata
	diffText, additions, removals := diff.GenerateDiff(
		oldContent,
		newContent,
		params.FilePath,
	)

	// Request permission to apply the patch
	p.permissions.Request(
		permission.CreatePermissionRequest{
			Path:        filepath.Dir(params.FilePath),
			ToolName:    PatchToolName,
			Action:      "patch",
			Description: fmt.Sprintf("Apply patch to file %s", params.FilePath),
			Params: PatchPermissionsParams{
				FilePath: params.FilePath,
				Diff:     diffText,
			},
		},
	)

	// Write the new content to the file
	err = os.WriteFile(params.FilePath, []byte(newContent), 0o644)
	if err != nil {
		return ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
	}

	// Update file history
	file, err := p.files.GetByPathAndSession(ctx, params.FilePath, sessionID)
	if err != nil {
		_, err = p.files.Create(ctx, sessionID, params.FilePath, oldContent)
		if err != nil {
			return ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
		}
	}
	if file.Content != oldContent {
		// User manually changed the content, store an intermediate version
		_, err = p.files.CreateVersion(ctx, sessionID, params.FilePath, oldContent)
		if err != nil {
			fmt.Printf("Error creating file history version: %v\n", err)
		}
	}
	// Store the new version
	_, err = p.files.CreateVersion(ctx, sessionID, params.FilePath, newContent)
	if err != nil {
		fmt.Printf("Error creating file history version: %v\n", err)
	}

	recordFileWrite(params.FilePath)
	recordFileRead(params.FilePath)

	// Wait for LSP diagnostics and include them in the response
	waitForLspDiagnostics(ctx, params.FilePath, p.lspClients)
	text := fmt.Sprintf("<r>\nPatch applied to file: %s\n</r>\n", params.FilePath)
	text += getDiagnostics(params.FilePath, p.lspClients)

	return WithResponseMetadata(
		NewTextResponse(text),
		PatchResponseMetadata{
			Diff:      diffText,
			Additions: additions,
			Removals:  removals,
		}), nil
}

// applyPatch applies a parsed diff to a string and returns the resulting content
func applyPatch(content string, diffResult diff.DiffResult) (string, error) {
	lines := strings.Split(content, "\n")

	// Process each hunk in the diff
	for _, hunk := range diffResult.Hunks {
		// Parse the hunk header to get line numbers
		var oldStart, oldCount, newStart, newCount int
		_, err := fmt.Sscanf(hunk.Header, "@@ -%d,%d +%d,%d @@", &oldStart, &oldCount, &newStart, &newCount)
		if err != nil {
			// Try alternative format with single line counts
			_, err = fmt.Sscanf(hunk.Header, "@@ -%d +%d @@", &oldStart, &newStart)
			if err != nil {
				return "", fmt.Errorf("invalid hunk header format: %s", hunk.Header)
			}
			oldCount = 1
			newCount = 1
		}

		// Adjust for 0-based array indexing
		oldStart--
		newStart--

		// Apply the changes
		newLines := make([]string, 0)
		newLines = append(newLines, lines[:oldStart]...)

		// Process the hunk lines in order
		currentOldLine := oldStart
		for _, line := range hunk.Lines {
			switch line.Kind {
			case diff.LineContext:
				newLines = append(newLines, line.Content)
				currentOldLine++
			case diff.LineRemoved:
				// Skip this line in the output (it's being removed)
				currentOldLine++
			case diff.LineAdded:
				// Add the new line
				newLines = append(newLines, line.Content)
			}
		}

		// Append the rest of the file
		newLines = append(newLines, lines[currentOldLine:]...)
		lines = newLines
	}

	return strings.Join(lines, "\n"), nil
}