@workser/engine 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +8 -0
- package/README.md +70 -0
- package/dist/src/compact.d.ts +44 -0
- package/dist/src/compact.js +116 -0
- package/dist/src/engine.d.ts +84 -0
- package/dist/src/engine.js +142 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +13 -0
- package/dist/src/model.d.ts +56 -0
- package/dist/src/model.js +153 -0
- package/dist/src/place/e2b.d.ts +76 -0
- package/dist/src/place/e2b.js +114 -0
- package/dist/src/place/env.d.ts +3 -0
- package/dist/src/place/env.js +14 -0
- package/dist/src/place/local.d.ts +33 -0
- package/dist/src/place/local.js +138 -0
- package/dist/src/prompts.d.ts +27 -0
- package/dist/src/prompts.js +108 -0
- package/dist/src/session.d.ts +115 -0
- package/dist/src/session.js +523 -0
- package/dist/src/tools/files.d.ts +9 -0
- package/dist/src/tools/files.js +209 -0
- package/dist/src/tools/patch.d.ts +49 -0
- package/dist/src/tools/patch.js +272 -0
- package/dist/src/tools/relay.d.ts +25 -0
- package/dist/src/tools/relay.js +82 -0
- package/dist/src/tools/search.d.ts +5 -0
- package/dist/src/tools/search.js +99 -0
- package/dist/src/tools/shell.d.ts +26 -0
- package/dist/src/tools/shell.js +123 -0
- package/dist/src/tools/types.d.ts +49 -0
- package/dist/src/tools/types.js +16 -0
- package/dist/src/tools/workser-tool.d.ts +39 -0
- package/dist/src/tools/workser-tool.js +51 -0
- package/dist/src/vendor.d.ts +4 -0
- package/dist/src/vendor.js +17 -0
- package/package.json +42 -0
- package/vendor/opencode-1.18.23/COMMIT +1 -0
- package/vendor/opencode-1.18.23/LICENSE +21 -0
- package/vendor/opencode-1.18.23/session/prompt/anthropic.txt +105 -0
- package/vendor/opencode-1.18.23/session/prompt/beast.txt +147 -0
- package/vendor/opencode-1.18.23/session/prompt/build-switch.txt +5 -0
- package/vendor/opencode-1.18.23/session/prompt/codex.txt +79 -0
- package/vendor/opencode-1.18.23/session/prompt/copilot-gpt-5.txt +143 -0
- package/vendor/opencode-1.18.23/session/prompt/default.txt +95 -0
- package/vendor/opencode-1.18.23/session/prompt/gemini.txt +155 -0
- package/vendor/opencode-1.18.23/session/prompt/gpt.txt +107 -0
- package/vendor/opencode-1.18.23/session/prompt/kimi.txt +95 -0
- package/vendor/opencode-1.18.23/session/prompt/meta.txt +65 -0
- package/vendor/opencode-1.18.23/session/prompt/plan-mode.txt +70 -0
- package/vendor/opencode-1.18.23/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/vendor/opencode-1.18.23/session/prompt/plan.txt +26 -0
- package/vendor/opencode-1.18.23/session/prompt/trinity.txt +97 -0
- package/vendor/opencode-1.18.23/session/system.ts.ref +152 -0
- package/vendor/opencode-1.18.23/tool/apply_patch.txt +33 -0
- package/vendor/opencode-1.18.23/tool/edit-replace.ts +535 -0
- package/vendor/opencode-1.18.23/tool/edit.txt +10 -0
- package/vendor/opencode-1.18.23/tool/glob.txt +6 -0
- package/vendor/opencode-1.18.23/tool/grep.txt +8 -0
- package/vendor/opencode-1.18.23/tool/read.txt +14 -0
- package/vendor/opencode-1.18.23/tool/shell/prompt.ts.ref +293 -0
- package/vendor/opencode-1.18.23/tool/shell/shell.txt +21 -0
- package/vendor/opencode-1.18.23/tool/todowrite.txt +44 -0
- package/vendor/opencode-1.18.23/tool/write.txt +8 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
// VENDORED VERBATIM from OpenCode v1.18.23 packages/opencode/src/tool/edit.ts (MIT, see ./LICENSE).
|
|
2
|
+
// Lines 22-33 (line-ending helpers) and 217-737 (replacers + replace + trimDiff). Do not edit: update by re-vendoring.
|
|
3
|
+
function normalizeLineEndings(text: string): string {
|
|
4
|
+
return text.replaceAll("\r\n", "\n")
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function detectLineEnding(text: string): "\n" | "\r\n" {
|
|
8
|
+
return text.includes("\r\n") ? "\r\n" : "\n"
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function convertToLineEnding(text: string, ending: "\n" | "\r\n"): string {
|
|
12
|
+
if (ending === "\n") return text
|
|
13
|
+
return text.replaceAll("\n", "\r\n")
|
|
14
|
+
}
|
|
15
|
+
export type Replacer = (content: string, find: string) => Generator<string, void, unknown>
|
|
16
|
+
|
|
17
|
+
// Similarity thresholds for block anchor fallback matching
|
|
18
|
+
const SINGLE_CANDIDATE_SIMILARITY_THRESHOLD = 0.65
|
|
19
|
+
const MULTIPLE_CANDIDATES_SIMILARITY_THRESHOLD = 0.65
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Levenshtein distance algorithm implementation
|
|
23
|
+
*/
|
|
24
|
+
function levenshtein(a: string, b: string): number {
|
|
25
|
+
// Handle empty strings
|
|
26
|
+
if (a === "" || b === "") {
|
|
27
|
+
return Math.max(a.length, b.length)
|
|
28
|
+
}
|
|
29
|
+
const matrix = Array.from({ length: a.length + 1 }, (_, i) =>
|
|
30
|
+
Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
for (let i = 1; i <= a.length; i++) {
|
|
34
|
+
for (let j = 1; j <= b.length; j++) {
|
|
35
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1
|
|
36
|
+
matrix[i][j] = Math.min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + cost)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return matrix[a.length][b.length]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const SimpleReplacer: Replacer = function* (_content, find) {
|
|
43
|
+
yield find
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const LineTrimmedReplacer: Replacer = function* (content, find) {
|
|
47
|
+
const originalLines = content.split("\n")
|
|
48
|
+
const searchLines = find.split("\n")
|
|
49
|
+
|
|
50
|
+
if (searchLines[searchLines.length - 1] === "") {
|
|
51
|
+
searchLines.pop()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i <= originalLines.length - searchLines.length; i++) {
|
|
55
|
+
let matches = true
|
|
56
|
+
|
|
57
|
+
for (let j = 0; j < searchLines.length; j++) {
|
|
58
|
+
const originalTrimmed = originalLines[i + j].trim()
|
|
59
|
+
const searchTrimmed = searchLines[j].trim()
|
|
60
|
+
|
|
61
|
+
if (originalTrimmed !== searchTrimmed) {
|
|
62
|
+
matches = false
|
|
63
|
+
break
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (matches) {
|
|
68
|
+
let matchStartIndex = 0
|
|
69
|
+
for (let k = 0; k < i; k++) {
|
|
70
|
+
matchStartIndex += originalLines[k].length + 1
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let matchEndIndex = matchStartIndex
|
|
74
|
+
for (let k = 0; k < searchLines.length; k++) {
|
|
75
|
+
matchEndIndex += originalLines[i + k].length
|
|
76
|
+
if (k < searchLines.length - 1) {
|
|
77
|
+
matchEndIndex += 1 // Add newline character except for the last line
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
yield content.substring(matchStartIndex, matchEndIndex)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const BlockAnchorReplacer: Replacer = function* (content, find) {
|
|
87
|
+
const originalLines = content.split("\n")
|
|
88
|
+
const searchLines = find.split("\n")
|
|
89
|
+
|
|
90
|
+
if (searchLines.length < 3) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (searchLines[searchLines.length - 1] === "") {
|
|
95
|
+
searchLines.pop()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const firstLineSearch = searchLines[0].trim()
|
|
99
|
+
const lastLineSearch = searchLines[searchLines.length - 1].trim()
|
|
100
|
+
const searchBlockSize = searchLines.length
|
|
101
|
+
const maxLineDelta = Math.max(1, Math.floor(searchBlockSize * 0.25))
|
|
102
|
+
|
|
103
|
+
// Collect all candidate positions where both anchors match
|
|
104
|
+
const candidates: Array<{ startLine: number; endLine: number }> = []
|
|
105
|
+
for (let i = 0; i < originalLines.length; i++) {
|
|
106
|
+
if (originalLines[i].trim() !== firstLineSearch) {
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Look for the matching last line after this first line
|
|
111
|
+
for (let j = i + 2; j < originalLines.length; j++) {
|
|
112
|
+
if (originalLines[j].trim() === lastLineSearch) {
|
|
113
|
+
const actualBlockSize = j - i + 1
|
|
114
|
+
if (Math.abs(actualBlockSize - searchBlockSize) <= maxLineDelta) {
|
|
115
|
+
candidates.push({ startLine: i, endLine: j })
|
|
116
|
+
}
|
|
117
|
+
break // Only match the first occurrence of the last line
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Return immediately if no candidates
|
|
123
|
+
if (candidates.length === 0) {
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Handle single candidate scenario (using relaxed threshold)
|
|
128
|
+
if (candidates.length === 1) {
|
|
129
|
+
const { startLine, endLine } = candidates[0]
|
|
130
|
+
const actualBlockSize = endLine - startLine + 1
|
|
131
|
+
|
|
132
|
+
let similarity = 0
|
|
133
|
+
const linesToCheck = Math.min(searchBlockSize - 2, actualBlockSize - 2) // Middle lines only
|
|
134
|
+
|
|
135
|
+
if (linesToCheck > 0) {
|
|
136
|
+
for (let j = 1; j < searchBlockSize - 1 && j < actualBlockSize - 1; j++) {
|
|
137
|
+
const originalLine = originalLines[startLine + j].trim()
|
|
138
|
+
const searchLine = searchLines[j].trim()
|
|
139
|
+
const maxLen = Math.max(originalLine.length, searchLine.length)
|
|
140
|
+
if (maxLen === 0) {
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
143
|
+
const distance = levenshtein(originalLine, searchLine)
|
|
144
|
+
similarity += (1 - distance / maxLen) / linesToCheck
|
|
145
|
+
|
|
146
|
+
// Exit early when threshold is reached
|
|
147
|
+
if (similarity >= SINGLE_CANDIDATE_SIMILARITY_THRESHOLD) {
|
|
148
|
+
break
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
// No middle lines to compare, just accept based on anchors
|
|
153
|
+
similarity = 1.0
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (similarity >= SINGLE_CANDIDATE_SIMILARITY_THRESHOLD) {
|
|
157
|
+
let matchStartIndex = 0
|
|
158
|
+
for (let k = 0; k < startLine; k++) {
|
|
159
|
+
matchStartIndex += originalLines[k].length + 1
|
|
160
|
+
}
|
|
161
|
+
let matchEndIndex = matchStartIndex
|
|
162
|
+
for (let k = startLine; k <= endLine; k++) {
|
|
163
|
+
matchEndIndex += originalLines[k].length
|
|
164
|
+
if (k < endLine) {
|
|
165
|
+
matchEndIndex += 1 // Add newline character except for the last line
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
yield content.substring(matchStartIndex, matchEndIndex)
|
|
169
|
+
}
|
|
170
|
+
return
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Calculate similarity for multiple candidates
|
|
174
|
+
let bestMatch: { startLine: number; endLine: number } | null = null
|
|
175
|
+
let maxSimilarity = -1
|
|
176
|
+
|
|
177
|
+
for (const candidate of candidates) {
|
|
178
|
+
const { startLine, endLine } = candidate
|
|
179
|
+
const actualBlockSize = endLine - startLine + 1
|
|
180
|
+
|
|
181
|
+
let similarity = 0
|
|
182
|
+
const linesToCheck = Math.min(searchBlockSize - 2, actualBlockSize - 2) // Middle lines only
|
|
183
|
+
|
|
184
|
+
if (linesToCheck > 0) {
|
|
185
|
+
for (let j = 1; j < searchBlockSize - 1 && j < actualBlockSize - 1; j++) {
|
|
186
|
+
const originalLine = originalLines[startLine + j].trim()
|
|
187
|
+
const searchLine = searchLines[j].trim()
|
|
188
|
+
const maxLen = Math.max(originalLine.length, searchLine.length)
|
|
189
|
+
if (maxLen === 0) {
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
const distance = levenshtein(originalLine, searchLine)
|
|
193
|
+
similarity += 1 - distance / maxLen
|
|
194
|
+
}
|
|
195
|
+
similarity /= linesToCheck // Average similarity
|
|
196
|
+
} else {
|
|
197
|
+
// No middle lines to compare, just accept based on anchors
|
|
198
|
+
similarity = 1.0
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (similarity > maxSimilarity) {
|
|
202
|
+
maxSimilarity = similarity
|
|
203
|
+
bestMatch = candidate
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Threshold judgment
|
|
208
|
+
if (maxSimilarity >= MULTIPLE_CANDIDATES_SIMILARITY_THRESHOLD && bestMatch) {
|
|
209
|
+
const { startLine, endLine } = bestMatch
|
|
210
|
+
let matchStartIndex = 0
|
|
211
|
+
for (let k = 0; k < startLine; k++) {
|
|
212
|
+
matchStartIndex += originalLines[k].length + 1
|
|
213
|
+
}
|
|
214
|
+
let matchEndIndex = matchStartIndex
|
|
215
|
+
for (let k = startLine; k <= endLine; k++) {
|
|
216
|
+
matchEndIndex += originalLines[k].length
|
|
217
|
+
if (k < endLine) {
|
|
218
|
+
matchEndIndex += 1
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
yield content.substring(matchStartIndex, matchEndIndex)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const WhitespaceNormalizedReplacer: Replacer = function* (content, find) {
|
|
226
|
+
const normalizeWhitespace = (text: string) => text.replace(/\s+/g, " ").trim()
|
|
227
|
+
const normalizedFind = normalizeWhitespace(find)
|
|
228
|
+
|
|
229
|
+
// Handle single line matches
|
|
230
|
+
const lines = content.split("\n")
|
|
231
|
+
for (let i = 0; i < lines.length; i++) {
|
|
232
|
+
const line = lines[i]
|
|
233
|
+
if (normalizeWhitespace(line) === normalizedFind) {
|
|
234
|
+
yield line
|
|
235
|
+
} else {
|
|
236
|
+
// Only check for substring matches if the full line doesn't match
|
|
237
|
+
const normalizedLine = normalizeWhitespace(line)
|
|
238
|
+
if (normalizedLine.includes(normalizedFind)) {
|
|
239
|
+
// Find the actual substring in the original line that matches
|
|
240
|
+
const words = find.trim().split(/\s+/)
|
|
241
|
+
if (words.length > 0) {
|
|
242
|
+
const pattern = words.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("\\s+")
|
|
243
|
+
try {
|
|
244
|
+
const regex = new RegExp(pattern)
|
|
245
|
+
const match = line.match(regex)
|
|
246
|
+
if (match) {
|
|
247
|
+
yield match[0]
|
|
248
|
+
}
|
|
249
|
+
} catch {
|
|
250
|
+
// Invalid regex pattern, skip
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Handle multi-line matches
|
|
258
|
+
const findLines = find.split("\n")
|
|
259
|
+
if (findLines.length > 1) {
|
|
260
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
261
|
+
const block = lines.slice(i, i + findLines.length)
|
|
262
|
+
if (normalizeWhitespace(block.join("\n")) === normalizedFind) {
|
|
263
|
+
yield block.join("\n")
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export const IndentationFlexibleReplacer: Replacer = function* (content, find) {
|
|
270
|
+
const removeIndentation = (text: string) => {
|
|
271
|
+
const lines = text.split("\n")
|
|
272
|
+
const nonEmptyLines = lines.filter((line) => line.trim().length > 0)
|
|
273
|
+
if (nonEmptyLines.length === 0) return text
|
|
274
|
+
|
|
275
|
+
const minIndent = Math.min(
|
|
276
|
+
...nonEmptyLines.map((line) => {
|
|
277
|
+
const match = line.match(/^(\s*)/)
|
|
278
|
+
return match ? match[1].length : 0
|
|
279
|
+
}),
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
return lines.map((line) => (line.trim().length === 0 ? line : line.slice(minIndent))).join("\n")
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const normalizedFind = removeIndentation(find)
|
|
286
|
+
const contentLines = content.split("\n")
|
|
287
|
+
const findLines = find.split("\n")
|
|
288
|
+
|
|
289
|
+
for (let i = 0; i <= contentLines.length - findLines.length; i++) {
|
|
290
|
+
const block = contentLines.slice(i, i + findLines.length).join("\n")
|
|
291
|
+
if (removeIndentation(block) === normalizedFind) {
|
|
292
|
+
yield block
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export const EscapeNormalizedReplacer: Replacer = function* (content, find) {
|
|
298
|
+
const unescapeString = (str: string): string => {
|
|
299
|
+
return str.replace(/\\(n|t|r|'|"|`|\\|\n|\$)/g, (match, capturedChar) => {
|
|
300
|
+
switch (capturedChar) {
|
|
301
|
+
case "n":
|
|
302
|
+
return "\n"
|
|
303
|
+
case "t":
|
|
304
|
+
return "\t"
|
|
305
|
+
case "r":
|
|
306
|
+
return "\r"
|
|
307
|
+
case "'":
|
|
308
|
+
return "'"
|
|
309
|
+
case '"':
|
|
310
|
+
return '"'
|
|
311
|
+
case "`":
|
|
312
|
+
return "`"
|
|
313
|
+
case "\\":
|
|
314
|
+
return "\\"
|
|
315
|
+
case "\n":
|
|
316
|
+
return "\n"
|
|
317
|
+
case "$":
|
|
318
|
+
return "$"
|
|
319
|
+
default:
|
|
320
|
+
return match
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const unescapedFind = unescapeString(find)
|
|
326
|
+
|
|
327
|
+
// Try direct match with unescaped find string
|
|
328
|
+
if (content.includes(unescapedFind)) {
|
|
329
|
+
yield unescapedFind
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Also try finding escaped versions in content that match unescaped find
|
|
333
|
+
const lines = content.split("\n")
|
|
334
|
+
const findLines = unescapedFind.split("\n")
|
|
335
|
+
|
|
336
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
337
|
+
const block = lines.slice(i, i + findLines.length).join("\n")
|
|
338
|
+
const unescapedBlock = unescapeString(block)
|
|
339
|
+
|
|
340
|
+
if (unescapedBlock === unescapedFind) {
|
|
341
|
+
yield block
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export const MultiOccurrenceReplacer: Replacer = function* (content, find) {
|
|
347
|
+
// This replacer yields all exact matches, allowing the replace function
|
|
348
|
+
// to handle multiple occurrences based on replaceAll parameter
|
|
349
|
+
let startIndex = 0
|
|
350
|
+
|
|
351
|
+
while (true) {
|
|
352
|
+
const index = content.indexOf(find, startIndex)
|
|
353
|
+
if (index === -1) break
|
|
354
|
+
|
|
355
|
+
yield find
|
|
356
|
+
startIndex = index + find.length
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export const TrimmedBoundaryReplacer: Replacer = function* (content, find) {
|
|
361
|
+
const trimmedFind = find.trim()
|
|
362
|
+
|
|
363
|
+
if (trimmedFind === find) {
|
|
364
|
+
// Already trimmed, no point in trying
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Try to find the trimmed version
|
|
369
|
+
if (content.includes(trimmedFind)) {
|
|
370
|
+
yield trimmedFind
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Also try finding blocks where trimmed content matches
|
|
374
|
+
const lines = content.split("\n")
|
|
375
|
+
const findLines = find.split("\n")
|
|
376
|
+
|
|
377
|
+
for (let i = 0; i <= lines.length - findLines.length; i++) {
|
|
378
|
+
const block = lines.slice(i, i + findLines.length).join("\n")
|
|
379
|
+
|
|
380
|
+
if (block.trim() === trimmedFind) {
|
|
381
|
+
yield block
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export const ContextAwareReplacer: Replacer = function* (content, find) {
|
|
387
|
+
const findLines = find.split("\n")
|
|
388
|
+
if (findLines.length < 3) {
|
|
389
|
+
// Need at least 3 lines to have meaningful context
|
|
390
|
+
return
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Remove trailing empty line if present
|
|
394
|
+
if (findLines[findLines.length - 1] === "") {
|
|
395
|
+
findLines.pop()
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const contentLines = content.split("\n")
|
|
399
|
+
|
|
400
|
+
// Extract first and last lines as context anchors
|
|
401
|
+
const firstLine = findLines[0].trim()
|
|
402
|
+
const lastLine = findLines[findLines.length - 1].trim()
|
|
403
|
+
|
|
404
|
+
// Find blocks that start and end with the context anchors
|
|
405
|
+
for (let i = 0; i < contentLines.length; i++) {
|
|
406
|
+
if (contentLines[i].trim() !== firstLine) continue
|
|
407
|
+
|
|
408
|
+
// Look for the matching last line
|
|
409
|
+
for (let j = i + 2; j < contentLines.length; j++) {
|
|
410
|
+
if (contentLines[j].trim() === lastLine) {
|
|
411
|
+
// Found a potential context block
|
|
412
|
+
const blockLines = contentLines.slice(i, j + 1)
|
|
413
|
+
const block = blockLines.join("\n")
|
|
414
|
+
|
|
415
|
+
// Check if the middle content has reasonable similarity
|
|
416
|
+
// (simple heuristic: at least 50% of non-empty lines should match when trimmed)
|
|
417
|
+
if (blockLines.length === findLines.length) {
|
|
418
|
+
let matchingLines = 0
|
|
419
|
+
let totalNonEmptyLines = 0
|
|
420
|
+
|
|
421
|
+
for (let k = 1; k < blockLines.length - 1; k++) {
|
|
422
|
+
const blockLine = blockLines[k].trim()
|
|
423
|
+
const findLine = findLines[k].trim()
|
|
424
|
+
|
|
425
|
+
if (blockLine.length > 0 || findLine.length > 0) {
|
|
426
|
+
totalNonEmptyLines++
|
|
427
|
+
if (blockLine === findLine) {
|
|
428
|
+
matchingLines++
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
if (totalNonEmptyLines === 0 || matchingLines / totalNonEmptyLines >= 0.5) {
|
|
434
|
+
yield block
|
|
435
|
+
break // Only match the first occurrence
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
break
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export function trimDiff(diff: string): string {
|
|
445
|
+
const lines = diff.split("\n")
|
|
446
|
+
const contentLines = lines.filter(
|
|
447
|
+
(line) =>
|
|
448
|
+
(line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
|
|
449
|
+
!line.startsWith("---") &&
|
|
450
|
+
!line.startsWith("+++"),
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
if (contentLines.length === 0) return diff
|
|
454
|
+
|
|
455
|
+
let min = Infinity
|
|
456
|
+
for (const line of contentLines) {
|
|
457
|
+
const content = line.slice(1)
|
|
458
|
+
if (content.trim().length > 0) {
|
|
459
|
+
const match = content.match(/^(\s*)/)
|
|
460
|
+
if (match) min = Math.min(min, match[1].length)
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (min === Infinity || min === 0) return diff
|
|
464
|
+
const trimmedLines = lines.map((line) => {
|
|
465
|
+
if (
|
|
466
|
+
(line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
|
|
467
|
+
!line.startsWith("---") &&
|
|
468
|
+
!line.startsWith("+++")
|
|
469
|
+
) {
|
|
470
|
+
const prefix = line[0]
|
|
471
|
+
const content = line.slice(1)
|
|
472
|
+
return prefix + content.slice(min)
|
|
473
|
+
}
|
|
474
|
+
return line
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
return trimmedLines.join("\n")
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export function replace(content: string, oldString: string, newString: string, replaceAll = false): string {
|
|
481
|
+
if (oldString === newString) {
|
|
482
|
+
throw new Error("No changes to apply: oldString and newString are identical.")
|
|
483
|
+
}
|
|
484
|
+
if (oldString === "") {
|
|
485
|
+
throw new Error(
|
|
486
|
+
"oldString cannot be empty when editing an existing file. Provide the exact text to replace, or use write for an intentional full-file replacement.",
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
let notFound = true
|
|
491
|
+
|
|
492
|
+
for (const replacer of [
|
|
493
|
+
SimpleReplacer,
|
|
494
|
+
LineTrimmedReplacer,
|
|
495
|
+
BlockAnchorReplacer,
|
|
496
|
+
WhitespaceNormalizedReplacer,
|
|
497
|
+
IndentationFlexibleReplacer,
|
|
498
|
+
EscapeNormalizedReplacer,
|
|
499
|
+
TrimmedBoundaryReplacer,
|
|
500
|
+
ContextAwareReplacer,
|
|
501
|
+
MultiOccurrenceReplacer,
|
|
502
|
+
]) {
|
|
503
|
+
for (const search of replacer(content, oldString)) {
|
|
504
|
+
const index = content.indexOf(search)
|
|
505
|
+
if (index === -1) continue
|
|
506
|
+
notFound = false
|
|
507
|
+
if (isDisproportionateMatch(search, oldString)) {
|
|
508
|
+
throw new Error(
|
|
509
|
+
"Refusing replacement because the matched span is much larger than oldString. Re-read the file and provide the full exact oldString for the intended replacement.",
|
|
510
|
+
)
|
|
511
|
+
}
|
|
512
|
+
if (replaceAll) {
|
|
513
|
+
return content.replaceAll(search, newString)
|
|
514
|
+
}
|
|
515
|
+
const lastIndex = content.lastIndexOf(search)
|
|
516
|
+
if (index !== lastIndex) continue
|
|
517
|
+
return content.substring(0, index) + newString + content.substring(index + search.length)
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (notFound) {
|
|
522
|
+
throw new Error(
|
|
523
|
+
"Could not find oldString in the file. It must match exactly, including whitespace, indentation, and line endings.",
|
|
524
|
+
)
|
|
525
|
+
}
|
|
526
|
+
throw new Error("Found multiple matches for oldString. Provide more surrounding context to make the match unique.")
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function isDisproportionateMatch(search: string, oldString: string) {
|
|
530
|
+
const oldLines = oldString.split("\n").length
|
|
531
|
+
const searchLines = search.split("\n").length
|
|
532
|
+
if (searchLines >= Math.max(oldLines + 3, oldLines * 2)) return true
|
|
533
|
+
if (oldLines === 1) return false
|
|
534
|
+
return search.trim().length > Math.max(oldString.trim().length + 500, oldString.trim().length * 4)
|
|
535
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Performs exact string replacements in files.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
- You must use your `Read` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.
|
|
5
|
+
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: line number + colon + space (e.g., `1: `). Everything after that space is the actual file content to match. Never include any part of the line number prefix in the oldString or newString.
|
|
6
|
+
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
|
|
7
|
+
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
|
|
8
|
+
- The edit will FAIL if `oldString` is not found in the file with an error "oldString not found in content".
|
|
9
|
+
- The edit will FAIL if `oldString` is found multiple times in the file with an error "Found multiple matches for oldString. Provide more surrounding lines in oldString to identify the correct match." Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`.
|
|
10
|
+
- Use `replaceAll` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
- Fast file pattern matching tool that works with any codebase size
|
|
2
|
+
- Supports glob patterns like "**/*.js" or "src/**/*.ts"
|
|
3
|
+
- Returns matching file paths
|
|
4
|
+
- Use this tool when you need to find files by name patterns
|
|
5
|
+
- When you are doing an open-ended search that may require multiple rounds of globbing and grepping, use the Task tool instead
|
|
6
|
+
- You have the capability to call multiple tools in a single response. It is always better to speculatively perform multiple searches as a batch that are potentially useful.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
- Fast content search tool that works with any codebase size
|
|
2
|
+
- Searches file contents using regular expressions
|
|
3
|
+
- Supports full regex syntax (eg. "log.*Error", "function\s+\w+", etc.)
|
|
4
|
+
- Filter files by pattern with the include parameter (eg. "*.js", "*.{ts,tsx}")
|
|
5
|
+
- Returns file paths and line numbers with matching lines
|
|
6
|
+
- Use this tool when you need to find files containing specific patterns
|
|
7
|
+
- If you need to identify/count the number of matches within files, use the Bash tool with `rg` (ripgrep) directly. Do NOT use `grep`.
|
|
8
|
+
- When you are doing an open-ended search that may require multiple rounds of globbing and grepping, use the Task tool instead
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Read a file or directory from the local filesystem. If the path does not exist, an error is returned.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
- The filePath parameter should be an absolute path.
|
|
5
|
+
- By default, this tool returns up to 2000 lines from the start of the file.
|
|
6
|
+
- The offset parameter is the line number to start from (1-indexed).
|
|
7
|
+
- To read later sections, call this tool again with a larger offset.
|
|
8
|
+
- Use the grep tool to find specific content in large files or files with long lines.
|
|
9
|
+
- If you are unsure of the correct file path, use the glob tool to look up filenames by glob pattern.
|
|
10
|
+
- Contents are returned with each line prefixed by its line number as `<line>: <content>`. For example, if a file has contents "foo\n", you will receive "1: foo\n". For directories, entries are returned one per line (without line numbers) with a trailing `/` for subdirectories.
|
|
11
|
+
- Any line longer than 2000 characters is truncated.
|
|
12
|
+
- Call this tool in parallel when you know there are multiple files you want to read.
|
|
13
|
+
- Avoid tiny repeated slices (30 line chunks). If you need more context, read a larger window.
|
|
14
|
+
- This tool can read image files and PDFs and return them as file attachments.
|