Skip to content

Commit 50c56a9

Browse files
Refactor generate-diffs-prompt.ts to pass logger as parameter
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 4951bb5 commit 50c56a9

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

backend/src/__tests__/generate-diffs-prompt.test.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { expect, describe, it } from 'bun:test'
22

33
import { parseAndGetDiffBlocksSingleFile } from '../generate-diffs-prompt'
44

5+
import type { Logger } from '@codebuff/types/logger'
6+
7+
const logger: Logger = {
8+
debug: () => {},
9+
info: () => {},
10+
warn: () => {},
11+
error: () => {},
12+
}
13+
514
describe('parseAndGetDiffBlocksSingleFile', () => {
615
it('should parse diff blocks with newline before closing marker', () => {
716
const oldContent = 'function test() {\n return true;\n}\n'
@@ -16,7 +25,11 @@ function test() {
1625
}
1726
>>>>>>> REPLACE`
1827

19-
const result = parseAndGetDiffBlocksSingleFile(newContent, oldContent)
28+
const result = parseAndGetDiffBlocksSingleFile({
29+
newContent,
30+
oldFileContent: oldContent,
31+
logger,
32+
})
2033
console.log(JSON.stringify({ result }))
2134

2235
expect(result.diffBlocks.length).toBe(1)
@@ -41,7 +54,11 @@ function test() {
4154
return true;
4255
}>>>>>>> REPLACE`
4356

44-
const result = parseAndGetDiffBlocksSingleFile(newContent, oldContent)
57+
const result = parseAndGetDiffBlocksSingleFile({
58+
newContent,
59+
oldFileContent: oldContent,
60+
logger,
61+
})
4562

4663
expect(result.diffBlocks.length).toBe(1)
4764
expect(result.diffBlocksThatDidntMatch.length).toBe(0)
@@ -90,7 +107,11 @@ function subtract(a, b) {
90107
}
91108
>>>>>>> REPLACE`
92109

93-
const result = parseAndGetDiffBlocksSingleFile(newContent, oldContent)
110+
const result = parseAndGetDiffBlocksSingleFile({
111+
newContent,
112+
oldFileContent: oldContent,
113+
logger,
114+
})
94115

95116
expect(result.diffBlocks.length).toBe(2)
96117
expect(result.diffBlocksThatDidntMatch.length).toBe(0)
@@ -114,7 +135,11 @@ function subtract(a, b) {
114135
=======
115136
>>>>>>> REPLACE`
116137

117-
const result = parseAndGetDiffBlocksSingleFile(newContent, oldContent)
138+
const result = parseAndGetDiffBlocksSingleFile({
139+
newContent,
140+
oldFileContent: oldContent,
141+
logger,
142+
})
118143

119144
expect(result.diffBlocks.length).toBe(1)
120145
expect(result.diffBlocksThatDidntMatch.length).toBe(0)

backend/src/generate-diffs-prompt.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import {
55
} from '@codebuff/common/util/file'
66

77
import { promptAiSdk } from './llm-apis/vercel-ai-sdk/ai-sdk'
8-
import { logger } from './util/logger'
98

10-
export const parseAndGetDiffBlocksSingleFile = (
11-
newContent: string,
12-
oldFileContent: string,
13-
) => {
9+
import type { Logger } from '@codebuff/types/logger'
10+
11+
export const parseAndGetDiffBlocksSingleFile = (params: {
12+
newContent: string
13+
oldFileContent: string
14+
logger: Logger
15+
}) => {
16+
const { newContent, oldFileContent, logger } = params
1417
const diffBlocksThatDidntMatch: {
1518
searchContent: string
1619
replaceContent: string
@@ -138,6 +141,7 @@ export async function retryDiffBlocksPrompt(params: {
138141
userInputId: string
139142
userId: string | undefined
140143
diffBlocksThatDidntMatch: { searchContent: string; replaceContent: string }[]
144+
logger: Logger
141145
}) {
142146
const {
143147
filePath,
@@ -147,6 +151,7 @@ export async function retryDiffBlocksPrompt(params: {
147151
userInputId,
148152
userId,
149153
diffBlocksThatDidntMatch,
154+
logger,
150155
} = params
151156
const newPrompt =
152157
`The assistant failed to find a match for the following changes. Please help the assistant understand what the changes should be.
@@ -173,7 +178,11 @@ Provide a new set of SEARCH/REPLACE changes to make the intended edit from the o
173178
const {
174179
diffBlocks: newDiffBlocks,
175180
diffBlocksThatDidntMatch: newDiffBlocksThatDidntMatch,
176-
} = parseAndGetDiffBlocksSingleFile(response, oldContent)
181+
} = parseAndGetDiffBlocksSingleFile({
182+
newContent: response,
183+
oldFileContent: oldContent,
184+
logger,
185+
})
177186

178187
if (newDiffBlocksThatDidntMatch.length > 0) {
179188
logger.error(

backend/src/process-file-block.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ Please output just the SEARCH/REPLACE blocks like this:
298298
})
299299

300300
const { diffBlocks, diffBlocksThatDidntMatch } =
301-
parseAndGetDiffBlocksSingleFile(response, oldContent)
301+
parseAndGetDiffBlocksSingleFile({
302+
newContent: response,
303+
oldFileContent: oldContent,
304+
logger,
305+
})
302306

303307
let updatedContent = oldContent
304308
for (const { searchContent, replaceContent } of diffBlocks) {
@@ -328,6 +332,7 @@ Please output just the SEARCH/REPLACE blocks like this:
328332
userInputId,
329333
userId,
330334
diffBlocksThatDidntMatch,
335+
logger,
331336
})
332337

333338
if (newDiffBlocksThatDidntMatch.length > 0) {

plans/logger-refactor-plan.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ For all other files:
4141
- Update function calls to pass object with named properties
4242
- **For tests:** Create a no-op logger constant named `logger` (NOT `mockLogger`!) with all 4 methods:
4343
```typescript
44-
const logger = {
44+
const logger: Logger = {
4545
debug: () => {},
4646
info: () => {},
4747
warn: () => {},
@@ -51,12 +51,18 @@ For all other files:
5151
- **Check carefully** - there may be multiple call sites in the same file!
5252
- Repeat typecheck until ALL errors resolved
5353

54-
### Step 4: Commit changes
54+
### Step 4: Run reviewer agent
55+
56+
- After all typechecks pass, spawn the reviewer agent to review the changes
57+
- Address any feedback from the reviewer before committing
58+
- If you make _any_ changes, go back to Step 3.
59+
60+
### Step 5: Commit changes
5561

5662
- **Do NOT use git-committer agent** - it's too slow
5763
- Instead, manually run: `git add <files> && git commit -m "<message>" && git push`
5864
- This will push to a branch, where I can manually review the changes.
5965
- Keep commit message concise but descriptive
6066
- Include Codebuff footer in commit message
6167

62-
### Step 5: Go back to Step 1 and repeat
68+
### Step 6: Go back to Step 1 and repeat

0 commit comments

Comments
 (0)