Skip to content

Commit e2b2e16

Browse files
Refactor process-str-replace to accept logger as parameter
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 8d56a1c commit e2b2e16

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

backend/src/__tests__/process-str-replace.test.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ import {
2626
benchifyCanFixLanguage,
2727
} from '../tools/batch-str-replace'
2828

29+
import type { Logger } from '@codebuff/types/logger'
30+
31+
const logger: Logger = {
32+
debug: () => {},
33+
info: () => {},
34+
warn: () => {},
35+
error: () => {},
36+
}
37+
2938
describe('processStrReplace', () => {
3039
it('should replace exact string matches', async () => {
3140
const initialContent = 'const x = 1;\nconst y = 2;\n'
@@ -36,6 +45,7 @@ describe('processStrReplace', () => {
3645
path: 'test.ts',
3746
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
3847
initialContentPromise: Promise.resolve(initialContent),
48+
logger,
3949
})
4050

4151
expect(result).not.toBeNull()
@@ -56,6 +66,7 @@ describe('processStrReplace', () => {
5666
path: 'test.ts',
5767
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
5868
initialContentPromise: Promise.resolve(initialContent),
69+
logger,
5970
})
6071

6172
expect(result).not.toBeNull()
@@ -75,6 +86,7 @@ describe('processStrReplace', () => {
7586
path: 'test.ts',
7687
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
7788
initialContentPromise: Promise.resolve(initialContent),
89+
logger,
7890
})
7991

8092
expect(result).not.toBeNull()
@@ -93,6 +105,7 @@ describe('processStrReplace', () => {
93105
path: 'test.ts',
94106
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
95107
initialContentPromise: Promise.resolve(initialContent),
108+
logger,
96109
})
97110

98111
expect(result).not.toBeNull()
@@ -107,6 +120,7 @@ describe('processStrReplace', () => {
107120
path: 'test.ts',
108121
replacements: [{ old: 'old', new: 'new', allowMultiple: false }],
109122
initialContentPromise: Promise.resolve(null),
123+
logger,
110124
})
111125

112126
expect(result).not.toBeNull()
@@ -121,6 +135,7 @@ describe('processStrReplace', () => {
121135
path: 'test.ts',
122136
replacements: [{ old: '', new: 'new', allowMultiple: false }],
123137
initialContentPromise: Promise.resolve('content'),
138+
logger,
124139
})
125140

126141
expect(result).not.toBeNull()
@@ -139,6 +154,7 @@ describe('processStrReplace', () => {
139154
path: 'test.ts',
140155
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
141156
initialContentPromise: Promise.resolve(initialContent),
157+
logger,
142158
})
143159

144160
expect(result).not.toBeNull()
@@ -159,6 +175,7 @@ describe('processStrReplace', () => {
159175
path: 'test.ts',
160176
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
161177
initialContentPromise: Promise.resolve(initialContent),
178+
logger,
162179
})
163180

164181
expect(result).not.toBeNull()
@@ -177,6 +194,7 @@ describe('processStrReplace', () => {
177194
path: 'test.ts',
178195
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
179196
initialContentPromise: Promise.resolve(initialContent),
197+
logger,
180198
})
181199

182200
expect(result).not.toBeNull()
@@ -198,6 +216,7 @@ describe('processStrReplace', () => {
198216
path: 'test.ts',
199217
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
200218
initialContentPromise: Promise.resolve(initialContent),
219+
logger,
201220
})
202221

203222
expect(result).not.toBeNull()
@@ -221,6 +240,7 @@ describe('processStrReplace', () => {
221240
path: 'test.ts',
222241
replacements,
223242
initialContentPromise: Promise.resolve(initialContent),
243+
logger,
224244
})
225245

226246
expect(result).not.toBeNull()
@@ -245,6 +265,7 @@ describe('processStrReplace', () => {
245265
path: 'test.ts',
246266
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
247267
initialContentPromise: Promise.resolve(initialContent),
268+
logger,
248269
})
249270

250271
expect(result).not.toBeNull()
@@ -262,11 +283,12 @@ describe('processStrReplace', () => {
262283
const oldStr = 'const x'
263284
const newStr = 'let x'
264285

265-
const result = await processStrReplace({
266-
path: 'test.ts',
267-
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
268-
initialContentPromise: Promise.resolve(initialContent),
269-
})
286+
const result = await processStrReplace({
287+
path: 'test.ts',
288+
replacements: [{ old: oldStr, new: newStr, allowMultiple: false }],
289+
initialContentPromise: Promise.resolve(initialContent),
290+
logger,
291+
})
270292

271293
expect(result).not.toBeNull()
272294
expect('error' in result).toBe(true)
@@ -285,6 +307,7 @@ describe('processStrReplace', () => {
285307
path: 'test.ts',
286308
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
287309
initialContentPromise: Promise.resolve(initialContent),
310+
logger,
288311
})
289312

290313
expect(result).not.toBeNull()
@@ -303,6 +326,7 @@ describe('processStrReplace', () => {
303326
path: 'test.ts',
304327
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
305328
initialContentPromise: Promise.resolve(initialContent),
329+
logger,
306330
})
307331

308332
expect(result).not.toBeNull()
@@ -324,6 +348,7 @@ describe('processStrReplace', () => {
324348
path: 'test.ts',
325349
replacements,
326350
initialContentPromise: Promise.resolve(initialContent),
351+
logger,
327352
})
328353

329354
expect(result).not.toBeNull()
@@ -355,6 +380,7 @@ function test3() {
355380
path: 'test.ts',
356381
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
357382
initialContentPromise: Promise.resolve(initialContent),
383+
logger,
358384
})
359385

360386
expect(result).not.toBeNull()
@@ -378,6 +404,7 @@ function test3() {
378404
path: 'test.ts',
379405
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
380406
initialContentPromise: Promise.resolve(initialContent),
407+
logger,
381408
})
382409

383410
expect(result).not.toBeNull()
@@ -401,6 +428,7 @@ function test3() {
401428
path: 'test.ts',
402429
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
403430
initialContentPromise: Promise.resolve(initialContent),
431+
logger,
404432
})
405433

406434
expect(result).not.toBeNull()
@@ -420,6 +448,7 @@ function test3() {
420448
path: 'test.ts',
421449
replacements: [{ old: oldStr, new: newStr, allowMultiple: true }],
422450
initialContentPromise: Promise.resolve(initialContent),
451+
logger,
423452
})
424453

425454
expect(result).not.toBeNull()
@@ -451,6 +480,7 @@ function test3() {
451480
path: 'test.ts',
452481
replacements,
453482
initialContentPromise: Promise.resolve(initialContent),
483+
logger,
454484
})
455485

456486
expect('content' in result).toBe(true)

backend/src/process-str-replace.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createPatch } from 'diff'
22

33
import { tryToDoStringReplacementWithExtraIndentation } from './generate-diffs-prompt'
4-
import { logger } from './util/logger'
4+
5+
import type { Logger } from '@codebuff/types/logger'
56

67
function normalizeLineEndings(params: { str: string }): string {
78
return params.str.replace(/\r\n/g, '\n')
@@ -11,6 +12,7 @@ export async function processStrReplace(params: {
1112
path: string
1213
replacements: { old: string; new: string; allowMultiple: boolean }[]
1314
initialContentPromise: Promise<string | null>
15+
logger: Logger
1416
}): Promise<
1517
| {
1618
tool: 'str_replace'
@@ -21,7 +23,7 @@ export async function processStrReplace(params: {
2123
}
2224
| { tool: 'str_replace'; path: string; error: string }
2325
> {
24-
const { path, replacements, initialContentPromise } = params
26+
const { path, replacements, initialContentPromise, logger } = params
2527
const initialContent = await initialContentPromise
2628
if (initialContent === null) {
2729
return {
@@ -58,6 +60,7 @@ export async function processStrReplace(params: {
5860
oldStr: normalizedOldStr,
5961
newStr: normalizedNewStr,
6062
allowMultiple,
63+
logger,
6164
})
6265
let updatedOldStr: string | null
6366

@@ -125,8 +128,9 @@ const tryMatchOldStr = (params: {
125128
oldStr: string
126129
newStr: string
127130
allowMultiple: boolean
131+
logger: Logger
128132
}): { success: true; oldStr: string } | { success: false; error: string } => {
129-
const { initialContent, oldStr, newStr, allowMultiple } = params
133+
const { initialContent, oldStr, newStr, allowMultiple, logger } = params
130134
// count the number of occurrences of oldStr in initialContent
131135
const count = initialContent.split(oldStr).length - 1
132136
if (count === 1) {

backend/src/tools/handlers/tool/str-replace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const handleStrReplace = ((params: {
6767
path,
6868
replacements,
6969
initialContentPromise: latestContentPromise,
70+
logger,
7071
})
7172
.catch((error: any) => {
7273
logger.error(error, 'Error processing str_replace block')

0 commit comments

Comments
 (0)